Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mpm integration-test support fork from remote and call-api #3600

Merged
merged 13 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions rpc/api/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ pub trait StateApi {
#[rpc(name = "state.get")]
fn get(&self, access_path: AccessPath) -> FutureResult<Option<Vec<u8>>>;

/// Return state from StateTree storage directly by tree node key.
#[rpc(name = "state.get_state_node_by_node_hash")]
fn get_state_node_by_node_hash(&self, key_hash: HashValue) -> FutureResult<Option<Vec<u8>>>;

/// Return the Resource Or Code at the `access_path`, and provide a State Proof.
#[rpc(name = "state.get_with_proof")]
fn get_with_proof(&self, access_path: AccessPath) -> FutureResult<StateWithProofView>;
Expand Down
22 changes: 22 additions & 0 deletions rpc/api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1651,6 +1651,17 @@ pub struct AccumulatorInfoView {
pub num_nodes: StrView<u64>,
}

impl AccumulatorInfoView {
fn into_info(self) -> AccumulatorInfo {
AccumulatorInfo::new(
self.accumulator_root,
self.frozen_subtree_roots,
self.num_leaves.0,
self.num_nodes.0,
)
}
}

impl From<AccumulatorInfo> for AccumulatorInfoView {
fn from(info: AccumulatorInfo) -> Self {
AccumulatorInfoView {
Expand All @@ -1675,6 +1686,17 @@ pub struct BlockInfoView {
pub block_accumulator_info: AccumulatorInfoView,
}

impl BlockInfoView {
pub fn into_info(self) -> BlockInfo {
BlockInfo::new(
self.block_hash,
self.total_difficulty,
self.txn_accumulator_info.into_info(),
self.block_accumulator_info.into_info(),
)
}
}

impl From<BlockInfo> for BlockInfoView {
fn from(block_info: BlockInfo) -> Self {
BlockInfoView {
Expand Down
30 changes: 30 additions & 0 deletions rpc/generated_rpc_schema/state.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,36 @@
}
}
},
{
"name": "state.get_state_node_by_node_hash",
"params": [
{
"name": "key_hash",
"schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "HashValue",
"type": "string",
"format": "HashValue"
}
}
],
"result": {
"name": "Option < Vec < u8 > >",
"schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Nullable_Array_of_uint8",
"type": [
"array",
"null"
],
"items": {
"type": "integer",
"format": "uint8",
"minimum": 0.0
}
}
}
},
{
"name": "state.get_with_proof",
"params": [
Expand Down
9 changes: 9 additions & 0 deletions rpc/server/src/module/state_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ where
Box::pin(fut)
}

fn get_state_node_by_node_hash(&self, key_hash: HashValue) -> FutureResult<Option<Vec<u8>>> {
let state_store = self.state_store.clone();
let f = async move {
let node = state_store.get(&key_hash)?.map(|n| n.0);
Ok(node)
};
Box::pin(f.map_err(map_err).boxed())
}

fn get_with_proof(&self, access_path: AccessPath) -> FutureResult<StateWithProofView> {
let fut = self
.service
Expand Down
39 changes: 25 additions & 14 deletions vm/starcoin-transactional-test-harness/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,32 @@ serde = { version = "1" }
serde_json = { version = "1" }
tokio = { version = "^1", features = ["full"] }

bcs-ext = { path = "../../commons/bcs_ext" }
move-resource-viewer = { git = "https://github.com/starcoinorg/move", rev = "ed32208eb82e87000fe2d163d2b6df3851d2b9c1" }
starcoin-abi-decoder = { path = "../../abi/decoder" }
starcoin-config = { path = "../../config" }
starcoin-crypto = { git = "https://github.com/starcoinorg/starcoin-crypto", rev = "d871dfb4216f034ee334a575926c101574d9d6dc" }
starcoin-dev = { path = "../dev" }
starcoin-genesis = { path = "../../genesis" }
starcoin-resource-viewer = { path = "../resource-viewer" }
starcoin-rpc-api = { path = "../../rpc/api" }
starcoin-state-api = { path = "../../state/api" }
starcoin-statedb = { path = "../../state/statedb" }
starcoin-types = { path = "../../types" }
starcoin-vm-runtime = { path = "../../vm/vm-runtime" }
starcoin-vm-types = { path = "../../vm/types" }
bcs-ext = {path = "../../commons/bcs_ext"}
move-resource-viewer = {git = "https://github.com/starcoinorg/move", rev = "ed32208eb82e87000fe2d163d2b6df3851d2b9c1"}
starcoin-abi-decoder = {path = "../../abi/decoder"}
starcoin-config = {path = "../../config"}
starcoin-crypto = {git = "https://github.com/starcoinorg/starcoin-crypto", rev = "d871dfb4216f034ee334a575926c101574d9d6dc"}
starcoin-dev = {path = "../dev"}
starcoin-genesis = {path = "../../genesis"}
starcoin-resource-viewer = {path = "../resource-viewer"}
starcoin-rpc-api = {path = "../../rpc/api"}
starcoin-rpc-server = {path = "../../rpc/server"}
starcoin-state-api = {path = "../../state/api"}
starcoin-statedb = {path = "../../state/statedb"}
starcoin-types = {path = "../../types"}
starcoin-vm-runtime = {path = "../../vm/vm-runtime"}
starcoin-vm-types = {path = "../../vm/types"}
stdlib = { path = "../stdlib" }
starcoin-storage = {path = "../../storage"}
starcoin-accumulator = { package="starcoin-accumulator", path = "../../commons/accumulator"}
starcoin-chain-api = {path="../../chain/api"}
jsonrpc-core = { version = "18", features = ["arbitrary_precision"] }
jsonrpc-http-server = "18"
jsonrpc-derive = "18"
starcoin-state-tree = {path = "../../state/state-tree"}
starcoin-rpc-client = { path = "../../rpc/client" }
async-trait = "0.1"
futures = "0.3.12"

[dev-dependencies]
datatest-stable = "0.1.1"
Expand Down
143 changes: 143 additions & 0 deletions vm/starcoin-transactional-test-harness/src/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
use anyhow::{anyhow, Result};
use starcoin_config::{BuiltinNetworkID, ChainNetwork};
use starcoin_crypto::HashValue;
use starcoin_genesis::Genesis;
use starcoin_rpc_server::module::StateRpcImpl;
use starcoin_state_api::{ChainStateReader, ChainStateWriter, StateNodeStore};
use starcoin_statedb::ChainStateDB;
use starcoin_types::write_set::WriteSet;
use std::sync::{Arc, Mutex};
use tokio::runtime::Runtime;
use tokio::task::JoinHandle;

use jsonrpc_client_transports::RawClient;
use jsonrpc_core::{IoHandler, Params, Value};
use jsonrpc_core_client::transports::local;
use starcoin_rpc_api::chain::ChainApi;
use starcoin_rpc_api::state::StateApi;
use starcoin_state_tree;

use crate::fork_chain::{ForkBlockChain, MockChainApi};
use crate::fork_state::{MockChainStateAsyncService, MockStateNodeStore};
use crate::remote_state::RemoteRpcAsyncClient;

pub struct MockServer {
server_handle: JoinHandle<()>,
}

impl MockServer {
pub fn create_and_start(
chain_api: MockChainApi,
state_api: impl StateApi,
rt: Arc<Runtime>,
) -> Result<(Self, RawClient)> {
let mut io = IoHandler::new();
io.extend_with(ChainApi::to_delegate(chain_api));
io.extend_with(StateApi::to_delegate(state_api));

let (client, server) = local::connect::<RawClient, _, _>(io);
let server_handle = rt.spawn(async move { server.await.unwrap() });
Ok((Self { server_handle }, client))
}
}

impl Drop for MockServer {
fn drop(&mut self) {
self.server_handle.abort();
}
}

pub struct ForkContext {
pub chain: Arc<Mutex<ForkBlockChain>>,
pub storage: ChainStateDB,
_server: MockServer,
client: RawClient,
rt: Arc<Runtime>,
state_root: Arc<Mutex<HashValue>>,
}

impl ForkContext {
pub fn new_local(network: BuiltinNetworkID) -> Result<Self> {
let rt = Arc::new(
tokio::runtime::Builder::new_multi_thread()
.thread_name("fork-context-worker")
.enable_all()
.build()?,
);
let net = ChainNetwork::new_builtin(network);
let genesis_txn = Genesis::build_genesis_transaction(&net).unwrap();
let data_store = Arc::new(starcoin_state_tree::mock::MockStateNodeStore::new());
let state_db = ChainStateDB::new(data_store.clone(), None);
Genesis::execute_genesis_txn(&state_db, genesis_txn).unwrap();

let state_root = state_db.state_root();
let state_root = Arc::new(Mutex::new(state_root));
let chain = Arc::new(Mutex::new(ForkBlockChain::new(state_root.clone())?));
Self::new_inner(chain, state_db, data_store, rt, state_root)
}

pub fn new_fork(rpc: &str, block_number: Option<u64>) -> Result<Self> {
let rt = Arc::new(
tokio::runtime::Builder::new_multi_thread()
.thread_name("fork-context-worker")
.enable_all()
.build()?,
);

let remote_async_client = Arc::new(
rt.block_on(async { RemoteRpcAsyncClient::from_url(rpc, block_number).await })?,
);
let state_api_client = Arc::new(remote_async_client.get_state_client().clone());
let root_hash = rt
.block_on(async { state_api_client.get_state_root().await })
.map_err(|e| anyhow!("{}", e))?;
let data_store = Arc::new(MockStateNodeStore::new(state_api_client)?);
let state_db = ChainStateDB::new(data_store.clone(), Some(root_hash));

let fork_nubmer = remote_async_client.get_fork_block_number();
let state_root = Arc::new(Mutex::new(root_hash));
let chain = Arc::new(Mutex::new(ForkBlockChain::fork(
remote_async_client,
fork_nubmer,
state_root.clone(),
)?));
Self::new_inner(chain, state_db, data_store, rt, state_root)
}

fn new_inner(
chain: Arc<Mutex<ForkBlockChain>>,
storage: ChainStateDB,
data_store: Arc<dyn StateNodeStore>,
rt: Arc<Runtime>,
state_root: Arc<Mutex<HashValue>>,
) -> Result<Self> {
let chain_api = MockChainApi::new(chain.clone());
let state_svc = MockChainStateAsyncService::new(data_store.clone(), state_root.clone());
let state_api = StateRpcImpl::new(state_svc, data_store);
let (server, client) = MockServer::create_and_start(chain_api, state_api, rt.clone())?;

Ok(Self {
chain,
storage,
_server: server,
client,
rt,
state_root,
})
}

pub fn call_api(&self, method: &str, params: Params) -> Result<Value> {
let handle = self.rt.handle().clone();
let client = self.client.clone();
handle
.block_on(async move { client.call_method(method, params).await })
.map_err(|e| anyhow!(format!("{}", e)))
}

pub fn apply_write_set(&self, write_set: WriteSet) -> anyhow::Result<()> {
self.storage.apply_write_set(write_set)?;
let state_root = self.storage.commit()?;
*self.state_root.lock().unwrap() = state_root;
Ok(())
}
}
Loading