Skip to content

Commit

Permalink
Implement Deref and DerefMut for StateProviderDatabase (paradig…
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger authored Feb 15, 2024
1 parent 9450319 commit 4a8a68b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
27 changes: 16 additions & 11 deletions crates/revm/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use revm::{
primitives::{AccountInfo, Bytecode},
Database, StateDBBox,
};
use std::ops::{Deref, DerefMut};

/// SubState of database. Uses revm internal cache with binding to reth StateProvider trait.
pub type SubState<DB> = CacheDB<StateProviderDatabase<DB>>;
Expand All @@ -23,19 +24,23 @@ impl<DB: StateProvider> StateProviderDatabase<DB> {
Self(db)
}

/// Return inner state reference
pub fn state(&self) -> &DB {
&self.0
/// Consume State and return inner StateProvider.
pub fn into_inner(self) -> DB {
self.0
}
}

/// Return inner state mutable reference
pub fn state_mut(&mut self) -> &mut DB {
&mut self.0
impl<DB: StateProvider> Deref for StateProviderDatabase<DB> {
type Target = DB;

fn deref(&self) -> &Self::Target {
&self.0
}
}

/// Consume State and return inner StateProvider.
pub fn into_inner(self) -> DB {
self.0
impl<DB: StateProvider> DerefMut for StateProviderDatabase<DB> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

Expand Down Expand Up @@ -81,7 +86,7 @@ impl<DB: StateProvider> DatabaseRef for StateProviderDatabase<DB> {
/// Returns `Ok` with `Some(AccountInfo)` if the account exists,
/// `None` if it doesn't, or an error if encountered.
fn basic_ref(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
Ok(self.0.basic_account(address)?.map(|account| AccountInfo {
Ok(self.basic_account(address)?.map(|account| AccountInfo {
balance: account.balance,
nonce: account.nonce,
code_hash: account.bytecode_hash.unwrap_or(KECCAK_EMPTY),
Expand All @@ -93,7 +98,7 @@ impl<DB: StateProvider> DatabaseRef for StateProviderDatabase<DB> {
///
/// Returns `Ok` with the bytecode if found, or the default bytecode otherwise.
fn code_by_hash_ref(&self, code_hash: B256) -> Result<Bytecode, Self::Error> {
Ok(self.0.bytecode_by_hash(code_hash)?.unwrap_or_default().0)
Ok(self.bytecode_by_hash(code_hash)?.unwrap_or_default().0)
}

/// Retrieves the storage value at a specific index for a given address.
Expand Down
11 changes: 3 additions & 8 deletions crates/rpc/rpc/src/eth/api/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ where
// if the request is a simple transfer we can optimize
if env.tx.data.is_empty() {
if let TransactTo::Call(to) = env.tx.transact_to {
if let Ok(code) = db.db.state().account_code(to) {
if let Ok(code) = db.db.account_code(to) {
let no_code_callee = code.map(|code| code.is_empty()).unwrap_or(true);
if no_code_callee {
// simple transfer, check if caller has sufficient funds
Expand Down Expand Up @@ -427,13 +427,8 @@ where

// calculate the gas used using the access list
request.access_list = Some(access_list.clone());
let gas_used = self.estimate_gas_with(
cfg_with_spec_id,
env.block.clone(),
request,
db.db.state(),
None,
)?;
let gas_used =
self.estimate_gas_with(cfg_with_spec_id, env.block.clone(), request, &*db.db, None)?;

Ok(AccessListWithGasUsed { access_list, gas_used })
}
Expand Down

0 comments on commit 4a8a68b

Please sign in to comment.