Skip to content

Commit

Permalink
Improve EthersDB (#1208)
Browse files Browse the repository at this point in the history
* Improve EthersDB:
  - Rewrite(kinda) `new` function.
  - Add `set_block_number` function.
  - Move `block_on` function to separate module.
Add `utils` module.

* Remove `utils` module.

* Remove async blocks

* Make fmt happy

* Add `use super::*` back.
  • Loading branch information
CorrM authored Mar 22, 2024
1 parent 882211c commit 09df19c
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions crates/revm/src/db/ethersdb.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::primitives::{AccountInfo, Address, Bytecode, B256, KECCAK_EMPTY, U256};
use crate::{Database, DatabaseRef};
use ethers_core::types::{BlockId, H160 as eH160, H256, U64 as eU64};
use ethers_providers::Middleware;
use std::sync::Arc;

use ethers_core::types::{Block, BlockId, TxHash, H160 as eH160, H256, U64 as eU64};
use ethers_providers::Middleware;
use tokio::runtime::{Builder, Handle, RuntimeFlavor};

use crate::primitives::{AccountInfo, Address, Bytecode, B256, KECCAK_EMPTY, U256};
use crate::{Database, DatabaseRef};

#[derive(Debug, Clone)]
pub struct EthersDB<M: Middleware> {
client: Arc<M>,
Expand All @@ -14,25 +16,23 @@ pub struct EthersDB<M: Middleware> {
impl<M: Middleware> EthersDB<M> {
/// create ethers db connector inputs are url and block on what we are basing our database (None for latest)
pub fn new(client: Arc<M>, block_number: Option<BlockId>) -> Option<Self> {
let client = client;
let mut out = Self {
client,
block_number: None,
};

out.block_number = if block_number.is_some() {
let block_number: Option<BlockId> = if block_number.is_some() {
block_number
} else {
Some(BlockId::from(
out.block_on(out.client.get_block_number()).ok()?,
Self::block_on(client.get_block_number()).ok()?,
))
};

Some(out)
Some(Self {
client,
block_number,
})
}

/// internal utility function to call tokio feature and wait for output
fn block_on<F>(&self, f: F) -> F::Output
#[inline]
fn block_on<F>(f: F) -> F::Output
where
F: core::future::Future + Send,
F::Output: Send,
Expand Down Expand Up @@ -61,6 +61,12 @@ impl<M: Middleware> EthersDB<M> {
.block_on(f),
}
}

/// set block number on which upcoming queries will be based
#[inline]
pub fn set_block_number(&mut self, block_number: BlockId) {
self.block_number = Some(block_number);
}
}

impl<M: Middleware> DatabaseRef for EthersDB<M> {
Expand All @@ -75,7 +81,7 @@ impl<M: Middleware> DatabaseRef for EthersDB<M> {
let code = self.client.get_code(add, self.block_number);
tokio::join!(nonce, balance, code)
};
let (nonce, balance, code) = self.block_on(f);
let (nonce, balance, code) = Self::block_on(f);

let balance = U256::from_limbs(balance?.0);
let nonce = nonce?.as_u64();
Expand All @@ -92,26 +98,22 @@ impl<M: Middleware> DatabaseRef for EthersDB<M> {
fn storage_ref(&self, address: Address, index: U256) -> Result<U256, Self::Error> {
let add = eH160::from(address.0 .0);
let index = H256::from(index.to_be_bytes());
let f = async {
let storage = self
.client
.get_storage_at(add, index, self.block_number)
.await?;
Ok(U256::from_be_bytes(storage.to_fixed_bytes()))
};
self.block_on(f)
let slot_value: H256 =
Self::block_on(self.client.get_storage_at(add, index, self.block_number))?;
Ok(U256::from_be_bytes(slot_value.to_fixed_bytes()))
}

fn block_hash_ref(&self, number: U256) -> Result<B256, Self::Error> {
// saturate usize
if number > U256::from(u64::MAX) {
return Ok(KECCAK_EMPTY);
}
// We known number <= u64::MAX so unwrap is safe
// We know number <= u64::MAX so unwrap is safe
let number = eU64::from(u64::try_from(number).unwrap());
let f = async { self.client.get_block(BlockId::from(number)).await };
let block: Option<Block<TxHash>> =
Self::block_on(self.client.get_block(BlockId::from(number)))?;
// If number is given, the block is supposed to be finalized so unwrap is safe too.
Ok(B256::new(self.block_on(f)?.unwrap().hash.unwrap().0))
Ok(B256::new(block.unwrap().hash.unwrap().0))
}
}

Expand Down

0 comments on commit 09df19c

Please sign in to comment.