Skip to content

Commit 22e7b3a

Browse files
authored
chore(anvil): create EitherEvm using OpEvm and EthEvm factories (#12764)
chore(anvil): simplify `EitherEvm` creation by utilizing factory methods for `OpEvm` and `EthEvm` remove useless indirection for evm creation with `WrapDatabaseRef`
1 parent 3a46316 commit 22e7b3a

File tree

2 files changed

+16
-83
lines changed

2 files changed

+16
-83
lines changed

crates/anvil/src/eth/backend/executor.rs

Lines changed: 14 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ use alloy_eips::{
2323
eip7840::BlobParams,
2424
};
2525
use alloy_evm::{
26-
EthEvm, Evm, FromRecoveredTx,
26+
EthEvmFactory, Evm, EvmEnv, EvmFactory, FromRecoveredTx,
2727
eth::EthEvmContext,
2828
precompiles::{DynPrecompile, Precompile, PrecompilesMap},
2929
};
30-
use alloy_op_evm::OpEvm;
30+
use alloy_op_evm::OpEvmFactory;
3131
use alloy_primitives::{B256, Bloom, BloomInput, Log};
3232
use anvil_core::eth::{
3333
block::{BlockInfo, create_block},
@@ -40,17 +40,12 @@ use foundry_evm::{
4040
};
4141
use foundry_evm_networks::NetworkConfigs;
4242
use foundry_primitives::{FoundryReceiptEnvelope, FoundryTxEnvelope};
43-
use op_revm::{L1BlockInfo, OpContext, OpTransaction, precompiles::OpPrecompiles};
43+
use op_revm::{OpContext, OpTransaction};
4444
use revm::{
45-
Database, DatabaseRef, Inspector, Journal,
46-
context::{
47-
Block as RevmBlock, BlockEnv, Cfg, CfgEnv, Evm as RevmEvm, JournalTr, LocalContext, TxEnv,
48-
},
45+
Database, Inspector,
46+
context::{Block as RevmBlock, BlockEnv, Cfg, CfgEnv, TxEnv},
4947
context_interface::result::{EVMError, ExecutionResult, Output},
50-
database::WrapDatabaseRef,
51-
handler::{EthPrecompiles, instructions::EthInstructions},
5248
interpreter::InstructionResult,
53-
precompile::{PrecompileSpecId, Precompiles},
5449
primitives::hardfork::SpecId,
5550
};
5651
use std::{fmt::Debug, sync::Arc};
@@ -505,78 +500,16 @@ where
505500
I: Inspector<EthEvmContext<DB>> + Inspector<OpContext<DB>>,
506501
{
507502
if env.networks.is_optimism() {
508-
let op_cfg = env.evm_env.cfg_env.clone().with_spec(op_revm::OpSpecId::ISTHMUS);
509-
let op_context = OpContext {
510-
journaled_state: {
511-
let mut journal = Journal::new(db);
512-
// Converting SpecId into OpSpecId
513-
journal.set_spec_id(env.evm_env.cfg_env.spec);
514-
journal
515-
},
516-
block: env.evm_env.block_env.clone(),
517-
cfg: op_cfg.clone(),
518-
tx: env.tx.clone(),
519-
chain: L1BlockInfo::default(),
520-
local: LocalContext::default(),
521-
error: Ok(()),
522-
};
523-
524-
let op_precompiles = OpPrecompiles::new_with_spec(op_cfg.spec).precompiles();
525-
let op_evm = op_revm::OpEvm(RevmEvm::new_with_inspector(
526-
op_context,
527-
inspector,
528-
EthInstructions::default(),
529-
PrecompilesMap::from_static(op_precompiles),
530-
));
531-
532-
let op = OpEvm::new(op_evm, true);
533-
534-
EitherEvm::Op(op)
503+
let evm_env = EvmEnv::new(
504+
env.evm_env.cfg_env.clone().with_spec(op_revm::OpSpecId::ISTHMUS),
505+
env.evm_env.block_env.clone(),
506+
);
507+
EitherEvm::Op(OpEvmFactory::default().create_evm_with_inspector(db, evm_env, inspector))
535508
} else {
536-
let spec = env.evm_env.cfg_env.spec;
537-
let eth_context = EthEvmContext {
538-
journaled_state: {
539-
let mut journal = Journal::new(db);
540-
journal.set_spec_id(spec);
541-
journal
542-
},
543-
block: env.evm_env.block_env.clone(),
544-
cfg: env.evm_env.cfg_env.clone(),
545-
tx: env.tx.base.clone(),
546-
chain: (),
547-
local: LocalContext::default(),
548-
error: Ok(()),
549-
};
550-
551-
let eth_precompiles = EthPrecompiles {
552-
precompiles: Precompiles::new(PrecompileSpecId::from_spec_id(spec)),
553-
spec,
554-
}
555-
.precompiles;
556-
let eth_evm = RevmEvm::new_with_inspector(
557-
eth_context,
509+
EitherEvm::Eth(EthEvmFactory::default().create_evm_with_inspector(
510+
db,
511+
env.evm_env.clone(),
558512
inspector,
559-
EthInstructions::default(),
560-
PrecompilesMap::from_static(eth_precompiles),
561-
);
562-
563-
let eth = EthEvm::new(eth_evm, true);
564-
565-
EitherEvm::Eth(eth)
513+
))
566514
}
567515
}
568-
569-
/// Creates a new EVM with the given inspector and wraps the database in a `WrapDatabaseRef`.
570-
pub fn new_evm_with_inspector_ref<'db, DB, I>(
571-
db: &'db DB,
572-
env: &Env,
573-
inspector: &'db mut I,
574-
) -> EitherEvm<WrapDatabaseRef<&'db DB>, &'db mut I, PrecompilesMap>
575-
where
576-
DB: DatabaseRef<Error = DatabaseError> + Debug + 'db + ?Sized,
577-
I: Inspector<EthEvmContext<WrapDatabaseRef<&'db DB>>>
578-
+ Inspector<OpContext<WrapDatabaseRef<&'db DB>>>,
579-
WrapDatabaseRef<&'db DB>: Database<Error = DatabaseError>,
580-
{
581-
new_evm_with_inspector(WrapDatabaseRef(db), env, inspector)
582-
}

crates/anvil/src/eth/backend/mem/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! In-memory blockchain backend.
22
33
use self::state::trie_storage;
4-
use super::executor::new_evm_with_inspector_ref;
4+
use super::executor::new_evm_with_inspector;
55
use crate::{
66
ForkChoice, NodeConfig, PrecompileFactory,
77
config::PruneStateHistoryConfig,
@@ -1187,7 +1187,7 @@ impl Backend {
11871187
+ Inspector<OpContext<WrapDatabaseRef<&'db DB>>>,
11881188
WrapDatabaseRef<&'db DB>: Database<Error = DatabaseError>,
11891189
{
1190-
let mut evm = new_evm_with_inspector_ref(db, env, inspector);
1190+
let mut evm = new_evm_with_inspector(WrapDatabaseRef(db), env, inspector);
11911191
self.env.read().networks.inject_precompiles(evm.precompiles_mut());
11921192

11931193
if let Some(factory) = &self.precompile_factory {

0 commit comments

Comments
 (0)