revm_inspector/
inspector.rs

1use auto_impl::auto_impl;
2use context::{Database, Journal, JournalEntry};
3use interpreter::{
4    interpreter::EthInterpreter, CallInputs, CallOutcome, CreateInputs, CreateOutcome,
5    EOFCreateInputs, Interpreter, InterpreterTypes,
6};
7use primitives::{Address, Log, U256};
8use state::EvmState;
9
10/// EVM hooks into execution.
11///
12/// This trait is used to enabled tracing of the EVM execution.
13///
14/// Object that is implemented this trait is used in `InspectorHandler` to trace the EVM execution.
15/// And API that allow calling the inspector can be found in [`crate::InspectEvm`] and [`crate::InspectCommitEvm`].
16#[auto_impl(&mut, Box)]
17pub trait Inspector<CTX, INTR: InterpreterTypes = EthInterpreter> {
18    /// Called before the interpreter is initialized.
19    ///
20    /// If `interp.set_action` is set the execution of the interpreter is skipped.
21    #[inline]
22    fn initialize_interp(&mut self, interp: &mut Interpreter<INTR>, context: &mut CTX) {
23        let _ = interp;
24        let _ = context;
25    }
26
27    /// Called on each step of the interpreter.
28    ///
29    /// Information about the current execution, including the memory, stack and more is available
30    /// on `interp` (see [Interpreter]).
31    ///
32    /// # Example
33    ///
34    /// To get the current opcode, use `interp.current_opcode()`.
35    #[inline]
36    fn step(&mut self, interp: &mut Interpreter<INTR>, context: &mut CTX) {
37        let _ = interp;
38        let _ = context;
39    }
40
41    /// Called after `step` when the instruction has been executed.
42    ///
43    /// Setting `interp.bytecode.set_action` will result in stopping the execution of the interpreter.
44    #[inline]
45    fn step_end(&mut self, interp: &mut Interpreter<INTR>, context: &mut CTX) {
46        let _ = interp;
47        let _ = context;
48    }
49
50    /// Called when a log is emitted.
51    #[inline]
52    fn log(&mut self, interp: &mut Interpreter<INTR>, context: &mut CTX, log: Log) {
53        let _ = interp;
54        let _ = context;
55        let _ = log;
56    }
57
58    /// Called whenever a call to a contract is about to start.
59    ///
60    /// Returning `CallOutcome` will override the result of the call.
61    #[inline]
62    fn call(&mut self, context: &mut CTX, inputs: &mut CallInputs) -> Option<CallOutcome> {
63        let _ = context;
64        let _ = inputs;
65        None
66    }
67
68    /// Called when a call to a contract has concluded.
69    ///
70    /// The returned [CallOutcome] is used as the result of the call.
71    ///
72    /// This allows the inspector to modify the given `result` before returning it.
73    #[inline]
74    fn call_end(&mut self, context: &mut CTX, inputs: &CallInputs, outcome: &mut CallOutcome) {
75        let _ = context;
76        let _ = inputs;
77        let _ = outcome;
78    }
79
80    /// Called when a contract is about to be created.
81    ///
82    /// If this returns `Some` then the [CreateOutcome] is used to override the result of the creation.
83    ///
84    /// If this returns `None` then the creation proceeds as normal.
85    #[inline]
86    fn create(&mut self, context: &mut CTX, inputs: &mut CreateInputs) -> Option<CreateOutcome> {
87        let _ = context;
88        let _ = inputs;
89        None
90    }
91
92    /// Called when a contract has been created.
93    ///
94    /// InstructionResulting anything other than the values passed to this function (`(ret, remaining_gas,
95    /// address, out)`) will alter the result of the create.
96    #[inline]
97    fn create_end(
98        &mut self,
99        context: &mut CTX,
100        inputs: &CreateInputs,
101        outcome: &mut CreateOutcome,
102    ) {
103        let _ = context;
104        let _ = inputs;
105        let _ = outcome;
106    }
107
108    /// Called when EOF creating is called.
109    ///
110    /// This can happen from create TX or from EOFCREATE opcode.
111    fn eofcreate(
112        &mut self,
113        context: &mut CTX,
114        inputs: &mut EOFCreateInputs,
115    ) -> Option<CreateOutcome> {
116        let _ = context;
117        let _ = inputs;
118        None
119    }
120
121    /// Called when eof creating has ended.
122    fn eofcreate_end(
123        &mut self,
124        context: &mut CTX,
125        inputs: &EOFCreateInputs,
126        outcome: &mut CreateOutcome,
127    ) {
128        let _ = context;
129        let _ = inputs;
130        let _ = outcome;
131    }
132
133    /// Called when a contract has been self-destructed with funds transferred to target.
134    #[inline]
135    fn selfdestruct(&mut self, contract: Address, target: Address, value: U256) {
136        let _ = contract;
137        let _ = target;
138        let _ = value;
139    }
140}
141
142/// Extends the journal with additional methods that are used by the inspector.
143#[auto_impl(&mut, Box)]
144pub trait JournalExt {
145    /// Get all logs from the journal.
146    fn logs(&self) -> &[Log];
147
148    /// Get the journal entries that are created from last checkpoint.
149    /// new checkpoint is created when sub call is made.
150    fn journal(&self) -> &[JournalEntry];
151
152    /// Return the current Journaled state.
153    fn evm_state(&self) -> &EvmState;
154
155    /// Return the mutable current Journaled state.
156    fn evm_state_mut(&mut self) -> &mut EvmState;
157}
158
159impl<DB: Database> JournalExt for Journal<DB> {
160    #[inline]
161    fn logs(&self) -> &[Log] {
162        &self.logs
163    }
164
165    #[inline]
166    fn journal(&self) -> &[JournalEntry] {
167        &self.journal
168    }
169
170    #[inline]
171    fn evm_state(&self) -> &EvmState {
172        &self.state
173    }
174
175    #[inline]
176    fn evm_state_mut(&mut self) -> &mut EvmState {
177        &mut self.state
178    }
179}