example_custom_precompile_journal/
custom_evm.rs

1//! Custom EVM implementation with journal-accessing precompiles.
2
3use crate::precompile_provider::CustomPrecompileProvider;
4use revm::{
5    context::{ContextError, ContextSetters, ContextTr, Evm, FrameStack},
6    handler::{
7        evm::FrameTr, instructions::EthInstructions, EthFrame, EvmTr, FrameInitOrResult,
8        ItemOrResult,
9    },
10    inspector::{InspectorEvmTr, JournalExt},
11    interpreter::interpreter::EthInterpreter,
12    primitives::hardfork::SpecId,
13    Database, Inspector,
14};
15
16/// Custom EVM variant with journal-accessing precompiles.
17///
18/// This EVM extends the standard behavior by using a custom precompile provider
19/// that includes journal access functionality. It follows the same pattern as MyEvm
20/// but uses CustomPrecompileProvider instead of EthPrecompiles.
21#[derive(Debug)]
22pub struct CustomEvm<CTX, INSP>(
23    pub  Evm<
24        CTX,
25        INSP,
26        EthInstructions<EthInterpreter, CTX>,
27        CustomPrecompileProvider,
28        EthFrame<EthInterpreter>,
29    >,
30);
31
32impl<CTX, INSP> CustomEvm<CTX, INSP>
33where
34    CTX: ContextTr<Cfg: revm::context::Cfg<Spec = SpecId>>,
35{
36    /// Creates a new instance of CustomEvm with the provided context and inspector.
37    ///
38    /// # Arguments
39    ///
40    /// * `ctx` - The execution context that manages state, environment, and journaling
41    /// * `inspector` - The inspector for debugging and tracing execution
42    ///
43    /// # Returns
44    ///
45    /// A new CustomEvm instance configured with:
46    /// - The provided context and inspector
47    /// - Mainnet instruction set
48    /// - Custom precompiles with journal access
49    /// - A fresh frame stack for execution
50    pub fn new(ctx: CTX, inspector: INSP) -> Self {
51        Self(Evm {
52            ctx,
53            inspector,
54            instruction: EthInstructions::new_mainnet(),
55            precompiles: CustomPrecompileProvider::new_with_spec(SpecId::CANCUN),
56            frame_stack: FrameStack::new(),
57        })
58    }
59}
60
61impl<CTX, INSP> EvmTr for CustomEvm<CTX, INSP>
62where
63    CTX: ContextTr<Cfg: revm::context::Cfg<Spec = SpecId>>,
64{
65    type Context = CTX;
66    type Instructions = EthInstructions<EthInterpreter, CTX>;
67    type Precompiles = CustomPrecompileProvider;
68    type Frame = EthFrame<EthInterpreter>;
69
70    fn all(
71        &self,
72    ) -> (
73        &Self::Context,
74        &Self::Instructions,
75        &Self::Precompiles,
76        &FrameStack<Self::Frame>,
77    ) {
78        self.0.all()
79    }
80
81    fn all_mut(
82        &mut self,
83    ) -> (
84        &mut Self::Context,
85        &mut Self::Instructions,
86        &mut Self::Precompiles,
87        &mut FrameStack<Self::Frame>,
88    ) {
89        self.0.all_mut()
90    }
91
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    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    fn frame_return_result(
112        &mut self,
113        frame_result: <Self::Frame as FrameTr>::FrameResult,
114    ) -> Result<
115        Option<<Self::Frame as FrameTr>::FrameResult>,
116        ContextError<<<Self::Context as ContextTr>::Db as Database>::Error>,
117    > {
118        self.0.frame_return_result(frame_result)
119    }
120}
121
122impl<CTX, INSP> InspectorEvmTr for CustomEvm<CTX, INSP>
123where
124    CTX: ContextSetters<Cfg: revm::context::Cfg<Spec = SpecId>, Journal: JournalExt>,
125    INSP: Inspector<CTX, EthInterpreter>,
126{
127    type Inspector = INSP;
128
129    fn all_inspector(
130        &self,
131    ) -> (
132        &Self::Context,
133        &Self::Instructions,
134        &Self::Precompiles,
135        &FrameStack<Self::Frame>,
136        &Self::Inspector,
137    ) {
138        self.0.all_inspector()
139    }
140
141    fn all_mut_inspector(
142        &mut self,
143    ) -> (
144        &mut Self::Context,
145        &mut Self::Instructions,
146        &mut Self::Precompiles,
147        &mut FrameStack<Self::Frame>,
148        &mut Self::Inspector,
149    ) {
150        self.0.all_mut_inspector()
151    }
152}