Skip to content

Commit 82557ee

Browse files
authored
refactor(evm): remove useless Eth/Tempo EVM wrapper structs (foundry-rs#14350)
1 parent e5a9e6b commit 82557ee

6 files changed

Lines changed: 131 additions & 275 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/evm/core/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ op-alloy-network.workspace = true
5959
op-revm.workspace = true
6060
tempo-revm.workspace = true
6161
tempo-alloy.workspace = true
62-
tempo-chainspec.workspace = true
6362
tempo-contracts.workspace = true
6463
tempo-evm.workspace = true
6564
tempo-precompiles.workspace = true

crates/evm/core/src/evm/eth.rs

Lines changed: 36 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,54 @@
1-
use super::*;
2-
3-
type EthEvmHandler<'db, I> =
4-
MainnetHandler<EthRevmEvm<'db, I>, EVMError<DatabaseError>, EthFrame<EthInterpreter>>;
1+
use alloy_evm::{
2+
EthEvm, EthEvmFactory, Evm, EvmEnv, EvmFactory, eth::EthEvmContext, precompiles::PrecompilesMap,
3+
};
4+
use foundry_fork_db::DatabaseError;
5+
use revm::{
6+
context::{
7+
BlockEnv, ContextTr, Evm as RevmEvm, LocalContextTr, TxEnv,
8+
result::{EVMError, ResultAndState},
9+
},
10+
handler::{
11+
EthFrame, EvmTr, FrameResult, Handler, MainnetHandler, instructions::EthInstructions,
12+
},
13+
inspector::InspectorHandler,
14+
interpreter::{
15+
FrameInput, SharedMemory, interpreter::EthInterpreter, interpreter_action::FrameInit,
16+
},
17+
primitives::hardfork::SpecId,
18+
};
19+
20+
use crate::{
21+
FoundryContextExt, FoundryInspectorExt,
22+
backend::{DatabaseExt, JournaledState},
23+
evm::{FoundryEvmFactory, NestedEvm},
24+
};
25+
26+
type EthEvmHandler<'db, I> = MainnetHandler<EthRevmEvm<'db, I>, EVMError<DatabaseError>, EthFrame>;
527

628
pub type EthRevmEvm<'db, I> = RevmEvm<
729
EthEvmContext<&'db mut dyn DatabaseExt<EthEvmFactory>>,
830
I,
931
EthInstructions<EthInterpreter, EthEvmContext<&'db mut dyn DatabaseExt<EthEvmFactory>>>,
1032
PrecompilesMap,
11-
EthFrame<EthInterpreter>,
33+
EthFrame,
1234
>;
1335

14-
pub struct EthFoundryEvm<
15-
'db,
16-
I: FoundryInspectorExt<EthEvmContext<&'db mut dyn DatabaseExt<EthEvmFactory>>>,
17-
> {
18-
pub inner: EthRevmEvm<'db, I>,
19-
}
20-
21-
impl<'db, I: FoundryInspectorExt<EthEvmContext<&'db mut dyn DatabaseExt<EthEvmFactory>>>> Evm
22-
for EthFoundryEvm<'db, I>
23-
{
24-
type Precompiles = PrecompilesMap;
25-
type Inspector = I;
26-
type DB = &'db mut dyn DatabaseExt<EthEvmFactory>;
27-
type Error = EVMError<DatabaseError>;
28-
type HaltReason = HaltReason;
29-
type Spec = SpecId;
30-
type Tx = TxEnv;
31-
type BlockEnv = BlockEnv;
32-
33-
fn block(&self) -> &BlockEnv {
34-
&self.inner.block
35-
}
36-
37-
fn chain_id(&self) -> u64 {
38-
self.inner.ctx.cfg.chain_id
39-
}
40-
41-
fn components(&self) -> (&Self::DB, &Self::Inspector, &Self::Precompiles) {
42-
(&self.inner.ctx.journaled_state.database, &self.inner.inspector, &self.inner.precompiles)
43-
}
44-
45-
fn components_mut(&mut self) -> (&mut Self::DB, &mut Self::Inspector, &mut Self::Precompiles) {
46-
(
47-
&mut self.inner.ctx.journaled_state.database,
48-
&mut self.inner.inspector,
49-
&mut self.inner.precompiles,
50-
)
51-
}
52-
53-
fn set_inspector_enabled(&mut self, _enabled: bool) {
54-
unimplemented!("FoundryEvm is always inspecting")
55-
}
56-
57-
fn transact_raw(
58-
&mut self,
59-
tx: Self::Tx,
60-
) -> Result<ResultAndState<Self::HaltReason>, Self::Error> {
61-
self.inner.set_tx(tx);
62-
63-
let result = EthEvmHandler::<I>::default().inspect_run(&mut self.inner)?;
64-
65-
Ok(ResultAndState::new(result, self.inner.ctx.journaled_state.inner.state.clone()))
66-
}
67-
68-
fn transact_system_call(
69-
&mut self,
70-
_caller: Address,
71-
_contract: Address,
72-
_data: Bytes,
73-
) -> Result<ExecResultAndState<ExecutionResult>, Self::Error> {
74-
unimplemented!()
75-
}
76-
77-
fn finish(self) -> (Self::DB, EvmEnv<Self::Spec>)
78-
where
79-
Self: Sized,
80-
{
81-
let Context { block: block_env, cfg: cfg_env, journaled_state, .. } = self.inner.ctx;
82-
83-
(journaled_state.database, EvmEnv { block_env, cfg_env })
84-
}
85-
}
86-
87-
impl<'db, I: FoundryInspectorExt<EthEvmContext<&'db mut dyn DatabaseExt<EthEvmFactory>>>> Deref
88-
for EthFoundryEvm<'db, I>
89-
{
90-
type Target = EthEvmContext<&'db mut dyn DatabaseExt<EthEvmFactory>>;
91-
92-
fn deref(&self) -> &Self::Target {
93-
&self.inner.ctx
94-
}
95-
}
96-
97-
impl<'db, I: FoundryInspectorExt<EthEvmContext<&'db mut dyn DatabaseExt<EthEvmFactory>>>> DerefMut
98-
for EthFoundryEvm<'db, I>
99-
{
100-
fn deref_mut(&mut self) -> &mut Self::Target {
101-
&mut self.inner.ctx
102-
}
103-
}
104-
10536
impl FoundryEvmFactory for EthEvmFactory {
10637
type FoundryContext<'db> = EthEvmContext<&'db mut dyn DatabaseExt<Self>>;
10738

108-
type FoundryEvm<'db, I: FoundryInspectorExt<Self::FoundryContext<'db>>> = EthFoundryEvm<'db, I>;
39+
type FoundryEvm<'db, I: FoundryInspectorExt<Self::FoundryContext<'db>>> =
40+
EthEvm<&'db mut dyn DatabaseExt<Self>, I, Self::Precompiles>;
10941

11042
fn create_foundry_evm_with_inspector<'db, I: FoundryInspectorExt<Self::FoundryContext<'db>>>(
11143
&self,
11244
db: &'db mut dyn DatabaseExt<Self>,
11345
evm_env: EvmEnv,
11446
inspector: I,
11547
) -> Self::FoundryEvm<'db, I> {
116-
let eth_evm = Self::default().create_evm_with_inspector(db, evm_env, inspector);
117-
let mut inner = eth_evm.into_inner();
118-
inner.ctx.cfg.tx_chain_id_check = true;
119-
120-
let mut evm = EthFoundryEvm { inner };
121-
evm.inspector().get_networks().inject_precompiles(evm.precompiles_mut());
122-
evm
48+
let mut eth_evm = Self::default().create_evm_with_inspector(db, evm_env, inspector);
49+
eth_evm.cfg.tx_chain_id_check = true;
50+
eth_evm.inspector().get_networks().inject_precompiles(eth_evm.precompiles_mut());
51+
eth_evm
12352
}
12453

12554
fn create_foundry_nested_evm<'db>(
@@ -128,7 +57,7 @@ impl FoundryEvmFactory for EthEvmFactory {
12857
evm_env: EvmEnv,
12958
inspector: &'db mut dyn FoundryInspectorExt<Self::FoundryContext<'db>>,
13059
) -> Box<dyn NestedEvm<Spec = SpecId, Block = BlockEnv, Tx = TxEnv> + 'db> {
131-
Box::new(self.create_foundry_evm_with_inspector(db, evm_env, inspector).inner)
60+
Box::new(self.create_foundry_evm_with_inspector(db, evm_env, inspector).into_inner())
13261
}
13362
}
13463

@@ -148,7 +77,7 @@ impl<'db, I: FoundryInspectorExt<EthEvmContext<&'db mut dyn DatabaseExt<EthEvmFa
14877

14978
// Create first frame
15079
let memory =
151-
SharedMemory::new_with_buffer(self.ctx().local().shared_memory_buffer().clone());
80+
SharedMemory::new_with_buffer(self.ctx_ref().local().shared_memory_buffer().clone());
15281
let first_frame_input = FrameInit { depth: 0, memory, frame_input: frame };
15382

15483
// Run execution loop
@@ -160,10 +89,7 @@ impl<'db, I: FoundryInspectorExt<EthEvmContext<&'db mut dyn DatabaseExt<EthEvmFa
16089
Ok(frame_result)
16190
}
16291

163-
fn transact_raw(
164-
&mut self,
165-
tx: Self::Tx,
166-
) -> Result<ResultAndState<HaltReason>, EVMError<DatabaseError>> {
92+
fn transact_raw(&mut self, tx: Self::Tx) -> Result<ResultAndState, EVMError<DatabaseError>> {
16793
self.set_tx(tx);
16894

16995
let result = EthEvmHandler::<I>::default().inspect_run(self)?;

crates/evm/core/src/evm/mod.rs

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,39 @@
1-
use std::{
2-
fmt::Debug,
3-
ops::{Deref, DerefMut},
4-
};
1+
use std::{fmt::Debug, ops::Deref};
52

63
use crate::{
74
FoundryBlock, FoundryContextExt, FoundryInspectorExt, FoundryTransaction,
85
FromAnyRpcTransaction,
96
backend::{DatabaseExt, JournaledState},
10-
constants::{CALLER, TEST_CONTRACT_ADDRESS},
11-
tempo::{TEMPO_PRECOMPILE_ADDRESSES, TEMPO_TIP20_TOKENS, initialize_tempo_genesis_inner},
127
};
138
use alloy_consensus::{SignableTransaction, Signed, transaction::SignerRecoverable};
149
use alloy_evm::{
15-
EthEvmFactory, Evm, EvmEnv, EvmFactory, FromRecoveredTx, eth::EthEvmContext,
16-
precompiles::PrecompilesMap,
10+
EthEvmFactory, Evm, EvmEnv, EvmFactory, FromRecoveredTx, precompiles::PrecompilesMap,
1711
};
1812
use alloy_network::{Ethereum, Network};
19-
use alloy_op_evm::{OpEvmFactory, OpTx};
20-
use alloy_primitives::{Address, Bytes, Signature, U256};
13+
use alloy_op_evm::OpEvmFactory;
14+
use alloy_primitives::{Address, Signature, U256};
2115
use alloy_rlp::Decodable;
2216
use foundry_common::{FoundryReceiptResponse, FoundryTransactionBuilder, fmt::UIfmt};
2317
use foundry_config::FromEvmVersion;
2418
use foundry_fork_db::{DatabaseError, ForkBlockEnv};
2519
use op_alloy_network::Optimism;
26-
use op_revm::{
27-
L1BlockInfo, OpEvm, OpHaltReason, OpSpecId, OpTransaction, handler::OpHandler,
28-
precompiles::OpPrecompiles, transaction::error::OpTransactionError,
29-
};
20+
use op_revm::OpHaltReason;
3021
use revm::{
31-
Context, Database, Journal, MainContext,
22+
Database,
3223
context::{
33-
BlockEnv, CfgEnv, ContextTr, Evm as RevmEvm, JournalTr, LocalContextTr, TxEnv,
34-
result::{
35-
EVMError, ExecResultAndState, ExecutionResult, HaltReason, InvalidTransaction,
36-
ResultAndState,
37-
},
38-
},
39-
handler::{
40-
EthFrame, EvmTr, FrameResult, Handler, MainnetHandler, instructions::EthInstructions,
24+
JournalTr,
25+
result::{EVMError, HaltReason, ResultAndState},
4126
},
42-
inspector::{InspectorEvmTr, InspectorHandler},
27+
handler::FrameResult,
4328
interpreter::{
4429
CallInput, CallInputs, CallScheme, CallValue, CreateInputs, FrameInput, InstructionResult,
45-
SharedMemory, interpreter::EthInterpreter, interpreter_action::FrameInit,
4630
},
4731
primitives::hardfork::SpecId,
48-
state::Bytecode,
4932
};
5033
use serde::{Deserialize, Serialize};
5134
use tempo_alloy::TempoNetwork;
52-
use tempo_chainspec::hardfork::TempoHardfork;
5335
use tempo_evm::evm::TempoEvmFactory;
54-
use tempo_precompiles::storage::StorageCtx;
55-
use tempo_revm::{
56-
TempoBlockEnv, TempoHaltReason, TempoInvalidTransaction, TempoTxEnv, evm::TempoContext,
57-
gas_params::tempo_gas_params, handler::TempoEvmHandler,
58-
};
59-
60-
// Modified revm's OpContext with `OpTx`
61-
pub type OpContext<DB> = Context<BlockEnv, OpTx, CfgEnv<OpSpecId>, DB, Journal<DB>, L1BlockInfo>;
36+
use tempo_revm::TempoHaltReason;
6237

6338
pub mod eth;
6439
pub mod op;

crates/evm/core/src/evm/op.rs

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,40 @@
1-
use super::*;
2-
3-
type OpEvmHandler<'db, I> = OpHandler<
4-
OpRevmEvm<'db, I>,
5-
EVMError<DatabaseError, OpTransactionError>,
6-
EthFrame<EthInterpreter>,
7-
>;
1+
use std::ops::{Deref, DerefMut};
2+
3+
use alloy_evm::{Evm, EvmEnv, precompiles::PrecompilesMap};
4+
use alloy_op_evm::{OpEvmFactory, OpTx};
5+
use alloy_primitives::{Address, Bytes};
6+
use foundry_fork_db::DatabaseError;
7+
use op_revm::{
8+
L1BlockInfo, OpEvm, OpHaltReason, OpSpecId, OpTransaction, OpTransactionError,
9+
handler::OpHandler, precompiles::OpPrecompiles,
10+
};
11+
use revm::{
12+
Context, Journal, MainContext,
13+
context::{
14+
BlockEnv, CfgEnv, ContextTr, LocalContextTr,
15+
result::{
16+
EVMError, ExecResultAndState, ExecutionResult, HaltReason, InvalidTransaction,
17+
ResultAndState,
18+
},
19+
},
20+
handler::{EthFrame, EvmTr, FrameResult, Handler, instructions::EthInstructions},
21+
inspector::{InspectorEvmTr, InspectorHandler},
22+
interpreter::{
23+
FrameInput, SharedMemory, interpreter::EthInterpreter, interpreter_action::FrameInit,
24+
},
25+
};
26+
27+
use crate::{
28+
FoundryContextExt, FoundryInspectorExt,
29+
backend::{DatabaseExt, JournaledState},
30+
evm::{FoundryEvmFactory, NestedEvm},
31+
};
32+
33+
// Modified revm's OpContext with `OpTx`
34+
pub type OpContext<DB> = Context<BlockEnv, OpTx, CfgEnv<OpSpecId>, DB, Journal<DB>, L1BlockInfo>;
35+
36+
type OpEvmHandler<'db, I> =
37+
OpHandler<OpRevmEvm<'db, I>, EVMError<DatabaseError, OpTransactionError>, EthFrame>;
838

939
pub type OpRevmEvm<'db, I> = op_revm::OpEvm<
1040
OpContext<&'db mut dyn DatabaseExt<OpEvmFactory>>,
@@ -13,8 +43,8 @@ pub type OpRevmEvm<'db, I> = op_revm::OpEvm<
1343
PrecompilesMap,
1444
>;
1545

16-
/// Optimism counterpart of [`EthFoundryEvm`]. Wraps `op_revm::OpEvm` and routes execution
17-
/// through [`OpHandler`].
46+
/// Wraps [`op_revm::OpEvm`] and routes execution through [`OpHandler`].
47+
/// It uses foundry's custom [`OpContext`] as op-revm's one is not compatible with [`OpTx`].
1848
pub struct OpFoundryEvm<
1949
'db,
2050
I: FoundryInspectorExt<OpContext<&'db mut dyn DatabaseExt<OpEvmFactory>>>,
@@ -188,7 +218,7 @@ impl<'db, I: FoundryInspectorExt<OpContext<&'db mut dyn DatabaseExt<OpEvmFactory
188218
let mut handler = OpEvmHandler::<I>::new();
189219

190220
let memory =
191-
SharedMemory::new_with_buffer(self.ctx_ref().local.shared_memory_buffer().clone());
221+
SharedMemory::new_with_buffer(self.ctx_ref().local().shared_memory_buffer().clone());
192222
let first_frame_input = FrameInit { depth: 0, memory, frame_input: frame };
193223

194224
let mut frame_result =
@@ -217,6 +247,6 @@ impl<'db, I: FoundryInspectorExt<OpContext<&'db mut dyn DatabaseExt<OpEvmFactory
217247
}
218248

219249
fn to_evm_env(&self) -> EvmEnv<Self::Spec, Self::Block> {
220-
EvmEnv::new(self.ctx_ref().cfg.clone(), self.ctx_ref().block.clone())
250+
self.ctx_ref().evm_clone()
221251
}
222252
}

0 commit comments

Comments
 (0)