revm/
inspector.rs

1#[cfg(feature = "std")]
2mod customprinter;
3#[cfg(all(feature = "std", feature = "serde-json"))]
4mod eip3155;
5mod gas;
6mod handler_register;
7mod noop;
8
9pub use handler_register::{inspector_handle_register, GetInspector};
10
11use crate::{
12    interpreter::{
13        CallInputs, CallOutcome, CreateInputs, CreateOutcome, EOFCreateInputs, Interpreter,
14    },
15    primitives::{db::Database, Address, Log, U256},
16    EvmContext,
17};
18use auto_impl::auto_impl;
19
20/// [Inspector] implementations.
21pub mod inspectors {
22    #[cfg(feature = "std")]
23    pub use super::customprinter::CustomPrintTracer;
24    #[cfg(all(feature = "std", feature = "serde-json"))]
25    pub use super::eip3155::TracerEip3155;
26    pub use super::gas::GasInspector;
27    pub use super::noop::NoOpInspector;
28}
29
30/// EVM [Interpreter] callbacks.
31#[auto_impl(&mut, Box)]
32pub trait Inspector<DB: Database> {
33    /// Called before the interpreter is initialized.
34    ///
35    /// If `interp.instruction_result` is set to anything other than [crate::interpreter::InstructionResult::Continue] then the execution of the interpreter
36    /// is skipped.
37    #[inline]
38    fn initialize_interp(&mut self, interp: &mut Interpreter, context: &mut EvmContext<DB>) {
39        let _ = interp;
40        let _ = context;
41    }
42
43    /// Called on each step of the interpreter.
44    ///
45    /// Information about the current execution, including the memory, stack and more is available
46    /// on `interp` (see [Interpreter]).
47    ///
48    /// # Example
49    ///
50    /// To get the current opcode, use `interp.current_opcode()`.
51    #[inline]
52    fn step(&mut self, interp: &mut Interpreter, context: &mut EvmContext<DB>) {
53        let _ = interp;
54        let _ = context;
55    }
56
57    /// Called after `step` when the instruction has been executed.
58    ///
59    /// Setting `interp.instruction_result` to anything other than [crate::interpreter::InstructionResult::Continue] alters the execution
60    /// of the interpreter.
61    #[inline]
62    fn step_end(&mut self, interp: &mut Interpreter, context: &mut EvmContext<DB>) {
63        let _ = interp;
64        let _ = context;
65    }
66
67    /// Called when a log is emitted.
68    #[inline]
69    fn log(&mut self, interp: &mut Interpreter, context: &mut EvmContext<DB>, log: &Log) {
70        let _ = interp;
71        let _ = context;
72        let _ = log;
73    }
74
75    /// Called whenever a call to a contract is about to start.
76    ///
77    /// InstructionResulting anything other than [crate::interpreter::InstructionResult::Continue] overrides the result of the call.
78    #[inline]
79    fn call(
80        &mut self,
81        context: &mut EvmContext<DB>,
82        inputs: &mut CallInputs,
83    ) -> Option<CallOutcome> {
84        let _ = context;
85        let _ = inputs;
86        None
87    }
88
89    /// Called when a call to a contract has concluded.
90    ///
91    /// The returned [CallOutcome] is used as the result of the call.
92    ///
93    /// This allows the inspector to modify the given `result` before returning it.
94    #[inline]
95    fn call_end(
96        &mut self,
97        context: &mut EvmContext<DB>,
98        inputs: &CallInputs,
99        outcome: CallOutcome,
100    ) -> CallOutcome {
101        let _ = context;
102        let _ = inputs;
103        outcome
104    }
105
106    /// Called when a contract is about to be created.
107    ///
108    /// If this returns `Some` then the [CreateOutcome] is used to override the result of the creation.
109    ///
110    /// If this returns `None` then the creation proceeds as normal.
111    #[inline]
112    fn create(
113        &mut self,
114        context: &mut EvmContext<DB>,
115        inputs: &mut CreateInputs,
116    ) -> Option<CreateOutcome> {
117        let _ = context;
118        let _ = inputs;
119        None
120    }
121
122    /// Called when a contract has been created.
123    ///
124    /// InstructionResulting anything other than the values passed to this function (`(ret, remaining_gas,
125    /// address, out)`) will alter the result of the create.
126    #[inline]
127    fn create_end(
128        &mut self,
129        context: &mut EvmContext<DB>,
130        inputs: &CreateInputs,
131        outcome: CreateOutcome,
132    ) -> CreateOutcome {
133        let _ = context;
134        let _ = inputs;
135        outcome
136    }
137
138    /// Called when EOF creating is called.
139    ///
140    /// This can happen from create TX or from EOFCREATE opcode.
141    fn eofcreate(
142        &mut self,
143        context: &mut EvmContext<DB>,
144        inputs: &mut EOFCreateInputs,
145    ) -> Option<CreateOutcome> {
146        let _ = context;
147        let _ = inputs;
148        None
149    }
150
151    /// Called when eof creating has ended.
152    fn eofcreate_end(
153        &mut self,
154        context: &mut EvmContext<DB>,
155        inputs: &EOFCreateInputs,
156        outcome: CreateOutcome,
157    ) -> CreateOutcome {
158        let _ = context;
159        let _ = inputs;
160        outcome
161    }
162
163    /// Called when a contract has been self-destructed with funds transferred to target.
164    #[inline]
165    fn selfdestruct(&mut self, contract: Address, target: Address, value: U256) {
166        let _ = contract;
167        let _ = target;
168        let _ = value;
169    }
170}