Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/anvil/src/eth/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2981,7 +2981,7 @@ impl EthApi {
if let Some(block_overrides) = overrides.block {
cache_db.apply_block_overrides(*block_overrides, &mut block);
}
this.do_estimate_gas_with_state(request, &cache_db as &dyn DatabaseRef, block)
this.do_estimate_gas_with_state(request, &cache_db, block)
})
.await?
})
Expand Down
23 changes: 0 additions & 23 deletions crates/anvil/src/eth/backend/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ use crate::mem::storage::MinedTransaction;

/// Helper trait get access to the full state data of the database
pub trait MaybeFullDatabase: DatabaseRef<Error = DatabaseError> + Debug {
/// Returns a reference to the database as a `dyn DatabaseRef`.
// TODO: Required until trait upcasting is stabilized: <https://github.com/rust-lang/rust/issues/65991>
fn as_dyn(&self) -> &dyn DatabaseRef<Error = DatabaseError>;

fn maybe_as_full_db(&self) -> Option<&HashMap<Address, DbAccount>> {
None
}
Expand All @@ -63,10 +59,6 @@ impl<'a, T: 'a + MaybeFullDatabase + ?Sized> MaybeFullDatabase for &'a T
where
&'a T: DatabaseRef<Error = DatabaseError>,
{
fn as_dyn(&self) -> &dyn DatabaseRef<Error = DatabaseError> {
T::as_dyn(self)
}

fn maybe_as_full_db(&self) -> Option<&HashMap<Address, DbAccount>> {
T::maybe_as_full_db(self)
}
Expand Down Expand Up @@ -202,13 +194,6 @@ pub trait Db:
fn current_state(&self) -> StateDb;
}

impl dyn Db {
// TODO: Required until trait upcasting is stabilized: <https://github.com/rust-lang/rust/issues/65991>
pub fn as_dbref(&self) -> &dyn DatabaseRef<Error = DatabaseError> {
self.as_dyn()
}
}

/// Convenience impl only used to use any `Db` on the fly as the db layer for revm's CacheDB
/// This is useful to create blocks without actually writing to the `Db`, but rather in the cache of
/// the `CacheDB` see also
Expand Down Expand Up @@ -251,10 +236,6 @@ impl<T: DatabaseRef<Error = DatabaseError> + Send + Sync + Clone + fmt::Debug> D
}

impl<T: DatabaseRef<Error = DatabaseError> + Debug> MaybeFullDatabase for CacheDB<T> {
fn as_dyn(&self) -> &dyn DatabaseRef<Error = DatabaseError> {
self
}

fn maybe_as_full_db(&self) -> Option<&HashMap<Address, DbAccount>> {
Some(&self.cache.accounts)
}
Expand Down Expand Up @@ -364,10 +345,6 @@ impl DatabaseRef for StateDb {
}

impl MaybeFullDatabase for StateDb {
fn as_dyn(&self) -> &dyn DatabaseRef<Error = DatabaseError> {
self.0.as_dyn()
}

fn maybe_as_full_db(&self) -> Option<&HashMap<Address, DbAccount>> {
self.0.maybe_as_full_db()
}
Expand Down
14 changes: 2 additions & 12 deletions crates/anvil/src/eth/backend/mem/fork_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ use crate::eth::backend::db::{
use alloy_primitives::{Address, B256, U256, map::HashMap};
use alloy_rpc_types::BlockId;
use foundry_evm::{
backend::{
BlockchainDb, DatabaseError, DatabaseResult, RevertStateSnapshotAction, StateSnapshot,
},
backend::{BlockchainDb, DatabaseResult, RevertStateSnapshotAction, StateSnapshot},
fork::database::ForkDbStateSnapshot,
};
use revm::{
context::BlockEnv,
database::{Database, DatabaseRef, DbAccount},
database::{Database, DbAccount},
state::AccountInfo,
};

Expand Down Expand Up @@ -89,10 +87,6 @@ impl Db for ForkedDatabase {
}

impl MaybeFullDatabase for ForkedDatabase {
fn as_dyn(&self) -> &dyn DatabaseRef<Error = DatabaseError> {
self
}

fn maybe_as_full_db(&self) -> Option<&HashMap<Address, DbAccount>> {
Some(&self.database().cache.accounts)
}
Expand Down Expand Up @@ -128,10 +122,6 @@ impl MaybeFullDatabase for ForkedDatabase {
}

impl MaybeFullDatabase for ForkDbStateSnapshot {
fn as_dyn(&self) -> &dyn DatabaseRef<Error = DatabaseError> {
self
}

fn maybe_as_full_db(&self) -> Option<&HashMap<Address, DbAccount>> {
Some(&self.local.cache.accounts)
}
Expand Down
4 changes: 0 additions & 4 deletions crates/anvil/src/eth/backend/mem/in_memory_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ impl Db for MemDb {
}

impl MaybeFullDatabase for MemDb {
fn as_dyn(&self) -> &dyn DatabaseRef<Error = foundry_evm::backend::DatabaseError> {
self
}

fn maybe_as_full_db(&self) -> Option<&HashMap<Address, DbAccount>> {
Some(&self.inner.cache.accounts)
}
Expand Down
34 changes: 12 additions & 22 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ pub mod inspector;
pub mod state;
pub mod storage;

/// Helper trait that combines DatabaseRef with Debug.
/// Helper trait that combines revm::DatabaseRef with Debug.
/// This is needed because alloy-evm requires Debug on Database implementations.
/// Specific implementation for dyn Db since trait object upcasting is not stable.
/// With trait upcasting now stable, we can now upcast from this trait to revm::DatabaseRef.
pub trait DatabaseRef: revm::DatabaseRef<Error = DatabaseError> + Debug {}
impl<T> DatabaseRef for T where T: revm::DatabaseRef<Error = DatabaseError> + Debug {}
impl DatabaseRef for dyn crate::eth::backend::db::Db {}
Expand Down Expand Up @@ -1503,7 +1503,7 @@ impl Backend {
if let Some(block_overrides) = overrides.block {
cache_db.apply_block_overrides(*block_overrides, &mut block);
}
self.call_with_state(&cache_db as &dyn DatabaseRef, request, fee_details, block)
self.call_with_state(&cache_db, request, fee_details, block)
}?;
trace!(target: "backend", "call return {:?} out: {:?} gas {} on block {}", exit, out, gas, block_number);
Ok((exit, out, gas, state))
Expand Down Expand Up @@ -1715,7 +1715,7 @@ impl Backend {
// recorded and included in logs
inspector = inspector.with_transfers();
let mut evm= self.new_evm_with_inspector_ref(
&cache_db as &dyn DatabaseRef,
&cache_db,
&env,
&mut inspector,
);
Expand All @@ -1724,7 +1724,7 @@ impl Backend {
evm.transact(env.tx)?
} else {
let mut evm = self.new_evm_with_inspector_ref(
&cache_db as &dyn DatabaseRef,
&cache_db,
&env,
&mut inspector,
);
Expand Down Expand Up @@ -1932,11 +1932,8 @@ impl Backend {
);

let env = self.build_call_env(request, fee_details, block);
let mut evm = self.new_evm_with_inspector_ref(
&cache_db as &dyn DatabaseRef,
&env,
&mut inspector,
);
let mut evm =
self.new_evm_with_inspector_ref(&cache_db, &env, &mut inspector);
let ResultAndState { result, state: _ } = evm.transact(env.tx)?;

drop(evm);
Expand Down Expand Up @@ -1968,11 +1965,8 @@ impl Backend {
.map_err(|err| BlockchainError::Message(err.to_string()))?;

let env = self.build_call_env(request, fee_details, block.clone());
let mut evm = self.new_evm_with_inspector_ref(
&cache_db as &dyn DatabaseRef,
&env,
&mut inspector,
);
let mut evm =
self.new_evm_with_inspector_ref(&cache_db, &env, &mut inspector);
let result = evm.transact(env.tx.clone())?;
let res = evm
.inspector_mut()
Expand All @@ -1990,11 +1984,7 @@ impl Backend {
.with_tracing_config(TracingInspectorConfig::from_geth_config(&config));

let env = self.build_call_env(request, fee_details, block);
let mut evm = self.new_evm_with_inspector_ref(
&cache_db as &dyn DatabaseRef,
&env,
&mut inspector,
);
let mut evm = self.new_evm_with_inspector_ref(&cache_db, &env, &mut inspector);
let ResultAndState { result, state: _ } = evm.transact(env.tx)?;

let (exit_reason, gas_used, out) = match result {
Expand Down Expand Up @@ -2502,7 +2492,7 @@ impl Backend {

pub fn get_code_with_state(
&self,
state: &dyn DatabaseRef<Error = DatabaseError>,
state: &dyn DatabaseRef,
address: Address,
) -> Result<Bytes, BlockchainError> {
trace!(target: "backend", "get code for {:?}", address);
Expand Down Expand Up @@ -2554,7 +2544,7 @@ impl Backend {
address: Address,
) -> Result<U256, BlockchainError>
where
D: DatabaseRef<Error = DatabaseError>,
D: DatabaseRef,
{
trace!(target: "backend", "get balance for {:?}", address);
Ok(state.basic_ref(address)?.unwrap_or_default().balance)
Expand Down
Loading