1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
use auto_impl::auto_impl;
use context::{Database, Journal, JournalEntry};
use handler::FrameResult;
use interpreter::{
interpreter::EthInterpreter, CallInputs, CallOutcome, CreateInputs, CreateOutcome, FrameInput,
Interpreter, InterpreterTypes,
};
use primitives::{Address, Log, U256};
use state::EvmState;
/// EVM hooks into execution.
///
/// This trait is used to enabled tracing of the EVM execution.
///
/// Object that is implemented this trait is used in `InspectorHandler` to trace the EVM execution.
/// And API that allow calling the inspector can be found in [`crate::InspectEvm`] and [`crate::InspectCommitEvm`].
#[auto_impl(&mut, Box)]
pub trait Inspector<CTX, INTR: InterpreterTypes = EthInterpreter, FI = FrameInput, FR = FrameResult>
{
/// Called before the interpreter is initialized.
///
/// If `interp.bytecode.set_action` is set the execution of the interpreter is skipped.
#[inline]
fn initialize_interp(&mut self, interp: &mut Interpreter<INTR>, context: &mut CTX) {
let _ = interp;
let _ = context;
}
/// Called on each step of the interpreter.
///
/// Information about the current execution, including the memory, stack and more is available
/// on `interp` (see [Interpreter]).
///
/// # Example
///
/// To get the current opcode, use `interp.bytecode.opcode()`.
#[inline]
fn step(&mut self, interp: &mut Interpreter<INTR>, context: &mut CTX) {
let _ = interp;
let _ = context;
}
/// Called after `step` when the instruction has been executed.
///
/// Setting `interp.bytecode.set_action` will result in stopping the execution of the interpreter.
#[inline]
fn step_end(&mut self, interp: &mut Interpreter<INTR>, context: &mut CTX) {
let _ = interp;
let _ = context;
}
/// Called when a log is emitted, called on every new log.
/// If there is a needs for Interpreter context, use [`Inspector::log_full`] instead.
#[inline]
fn log(&mut self, context: &mut CTX, log: Log) {
let _ = context;
let _ = log;
}
/// Called when a log is emitted with the interpreter context.
///
/// This will not happen only if custom precompiles where logs will be
/// gethered after precompile call.
fn log_full(&mut self, interpreter: &mut Interpreter<INTR>, context: &mut CTX, log: Log) {
let _ = interpreter;
self.log(context, log);
}
/// Called before call/create dispatch. Called with a mutable reference to
/// the frame input, allowing mutations of the input before the variant-specific
/// hooks (`call`/`create`) are called.
///
/// Returning `Some(FrameResult)` will skip execution of the frame entirely,
/// and also skips calling `call()`/`create()`. `frame_end` will still be called.
#[inline]
fn frame_start(&mut self, context: &mut CTX, frame_input: &mut FI) -> Option<FR> {
let _ = context;
let _ = frame_input;
None
}
/// Called after `call_end()`/`create_end()` variant-specific hooks complete.
///
/// Allows transformation of the final result regardless of frame kind.
#[inline]
fn frame_end(&mut self, context: &mut CTX, frame_input: &FI, frame_result: &mut FR) {
let _ = context;
let _ = frame_input;
let _ = frame_result;
}
/// Called whenever a call to a contract is about to start.
///
/// Returning `CallOutcome` will override the result of the call.
#[inline]
fn call(&mut self, context: &mut CTX, inputs: &mut CallInputs) -> Option<CallOutcome> {
let _ = context;
let _ = inputs;
None
}
/// Called when a call to a contract has concluded.
///
/// The returned [CallOutcome] is used as the result of the call.
///
/// This allows the inspector to modify the given `result` before returning it.
#[inline]
fn call_end(&mut self, context: &mut CTX, inputs: &CallInputs, outcome: &mut CallOutcome) {
let _ = context;
let _ = inputs;
let _ = outcome;
}
/// Called when a contract is about to be created.
///
/// If this returns `Some` then the [CreateOutcome] is used to override the result of the creation.
///
/// If this returns `None` then the creation proceeds as normal.
#[inline]
fn create(&mut self, context: &mut CTX, inputs: &mut CreateInputs) -> Option<CreateOutcome> {
let _ = context;
let _ = inputs;
None
}
/// Called when a contract has been created.
///
/// Modifying the outcome will alter the result of the create operation.
#[inline]
fn create_end(
&mut self,
context: &mut CTX,
inputs: &CreateInputs,
outcome: &mut CreateOutcome,
) {
let _ = context;
let _ = inputs;
let _ = outcome;
}
/// Called when a contract has been self-destructed with funds transferred to target.
#[inline]
fn selfdestruct(&mut self, contract: Address, target: Address, value: U256) {
let _ = contract;
let _ = target;
let _ = value;
}
}
impl<CTX, INTR: InterpreterTypes, FI, FR, L, R> Inspector<CTX, INTR, FI, FR> for (L, R)
where
L: Inspector<CTX, INTR, FI, FR>,
R: Inspector<CTX, INTR, FI, FR>,
{
fn initialize_interp(&mut self, interp: &mut Interpreter<INTR>, context: &mut CTX) {
self.0.initialize_interp(interp, context);
self.1.initialize_interp(interp, context);
}
fn step(&mut self, interp: &mut Interpreter<INTR>, context: &mut CTX) {
self.0.step(interp, context);
self.1.step(interp, context);
}
fn step_end(&mut self, interp: &mut Interpreter<INTR>, context: &mut CTX) {
self.0.step_end(interp, context);
self.1.step_end(interp, context);
}
fn log(&mut self, context: &mut CTX, log: Log) {
self.0.log(context, log.clone());
self.1.log(context, log);
}
fn log_full(&mut self, interp: &mut Interpreter<INTR>, context: &mut CTX, log: Log) {
self.0.log_full(interp, context, log.clone());
self.1.log_full(interp, context, log);
}
fn frame_start(&mut self, context: &mut CTX, frame_input: &mut FI) -> Option<FR> {
let first = self.0.frame_start(context, frame_input);
let second = self.1.frame_start(context, frame_input);
first.or(second)
}
fn frame_end(&mut self, context: &mut CTX, frame_input: &FI, frame_result: &mut FR) {
self.0.frame_end(context, frame_input, frame_result);
self.1.frame_end(context, frame_input, frame_result);
}
fn call(&mut self, context: &mut CTX, inputs: &mut CallInputs) -> Option<CallOutcome> {
let first = self.0.call(context, inputs);
let second = self.1.call(context, inputs);
first.or(second)
}
fn call_end(&mut self, context: &mut CTX, inputs: &CallInputs, outcome: &mut CallOutcome) {
self.0.call_end(context, inputs, outcome);
self.1.call_end(context, inputs, outcome);
}
fn create(&mut self, context: &mut CTX, inputs: &mut CreateInputs) -> Option<CreateOutcome> {
let first = self.0.create(context, inputs);
let second = self.1.create(context, inputs);
first.or(second)
}
fn create_end(
&mut self,
context: &mut CTX,
inputs: &CreateInputs,
outcome: &mut CreateOutcome,
) {
self.0.create_end(context, inputs, outcome);
self.1.create_end(context, inputs, outcome);
}
fn selfdestruct(&mut self, contract: Address, target: Address, value: U256) {
self.0.selfdestruct(contract, target, value);
self.1.selfdestruct(contract, target, value);
}
}
/// Extends the journal with additional methods that are used by the inspector.
#[auto_impl(&mut, Box)]
pub trait JournalExt {
/// Get the journal entries that are created from last checkpoint.
/// new checkpoint is created when sub call is made.
fn journal(&self) -> &[JournalEntry];
/// Return the current Journaled state.
fn evm_state(&self) -> &EvmState;
/// Return the mutable current Journaled state.
fn evm_state_mut(&mut self) -> &mut EvmState;
}
impl<DB: Database> JournalExt for Journal<DB> {
#[inline]
fn journal(&self) -> &[JournalEntry] {
&self.journal
}
#[inline]
fn evm_state(&self) -> &EvmState {
&self.state
}
#[inline]
fn evm_state_mut(&mut self) -> &mut EvmState {
&mut self.state
}
}