From 1f6d4382a4dfede3171cb14a59b890f6feb1d73d Mon Sep 17 00:00:00 2001 From: rakita Date: Wed, 18 Dec 2024 16:35:33 +0100 Subject: [PATCH] fix: Clear journal (#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 --- .../context/interface/src/journaled_state.rs | 2 + crates/context/src/journaled_state.rs | 22 +++++---- crates/inspector/src/eip3155.rs | 47 ++++--------------- crates/inspector/src/gas.rs | 21 --------- 4 files changed, 23 insertions(+), 69 deletions(-) diff --git a/crates/context/interface/src/journaled_state.rs b/crates/context/interface/src/journaled_state.rs index 69dd4eb742..c85bb7b4ea 100644 --- a/crates/context/interface/src/journaled_state.rs +++ b/crates/context/interface/src/journaled_state.rs @@ -76,6 +76,8 @@ pub trait JournaledState { spec_id: SpecId, ) -> Result; + fn depth(&self) -> usize; + /// Does cleanup and returns modified state. /// /// This resets the [JournaledState] to its initial state. diff --git a/crates/context/src/journaled_state.rs b/crates/context/src/journaled_state.rs index 667aabd96f..d74403d612 100644 --- a/crates/context/src/journaled_state.rs +++ b/crates/context/src/journaled_state.rs @@ -62,6 +62,12 @@ impl JournaledStateTrait for JournaledState { 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, @@ -126,10 +132,12 @@ impl JournaledStateTrait for JournaledState { 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( @@ -234,12 +242,6 @@ impl JournaledState { .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. diff --git a/crates/inspector/src/eip3155.rs b/crates/inspector/src/eip3155.rs index 4ed1d6d769..47f5011c04 100644 --- a/crates/inspector/src/eip3155.rs +++ b/crates/inspector/src/eip3155.rs @@ -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}, }; @@ -20,13 +22,8 @@ pub struct TracerEip3155 { #[derive_where(skip)] output: Box, gas_inspector: GasInspector, - /// Print summary of the execution. print_summary: bool, - - /// depth - depth: usize, - stack: Vec, pc: usize, opcode: u8, @@ -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, @@ -232,7 +228,7 @@ where self.refunded = interp.control.gas().refunded(); } - fn step_end(&mut self, interp: &mut Interpreter, _: &mut CTX) { + fn step_end(&mut self, interp: &mut Interpreter, context: &mut CTX) { self.gas_inspector.step_end(interp.control.gas()); if self.skip { self.skip = false; @@ -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(), @@ -263,30 +259,10 @@ where let _ = self.write_value(&value); } - fn call(&mut self, _: &mut Self::Context, _: &mut CallInputs) -> Option { - self.depth += 1; - None - } - - fn create(&mut self, _: &mut Self::Context, _: &mut CreateInputs) -> Option { - self.depth += 1; - None - } - - fn eofcreate( - &mut self, - _: &mut Self::Context, - _: &mut EOFCreateInputs, - ) -> Option { - 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(); @@ -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 { diff --git a/crates/inspector/src/gas.rs b/crates/inspector/src/gas.rs index 5913f2251d..dcc4133f79 100644 --- a/crates/inspector/src/gas.rs +++ b/crates/inspector/src/gas.rs @@ -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 { @@ -30,7 +28,6 @@ impl GasInspector { Self { gas_remaining: 0, last_gas_cost: 0, - depth: 0, } } @@ -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)]