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
use context::result::ExecResultAndState;
use handler::{system_call::SYSTEM_ADDRESS, ExecuteCommitEvm, ExecuteEvm, SystemCallEvm};
use primitives::{Address, Bytes};
/// InspectEvm is a API that allows inspecting the EVM.
///
/// It extends the `ExecuteEvm` trait and enabled setting inspector
///
pub trait InspectEvm: ExecuteEvm {
/// The inspector type used for inspecting EVM execution.
type Inspector;
/// Set the inspector for the EVM.
///
/// this function is used to change inspector during execution.
/// This function can't change Inspector type, changing inspector type can be done in
/// `Evm` with `with_inspector` function.
fn set_inspector(&mut self, inspector: Self::Inspector);
/// Inspect the EVM with the given transaction.
fn inspect_one_tx(&mut self, tx: Self::Tx) -> Result<Self::ExecutionResult, Self::Error>;
/// Inspect the EVM and finalize the state.
///
/// # Outcome of Error
///
/// If the transaction fails, the journal is finalized (cleared) so that the
/// next transaction starts from a clean state. This mirrors [`ExecuteEvm::transact`].
fn inspect_tx(
&mut self,
tx: Self::Tx,
) -> Result<ExecResultAndState<Self::ExecutionResult, Self::State>, Self::Error> {
// finalize the journal unconditionally, even on error, so the
// EIP-2929 warm set and state do not leak into the next transaction.
let output_or_error = self.inspect_one_tx(tx);
let state = self.finalize();
let output = output_or_error?;
Ok(ExecResultAndState::new(output, state))
}
/// Inspect the EVM with the given inspector and transaction, and finalize the state.
///
/// # Outcome of Error
///
/// Same as [`InspectEvm::inspect_tx`]: the journal is finalized on error.
fn inspect(
&mut self,
tx: Self::Tx,
inspector: Self::Inspector,
) -> Result<ExecResultAndState<Self::ExecutionResult, Self::State>, Self::Error> {
let output_or_error = self.inspect_one(tx, inspector);
let state = self.finalize();
let output = output_or_error?;
Ok(ExecResultAndState::new(output, state))
}
/// Inspect the EVM with the given inspector and transaction.
fn inspect_one(
&mut self,
tx: Self::Tx,
inspector: Self::Inspector,
) -> Result<Self::ExecutionResult, Self::Error> {
self.set_inspector(inspector);
self.inspect_one_tx(tx)
}
}
/// InspectCommitEvm is a API that allows inspecting similar to `InspectEvm` but it has
/// functions that commit the state diff to the database.
///
/// Functions return CommitOutput from [`ExecuteCommitEvm`] trait.
pub trait InspectCommitEvm: InspectEvm + ExecuteCommitEvm {
/// Inspect the EVM with the current inspector and previous transaction by replaying, similar to [`InspectEvm::inspect_tx`]
/// and commit the state diff to the database.
///
/// # Outcome of Error
///
/// If the transaction fails, the journal is finalized (not committed) so it
/// does not leak into the next transaction.
fn inspect_tx_commit(&mut self, tx: Self::Tx) -> Result<Self::ExecutionResult, Self::Error> {
let output = self.inspect_one_tx(tx).inspect_err(|_| {
// finalize (clear) the journal on error; do not commit it.
let _ = self.finalize();
})?;
self.commit_inner();
Ok(output)
}
/// Inspect the EVM with the given transaction and inspector similar to [`InspectEvm::inspect`]
/// and commit the state diff to the database.
///
/// # Outcome of Error
///
/// Same as [`InspectCommitEvm::inspect_tx_commit`]: the journal is finalized on error.
fn inspect_commit(
&mut self,
tx: Self::Tx,
inspector: Self::Inspector,
) -> Result<Self::ExecutionResult, Self::Error> {
let output = self.inspect_one(tx, inspector).inspect_err(|_| {
let _ = self.finalize();
})?;
self.commit_inner();
Ok(output)
}
}
/// InspectSystemCallEvm is an API that allows inspecting system calls in the EVM.
///
/// It extends [`InspectEvm`] and [`SystemCallEvm`] traits to provide inspection
/// capabilities for system transactions, enabling tracing and debugging of
/// system calls similar to regular transactions.
pub trait InspectSystemCallEvm: InspectEvm + SystemCallEvm {
/// Inspect a system call with the current inspector.
///
/// Similar to [`InspectEvm::inspect_one_tx`] but for system calls.
/// Uses [`SYSTEM_ADDRESS`] as the caller.
fn inspect_one_system_call(
&mut self,
system_contract_address: Address,
data: Bytes,
) -> Result<Self::ExecutionResult, Self::Error> {
self.inspect_one_system_call_with_caller(SYSTEM_ADDRESS, system_contract_address, data)
}
/// Inspect a system call with the current inspector and a custom caller.
///
/// Similar to [`InspectEvm::inspect_one_tx`] but for system calls with a custom caller.
fn inspect_one_system_call_with_caller(
&mut self,
caller: Address,
system_contract_address: Address,
data: Bytes,
) -> Result<Self::ExecutionResult, Self::Error>;
/// Inspect a system call and finalize the state.
///
/// Similar to [`InspectEvm::inspect_tx`] but for system calls.
///
/// # Outcome of Error
///
/// Same as [`InspectEvm::inspect_tx`]: the journal is finalized on error.
fn inspect_system_call(
&mut self,
system_contract_address: Address,
data: Bytes,
) -> Result<ExecResultAndState<Self::ExecutionResult, Self::State>, Self::Error> {
let output_or_error = self.inspect_one_system_call(system_contract_address, data);
let state = self.finalize();
let output = output_or_error?;
Ok(ExecResultAndState::new(output, state))
}
/// Inspect a system call with a custom caller and finalize the state.
///
/// Similar to [`InspectEvm::inspect_tx`] but for system calls with a custom caller.
///
/// # Outcome of Error
///
/// Same as [`InspectEvm::inspect_tx`]: the journal is finalized on error.
fn inspect_system_call_with_caller(
&mut self,
caller: Address,
system_contract_address: Address,
data: Bytes,
) -> Result<ExecResultAndState<Self::ExecutionResult, Self::State>, Self::Error> {
let output_or_error =
self.inspect_one_system_call_with_caller(caller, system_contract_address, data);
let state = self.finalize();
let output = output_or_error?;
Ok(ExecResultAndState::new(output, state))
}
/// Inspect a system call with a given inspector.
///
/// Similar to [`InspectEvm::inspect_one`] but for system calls.
fn inspect_one_system_call_with_inspector(
&mut self,
system_contract_address: Address,
data: Bytes,
inspector: Self::Inspector,
) -> Result<Self::ExecutionResult, Self::Error> {
self.set_inspector(inspector);
self.inspect_one_system_call(system_contract_address, data)
}
/// Inspect a system call with a given inspector and finalize the state.
///
/// Similar to [`InspectEvm::inspect`] but for system calls.
///
/// # Outcome of Error
///
/// Same as [`InspectEvm::inspect_tx`]: the journal is finalized on error.
fn inspect_system_call_with_inspector(
&mut self,
system_contract_address: Address,
data: Bytes,
inspector: Self::Inspector,
) -> Result<ExecResultAndState<Self::ExecutionResult, Self::State>, Self::Error> {
let output_or_error =
self.inspect_one_system_call_with_inspector(system_contract_address, data, inspector);
let state = self.finalize();
let output = output_or_error?;
Ok(ExecResultAndState::new(output, state))
}
/// Inspect a system call with a given inspector and caller.
///
/// Similar to [`InspectEvm::inspect_one`] but for system calls.
fn inspect_one_system_call_with_inspector_and_caller(
&mut self,
caller: Address,
system_contract_address: Address,
data: Bytes,
inspector: Self::Inspector,
) -> Result<Self::ExecutionResult, Self::Error> {
self.set_inspector(inspector);
self.inspect_one_system_call_with_caller(caller, system_contract_address, data)
}
/// Inspect a system call with a given inspector and finalize the state.
///
/// Similar to [`InspectEvm::inspect`] but for system calls.
///
/// # Outcome of Error
///
/// Same as [`InspectEvm::inspect_tx`]: the journal is finalized on error.
fn inspect_system_call_with_inspector_and_caller(
&mut self,
caller: Address,
system_contract_address: Address,
data: Bytes,
inspector: Self::Inspector,
) -> Result<ExecResultAndState<Self::ExecutionResult, Self::State>, Self::Error> {
let output_or_error = self.inspect_one_system_call_with_inspector_and_caller(
caller,
system_contract_address,
data,
inspector,
);
let state = self.finalize();
let output = output_or_error?;
Ok(ExecResultAndState::new(output, state))
}
}