revm_inspector/
traits.rs

1use context::{result::FromStringError, ContextTr};
2use handler::{
3    instructions::InstructionProvider, ContextTrDbError, EthFrame, EvmTr, Frame, FrameInitOrResult,
4    PrecompileProvider,
5};
6use interpreter::{
7    interpreter::EthInterpreter, FrameInput, Interpreter, InterpreterResult, InterpreterTypes,
8};
9
10/// Inspector EVM trait. Extends the [`EvmTr`] trait with inspector related methods.
11///
12/// It contains execution of interpreter with [`crate::Inspector`] calls [`crate::Inspector::step`] and [`crate::Inspector::step_end`] calls.
13///
14/// It is used inside [`crate::InspectorHandler`] to extend evm with support for inspection.
15pub trait InspectorEvmTr: EvmTr {
16    type Inspector;
17
18    /// Returns a mutable reference to the inspector.
19    fn inspector(&mut self) -> &mut Self::Inspector;
20
21    /// Returns a tuple of mutable references to the context and the inspector.
22    ///
23    /// Useful when you want to allow inspector to modify the context.
24    fn ctx_inspector(&mut self) -> (&mut Self::Context, &mut Self::Inspector);
25
26    /// Runs the inspector on the interpreter.
27    ///
28    /// This function is called by the EVM when it needs to inspect the Interpreter loop.
29    /// It is responsible for calling the inspector's methods and instructions from table.
30    fn run_inspect_interpreter(
31        &mut self,
32        interpreter: &mut Interpreter<
33            <Self::Instructions as InstructionProvider>::InterpreterTypes,
34        >,
35    ) -> <<Self::Instructions as InstructionProvider>::InterpreterTypes as InterpreterTypes>::Output;
36}
37
38/// Traits that extends the Frame with additional functionality that is needed for inspection
39///
40/// It is implemented for [`EthFrame`] as default Ethereum frame implementation.
41pub trait InspectorFrame: Frame {
42    type IT: InterpreterTypes;
43
44    /// It runs the frame in inspection mode.
45    ///
46    /// This will internally call [`InspectorEvmTr::run_inspect_interpreter`]
47    fn run_inspect(&mut self, evm: &mut Self::Evm) -> Result<FrameInitOrResult<Self>, Self::Error>;
48
49    /// Returns a mutable reference to the interpreter.
50    fn interpreter(&mut self) -> &mut Interpreter<Self::IT>;
51
52    /// Returns a reference to the frame input. Frame input is needed for call/create/eofcreate [`crate::Inspector`] methods
53    fn frame_input(&self) -> &FrameInput;
54}
55
56/// Impl InspectorFrame for EthFrame.
57impl<EVM, ERROR> InspectorFrame for EthFrame<EVM, ERROR, EthInterpreter>
58where
59    EVM: EvmTr<
60            Context: ContextTr,
61            Precompiles: PrecompileProvider<EVM::Context, Output = InterpreterResult>,
62            Instructions: InstructionProvider<
63                Context = EVM::Context,
64                InterpreterTypes = EthInterpreter,
65            >,
66        > + InspectorEvmTr,
67    ERROR: From<ContextTrDbError<EVM::Context>> + FromStringError,
68{
69    type IT = EthInterpreter;
70
71    fn run_inspect(&mut self, evm: &mut Self::Evm) -> Result<FrameInitOrResult<Self>, Self::Error> {
72        let interpreter = self.interpreter();
73        let next_action = evm.run_inspect_interpreter(interpreter);
74        self.process_next_action(evm, next_action)
75    }
76
77    fn interpreter(&mut self) -> &mut Interpreter<Self::IT> {
78        &mut self.interpreter
79    }
80
81    fn frame_input(&self) -> &FrameInput {
82        &self.input
83    }
84}