diff --git a/vm/starcoin-transactional-test-harness/Cargo.toml b/vm/starcoin-transactional-test-harness/Cargo.toml index cd4b18c7d5..c916f4e15a 100644 --- a/vm/starcoin-transactional-test-harness/Cargo.toml +++ b/vm/starcoin-transactional-test-harness/Cargo.toml @@ -65,7 +65,7 @@ starcoin-storage = {path = "../../storage"} starcoin-types = {path = "../../types"} starcoin-vm-runtime = {path = "../../vm/vm-runtime"} starcoin-vm-types = {path = "../../vm/types"} -stdlib = {path = "../stdlib"} +stdlib = { path = "../stdlib" } [dev-dependencies] datatest-stable = "0.1.1" diff --git a/vm/starcoin-transactional-test-harness/src/context.rs b/vm/starcoin-transactional-test-harness/src/context.rs index 461d8f28b5..75ba858b4e 100644 --- a/vm/starcoin-transactional-test-harness/src/context.rs +++ b/vm/starcoin-transactional-test-harness/src/context.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 use anyhow::{anyhow, Result}; +use futures::executor::block_on; use starcoin_config::{BuiltinNetworkID, ChainNetwork}; use starcoin_crypto::HashValue; use starcoin_genesis::Genesis; @@ -10,8 +11,8 @@ use starcoin_state_api::{ChainStateReader, ChainStateWriter, StateNodeStore}; use starcoin_statedb::ChainStateDB; use starcoin_types::write_set::WriteSet; use std::sync::{Arc, Mutex}; +use std::thread::{self, JoinHandle}; use tokio::runtime::Runtime; -use tokio::task::JoinHandle; use jsonrpc_client_transports::RawClient; use jsonrpc_core::{IoHandler, Params, Value}; @@ -25,28 +26,27 @@ use crate::fork_state::{MockChainStateAsyncService, MockStateNodeStore}; use crate::remote_state::RemoteRpcAsyncClient; pub struct MockServer { - server_handle: JoinHandle<()>, + _server_handle: JoinHandle<()>, } impl MockServer { pub fn create_and_start( chain_api: MockChainApi, state_api: impl StateApi, - rt: Arc, ) -> 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::(io); - let server_handle = rt.spawn(async move { server.await.unwrap() }); - Ok((Self { server_handle }, client)) - } -} + let server_handle = thread::spawn(move || block_on(server).unwrap()); -impl Drop for MockServer { - fn drop(&mut self) { - self.server_handle.abort(); + Ok(( + Self { + _server_handle: server_handle, + }, + client, + )) } } @@ -91,10 +91,8 @@ impl ForkContext { 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 root_hash = remote_async_client.get_fork_state_root(); + let data_store = Arc::new(MockStateNodeStore::new(state_api_client, rt.clone())); let state_db = ChainStateDB::new(data_store.clone(), Some(root_hash)); let fork_nubmer = remote_async_client.get_fork_block_number(); @@ -117,7 +115,7 @@ impl ForkContext { 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())?; + let (server, client) = MockServer::create_and_start(chain_api, state_api)?; Ok(Self { chain, diff --git a/vm/starcoin-transactional-test-harness/src/fork_state.rs b/vm/starcoin-transactional-test-harness/src/fork_state.rs index ea2f1c7308..d94c1613ad 100644 --- a/vm/starcoin-transactional-test-harness/src/fork_state.rs +++ b/vm/starcoin-transactional-test-harness/src/fork_state.rs @@ -14,27 +14,29 @@ use starcoin_statedb::ChainStateDB; use starcoin_storage::state_node::StateStorage; use starcoin_storage::storage::{CodecKVStore, CodecWriteBatch, StorageInstance}; -use futures::executor::block_on; use starcoin_rpc_api::state::StateApiClient; use starcoin_state_tree::StateNode; use starcoin_types::access_path::AccessPath; use starcoin_types::account_state::AccountState; use starcoin_types::state_set::AccountStateSet; +use tokio::runtime::Runtime; pub struct MockStateNodeStore { local_storage: StateStorage, remote: Arc, + rt: Arc, } impl MockStateNodeStore { - pub fn new(remote: Arc) -> Result { + pub fn new(remote: Arc, rt: Arc) -> Self { let storage_instance = StorageInstance::new_cache_instance(); let storage = StateStorage::new(storage_instance); - Ok(Self { + Self { local_storage: storage, remote, - }) + rt, + } } } @@ -43,14 +45,19 @@ impl StateNodeStore for MockStateNodeStore { match self.local_storage.get(*hash)? { Some(sn) => Ok(Some(sn)), None => { - let blob = - block_on(async move { self.remote.get_state_node_by_node_hash(*hash).await }) - .map(|res| res.map(StateNode)) - .map_err(|e| anyhow!("{}", e))?; + let client = self.remote.clone(); + let handle = self.rt.handle().clone(); + let hash = *hash; + let blob = // thread::spawn(move || { + handle.block_on(client.get_state_node_by_node_hash(hash)) + // }) + // .join() + // .expect("Thread getting StateNode from remote panicked") + .map(|res| res.map(StateNode)) + .map_err(|e| anyhow!("{}", e))?; - // Put result to local storage to accelerate the following getting. if let Some(node) = blob.clone() { - self.put(*hash, node)?; + self.put(hash, node)?; }; Ok(blob) } diff --git a/vm/starcoin-transactional-test-harness/src/lib.rs b/vm/starcoin-transactional-test-harness/src/lib.rs index 6002888e57..06f81f3e72 100644 --- a/vm/starcoin-transactional-test-harness/src/lib.rs +++ b/vm/starcoin-transactional-test-harness/src/lib.rs @@ -717,6 +717,7 @@ impl<'a> StarcoinTestAdapter<'a> { } } } + fn handle_new_block( &mut self, author: Option, diff --git a/vm/starcoin-transactional-test-harness/src/remote_state.rs b/vm/starcoin-transactional-test-harness/src/remote_state.rs index 134cb42273..54b10bae20 100644 --- a/vm/starcoin-transactional-test-harness/src/remote_state.rs +++ b/vm/starcoin-transactional-test-harness/src/remote_state.rs @@ -2,16 +2,14 @@ // SPDX-License-Identifier: Apache-2.0 use anyhow::{anyhow, Result}; -use jsonrpc_client_transports::{RawClient, RpcChannel}; +use jsonrpc_client_transports::RpcChannel; use move_binary_format::errors::VMError; use move_core_types::resolver::{ModuleResolver, ResourceResolver}; -use serde_json::Value; use starcoin_crypto::HashValue; use starcoin_rpc_api::chain::ChainApiClient; use starcoin_rpc_api::state::StateApiClient; use starcoin_rpc_api::types::{BlockView, StateWithProofView}; -use starcoin_rpc_api::Params; use starcoin_state_api::ChainStateWriter; use starcoin_types::access_path::{AccessPath, DataPath}; use starcoin_types::account_address::AccountAddress; @@ -190,7 +188,6 @@ where pub struct RemoteRpcAsyncClient { state_client: StateApiClient, chain_client: ChainApiClient, - raw_client: RawClient, state_root: HashValue, fork_number: u64, } @@ -219,11 +216,9 @@ impl RemoteRpcAsyncClient { } }; let state_client: starcoin_rpc_api::state::StateApiClient = rpc_channel.clone().into(); - let raw_client = rpc_channel.clone().into(); Ok(Self { state_client, chain_client, - raw_client, state_root, fork_number, }) @@ -281,13 +276,6 @@ impl RemoteRpcAsyncClient { Ok(state_with_proof.state.map(|v| v.0)) } - pub async fn call_api(&self, method: &str, params: Params) -> Result { - self.raw_client - .call_method(method, params) - .await - .map_err(|e| anyhow!(format!("{}", e))) - } - pub fn get_chain_client(&self) -> &ChainApiClient { &self.chain_client } @@ -299,6 +287,10 @@ impl RemoteRpcAsyncClient { pub fn get_fork_block_number(&self) -> u64 { self.fork_number } + + pub fn get_fork_state_root(&self) -> HashValue { + self.state_root + } } #[derive(Clone)] @@ -337,11 +329,6 @@ impl RemoteViewer { let handle = self.rt.handle().clone(); handle.block_on(self.svc.get_modules_async(addr)) } - - pub fn call_api(&self, method: &str, params: Params) -> Result { - let handle = self.rt.handle().clone(); - handle.block_on(self.svc.call_api(method, params)) - } } impl ModuleResolver for RemoteViewer { diff --git a/vm/starcoin-transactional-test-harness/tests/cases/call_api_cmd_halley.move b/vm/starcoin-transactional-test-harness/tests/cases/call_api_cmd_halley.move index b22fbfc70a..d38fab277a 100644 --- a/vm/starcoin-transactional-test-harness/tests/cases/call_api_cmd_halley.move +++ b/vm/starcoin-transactional-test-harness/tests/cases/call_api_cmd_halley.move @@ -6,9 +6,9 @@ //# run --signers creator --args {{$.call-api[0].header.number}}u64 --args {{$.call-api[0].header.block_hash}} script{ - use StarcoinFramework::Vector; + use StarcoinFramework::Vector; fun main(_sender: signer, block_number: u64, block_hash: vector){ - assert!(block_number == 1, 1000); - assert!(Vector::length(&block_hash) == 32, 1001); - } + assert!(block_number == 1, 1000); + assert!(Vector::length(&block_hash) == 32, 1001); + } } \ No newline at end of file