Skip to content

Commit cace866

Browse files
rakitaroyvardhan
authored andcommitted
fix: Clear journal (bluealloy#1927)
* chore: Simplify GasInspector * fmt * chore: add depth to GasInspector * rm doc * chore: tie journal database with database getter * use exec * fmt * include box * fix: Clear Journal
1 parent 4a389d5 commit cace866

File tree

4 files changed

+23
-69
lines changed

4 files changed

+23
-69
lines changed

crates/context/interface/src/journaled_state.rs

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ pub trait JournaledState {
7676
spec_id: SpecId,
7777
) -> Result<JournalCheckpoint, TransferError>;
7878

79+
fn depth(&self) -> usize;
80+
7981
/// Does cleanup and returns modified state.
8082
///
8183
/// This resets the [JournaledState] to its initial state.

crates/context/src/journaled_state.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ impl<DB: Database> JournaledStateTrait for JournaledState<DB> {
6262
self.warm_preloaded_addresses.insert(address);
6363
}
6464

65+
/// Returns call depth.
66+
#[inline]
67+
fn depth(&self) -> usize {
68+
self.depth
69+
}
70+
6571
fn warm_account_and_storage(
6672
&mut self,
6773
address: Address,
@@ -126,10 +132,12 @@ impl<DB: Database> JournaledStateTrait for JournaledState<DB> {
126132

127133
fn clear(&mut self) {
128134
// Clears the JournaledState. Preserving only the spec.
129-
//let spec = self.spec;
130-
// TODO WIRING Clear it up
131-
//let db = self.database;
132-
//*self = Self::new(spec, db, HashSet::default());
135+
self.state.clear();
136+
self.transient_storage.clear();
137+
self.logs.clear();
138+
self.journal.clear();
139+
self.depth = 0;
140+
self.warm_preloaded_addresses.clear();
133141
}
134142

135143
fn create_account_checkpoint(
@@ -234,12 +242,6 @@ impl<DB: Database> JournaledState<DB> {
234242
.expect("Account expected to be loaded") // Always assume that acc is already loaded
235243
}
236244

237-
/// Returns call depth.
238-
#[inline]
239-
pub fn depth(&self) -> u64 {
240-
self.depth as u64
241-
}
242-
243245
/// Set code and its hash to the account.
244246
///
245247
/// Note: Assume account is warm and that hash is calculated from code.

crates/inspector/src/eip3155.rs

+9-38
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ use derive_where::derive_where;
33
use revm::{
44
bytecode::opcode::OpCode,
55
context::Cfg,
6-
context_interface::{CfgGetter, JournalStateGetter, Transaction, TransactionGetter},
6+
context_interface::{
7+
CfgGetter, JournalStateGetter, JournaledState, Transaction, TransactionGetter,
8+
},
79
interpreter::{
810
interpreter_types::{Jumps, LoopControl, MemoryTrait, StackTrait},
9-
CallInputs, CallOutcome, CreateInputs, CreateOutcome, EOFCreateInputs, Interpreter,
10-
InterpreterResult, InterpreterTypes, Stack,
11+
CallInputs, CallOutcome, CreateInputs, CreateOutcome, Interpreter, InterpreterResult,
12+
InterpreterTypes, Stack,
1113
},
1214
primitives::{hex, HashMap, B256, U256},
1315
};
@@ -20,13 +22,8 @@ pub struct TracerEip3155<CTX, INTR> {
2022
#[derive_where(skip)]
2123
output: Box<dyn Write>,
2224
gas_inspector: GasInspector,
23-
2425
/// Print summary of the execution.
2526
print_summary: bool,
26-
27-
/// depth
28-
depth: usize,
29-
3027
stack: Vec<U256>,
3128
pc: usize,
3229
opcode: u8,
@@ -145,7 +142,6 @@ where
145142
gas_inspector: GasInspector::new(),
146143
print_summary: true,
147144
include_memory: false,
148-
depth: 0,
149145
stack: Default::default(),
150146
memory: Default::default(),
151147
pc: 0,
@@ -232,7 +228,7 @@ where
232228
self.refunded = interp.control.gas().refunded();
233229
}
234230

235-
fn step_end(&mut self, interp: &mut Interpreter<INTR>, _: &mut CTX) {
231+
fn step_end(&mut self, interp: &mut Interpreter<INTR>, context: &mut CTX) {
236232
self.gas_inspector.step_end(interp.control.gas());
237233
if self.skip {
238234
self.skip = false;
@@ -245,7 +241,7 @@ where
245241
gas: hex_number(self.gas),
246242
gas_cost: hex_number(self.gas_inspector.last_gas_cost()),
247243
stack: self.stack.iter().map(hex_number_u256).collect(),
248-
depth: self.depth as u64,
244+
depth: context.journal().depth() as u64,
249245
return_data: "0x".to_string(),
250246
refund: hex_number(self.refunded as u64),
251247
mem_size: self.mem_size.to_string(),
@@ -263,30 +259,10 @@ where
263259
let _ = self.write_value(&value);
264260
}
265261

266-
fn call(&mut self, _: &mut Self::Context, _: &mut CallInputs) -> Option<CallOutcome> {
267-
self.depth += 1;
268-
None
269-
}
270-
271-
fn create(&mut self, _: &mut Self::Context, _: &mut CreateInputs) -> Option<CreateOutcome> {
272-
self.depth += 1;
273-
None
274-
}
275-
276-
fn eofcreate(
277-
&mut self,
278-
_: &mut Self::Context,
279-
_: &mut EOFCreateInputs,
280-
) -> Option<CreateOutcome> {
281-
self.depth += 1;
282-
None
283-
}
284-
285262
fn call_end(&mut self, context: &mut CTX, _: &CallInputs, outcome: &mut CallOutcome) {
286263
self.gas_inspector.call_end(outcome);
287-
self.depth -= 1;
288264

289-
if self.depth == 0 {
265+
if context.journal().depth() == 0 {
290266
self.print_summary(&outcome.result, context);
291267
// clear the state if we are at the top level
292268
self.clear();
@@ -295,19 +271,14 @@ where
295271

296272
fn create_end(&mut self, context: &mut CTX, _: &CreateInputs, outcome: &mut CreateOutcome) {
297273
self.gas_inspector.create_end(outcome);
298-
self.depth -= 1;
299274

300-
if self.depth == 0 {
275+
if context.journal().depth() == 0 {
301276
self.print_summary(&outcome.result, context);
302277

303278
// clear the state if we are at the top level
304279
self.clear();
305280
}
306281
}
307-
308-
fn eofcreate_end(&mut self, _: &mut Self::Context, _: &EOFCreateInputs, _: &mut CreateOutcome) {
309-
self.depth -= 1;
310-
}
311282
}
312283

313284
fn hex_number(uint: u64) -> String {

crates/inspector/src/gas.rs

-21
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ use revm::interpreter::{CallOutcome, CreateOutcome, Gas};
77
pub struct GasInspector {
88
gas_remaining: u64,
99
last_gas_cost: u64,
10-
/// depth
11-
depth: usize,
1210
}
1311

1412
impl Default for GasInspector {
@@ -30,7 +28,6 @@ impl GasInspector {
3028
Self {
3129
gas_remaining: 0,
3230
last_gas_cost: 0,
33-
depth: 0,
3431
}
3532
}
3633

@@ -66,24 +63,6 @@ impl GasInspector {
6663
self.gas_remaining = 0;
6764
}
6865
}
69-
70-
/// Returns depth
71-
#[inline]
72-
pub fn depth(&self) -> usize {
73-
self.depth
74-
}
75-
76-
/// Increment depth
77-
#[inline]
78-
pub fn incr_depth(&mut self) {
79-
self.depth += 1;
80-
}
81-
82-
/// Decrement depth
83-
#[inline]
84-
pub fn decr_depth(&mut self) {
85-
self.depth -= 1;
86-
}
8766
}
8867

8968
// #[cfg(test)]

0 commit comments

Comments
 (0)