example_my_evm/
evm.rs

1use revm::{
2    context::{ContextSetters, ContextTr, Evm},
3    handler::{
4        instructions::{EthInstructions, InstructionProvider},
5        EthPrecompiles, EvmTr,
6    },
7    inspector::{inspect_instructions, InspectorEvmTr, JournalExt},
8    interpreter::{interpreter::EthInterpreter, Interpreter, InterpreterTypes},
9    Inspector,
10};
11
12/// MyEvm variant of the EVM.
13pub struct MyEvm<CTX, INSP>(
14    pub Evm<CTX, INSP, EthInstructions<EthInterpreter, CTX>, EthPrecompiles>,
15);
16
17impl<CTX: ContextTr, INSP> MyEvm<CTX, INSP> {
18    pub fn new(ctx: CTX, inspector: INSP) -> Self {
19        Self(Evm {
20            ctx,
21            inspector,
22            instruction: EthInstructions::new_mainnet(),
23            precompiles: EthPrecompiles::default(),
24        })
25    }
26}
27
28impl<CTX: ContextTr, INSP> EvmTr for MyEvm<CTX, INSP>
29where
30    CTX: ContextTr,
31{
32    type Context = CTX;
33    type Instructions = EthInstructions<EthInterpreter, CTX>;
34    type Precompiles = EthPrecompiles;
35
36    fn ctx(&mut self) -> &mut Self::Context {
37        &mut self.0.ctx
38    }
39
40    fn ctx_ref(&self) -> &Self::Context {
41        self.0.ctx_ref()
42    }
43
44    fn ctx_instructions(&mut self) -> (&mut Self::Context, &mut Self::Instructions) {
45        self.0.ctx_instructions()
46    }
47
48    fn run_interpreter(
49        &mut self,
50        interpreter: &mut Interpreter<
51            <Self::Instructions as InstructionProvider>::InterpreterTypes,
52        >,
53    ) -> <<Self::Instructions as InstructionProvider>::InterpreterTypes as InterpreterTypes>::Output
54    {
55        self.0.run_interpreter(interpreter)
56    }
57
58    fn ctx_precompiles(&mut self) -> (&mut Self::Context, &mut Self::Precompiles) {
59        self.0.ctx_precompiles()
60    }
61}
62
63impl<CTX: ContextTr, INSP> InspectorEvmTr for MyEvm<CTX, INSP>
64where
65    CTX: ContextSetters<Journal: JournalExt>,
66    INSP: Inspector<CTX, EthInterpreter>,
67{
68    type Inspector = INSP;
69
70    fn inspector(&mut self) -> &mut Self::Inspector {
71        self.0.inspector()
72    }
73
74    fn ctx_inspector(&mut self) -> (&mut Self::Context, &mut Self::Inspector) {
75        self.0.ctx_inspector()
76    }
77
78    fn run_inspect_interpreter(
79        &mut self,
80        interpreter: &mut Interpreter<
81            <Self::Instructions as InstructionProvider>::InterpreterTypes,
82        >,
83    ) -> <<Self::Instructions as InstructionProvider>::InterpreterTypes as InterpreterTypes>::Output
84    {
85        let context = &mut self.0.ctx;
86        let instructions = &mut self.0.instruction;
87        let inspector = &mut self.0.inspector;
88
89        inspect_instructions(
90            context,
91            interpreter,
92            inspector,
93            instructions.instruction_table(),
94        )
95    }
96}