Skip to content

Commit

Permalink
isolateevm_call and evm_exec
Browse files Browse the repository at this point in the history
  • Loading branch information
ktmlm committed Jul 23, 2023
1 parent ed0a642 commit f922231
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 121 deletions.
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

####################################################################
####################################################################
Expand Down Expand Up @@ -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]
1 change: 1 addition & 0 deletions crates/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ rt-evm-model = { workspace = true }
rt-evm-executor = { workspace = true }
rt-evm-mempool = { workspace = true }
rt-evm-storage = { workspace = true }
rt-evm-blockmgmt = { workspace = true }

[dev-dependencies]
fastrand = "1.8"
Expand Down
42 changes: 16 additions & 26 deletions crates/api/src/adapter.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -33,13 +32,13 @@ impl DefaultAPIAdapter {
}
}

pub async fn evm_backend(
pub fn evm_backend(
&self,
number: Option<BlockNumber>,
) -> Result<RTEvmExecutorAdapter> {
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);
Expand All @@ -53,68 +52,59 @@ 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<u64>) -> Result<Option<Block>> {
fn get_block_by_number(&self, height: Option<u64>) -> Result<Option<Block>> {
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<Option<Block>> {
fn get_block_by_hash(&self, hash: Hash) -> Result<Option<Block>> {
self.storage.get_block_by_hash(&hash)
}

async fn get_block_header_by_number(
&self,
number: Option<u64>,
) -> Result<Option<Header>> {
fn get_block_header_by_number(&self, number: Option<u64>) -> Result<Option<Header>> {
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<Option<Receipt>> {
fn get_receipt_by_tx_hash(&self, tx_hash: Hash) -> Result<Option<Receipt>> {
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],
) -> Result<Vec<Option<Receipt>>> {
self.storage.get_receipts(block_number, tx_hashes)
}

async fn get_tx_by_hash(&self, tx_hash: Hash) -> Result<Option<SignedTransaction>> {
fn get_tx_by_hash(&self, tx_hash: Hash) -> Result<Option<SignedTransaction>> {
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],
) -> Result<Vec<Option<SignedTransaction>>> {
self.storage.get_txs(block_number, tx_hashes)
}

async fn get_account(
fn get_account(
&self,
address: H160,
number: Option<BlockNumber>,
) -> Result<Account> {
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(),
Expand All @@ -125,11 +115,11 @@ impl APIAdapter for DefaultAPIAdapter {
}
}

async fn get_pending_tx_count(&self, address: H160) -> Result<U256> {
fn get_pending_tx_count(&self, address: H160) -> Result<U256> {
Ok(self.mempool.tx_pending_cnt(Some(address)).into())
}

async fn evm_call(
fn evm_call(
&self,
from: Option<H160>,
to: Option<H160>,
Expand Down Expand Up @@ -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<Option<Vec<u8>>> {
fn get_code_by_hash(&self, hash: &Hash) -> Result<Option<Vec<u8>>> {
self.storage.get_code_by_hash(hash)
}

async fn get_storage_at(
fn get_storage_at(
&self,
address: H160,
position: U256,
Expand Down
14 changes: 1 addition & 13 deletions crates/api/src/jsonrpc/impls/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -224,7 +223,6 @@ where
let header = self
.adapter
.get_block_header_by_number(None)
.await
.unwrap()
.unwrap();
self.blocks_hub
Expand Down Expand Up @@ -266,12 +264,7 @@ where

async fn filter_block(&mut self, id: &U256) -> Vec<H256> {
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();
}
Expand All @@ -283,7 +276,6 @@ where
let block = self
.adapter
.get_block_by_number(Some(number))
.await
.unwrap()
.unwrap();

Expand All @@ -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();

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Loading

0 comments on commit f922231

Please sign in to comment.