Skip to content

Commit

Permalink
clean
Browse files Browse the repository at this point in the history
  • Loading branch information
pause125 committed Aug 2, 2022
1 parent 9ae18fc commit 367e4d2
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 258 deletions.
23 changes: 4 additions & 19 deletions vm/starcoin-transactional-test-harness/src/context.rs
Original file line number Diff line number Diff line change
@@ -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<()>,
Expand All @@ -42,7 +38,6 @@ impl MockServer {

pub struct ForkContext {
pub chain: Arc<Mutex<ForkBlockChain>>,
//pub storage: SelectableStateView<ChainStateDB, ChainStateDB>,
pub storage: ChainStateDB,
server: MockServer,
client: RawClient,
Expand Down Expand Up @@ -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 })
Expand All @@ -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(
Expand All @@ -107,9 +94,7 @@ impl ForkContext {

fn new_inner(
chain: Arc<Mutex<ForkBlockChain>>,
// storage: SelectableStateView<ChainStateDB, ChainStateDB>,
storage: ChainStateDB,
// state_db: ChainStateDB,
rt: Arc<Runtime>,
) -> Result<Self> {
let chain_api = MockChainApi::new(chain.clone());
Expand Down
9 changes: 3 additions & 6 deletions vm/starcoin-transactional-test-harness/src/fork_chain.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
144 changes: 8 additions & 136 deletions vm/starcoin-transactional-test-harness/src/fork_state.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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() {
Expand All @@ -59,7 +49,7 @@ impl StateNodeStore for MockStateNodeStore {
}
}
}

fn put(&self, key: HashValue, node: StateNode) -> Result<()> {
self.local_storage.put(key, node)
}
Expand All @@ -69,121 +59,3 @@ impl StateNodeStore for MockStateNodeStore {
self.local_storage.write_batch(batch)
}
}

pub struct ForkStateDB {
local: ChainStateDB,
remote: Arc<StateApiClient>,
rt: Arc<Runtime>,
}

impl ForkStateDB {
pub fn new(
root_hash: Option<HashValue>,
state_client: Arc<StateApiClient>,
rt: Arc<Runtime>,
) -> Result<Self> {
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<F, T>(
&self,
f: impl FnOnce(Arc<StateApiClient>) -> F + Send,
) -> anyhow::Result<T>
where
T: Send,
F: std::future::Future<Output = Result<T, jsonrpc_client_transports::RpcError>> + 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<u8>) -> 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<HashValue> {
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<StateWithProof> {
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<Option<starcoin_types::account_state::AccountState>> {
todo!()
}

fn get_account_state_set(
&self,
address: &move_core_types::account_address::AccountAddress,
) -> anyhow::Result<Option<starcoin_types::state_set::AccountStateSet>> {
todo!()
}

fn state_root(&self) -> HashValue {
todo!()
}

fn dump(&self) -> anyhow::Result<ChainStateSet> {
todo!()
}

fn dump_iter(&self) -> anyhow::Result<AccountStateSetIterator> {
todo!()
}
}

impl StateView for ForkStateDB {
fn get(&self, access_path: &AccessPath) -> anyhow::Result<Option<Vec<u8>>> {
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
}
}

This file was deleted.

Loading

0 comments on commit 367e4d2

Please sign in to comment.