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.bytecode.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.bytecode.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 /// Modifying the outcome will alter the result of the create operation.
95 #[inline]
96 fn create_end(
97 &mut self,
98 context: &mut CTX,
99 inputs: &CreateInputs,
100 outcome: &mut CreateOutcome,
101 ) {
102 let _ = context;
103 let _ = inputs;
104 let _ = outcome;
105 }
106
107 /// Called when a contract has been self-destructed with funds transferred to target.
108 #[inline]
109 fn selfdestruct(&mut self, contract: Address, target: Address, value: U256) {
110 let _ = contract;
111 let _ = target;
112 let _ = value;
113 }
114}
115
116/// Extends the journal with additional methods that are used by the inspector.
117#[auto_impl(&mut, Box)]
118pub trait JournalExt {
119 /// Get all logs from the journal.
120 fn logs(&self) -> &[Log];
121
122 /// Get the journal entries that are created from last checkpoint.
123 /// new checkpoint is created when sub call is made.
124 fn journal(&self) -> &[JournalEntry];
125
126 /// Return the current Journaled state.
127 fn evm_state(&self) -> &EvmState;
128
129 /// Return the mutable current Journaled state.
130 fn evm_state_mut(&mut self) -> &mut EvmState;
131}
132
133impl<DB: Database> JournalExt for Journal<DB> {
134 #[inline]
135 fn logs(&self) -> &[Log] {
136 &self.logs
137 }
138
139 #[inline]
140 fn journal(&self) -> &[JournalEntry] {
141 &self.journal
142 }
143
144 #[inline]
145 fn evm_state(&self) -> &EvmState {
146 &self.state
147 }
148
149 #[inline]
150 fn evm_state_mut(&mut self) -> &mut EvmState {
151 &mut self.state
152 }
153}