example_my_evm/
evm.rs

1use revm::{
2    context::{ContextError, ContextSetters, ContextTr, Evm, FrameStack},
3    handler::{
4        evm::FrameTr, instructions::EthInstructions, EthFrame, EthPrecompiles, EvmTr,
5        FrameInitOrResult, ItemOrResult,
6    },
7    inspector::{InspectorEvmTr, JournalExt},
8    interpreter::interpreter::EthInterpreter,
9    Database, Inspector,
10};
11
12/// MyEvm variant of the EVM.
13///
14/// This struct demonstrates how to create a custom EVM implementation by wrapping
15/// the standard REVM components. It combines a context (CTX), an inspector (INSP),
16/// and the standard Ethereum instructions, precompiles, and frame execution logic.
17///
18/// The generic parameters allow for flexibility in the underlying database and
19/// inspection capabilities while maintaining the standard Ethereum execution semantics.
20#[derive(Debug)]
21pub struct MyEvm<CTX, INSP>(
22    pub  Evm<
23        CTX,
24        INSP,
25        EthInstructions<EthInterpreter, CTX>,
26        EthPrecompiles,
27        EthFrame<EthInterpreter>,
28    >,
29);
30
31impl<CTX: ContextTr, INSP> MyEvm<CTX, INSP> {
32    /// Creates a new instance of MyEvm with the provided context and inspector.
33    ///
34    /// # Arguments
35    ///
36    /// * `ctx` - The execution context that manages state, environment, and journaling
37    /// * `inspector` - The inspector for debugging and tracing execution
38    ///
39    /// # Returns
40    ///
41    /// A new MyEvm instance configured with:
42    /// - The provided context and inspector
43    /// - Mainnet instruction set
44    /// - Default Ethereum precompiles
45    /// - A fresh frame stack for execution
46    pub fn new(ctx: CTX, inspector: INSP) -> Self {
47        Self(Evm {
48            ctx,
49            inspector,
50            instruction: EthInstructions::new_mainnet(),
51            precompiles: EthPrecompiles::default(),
52            frame_stack: FrameStack::new(),
53        })
54    }
55}
56
57impl<CTX: ContextTr, INSP> EvmTr for MyEvm<CTX, INSP>
58where
59    CTX: ContextTr,
60{
61    type Context = CTX;
62    type Instructions = EthInstructions<EthInterpreter, CTX>;
63    type Precompiles = EthPrecompiles;
64    type Frame = EthFrame<EthInterpreter>;
65    fn ctx(&mut self) -> &mut Self::Context {
66        &mut self.0.ctx
67    }
68
69    fn ctx_ref(&self) -> &Self::Context {
70        self.0.ctx_ref()
71    }
72
73    fn ctx_instructions(&mut self) -> (&mut Self::Context, &mut Self::Instructions) {
74        self.0.ctx_instructions()
75    }
76
77    fn ctx_precompiles(&mut self) -> (&mut Self::Context, &mut Self::Precompiles) {
78        self.0.ctx_precompiles()
79    }
80
81    fn frame_stack(&mut self) -> &mut FrameStack<Self::Frame> {
82        self.0.frame_stack()
83    }
84
85    fn frame_init(
86        &mut self,
87        frame_input: <Self::Frame as FrameTr>::FrameInit,
88    ) -> Result<
89        ItemOrResult<&mut Self::Frame, <Self::Frame as FrameTr>::FrameResult>,
90        ContextError<<<Self::Context as ContextTr>::Db as Database>::Error>,
91    > {
92        self.0.frame_init(frame_input)
93    }
94
95    fn frame_run(
96        &mut self,
97    ) -> Result<
98        FrameInitOrResult<Self::Frame>,
99        ContextError<<<Self::Context as ContextTr>::Db as Database>::Error>,
100    > {
101        self.0.frame_run()
102    }
103
104    fn frame_return_result(
105        &mut self,
106        frame_result: <Self::Frame as FrameTr>::FrameResult,
107    ) -> Result<
108        Option<<Self::Frame as FrameTr>::FrameResult>,
109        ContextError<<<Self::Context as ContextTr>::Db as Database>::Error>,
110    > {
111        self.0.frame_return_result(frame_result)
112    }
113}
114
115impl<CTX: ContextTr, INSP> InspectorEvmTr for MyEvm<CTX, INSP>
116where
117    CTX: ContextSetters<Journal: JournalExt>,
118    INSP: Inspector<CTX, EthInterpreter>,
119{
120    type Inspector = INSP;
121
122    fn inspector(&mut self) -> &mut Self::Inspector {
123        self.0.inspector()
124    }
125
126    fn ctx_inspector(&mut self) -> (&mut Self::Context, &mut Self::Inspector) {
127        self.0.ctx_inspector()
128    }
129
130    fn ctx_inspector_frame(
131        &mut self,
132    ) -> (&mut Self::Context, &mut Self::Inspector, &mut Self::Frame) {
133        self.0.ctx_inspector_frame()
134    }
135
136    fn ctx_inspector_frame_instructions(
137        &mut self,
138    ) -> (
139        &mut Self::Context,
140        &mut Self::Inspector,
141        &mut Self::Frame,
142        &mut Self::Instructions,
143    ) {
144        self.0.ctx_inspector_frame_instructions()
145    }
146}