Skip to main content

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