Skip to content

Commit

Permalink
fix: Clear journal (#1927)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rakita authored Dec 18, 2024
1 parent f2bc86e commit 1f6d438
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 69 deletions.
2 changes: 2 additions & 0 deletions crates/context/interface/src/journaled_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ pub trait JournaledState {
spec_id: SpecId,
) -> Result<JournalCheckpoint, TransferError>;

fn depth(&self) -> usize;

/// Does cleanup and returns modified state.
///
/// This resets the [JournaledState] to its initial state.
Expand Down
22 changes: 12 additions & 10 deletions crates/context/src/journaled_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ impl<DB: Database> JournaledStateTrait for JournaledState<DB> {
self.warm_preloaded_addresses.insert(address);
}

/// Returns call depth.
#[inline]
fn depth(&self) -> usize {
self.depth
}

fn warm_account_and_storage(
&mut self,
address: Address,
Expand Down Expand Up @@ -126,10 +132,12 @@ impl<DB: Database> JournaledStateTrait for JournaledState<DB> {

fn clear(&mut self) {
// Clears the JournaledState. Preserving only the spec.
//let spec = self.spec;
// TODO WIRING Clear it up
//let db = self.database;
//*self = Self::new(spec, db, HashSet::default());
self.state.clear();
self.transient_storage.clear();
self.logs.clear();
self.journal.clear();
self.depth = 0;
self.warm_preloaded_addresses.clear();
}

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

/// Returns call depth.
#[inline]
pub fn depth(&self) -> u64 {
self.depth as u64
}

/// Set code and its hash to the account.
///
/// Note: Assume account is warm and that hash is calculated from code.
Expand Down
47 changes: 9 additions & 38 deletions crates/inspector/src/eip3155.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ use derive_where::derive_where;
use revm::{
bytecode::opcode::OpCode,
context::Cfg,
context_interface::{CfgGetter, JournalStateGetter, Transaction, TransactionGetter},
context_interface::{
CfgGetter, JournalStateGetter, JournaledState, Transaction, TransactionGetter,
},
interpreter::{
interpreter_types::{Jumps, LoopControl, MemoryTrait, StackTrait},
CallInputs, CallOutcome, CreateInputs, CreateOutcome, EOFCreateInputs, Interpreter,
InterpreterResult, InterpreterTypes, Stack,
CallInputs, CallOutcome, CreateInputs, CreateOutcome, Interpreter, InterpreterResult,
InterpreterTypes, Stack,
},
primitives::{hex, HashMap, B256, U256},
};
Expand All @@ -20,13 +22,8 @@ pub struct TracerEip3155<CTX, INTR> {
#[derive_where(skip)]
output: Box<dyn Write>,
gas_inspector: GasInspector,

/// Print summary of the execution.
print_summary: bool,

/// depth
depth: usize,

stack: Vec<U256>,
pc: usize,
opcode: u8,
Expand Down Expand Up @@ -145,7 +142,6 @@ where
gas_inspector: GasInspector::new(),
print_summary: true,
include_memory: false,
depth: 0,
stack: Default::default(),
memory: Default::default(),
pc: 0,
Expand Down Expand Up @@ -232,7 +228,7 @@ where
self.refunded = interp.control.gas().refunded();
}

fn step_end(&mut self, interp: &mut Interpreter<INTR>, _: &mut CTX) {
fn step_end(&mut self, interp: &mut Interpreter<INTR>, context: &mut CTX) {
self.gas_inspector.step_end(interp.control.gas());
if self.skip {
self.skip = false;
Expand All @@ -245,7 +241,7 @@ where
gas: hex_number(self.gas),
gas_cost: hex_number(self.gas_inspector.last_gas_cost()),
stack: self.stack.iter().map(hex_number_u256).collect(),
depth: self.depth as u64,
depth: context.journal().depth() as u64,
return_data: "0x".to_string(),
refund: hex_number(self.refunded as u64),
mem_size: self.mem_size.to_string(),
Expand All @@ -263,30 +259,10 @@ where
let _ = self.write_value(&value);
}

fn call(&mut self, _: &mut Self::Context, _: &mut CallInputs) -> Option<CallOutcome> {
self.depth += 1;
None
}

fn create(&mut self, _: &mut Self::Context, _: &mut CreateInputs) -> Option<CreateOutcome> {
self.depth += 1;
None
}

fn eofcreate(
&mut self,
_: &mut Self::Context,
_: &mut EOFCreateInputs,
) -> Option<CreateOutcome> {
self.depth += 1;
None
}

fn call_end(&mut self, context: &mut CTX, _: &CallInputs, outcome: &mut CallOutcome) {
self.gas_inspector.call_end(outcome);
self.depth -= 1;

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

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

if self.depth == 0 {
if context.journal().depth() == 0 {
self.print_summary(&outcome.result, context);

// clear the state if we are at the top level
self.clear();
}
}

fn eofcreate_end(&mut self, _: &mut Self::Context, _: &EOFCreateInputs, _: &mut CreateOutcome) {
self.depth -= 1;
}
}

fn hex_number(uint: u64) -> String {
Expand Down
21 changes: 0 additions & 21 deletions crates/inspector/src/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use revm::interpreter::{CallOutcome, CreateOutcome, Gas};
pub struct GasInspector {
gas_remaining: u64,
last_gas_cost: u64,
/// depth
depth: usize,
}

impl Default for GasInspector {
Expand All @@ -30,7 +28,6 @@ impl GasInspector {
Self {
gas_remaining: 0,
last_gas_cost: 0,
depth: 0,
}
}

Expand Down Expand Up @@ -66,24 +63,6 @@ impl GasInspector {
self.gas_remaining = 0;
}
}

/// Returns depth
#[inline]
pub fn depth(&self) -> usize {
self.depth
}

/// Increment depth
#[inline]
pub fn incr_depth(&mut self) {
self.depth += 1;
}

/// Decrement depth
#[inline]
pub fn decr_depth(&mut self) {
self.depth -= 1;
}
}

// #[cfg(test)]
Expand Down

0 comments on commit 1f6d438

Please sign in to comment.