revm_inspector/inspector.rs
1use auto_impl::auto_impl;
2use context::{Database, Journal, JournalEntry};
3use interpreter::{
4 interpreter::EthInterpreter, CallInputs, CallOutcome, CreateInputs, CreateOutcome, Interpreter,
5 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 a contract has been self-destructed with funds transferred to target.
109 #[inline]
110 fn selfdestruct(&mut self, contract: Address, target: Address, value: U256) {
111 let _ = contract;
112 let _ = target;
113 let _ = value;
114 }
115}
116
117/// Extends the journal with additional methods that are used by the inspector.
118#[auto_impl(&mut, Box)]
119pub trait JournalExt {
120 /// Get all logs from the journal.
121 fn logs(&self) -> &[Log];
122
123 /// Get the journal entries that are created from last checkpoint.
124 /// new checkpoint is created when sub call is made.
125 fn journal(&self) -> &[JournalEntry];
126
127 /// Return the current Journaled state.
128 fn evm_state(&self) -> &EvmState;
129
130 /// Return the mutable current Journaled state.
131 fn evm_state_mut(&mut self) -> &mut EvmState;
132}
133
134impl<DB: Database> JournalExt for Journal<DB> {
135 #[inline]
136 fn logs(&self) -> &[Log] {
137 &self.logs
138 }
139
140 #[inline]
141 fn journal(&self) -> &[JournalEntry] {
142 &self.journal
143 }
144
145 #[inline]
146 fn evm_state(&self) -> &EvmState {
147 &self.state
148 }
149
150 #[inline]
151 fn evm_state_mut(&mut self) -> &mut EvmState {
152 &mut self.state
153 }
154}