From 367e4d23527c9ff3938a8db2abd3bdbc429d2c58 Mon Sep 17 00:00:00 2001 From: Allen Chow Date: Tue, 2 Aug 2022 14:51:16 +0000 Subject: [PATCH] clean --- .../src/context.rs | 23 +-- .../src/fork_chain.rs | 9 +- .../src/fork_state.rs | 144 +----------------- .../src/in_memory_state_cache.rs | 91 ----------- .../src/lib.rs | 10 +- 5 files changed, 19 insertions(+), 258 deletions(-) delete mode 100644 vm/starcoin-transactional-test-harness/src/in_memory_state_cache.rs diff --git a/vm/starcoin-transactional-test-harness/src/context.rs b/vm/starcoin-transactional-test-harness/src/context.rs index f2be738135..8152311fac 100644 --- a/vm/starcoin-transactional-test-harness/src/context.rs +++ b/vm/starcoin-transactional-test-harness/src/context.rs @@ -1,24 +1,20 @@ use anyhow::{anyhow, Result}; use starcoin_config::{BuiltinNetworkID, ChainNetwork}; -use starcoin_crypto::HashValue; use starcoin_genesis::Genesis; -use starcoin_state_api::ChainStateAsyncService; use starcoin_statedb::ChainStateDB; use std::sync::{Arc, Mutex}; use tokio::runtime::Runtime; use tokio::task::JoinHandle; -use jsonrpc_client_transports::{RawClient, RpcChannel}; -use jsonrpc_core::futures::{self, future, TryFutureExt}; -use jsonrpc_core::{BoxFuture, IoHandler, Params, Value}; +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 crate::fork_chain::{ForkBlockChain, MockChainApi, MockStateApi}; -use crate::fork_state::{ForkStateDB, MockStateNodeStore}; -use crate::in_memory_state_cache::InMemoryStateCache; -use crate::remote_state::{RemoteRpcAsyncClient, RemoteViewer, SelectableStateView}; +use crate::fork_state::MockStateNodeStore; +use crate::remote_state::RemoteRpcAsyncClient; pub struct MockServer { server_handle: JoinHandle<()>, @@ -42,7 +38,6 @@ impl MockServer { pub struct ForkContext { pub chain: Arc>, - //pub storage: SelectableStateView, pub storage: ChainStateDB, server: MockServer, client: RawClient, @@ -79,12 +74,6 @@ impl ForkContext { Arc::new(rt.block_on(async { RemoteRpcAsyncClient::from_url(&rpc[..], block_number).await })?); - // let remote_viewer = RemoteViewer::new(remote_async_client.clone(), rt.clone()); - // let state_db = ForkStateDB::new( - // Some(HashValue::random()), - // Arc::new(remote_async_client.get_state_client().clone()), - // rt.clone(), - // )?; 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 }) @@ -93,8 +82,6 @@ impl ForkContext { Arc::new(MockStateNodeStore::new(state_api_client, rt.clone())?), Some(root_hash), ); - //let storage = SelectableStateView::B(data_store.clone()); - //let storage = SelectableStateView::B(InMemoryStateCache::new(remote_viewer)); let fork_nubmer = remote_async_client.get_fork_block_number(); let chain = Arc::new(Mutex::new(ForkBlockChain::fork( @@ -107,9 +94,7 @@ impl ForkContext { fn new_inner( chain: Arc>, - // storage: SelectableStateView, storage: ChainStateDB, - // state_db: ChainStateDB, rt: Arc, ) -> Result { let chain_api = MockChainApi::new(chain.clone()); diff --git a/vm/starcoin-transactional-test-harness/src/fork_chain.rs b/vm/starcoin-transactional-test-harness/src/fork_chain.rs index bb2f0531e4..f080f3cde3 100644 --- a/vm/starcoin-transactional-test-harness/src/fork_chain.rs +++ b/vm/starcoin-transactional-test-harness/src/fork_chain.rs @@ -1,21 +1,18 @@ // Copyright (c) The Starcoin Core Contributors // SPDX-License-Identifier: Apache-2.0 -use crate::remote_state::{RemoteRpcAsyncClient, SelectableStateView}; -use anyhow::{anyhow, bail, ensure, format_err, Result}; +use crate::remote_state::RemoteRpcAsyncClient; +use anyhow::{anyhow, bail, Result}; use dashmap::DashMap; -use jsonrpc_client_transports::RpcError; -use jsonrpc_core::futures_util::future::Remote; use jsonrpc_core::futures_util::{FutureExt, TryFutureExt}; use starcoin_accumulator::{node::AccumulatorStoreType, Accumulator, MerkleAccumulator}; -use starcoin_chain_api::ChainReader; use starcoin_config::{BuiltinNetworkID, ChainNetworkID}; use starcoin_crypto::HashValue; use starcoin_rpc_api::chain::ChainApiClient; use starcoin_rpc_api::chain::{ChainApi, GetBlockOption}; use starcoin_rpc_api::state::StateApi; use starcoin_rpc_api::types::{BlockInfoView, BlockView, ChainId, ChainInfoView, StrView}; -use starcoin_rpc_api::{debug, FutureResult}; +use starcoin_rpc_api::FutureResult; use starcoin_rpc_server::module::map_err; use starcoin_state_api::{ChainStateReader, StateView}; use starcoin_statedb::ChainStateDB; diff --git a/vm/starcoin-transactional-test-harness/src/fork_state.rs b/vm/starcoin-transactional-test-harness/src/fork_state.rs index b32ef7b4a9..ccaa08dbf1 100644 --- a/vm/starcoin-transactional-test-harness/src/fork_state.rs +++ b/vm/starcoin-transactional-test-harness/src/fork_state.rs @@ -1,23 +1,14 @@ use std::collections::BTreeMap; -use std::hash::Hash; use std::sync::Arc; use crate::HashValue; use anyhow::{anyhow, Result}; -use dashmap::DashMap; -use starcoin_rpc_client::{RemoteStateReader, RpcClient, StateRootOption}; -use starcoin_state_api::{ChainStateReader, ChainStateWriter, StateNodeStore, StateWithProof}; -use starcoin_statedb::ChainStateDB; +use starcoin_state_api::StateNodeStore; use starcoin_storage::state_node::StateStorage; -use starcoin_storage::storage::{StorageInstance, CodecWriteBatch, CodecKVStore}; -use starcoin_storage::Storage; -use starcoin_types::state_set::ChainStateSet; -use starcoin_vm_types::access_path::AccessPath; -use starcoin_vm_types::state_view::StateView; -use starcoin_vm_types::write_set::{WriteOp, WriteSet, WriteSetMut}; +use starcoin_storage::storage::{CodecKVStore, CodecWriteBatch, StorageInstance}; use starcoin_rpc_api::state::StateApiClient; -use starcoin_state_tree::{AccountStateSetIterator, StateNode}; +use starcoin_state_tree::StateNode; use tokio::runtime::Runtime; pub struct MockStateNodeStore { @@ -45,11 +36,10 @@ impl StateNodeStore for MockStateNodeStore { Some(sn) => Ok(Some(sn)), None => { let handle = self.rt.handle().clone(); - let blob = handle.block_on( async move { - self.remote.get_by_hash(*hash).await - }) - .map(|res| res.map(|b| StateNode(b))) - .map_err(|e| anyhow!("{}", e))?; + let blob = handle + .block_on(async move { self.remote.get_by_hash(*hash).await }) + .map(|res| res.map(|b| StateNode(b))) + .map_err(|e| anyhow!("{}", e))?; // Put result to local storage to accelerate the following getting. if let Some(node) = blob.clone() { @@ -59,7 +49,7 @@ impl StateNodeStore for MockStateNodeStore { } } } - + fn put(&self, key: HashValue, node: StateNode) -> Result<()> { self.local_storage.put(key, node) } @@ -69,121 +59,3 @@ impl StateNodeStore for MockStateNodeStore { self.local_storage.write_batch(batch) } } - -pub struct ForkStateDB { - local: ChainStateDB, - remote: Arc, - rt: Arc, -} - -impl ForkStateDB { - pub fn new( - root_hash: Option, - state_client: Arc, - rt: Arc, - ) -> Result { - let store = Arc::new( - MockStateNodeStore::new(state_client.clone(), rt.clone())? - ); - Ok(ForkStateDB { - local: ChainStateDB::new(store, root_hash), - remote: state_client, - rt, - }) - } - - fn call_rpc_blocking( - &self, - f: impl FnOnce(Arc) -> F + Send, - ) -> anyhow::Result - where - T: Send, - F: std::future::Future> + Send, - { - let client = self.remote.clone(); - self.rt - .handle() - .clone() - .block_on(f(client)) - .map_err(|e| anyhow!(format!("{}", e))) - } -} - -impl ChainStateWriter for ForkStateDB { - fn set(&self, access_path: &AccessPath, value: Vec) -> anyhow::Result<()> { - self.local.set(access_path, value) - } - - fn remove(&self, access_path: &AccessPath) -> anyhow::Result<()> { - self.local.remove(access_path) - } - - fn apply(&self, state_set: ChainStateSet) -> anyhow::Result<()> { - self.local.apply(state_set) - } - - fn apply_write_set(&self, write_set: WriteSet) -> anyhow::Result<()> { - self.local.apply_write_set(write_set) - } - - fn commit(&self) -> anyhow::Result { - self.local.commit() - } - - fn flush(&self) -> anyhow::Result<()> { - self.local.flush() - } -} - -impl ChainStateReader for ForkStateDB { - fn get_with_proof(&self, access_path: &AccessPath) -> anyhow::Result { - let local = self.local.get_with_proof(access_path)?; - match local.clone().state { - Some(_st) => Ok(local), - None => { - let access_path = access_path.clone(); - self.call_rpc_blocking(|client| client.get_with_proof(access_path)) - .map(Into::into) - } - } - } - - fn get_account_state( - &self, - address: &move_core_types::account_address::AccountAddress, - ) -> anyhow::Result> { - todo!() - } - - fn get_account_state_set( - &self, - address: &move_core_types::account_address::AccountAddress, - ) -> anyhow::Result> { - todo!() - } - - fn state_root(&self) -> HashValue { - todo!() - } - - fn dump(&self) -> anyhow::Result { - todo!() - } - - fn dump_iter(&self) -> anyhow::Result { - todo!() - } -} - -impl StateView for ForkStateDB { - fn get(&self, access_path: &AccessPath) -> anyhow::Result>> { - match self.local.get(access_path)? { - Some(opt_data) => Ok(Some(opt_data.clone())), - None => self.call_rpc_blocking(|client| client.get(access_path.clone())), - } - } - - fn is_genesis(&self) -> bool { - false - } -} diff --git a/vm/starcoin-transactional-test-harness/src/in_memory_state_cache.rs b/vm/starcoin-transactional-test-harness/src/in_memory_state_cache.rs deleted file mode 100644 index bfe44bd819..0000000000 --- a/vm/starcoin-transactional-test-harness/src/in_memory_state_cache.rs +++ /dev/null @@ -1,91 +0,0 @@ -use crate::HashValue; -use dashmap::DashMap; -use starcoin_state_api::ChainStateWriter; -use starcoin_types::state_set::ChainStateSet; -use starcoin_vm_types::access_path::AccessPath; -use starcoin_vm_types::state_view::StateView; -use starcoin_vm_types::write_set::{WriteOp, WriteSet, WriteSetMut}; - -pub struct InMemoryStateCache { - data_map: DashMap>>, - data_view: V, -} - -impl InMemoryStateCache { - /// Create a `StateViewCache` give a `StateView`. Hold updates to the data store and - /// forward data request to the `StateView` if not in the local cache. - pub fn new(data_view: V) -> Self { - InMemoryStateCache { - data_view, - data_map: DashMap::new(), - } - } - - // Publishes a `WriteSet` computed at the end of a transaction. - // The effect is to build a layer in front of the `StateView` which keeps - // track of the data as if the changes were applied immediately. - pub(crate) fn push_write_set(&self, write_set: &WriteSet) { - for (ref ap, ref write_op) in write_set.iter() { - match write_op { - WriteOp::Value(blob) => { - self.data_map.insert(ap.clone(), Some(blob.clone())); - } - WriteOp::Deletion => { - self.data_map.remove(ap); - self.data_map.insert(ap.clone(), None); - } - } - } - } -} - -impl ChainStateWriter for InMemoryStateCache { - fn set(&self, access_path: &AccessPath, value: Vec) -> anyhow::Result<()> { - self.apply_write_set( - WriteSetMut::new(vec![(access_path.clone(), WriteOp::Value(value))]).freeze()?, - ) - } - - fn remove(&self, access_path: &AccessPath) -> anyhow::Result<()> { - self.apply_write_set( - WriteSetMut::new(vec![(access_path.clone(), WriteOp::Deletion)]).freeze()?, - ) - } - - fn apply(&self, _state_set: ChainStateSet) -> anyhow::Result<()> { - unimplemented!() - } - - fn apply_write_set(&self, write_set: WriteSet) -> anyhow::Result<()> { - self.push_write_set(&write_set); - Ok(()) - } - - fn commit(&self) -> anyhow::Result { - Ok(HashValue::random()) - } - - fn flush(&self) -> anyhow::Result<()> { - Ok(()) - } -} -impl StateView for InMemoryStateCache { - // Get some data either through the cache or the `StateView` on a cache miss. - fn get(&self, access_path: &AccessPath) -> anyhow::Result>> { - match self.data_map.get(access_path) { - Some(opt_data) => Ok(opt_data.clone()), - None => match self.data_view.get(access_path) { - Ok(remote_data) => Ok(remote_data), - // TODO: should we forward some error info? - Err(e) => { - // error!("[VM] Error getting data from storage for {:?}", access_path); - Err(e) - } - }, - } - } - - fn is_genesis(&self) -> bool { - self.data_view.is_genesis() - } -} diff --git a/vm/starcoin-transactional-test-harness/src/lib.rs b/vm/starcoin-transactional-test-harness/src/lib.rs index 9def0bd6d5..0b0a6083b9 100644 --- a/vm/starcoin-transactional-test-harness/src/lib.rs +++ b/vm/starcoin-transactional-test-harness/src/lib.rs @@ -1,6 +1,4 @@ use crate::context::ForkContext; -use crate::in_memory_state_cache::InMemoryStateCache; -use crate::remote_state::{RemoteViewer, SelectableStateView}; use anyhow::{bail, Result}; use move_binary_format::{file_format::CompiledScript, CompiledModule}; use move_compiler::compiled_unit::CompiledUnitEnum; @@ -24,7 +22,7 @@ use serde::Deserialize; use serde::Serialize; use serde_json::Value; use starcoin_abi_decoder::decode_txn_payload; -use starcoin_config::{genesis_key_pair, BuiltinNetworkID, ChainNetwork}; +use starcoin_config::{genesis_key_pair, BuiltinNetworkID}; use starcoin_crypto::HashValue; use starcoin_dev::playground::call_contract; use starcoin_genesis::Genesis; @@ -33,7 +31,7 @@ use starcoin_rpc_api::types::{ TransactionOutputView, TransactionStatusView, TypeTagView, }; use starcoin_rpc_api::Params; -use starcoin_state_api::{ChainStateWriter, StateReaderExt}; +use starcoin_state_api::{ChainStateReader, ChainStateWriter, StateReaderExt}; use starcoin_statedb::ChainStateDB; use starcoin_types::account::{Account, AccountData}; use starcoin_types::block::{Block, BlockBody, BlockHeader, BlockHeaderExtra}; @@ -71,7 +69,6 @@ use stdlib::{starcoin_framework_named_addresses, G_PRECOMPILED_STARCOIN_FRAMEWOR pub mod context; pub mod fork_chain; pub mod fork_state; -mod in_memory_state_cache; pub mod remote_state; #[derive(Parser, Debug, Default)] @@ -662,7 +659,7 @@ impl<'a> StarcoinTestAdapter<'a> { author, HashValue::random(), HashValue::random(), - HashValue::random(), + self.context.storage.state_root(), 0u64, U256::zero(), HashValue::random(), @@ -670,6 +667,7 @@ impl<'a> StarcoinTestAdapter<'a> { 0, BlockHeaderExtra::new([0u8; 4]), ); + println!("new block with state root: {}", block_header.state_root()); let block_body = BlockBody::new(vec![], None); let new_block = Block::new(block_header, block_body); let new_block_meta = new_block.to_metadata(0);