Skip to content

Commit

Permalink
chore: use dyn DatabaseExt everywhere (#8924)
Browse files Browse the repository at this point in the history
* wip

* feat: use `dyn DatabaseExt` (#9010)

* wip

* clean up

* fix

* clippy

* doc

* fix imports

* chore: simplify InspectorExt by making it lifetime-generic

* fmt

* chore: remove unnecessary casts and lifetimes

* chore: more unused lifetimes (clippy)

---------

Co-authored-by: DaniPopes <[email protected]>

---------

Co-authored-by: Arsenii Kulikov <[email protected]>
  • Loading branch information
DaniPopes and klkvr authored Oct 3, 2024
1 parent 6a1e0b7 commit 471e4ac
Show file tree
Hide file tree
Showing 23 changed files with 499 additions and 629 deletions.
42 changes: 38 additions & 4 deletions crates/anvil/src/eth/backend/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ use foundry_evm::{
},
},
traces::CallTraceNode,
utils::alphanet_handler_register,
};
use revm::primitives::MAX_BLOB_GAS_PER_BLOCK;
use revm::{db::WrapDatabaseRef, primitives::MAX_BLOB_GAS_PER_BLOCK};
use std::sync::Arc;

/// Represents an executed transaction (transacted on the DB)
Expand Down Expand Up @@ -303,7 +304,7 @@ impl<'a, 'b, DB: Db + ?Sized, V: TransactionValidator> Iterator
let nonce = account.nonce;

// records all call and step traces
let mut inspector = Inspector::default().with_tracing().with_alphanet(self.alphanet);
let mut inspector = Inspector::default().with_tracing();
if self.enable_steps_tracing {
inspector = inspector.with_steps_tracing();
}
Expand All @@ -312,8 +313,7 @@ impl<'a, 'b, DB: Db + ?Sized, V: TransactionValidator> Iterator
}

let exec_result = {
let mut evm =
foundry_evm::utils::new_evm_with_inspector(&mut *self.db, env, &mut inspector);
let mut evm = new_evm_with_inspector(&mut *self.db, env, &mut inspector, self.alphanet);
if let Some(factory) = &self.precompile_factory {
inject_precompiles(&mut evm, factory.precompiles());
}
Expand Down Expand Up @@ -396,3 +396,37 @@ fn build_logs_bloom(logs: Vec<Log>, bloom: &mut Bloom) {
}
}
}

/// Creates a database with given database and inspector, optionally enabling alphanet features.
pub fn new_evm_with_inspector<DB: revm::Database>(
db: DB,
env: EnvWithHandlerCfg,
inspector: &mut dyn revm::Inspector<DB>,
alphanet: bool,
) -> revm::Evm<'_, &mut dyn revm::Inspector<DB>, DB> {
let EnvWithHandlerCfg { env, handler_cfg } = env;

let mut handler = revm::Handler::new(handler_cfg);

handler.append_handler_register_plain(revm::inspector_handle_register);
if alphanet {
handler.append_handler_register_plain(alphanet_handler_register);
}

let context = revm::Context::new(revm::EvmContext::new_with_env(db, env), inspector);

revm::Evm::new(context, handler)
}

/// Creates a new EVM with the given inspector and wraps the database in a `WrapDatabaseRef`.
pub fn new_evm_with_inspector_ref<'a, DB>(
db: DB,
env: EnvWithHandlerCfg,
inspector: &mut dyn revm::Inspector<WrapDatabaseRef<DB>>,
alphanet: bool,
) -> revm::Evm<'a, &mut dyn revm::Inspector<WrapDatabaseRef<DB>>, WrapDatabaseRef<DB>>
where
DB: revm::DatabaseRef,
{
new_evm_with_inspector(WrapDatabaseRef(db), env, inspector, alphanet)
}
15 changes: 0 additions & 15 deletions crates/anvil/src/eth/backend/mem/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use foundry_evm::{
EvmContext,
},
traces::TracingInspectorConfig,
InspectorExt,
};

/// The [`revm::Inspector`] used when transacting in the evm
Expand All @@ -23,8 +22,6 @@ pub struct Inspector {
pub tracer: Option<TracingInspector>,
/// collects all `console.sol` logs
pub log_collector: Option<LogCollector>,
/// Whether to enable Alphanet support
pub alphanet: bool,
}

impl Inspector {
Expand Down Expand Up @@ -59,12 +56,6 @@ impl Inspector {
self.log_collector = Some(Default::default());
self
}

/// Enables Alphanet features
pub fn with_alphanet(mut self, yes: bool) -> Self {
self.alphanet = yes;
self
}
}

impl<DB: Database> revm::Inspector<DB> for Inspector {
Expand Down Expand Up @@ -176,12 +167,6 @@ impl<DB: Database> revm::Inspector<DB> for Inspector {
}
}

impl<DB: Database> InspectorExt<DB> for Inspector {
fn is_alphanet(&self) -> bool {
self.alphanet
}
}

/// Prints all the logs
pub fn print_logs(logs: &[Log]) {
for log in decode_console_logs(logs) {
Expand Down
14 changes: 6 additions & 8 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! In-memory blockchain backend.

use self::state::trie_storage;
use super::executor::new_evm_with_inspector_ref;
use crate::{
config::PruneStateHistoryConfig,
eth::{
Expand Down Expand Up @@ -32,6 +33,7 @@ use crate::{
revm::{db::DatabaseRef, primitives::AccountInfo},
ForkChoice, NodeConfig, PrecompileFactory,
};
use alloy_chains::NamedChain;
use alloy_consensus::{Account, Header, Receipt, ReceiptWithBloom};
use alloy_eips::eip4844::MAX_BLOBS_PER_BLOCK;
use alloy_primitives::{keccak256, Address, Bytes, TxHash, TxKind, B256, U256, U64};
Expand Down Expand Up @@ -64,8 +66,6 @@ use anvil_core::eth::{
utils::meets_eip155,
};
use anvil_rpc::error::RpcError;

use alloy_chains::NamedChain;
use flate2::{read::GzDecoder, write::GzEncoder, Compression};
use foundry_evm::{
backend::{DatabaseError, DatabaseResult, RevertStateSnapshotAction},
Expand All @@ -81,8 +81,6 @@ use foundry_evm::{
},
},
traces::TracingInspectorConfig,
utils::new_evm_with_inspector_ref,
InspectorExt,
};
use futures::channel::mpsc::{unbounded, UnboundedSender};
use parking_lot::{Mutex, RwLock};
Expand Down Expand Up @@ -864,15 +862,15 @@ impl Backend {
&self,
db: &'db dyn DatabaseRef<Error = DatabaseError>,
env: EnvWithHandlerCfg,
inspector: &'i mut dyn InspectorExt<
inspector: &'i mut dyn revm::Inspector<
WrapDatabaseRef<&'db dyn DatabaseRef<Error = DatabaseError>>,
>,
) -> revm::Evm<
'_,
&'i mut dyn InspectorExt<WrapDatabaseRef<&'db dyn DatabaseRef<Error = DatabaseError>>>,
&'i mut dyn revm::Inspector<WrapDatabaseRef<&'db dyn DatabaseRef<Error = DatabaseError>>>,
WrapDatabaseRef<&'db dyn DatabaseRef<Error = DatabaseError>>,
> {
let mut evm = new_evm_with_inspector_ref(db, env, inspector);
let mut evm = new_evm_with_inspector_ref(db, env, inspector, self.alphanet);
if let Some(factory) = &self.precompile_factory {
inject_precompiles(&mut evm, factory.precompiles());
}
Expand Down Expand Up @@ -1269,7 +1267,7 @@ impl Backend {

/// Builds [`Inspector`] with the configured options
fn build_inspector(&self) -> Inspector {
let mut inspector = Inspector::default().with_alphanet(self.alphanet);
let mut inspector = Inspector::default();

if self.print_logs {
inspector = inspector.with_log_collector();
Expand Down
Loading

0 comments on commit 471e4ac

Please sign in to comment.