1use crate::{
3 evm::OpEvm, handler::OpHandler, transaction::OpTxTr, L1BlockInfo, OpHaltReason, OpSpecId,
4 OpTransactionError,
5};
6use revm::{
7 context::{result::ResultAndState, ContextSetters},
8 context_interface::{
9 result::{EVMError, ExecutionResult},
10 Cfg, ContextTr, Database, JournalTr,
11 },
12 handler::{
13 instructions::EthInstructions, system_call::SystemCallEvm, EthFrame, Handler,
14 PrecompileProvider, SystemCallTx,
15 },
16 inspector::{InspectCommitEvm, InspectEvm, Inspector, InspectorHandler, JournalExt},
17 interpreter::{interpreter::EthInterpreter, InterpreterResult},
18 primitives::{Address, Bytes},
19 state::EvmState,
20 DatabaseCommit, ExecuteCommitEvm, ExecuteEvm,
21};
22
23pub trait OpContextTr:
25 ContextTr<
26 Journal: JournalTr<State = EvmState>,
27 Tx: OpTxTr,
28 Cfg: Cfg<Spec = OpSpecId>,
29 Chain = L1BlockInfo,
30>
31{
32}
33
34impl<T> OpContextTr for T where
35 T: ContextTr<
36 Journal: JournalTr<State = EvmState>,
37 Tx: OpTxTr,
38 Cfg: Cfg<Spec = OpSpecId>,
39 Chain = L1BlockInfo,
40 >
41{
42}
43
44pub type OpError<CTX> = EVMError<<<CTX as ContextTr>::Db as Database>::Error, OpTransactionError>;
46
47impl<CTX, INSP, PRECOMPILE> ExecuteEvm
48 for OpEvm<CTX, INSP, EthInstructions<EthInterpreter, CTX>, PRECOMPILE>
49where
50 CTX: OpContextTr + ContextSetters,
51 PRECOMPILE: PrecompileProvider<CTX, Output = InterpreterResult>,
52{
53 type Tx = <CTX as ContextTr>::Tx;
54 type Block = <CTX as ContextTr>::Block;
55 type State = EvmState;
56 type Error = OpError<CTX>;
57 type ExecutionResult = ExecutionResult<OpHaltReason>;
58
59 fn set_block(&mut self, block: Self::Block) {
60 self.0.ctx.set_block(block);
61 }
62
63 fn transact(&mut self, tx: Self::Tx) -> Result<Self::ExecutionResult, Self::Error> {
64 self.0.ctx.set_tx(tx);
65 let mut h = OpHandler::<_, _, EthFrame<_, _, _>>::new();
66 h.run(self)
67 }
68
69 fn finalize(&mut self) -> Self::State {
70 self.0.ctx.journal_mut().finalize()
71 }
72
73 fn replay(
74 &mut self,
75 ) -> Result<ResultAndState<Self::ExecutionResult, Self::State>, Self::Error> {
76 let mut h = OpHandler::<_, _, EthFrame<_, _, _>>::new();
77 h.run(self).map(|result| {
78 let state = self.finalize();
79 ResultAndState::new(result, state)
80 })
81 }
82}
83
84impl<CTX, INSP, PRECOMPILE> ExecuteCommitEvm
85 for OpEvm<CTX, INSP, EthInstructions<EthInterpreter, CTX>, PRECOMPILE>
86where
87 CTX: OpContextTr<Db: DatabaseCommit> + ContextSetters,
88 PRECOMPILE: PrecompileProvider<CTX, Output = InterpreterResult>,
89{
90 fn commit(&mut self, state: Self::State) {
91 self.0.ctx.db_mut().commit(state);
92 }
93}
94
95impl<CTX, INSP, PRECOMPILE> InspectEvm
96 for OpEvm<CTX, INSP, EthInstructions<EthInterpreter, CTX>, PRECOMPILE>
97where
98 CTX: OpContextTr<Journal: JournalExt> + ContextSetters,
99 INSP: Inspector<CTX, EthInterpreter>,
100 PRECOMPILE: PrecompileProvider<CTX, Output = InterpreterResult>,
101{
102 type Inspector = INSP;
103
104 fn set_inspector(&mut self, inspector: Self::Inspector) {
105 self.0.inspector = inspector;
106 }
107
108 fn inspect_tx(&mut self, tx: Self::Tx) -> Result<Self::ExecutionResult, Self::Error> {
109 self.0.ctx.set_tx(tx);
110 let mut h = OpHandler::<_, _, EthFrame<_, _, _>>::new();
111 h.inspect_run(self)
112 }
113}
114
115impl<CTX, INSP, PRECOMPILE> InspectCommitEvm
116 for OpEvm<CTX, INSP, EthInstructions<EthInterpreter, CTX>, PRECOMPILE>
117where
118 CTX: OpContextTr<Journal: JournalExt, Db: DatabaseCommit> + ContextSetters,
119 INSP: Inspector<CTX, EthInterpreter>,
120 PRECOMPILE: PrecompileProvider<CTX, Output = InterpreterResult>,
121{
122}
123
124impl<CTX, INSP, PRECOMPILE> SystemCallEvm
125 for OpEvm<CTX, INSP, EthInstructions<EthInterpreter, CTX>, PRECOMPILE>
126where
127 CTX: OpContextTr<Tx: SystemCallTx> + ContextSetters,
128 PRECOMPILE: PrecompileProvider<CTX, Output = InterpreterResult>,
129{
130 fn transact_system_call(
131 &mut self,
132 system_contract_address: Address,
133 data: Bytes,
134 ) -> Result<Self::ExecutionResult, Self::Error> {
135 self.0
136 .ctx
137 .set_tx(CTX::Tx::new_system_tx(data, system_contract_address));
138 let mut h = OpHandler::<_, _, EthFrame<_, _, _>>::new();
139 h.run_system_call(self)
140 }
141}