From a201a2d92c4a41f6530fe2b84ec23467255c0636 Mon Sep 17 00:00:00 2001 From: A_A <21040751+Otto-AA@users.noreply.github.com> Date: Fri, 29 Mar 2024 20:32:35 +0100 Subject: [PATCH] feat: TracerEip3155 optionally traces memory (#1234) * feat: TracerEip3155 optionally traces memory BREAKING CHANGE: the TracerEIP3155 constructor takes an additional "include_memory" flag * perf: use hex::encode_prefixed for TracerEip3155 * refactor: use builder pattern for TracerEip3155 BREAKING CHANGE: update constructor syntax for TracerEip3155 * refactor: use String for TracerEip3155 memory --- bins/revme/src/cmd/statetest/runner.rs | 7 ++----- crates/revm/src/inspector/eip3155.rs | 29 ++++++++++++++++++++++---- examples/db_by_ref.rs | 2 +- examples/generate_block_traces.rs | 2 +- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/bins/revme/src/cmd/statetest/runner.rs b/bins/revme/src/cmd/statetest/runner.rs index 3c75f38e04..d235c572fe 100644 --- a/bins/revme/src/cmd/statetest/runner.rs +++ b/bins/revme/src/cmd/statetest/runner.rs @@ -355,10 +355,7 @@ pub fn execute_test_suite( let (e, exec_result) = if trace { let mut evm = evm .modify() - .reset_handler_with_external_context(TracerEip3155::new( - Box::new(stderr()), - false, - )) + .reset_handler_with_external_context(TracerEip3155::new(Box::new(stderr()))) .append_handler_register(inspector_handle_register) .build(); @@ -421,7 +418,7 @@ pub fn execute_test_suite( let mut evm = Evm::builder() .with_spec_id(spec_id) .with_db(state) - .with_external_context(TracerEip3155::new(Box::new(stdout()), false)) + .with_external_context(TracerEip3155::new(Box::new(stdout()))) .append_handler_register(inspector_handle_register) .build(); let _ = evm.transact_commit(); diff --git a/crates/revm/src/inspector/eip3155.rs b/crates/revm/src/inspector/eip3155.rs index fc9ae809cc..0a7f62ec0d 100644 --- a/crates/revm/src/inspector/eip3155.rs +++ b/crates/revm/src/inspector/eip3155.rs @@ -22,6 +22,8 @@ pub struct TracerEip3155 { refunded: i64, mem_size: usize, skip: bool, + include_memory: bool, + memory: Option, } // # Output @@ -58,7 +60,7 @@ struct Output { error: Option, /// Array of all allocated values #[serde(default, skip_serializing_if = "Option::is_none")] - memory: Option>, + memory: Option, /// Array of all stored values #[serde(default, skip_serializing_if = "Option::is_none")] storage: Option>, @@ -98,12 +100,14 @@ impl TracerEip3155 { } impl TracerEip3155 { - pub fn new(output: Box, print_summary: bool) -> Self { + pub fn new(output: Box) -> Self { Self { output, gas_inspector: GasInspector::default(), - print_summary, + print_summary: true, + include_memory: false, stack: Default::default(), + memory: Default::default(), pc: 0, opcode: 0, gas: 0, @@ -113,6 +117,18 @@ impl TracerEip3155 { } } + /// Don't include a summary at the end of the trace + pub fn without_summary(mut self) -> Self { + self.print_summary = false; + self + } + + /// Include a memory field for each step. This significantly increases processing time and output size. + pub fn with_memory(mut self) -> Self { + self.include_memory = true; + self + } + fn write_value(&mut self, value: &impl serde::Serialize) -> std::io::Result<()> { serde_json::to_writer(&mut *self.output, value)?; self.output.write_all(b"\n")?; @@ -128,6 +144,11 @@ impl Inspector for TracerEip3155 { fn step(&mut self, interp: &mut Interpreter, context: &mut EvmContext) { self.gas_inspector.step(interp, context); self.stack = interp.stack.data().clone(); + self.memory = if self.include_memory { + Some(hex::encode_prefixed(interp.shared_memory.context_memory())) + } else { + None + }; self.pc = interp.program_counter(); self.opcode = interp.current_opcode(); self.mem_size = interp.shared_memory.len(); @@ -159,7 +180,7 @@ impl Inspector for TracerEip3155 { } else { None }, - memory: None, + memory: self.memory.take(), storage: None, return_stack: None, }; diff --git a/examples/db_by_ref.rs b/examples/db_by_ref.rs index c9094a905b..9e3615400c 100644 --- a/examples/db_by_ref.rs +++ b/examples/db_by_ref.rs @@ -72,7 +72,7 @@ fn run_transaction_and_commit(db: &mut CacheDB) -> anyhow::Result<()> { fn main() -> anyhow::Result<()> { let mut cache_db = CacheDB::new(EmptyDB::default()); - let mut tracer = TracerEip3155::new(Box::new(std::io::stdout()), true); + let mut tracer = TracerEip3155::new(Box::new(std::io::stdout())); run_transaction_and_commit_with_ext(&mut cache_db, &mut tracer, inspector_handle_register)?; run_transaction_and_commit(&mut cache_db)?; diff --git a/examples/generate_block_traces.rs b/examples/generate_block_traces.rs index 37a1550778..5270bb9abd 100644 --- a/examples/generate_block_traces.rs +++ b/examples/generate_block_traces.rs @@ -76,7 +76,7 @@ async fn main() -> anyhow::Result<()> { let mut state = StateBuilder::new_with_database(cache_db).build(); let mut evm = Evm::builder() .with_db(&mut state) - .with_external_context(TracerEip3155::new(Box::new(std::io::stdout()), true)) + .with_external_context(TracerEip3155::new(Box::new(std::io::stdout()))) .modify_block_env(|b| { if let Some(number) = block.number { let nn = number.0[0];