From 77264768f0eeda06eb2409689157eb1bbad5cd3b Mon Sep 17 00:00:00 2001 From: fh Date: Sun, 23 Jul 2023 23:45:12 +0800 Subject: [PATCH] use the full history version of trie --- Cargo.toml | 6 ++-- crates/api/src/adapter.rs | 42 +++++++++------------- crates/api/src/jsonrpc/impls/filter.rs | 14 +------- crates/api/src/jsonrpc/impls/web3.rs | 39 +++----------------- crates/api/src/jsonrpc/ws_subscription.rs | 16 ++------- crates/executor/Cargo.toml | 1 + crates/executor/src/adapter/mod.rs | 2 +- crates/executor/src/lib.rs | 7 ++-- crates/model/src/traits/api.rs | 44 +++++++++-------------- 9 files changed, 50 insertions(+), 121 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6ec4063..3e7af3d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,8 +52,8 @@ sha2 = "0.10" ripemd = "0.1" ruc = { version = "5.0.10", features = ["crypto"] } -vsdb = { version = "0.60.0", default-features = false, features = ["extra_types"] } -vsdb_trie_db = "0.15.0" +vsdb = { version = "0.61.0", default-features = false, features = ["rocks_backend", "extra_types"] } +vsdb_trie_db = "0.20.1" #################################################################### #################################################################### @@ -91,3 +91,5 @@ benchmark = ["rt-evm-api/benchmark", "rt-evm-executor/benchmark", "rt-evm-mempoo vsdb_bcs_codec = ["rt-evm-storage/vsdb_bcs_codec"] vsdb_json_codec = ["rt-evm-storage/vsdb_json_codec"] vsdb_msgpack_codec = ["rt-evm-storage/vsdb_msgpack_codec"] + +[patch.crates-io] diff --git a/crates/api/src/adapter.rs b/crates/api/src/adapter.rs index eb3033c..d82b1fb 100644 --- a/crates/api/src/adapter.rs +++ b/crates/api/src/adapter.rs @@ -1,7 +1,6 @@ use rt_evm_executor::{RTEvmExecutor, RTEvmExecutorAdapter}; use rt_evm_mempool::Mempool; use rt_evm_model::{ - async_trait, codec::ProtocolCodec, traits::{APIAdapter, BlockStorage, Executor, ExecutorAdapter, TxStorage}, types::{ @@ -33,13 +32,13 @@ impl DefaultAPIAdapter { } } - pub async fn evm_backend( + pub fn evm_backend( &self, number: Option, ) -> Result { let block = self .get_block_by_number(number) - .await? + .c(d!())? .c(d!("Cannot get {:?} block", number))?; let state_root = block.header.state_root; let proposal = Proposal::from(&block); @@ -53,38 +52,34 @@ impl DefaultAPIAdapter { } } -#[async_trait] impl APIAdapter for DefaultAPIAdapter { - async fn insert_signed_tx(&self, signed_tx: SignedTransaction) -> Result<()> { + fn insert_signed_tx(&self, signed_tx: SignedTransaction) -> Result<()> { self.mempool.tx_insert(signed_tx, true).c(d!()) } - async fn get_block_by_number(&self, height: Option) -> Result> { + fn get_block_by_number(&self, height: Option) -> Result> { match height { Some(number) => self.storage.get_block(number), None => self.storage.get_latest_block().map(Option::Some), } } - async fn get_block_by_hash(&self, hash: Hash) -> Result> { + fn get_block_by_hash(&self, hash: Hash) -> Result> { self.storage.get_block_by_hash(&hash) } - async fn get_block_header_by_number( - &self, - number: Option, - ) -> Result> { + fn get_block_header_by_number(&self, number: Option) -> Result> { match number { Some(num) => self.storage.get_block_header(num), None => self.storage.get_latest_block_header().map(Option::Some), } } - async fn get_receipt_by_tx_hash(&self, tx_hash: Hash) -> Result> { + fn get_receipt_by_tx_hash(&self, tx_hash: Hash) -> Result> { self.storage.get_receipt_by_hash(&tx_hash) } - async fn get_receipts_by_hashes( + fn get_receipts_by_hashes( &self, block_number: u64, tx_hashes: &[Hash], @@ -92,11 +87,11 @@ impl APIAdapter for DefaultAPIAdapter { self.storage.get_receipts(block_number, tx_hashes) } - async fn get_tx_by_hash(&self, tx_hash: Hash) -> Result> { + fn get_tx_by_hash(&self, tx_hash: Hash) -> Result> { self.storage.get_tx_by_hash(&tx_hash) } - async fn get_txs_by_hashes( + fn get_txs_by_hashes( &self, block_number: u64, tx_hashes: &[Hash], @@ -104,17 +99,12 @@ impl APIAdapter for DefaultAPIAdapter { self.storage.get_txs(block_number, tx_hashes) } - async fn get_account( + fn get_account( &self, address: H160, number: Option, ) -> Result { - match self - .evm_backend(number) - .await - .c(d!())? - .get(address.as_bytes()) - { + match self.evm_backend(number).c(d!())?.get(address.as_bytes()) { Some(bytes) => Account::decode(bytes), None => Ok(Account { nonce: U256::zero(), @@ -125,11 +115,11 @@ impl APIAdapter for DefaultAPIAdapter { } } - async fn get_pending_tx_count(&self, address: H160) -> Result { + fn get_pending_tx_count(&self, address: H160) -> Result { Ok(self.mempool.tx_pending_cnt(Some(address)).into()) } - async fn evm_call( + fn evm_call( &self, from: Option, to: Option, @@ -157,11 +147,11 @@ impl APIAdapter for DefaultAPIAdapter { Ok(RTEvmExecutor.call(&backend, gas_limit, from, to, value, data)) } - async fn get_code_by_hash(&self, hash: &Hash) -> Result>> { + fn get_code_by_hash(&self, hash: &Hash) -> Result>> { self.storage.get_code_by_hash(hash) } - async fn get_storage_at( + fn get_storage_at( &self, address: H160, position: U256, diff --git a/crates/api/src/jsonrpc/impls/filter.rs b/crates/api/src/jsonrpc/impls/filter.rs index eca1684..7cfb443 100644 --- a/crates/api/src/jsonrpc/impls/filter.rs +++ b/crates/api/src/jsonrpc/impls/filter.rs @@ -201,7 +201,6 @@ where let header = self .adapter .get_block_header_by_number(None) - .await .unwrap() .unwrap(); let from = filter.from_block.as_ref().unwrap_or(&BlockId::Latest); @@ -224,7 +223,6 @@ where let header = self .adapter .get_block_header_by_number(None) - .await .unwrap() .unwrap(); self.blocks_hub @@ -266,12 +264,7 @@ where async fn filter_block(&mut self, id: &U256) -> Vec { let (start, time) = self.blocks_hub.get_mut(id).unwrap(); - let latest = self - .adapter - .get_block_by_number(None) - .await - .unwrap() - .unwrap(); + let latest = self.adapter.get_block_by_number(None).unwrap().unwrap(); if *start >= latest.header.number { return Vec::new(); } @@ -283,7 +276,6 @@ where let block = self .adapter .get_block_by_number(Some(number)) - .await .unwrap() .unwrap(); @@ -308,7 +300,6 @@ where let latest_block = self .adapter .get_block_by_number(None) - .await .map_err(|e| Error::Custom(e.to_string()))? .unwrap(); @@ -363,13 +354,11 @@ where let block = self .adapter .get_block_by_number(Some(n)) - .await .map_err(|e| Error::Custom(e.to_string()))? .unwrap(); let receipts = self .adapter .get_receipts_by_hashes(block.header.number, &block.tx_hashes) - .await .map_err(|e| Error::Custom(e.to_string()))?; extend_logs(&mut all_logs, receipts); @@ -383,7 +372,6 @@ where latest_block.header.number, &latest_block.tx_hashes, ) - .await .map_err(|e| Error::Custom(e.to_string()))?; extend_logs(&mut all_logs, receipts); diff --git a/crates/api/src/jsonrpc/impls/web3.rs b/crates/api/src/jsonrpc/impls/web3.rs index ab1df9f..00afcad 100644 --- a/crates/api/src/jsonrpc/impls/web3.rs +++ b/crates/api/src/jsonrpc/impls/web3.rs @@ -32,7 +32,7 @@ impl Web3RpcImpl { Self { adapter } } - async fn call_evm( + fn call_evm( &self, req: Web3CallRequest, data: Bytes, @@ -45,8 +45,8 @@ impl Web3RpcImpl { let header = self .adapter .get_block_header_by_number(number) - .await? - .c(d!("Cannot get {:?} header", number))?; + .c(d!("Cannot get {:?} header", number))? + .c(d!())?; let mock_header = mock_header_by_call_req(header, &req); @@ -61,7 +61,7 @@ impl Web3RpcImpl { mock_header.state_root, mock_header.into(), ) - .await + .c(d!()) } } @@ -77,7 +77,6 @@ impl RTEvmWeb3RpcServer for Web3RpcImpl self.adapter .insert_signed_tx(stx) - .await .map_err(|e| Error::Custom(e.to_string()))?; Ok(hash) @@ -87,14 +86,12 @@ impl RTEvmWeb3RpcServer for Web3RpcImpl let res = self .adapter .get_tx_by_hash(hash) - .await .map_err(|e| Error::Custom(e.to_string()))?; if let Some(stx) = res { if let Some(receipt) = self .adapter .get_receipt_by_tx_hash(hash) - .await .map_err(|e| Error::Custom(e.to_string()))? { Ok(Some((stx, receipt).into())) @@ -117,7 +114,6 @@ impl RTEvmWeb3RpcServer for Web3RpcImpl let block = self .adapter .get_block_by_number(number.into()) - .await .map_err(|e| Error::Custom(e.to_string()))?; match block { @@ -132,7 +128,6 @@ impl RTEvmWeb3RpcServer for Web3RpcImpl let tx = self .adapter .get_tx_by_hash(tx.get_hash()) - .await .map_err(|e| Error::Custom(e.to_string()))? .unwrap(); @@ -161,7 +156,6 @@ impl RTEvmWeb3RpcServer for Web3RpcImpl let block = self .adapter .get_block_by_hash(hash) - .await .map_err(|e| Error::Custom(e.to_string()))?; match block { @@ -176,7 +170,6 @@ impl RTEvmWeb3RpcServer for Web3RpcImpl let tx = self .adapter .get_tx_by_hash(tx.get_hash()) - .await .map_err(|e| Error::Custom(e.to_string()))? .unwrap(); @@ -207,19 +200,16 @@ impl RTEvmWeb3RpcServer for Web3RpcImpl let pending_tx_count = self .adapter .get_pending_tx_count(address) - .await .map_err(|e| Error::Custom(e.to_string()))?; Ok(self .adapter .get_account(address, BlockId::Pending.into()) - .await .map(|account| account.nonce + pending_tx_count) .unwrap_or_default()) } b => Ok(self .adapter .get_account(address, b.into()) - .await .map(|account| account.nonce) .unwrap_or_default()), } @@ -228,7 +218,6 @@ impl RTEvmWeb3RpcServer for Web3RpcImpl async fn block_number(&self) -> RpcResult { self.adapter .get_block_header_by_number(None) - .await .map_err(|e| Error::Custom(e.to_string()))? .map(|h| U256::from(h.number)) .ok_or_else(|| Error::Custom("Cannot get latest block header".to_string())) @@ -242,7 +231,6 @@ impl RTEvmWeb3RpcServer for Web3RpcImpl Ok(self .adapter .get_account(address, number.unwrap_or_default().into()) - .await .map_or(U256::zero(), |account| account.balance)) } @@ -266,7 +254,6 @@ impl RTEvmWeb3RpcServer for Web3RpcImpl .unwrap_or_default(); let resp = self .call_evm(req, data_bytes, number.unwrap_or_default().into()) - .await .map_err(|e| Error::Custom(e.to_string()))?; if resp.exit_reason.is_succeed() { @@ -305,7 +292,6 @@ impl RTEvmWeb3RpcServer for Web3RpcImpl .unwrap_or_default(); let resp = self .call_evm(req.clone(), data_bytes.clone(), num) - .await .map_err(|e| Error::Custom(e.to_string()))?; if resp.exit_reason.is_succeed() { @@ -327,7 +313,6 @@ impl RTEvmWeb3RpcServer for Web3RpcImpl req2.gas = Some(mid); let resp2 = self .call_evm(req2, data_bytes.clone(), num) - .await .map_err(|e| Error::Custom(e.to_string()))?; match resp2.exit_reason { ExitReason::Succeed(_) => { @@ -357,13 +342,11 @@ impl RTEvmWeb3RpcServer for Web3RpcImpl let account = self .adapter .get_account(address, number.unwrap_or_default().into()) - .await .map_err(|e| Error::Custom(e.to_string()))?; let code_result = self .adapter .get_code_by_hash(&account.code_hash) - .await .map_err(|e| Error::Custom(e.to_string()))?; if let Some(code_bytes) = code_result { Ok(Hex::encode(code_bytes)) @@ -376,7 +359,6 @@ impl RTEvmWeb3RpcServer for Web3RpcImpl let block = self .adapter .get_block_by_number(number.into()) - .await .map_err(|e| Error::Custom(e.to_string()))?; let count = match block { Some(bc) => bc.tx_hashes.len(), @@ -389,14 +371,12 @@ impl RTEvmWeb3RpcServer for Web3RpcImpl let res = self .adapter .get_tx_by_hash(hash) - .await .map_err(|e| Error::Custom(e.to_string()))?; if let Some(stx) = res { if let Some(receipt) = self .adapter .get_receipt_by_tx_hash(hash) - .await .map_err(|e| Error::Custom(e.to_string()))? { Ok(Some(Web3Receipt::new(receipt, stx))) @@ -471,7 +451,6 @@ impl RTEvmWeb3RpcServer for Web3RpcImpl match position { BlockPosition::Hash(hash) => match adapter .get_block_by_hash(hash) - .await .map_err(|e| Error::Custom(e.to_string()))? { Some(block) => { @@ -480,7 +459,6 @@ impl RTEvmWeb3RpcServer for Web3RpcImpl block.header.number, &block.tx_hashes, ) - .await .map_err(|e| Error::Custom(e.to_string()))?; extend_logs(logs, receipts, early_return); Ok(()) @@ -494,12 +472,10 @@ impl RTEvmWeb3RpcServer for Web3RpcImpl BlockPosition::Num(n) => { let block = adapter .get_block_by_number(Some(n)) - .await .map_err(|e| Error::Custom(e.to_string()))? .unwrap(); let receipts = adapter .get_receipts_by_hashes(block.header.number, &block.tx_hashes) - .await .map_err(|e| Error::Custom(e.to_string()))?; extend_logs(logs, receipts, early_return); @@ -508,7 +484,6 @@ impl RTEvmWeb3RpcServer for Web3RpcImpl BlockPosition::Block(block) => { let receipts = adapter .get_receipts_by_hashes(block.header.number, &block.tx_hashes) - .await .map_err(|e| Error::Custom(e.to_string()))?; extend_logs(logs, receipts, early_return); @@ -536,7 +511,6 @@ impl RTEvmWeb3RpcServer for Web3RpcImpl let latest_block = self .adapter .get_block_by_number(None) - .await .map_err(|e| Error::Custom(e.to_string()))? .unwrap(); let latest_number = latest_block.header.number; @@ -622,7 +596,6 @@ impl RTEvmWeb3RpcServer for Web3RpcImpl Ok(self .adapter .get_block_by_hash(hash) - .await .map_err(|e| Error::Custom(e.to_string()))? .map(|b| U256::from(b.tx_hashes.len())) .unwrap_or_default()) @@ -647,7 +620,6 @@ impl RTEvmWeb3RpcServer for Web3RpcImpl let block = self .adapter .get_block_by_hash(hash) - .await .map_err(|e| Error::Custom(e.to_string()))?; if let Some(block) = block { @@ -678,7 +650,6 @@ impl RTEvmWeb3RpcServer for Web3RpcImpl let block = self .adapter .get_block_by_number(number.into()) - .await .map_err(|e| Error::Custom(e.to_string()))?; if let Some(block) = block { @@ -698,13 +669,11 @@ impl RTEvmWeb3RpcServer for Web3RpcImpl let block = self .adapter .get_block_by_number(number.unwrap_or_default().into()) - .await .map_err(|e| Error::Custom(e.to_string()))? .ok_or_else(|| Error::Custom("Can't find this block".to_string()))?; let value = self .adapter .get_storage_at(address, position, block.header.state_root) - .await .unwrap_or_else(|_| H256::default().as_bytes().to_vec()); Ok(Hex::encode(value)) diff --git a/crates/api/src/jsonrpc/ws_subscription.rs b/crates/api/src/jsonrpc/ws_subscription.rs index a58e800..1cedaa9 100644 --- a/crates/api/src/jsonrpc/ws_subscription.rs +++ b/crates/api/src/jsonrpc/ws_subscription.rs @@ -84,11 +84,7 @@ where Adapter: APIAdapter + 'static, { pub async fn new(adapter: Arc, recv: Receiver) -> Self { - let latest = adapter - .get_block_header_by_number(None) - .await - .unwrap() - .unwrap(); + let latest = adapter.get_block_header_by_number(None).unwrap().unwrap(); Self { log_hubs: Vec::new(), @@ -105,12 +101,7 @@ where self.sync_hubs.retain(|hub| !hub.sink.is_closed()); self.log_hubs.retain(|hub| !hub.sink.is_closed()); - let latest_block = self - .adapter - .get_block_by_number(None) - .await - .unwrap() - .unwrap(); + let latest_block = self.adapter.get_block_by_number(None).unwrap().unwrap(); if self.current_number == latest_block.header.number { return; @@ -125,7 +116,6 @@ where let block = self .adapter .get_block_by_number(Some(number)) - .await .unwrap() .unwrap(); @@ -160,7 +150,6 @@ where let block = self .adapter .get_block_by_number(Some(number)) - .await .unwrap() .unwrap(); @@ -174,7 +163,6 @@ where let receipts = self .adapter .get_receipts_by_hashes(number, &tx_hashes) - .await .unwrap(); let mut index = 0; diff --git a/crates/executor/Cargo.toml b/crates/executor/Cargo.toml index 6b1b699..2d03925 100644 --- a/crates/executor/Cargo.toml +++ b/crates/executor/Cargo.toml @@ -5,6 +5,7 @@ version = "0.1.0" [dependencies] ruc = { workspace = true } + rlp = { workspace = true } evm = { workspace = true } diff --git a/crates/executor/src/adapter/mod.rs b/crates/executor/src/adapter/mod.rs index f0bb783..ea3d795 100644 --- a/crates/executor/src/adapter/mod.rs +++ b/crates/executor/src/adapter/mod.rs @@ -45,7 +45,7 @@ impl<'a> ExecutorAdapter for RTEvmExecutorAdapter<'a> { } fn commit(&mut self) -> MerkleRoot { - self.state.commit().into() + self.commit() } fn get(&self, key: &[u8]) -> Option> { diff --git a/crates/executor/src/lib.rs b/crates/executor/src/lib.rs index 7f1aaac..209dbc5 100644 --- a/crates/executor/src/lib.rs +++ b/crates/executor/src/lib.rs @@ -33,7 +33,8 @@ use std::collections::BTreeMap; pub struct RTEvmExecutor; impl Executor for RTEvmExecutor { - // Used for query data API, this function will not modify the world state. + // Used for query data API, + // this function will not modify the world state. fn call( &self, backend: &B, @@ -124,7 +125,7 @@ impl Executor for RTEvmExecutor { let mut r = Self::evm_exec(backend, &config, &precompiles, tx); - backend.commit(); + // backend.commit(); r.logs = backend.get_logs(); gas += r.gas_used; @@ -136,7 +137,7 @@ impl Executor for RTEvmExecutor { res.push(r); } - // Get the new root, the look-like `commit` is a noop here + // Get the new root let new_state_root = backend.commit(); let transaction_root = trie_root_indexed(&tx_hashes); diff --git a/crates/model/src/traits/api.rs b/crates/model/src/traits/api.rs index 73cd2fa..50e6b3e 100644 --- a/crates/model/src/traits/api.rs +++ b/crates/model/src/traits/api.rs @@ -1,51 +1,41 @@ -use crate::{ - async_trait, - types::{ - Account, Block, BlockNumber, Hash, Header, Proposal, Receipt, SignedTransaction, - TxResp, H160, U256, - }, +use crate::types::{ + Account, Block, BlockNumber, Hash, Header, Proposal, Receipt, SignedTransaction, + TxResp, H160, U256, }; use ruc::*; -#[async_trait] pub trait APIAdapter: Send + Sync { - async fn insert_signed_tx(&self, signed_tx: SignedTransaction) -> Result<()>; + fn insert_signed_tx(&self, signed_tx: SignedTransaction) -> Result<()>; - async fn get_block_by_number(&self, height: Option) -> Result>; + fn get_block_by_number(&self, height: Option) -> Result>; - async fn get_block_by_hash(&self, hash: Hash) -> Result>; + fn get_block_by_hash(&self, hash: Hash) -> Result>; - async fn get_block_header_by_number( - &self, - height: Option, - ) -> Result>; + fn get_block_header_by_number(&self, height: Option) -> Result>; - async fn get_receipt_by_tx_hash(&self, tx_hash: Hash) -> Result>; + fn get_receipt_by_tx_hash(&self, tx_hash: Hash) -> Result>; - async fn get_receipts_by_hashes( + fn get_receipts_by_hashes( &self, block_number: u64, tx_hashes: &[Hash], ) -> Result>>; - async fn get_tx_by_hash(&self, tx_hash: Hash) -> Result>; + fn get_tx_by_hash(&self, tx_hash: Hash) -> Result>; - async fn get_txs_by_hashes( + fn get_txs_by_hashes( &self, block_number: u64, tx_hashes: &[Hash], ) -> Result>>; - async fn get_account( - &self, - address: H160, - number: Option, - ) -> Result; + fn get_account(&self, address: H160, number: Option) + -> Result; - async fn get_pending_tx_count(&self, address: H160) -> Result; + fn get_pending_tx_count(&self, address: H160) -> Result; #[allow(clippy::too_many_arguments)] - async fn evm_call( + fn evm_call( &self, from: Option, to: Option, @@ -57,9 +47,9 @@ pub trait APIAdapter: Send + Sync { proposal: Proposal, ) -> Result; - async fn get_code_by_hash(&self, hash: &Hash) -> Result>>; + fn get_code_by_hash(&self, hash: &Hash) -> Result>>; - async fn get_storage_at( + fn get_storage_at( &self, address: H160, position: U256,