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
66    #[inline]
67    fn all(
68        &self,
69    ) -> (
70        &Self::Context,
71        &Self::Instructions,
72        &Self::Precompiles,
73        &FrameStack<Self::Frame>,
74    ) {
75        self.0.all()
76    }
77
78    #[inline]
79    fn all_mut(
80        &mut self,
81    ) -> (
82        &mut Self::Context,
83        &mut Self::Instructions,
84        &mut Self::Precompiles,
85        &mut FrameStack<Self::Frame>,
86    ) {
87        self.0.all_mut()
88    }
89
90    #[inline]
91    fn frame_init(
92        &mut self,
93        frame_input: <Self::Frame as FrameTr>::FrameInit,
94    ) -> Result<
95        ItemOrResult<&mut Self::Frame, <Self::Frame as FrameTr>::FrameResult>,
96        ContextError<<<Self::Context as ContextTr>::Db as Database>::Error>,
97    > {
98        self.0.frame_init(frame_input)
99    }
100
101    #[inline]
102    fn frame_run(
103        &mut self,
104    ) -> Result<
105        FrameInitOrResult<Self::Frame>,
106        ContextError<<<Self::Context as ContextTr>::Db as Database>::Error>,
107    > {
108        self.0.frame_run()
109    }
110
111    #[inline]
112    fn frame_return_result(
113        &mut self,
114        frame_result: <Self::Frame as FrameTr>::FrameResult,
115    ) -> Result<
116        Option<<Self::Frame as FrameTr>::FrameResult>,
117        ContextError<<<Self::Context as ContextTr>::Db as Database>::Error>,
118    > {
119        self.0.frame_return_result(frame_result)
120    }
121}
122
123impl<CTX: ContextTr, INSP> InspectorEvmTr for MyEvm<CTX, INSP>
124where
125    CTX: ContextSetters<Journal: JournalExt>,
126    INSP: Inspector<CTX, EthInterpreter>,
127{
128    type Inspector = INSP;
129
130    fn all_inspector(
131        &self,
132    ) -> (
133        &Self::Context,
134        &Self::Instructions,
135        &Self::Precompiles,
136        &FrameStack<Self::Frame>,
137        &Self::Inspector,
138    ) {
139        self.0.all_inspector()
140    }
141
142    fn all_mut_inspector(
143        &mut self,
144    ) -> (
145        &mut Self::Context,
146        &mut Self::Instructions,
147        &mut Self::Precompiles,
148        &mut FrameStack<Self::Frame>,
149        &mut Self::Inspector,
150    ) {
151        self.0.all_mut_inspector()
152    }
153}