From d7a958129f6e9051d2fe9b857c999cdc8a318107 Mon Sep 17 00:00:00 2001 From: rakita Date: Wed, 10 Mar 2021 16:39:32 +0100 Subject: [PATCH] [evmbin] Omit storage output, now for std-json (#311) * [evmbin] Omit storage output, now for std-json * fix tests --- bin/evmbin/src/display/config.rs | 2 +- bin/evmbin/src/display/json.rs | 19 +++++++++++++- bin/evmbin/src/display/std_json.rs | 40 +++++++++++++++++++++++------- bin/evmbin/src/info.rs | 4 +-- 4 files changed, 52 insertions(+), 13 deletions(-) diff --git a/bin/evmbin/src/display/config.rs b/bin/evmbin/src/display/config.rs index 108091966..128c2eecc 100644 --- a/bin/evmbin/src/display/config.rs +++ b/bin/evmbin/src/display/config.rs @@ -16,7 +16,7 @@ //! Config used by display informants -#[derive(Default, Copy, Clone)] +#[derive(Default, Copy, Clone, Debug)] pub struct Config { omit_storage_output: bool, omit_memory_output: bool, diff --git a/bin/evmbin/src/display/json.rs b/bin/evmbin/src/display/json.rs index e3fc82057..55b70f86d 100644 --- a/bin/evmbin/src/display/json.rs +++ b/bin/evmbin/src/display/json.rs @@ -284,7 +284,7 @@ mod tests { gas_cost: U256, memory: String, stack: Vec, - storage: HashMap, + storage: Option>, depth: usize, } @@ -399,6 +399,23 @@ mod tests { {"pc":1,"op":96,"opName":"PUSH1","gas":"0xfffd","gasCost":"0x3","memory":"0x","stack":["0x0"],"storage":{},"depth":1} {"pc":3,"op":85,"opName":"SSTORE","gas":"0xfffa","gasCost":"0x1388","memory":"0x","stack":["0x0","0xd8"],"storage":{},"depth":1} {"pc":4,"op":84,"opName":"SLOAD","gas":"0xec72","gasCost":"0x0","memory":"0x","stack":[],"storage":{"0x00000000000000000000000000000000000000000000000000000000000000d8":"0x0000000000000000000000000000000000000000000000000000000000000000"},"depth":1} +"#, + ); + } + + #[test] + fn should_omit_storage_and_memory_flag() { + // should omit storage + run_test( + Informant::new(Config::new(true, true)), + &compare_json, + "3260D85554", + 0xffff, + r#" +{"pc":0,"op":50,"opName":"ORIGIN","gas":"0xffff","gasCost":"0x2","memory":"","stack":[],"storage":null,"depth":1} +{"pc":1,"op":96,"opName":"PUSH1","gas":"0xfffd","gasCost":"0x3","memory":"","stack":["0x0"],"storage":null,"depth":1} +{"pc":3,"op":85,"opName":"SSTORE","gas":"0xfffa","gasCost":"0x1388","memory":"","stack":["0x0","0xd8"],"storage":null,"depth":1} +{"pc":4,"op":84,"opName":"SLOAD","gas":"0xec72","gasCost":"0x0","memory":"","stack":[],"storage":null,"depth":1} "#, ) } diff --git a/bin/evmbin/src/display/std_json.rs b/bin/evmbin/src/display/std_json.rs index b6efa4c0c..ae7e5a7e0 100644 --- a/bin/evmbin/src/display/std_json.rs +++ b/bin/evmbin/src/display/std_json.rs @@ -203,6 +203,11 @@ impl trace::VMTracer for Informant { fn trace_next_instruction(&mut self, pc: usize, instruction: u8, current_gas: U256) -> bool { let subdepth = self.subdepth; Self::with_informant_in_depth(self, subdepth, |informant: &mut Informant| { + let storage = if informant.config.omit_storage_output() { + None + } else { + Some(&informant.storage) + }; let info = ::evm::Instruction::from_u8(instruction).map(|i| i.info()); informant.instruction = instruction; let trace_data = json!({ @@ -211,7 +216,7 @@ impl trace::VMTracer for Informant { "opName": info.map(|i| i.name).unwrap_or(""), "gas": format!("{:#x}", current_gas), "stack": informant.stack, - "storage": informant.storage, + "storage": storage, "depth": informant.depth, }); @@ -307,19 +312,16 @@ pub mod tests { } } - pub fn informant() -> (Informant, Arc>>) { + pub fn informant(config: Config) -> (Informant, Arc>>) { let trace_writer: TestWriter = Default::default(); let out_writer: TestWriter = Default::default(); let res = trace_writer.0.clone(); - ( - Informant::new(trace_writer, out_writer, Config::default()), - res, - ) + (Informant::new(trace_writer, out_writer, config), res) } #[test] fn should_trace_failure() { - let (inf, res) = informant(); + let (inf, res) = informant(Config::default()); run_test( inf, move |_, expected| { @@ -333,7 +335,7 @@ pub mod tests { "#, ); - let (inf, res) = informant(); + let (inf, res) = informant(Config::default()); run_test( inf, move |_, expected| { @@ -349,7 +351,7 @@ pub mod tests { #[test] fn should_trace_create_correctly() { - let (informant, res) = informant(); + let (informant, res) = informant(Config::default()); run_test( informant, move |_, expected| { @@ -382,6 +384,26 @@ pub mod tests { {"depth":2,"gas":"0x2102","op":88,"opName":"PC","pc":5,"stack":["0x0","0x0","0x0","0x0","0x0"],"storage":{}} {"depth":2,"gas":"0x2100","op":48,"opName":"ADDRESS","pc":6,"stack":["0x0","0x0","0x0","0x0","0x0","0x5"],"storage":{}} {"depth":2,"gas":"0x20fe","op":241,"opName":"CALL","pc":7,"stack":["0x0","0x0","0x0","0x0","0x0","0x5","0xbd770416a3345f91e4b34576cb804a576fa48eb1"],"storage":{}} +"#, + ) + } + + #[test] + fn should_omit_storage_and_memory_flag() { + // should omit storage + let (informant, res) = informant(Config::new(true, true)); + run_test( + informant, + move |_, expected| { + let bytes = res.lock().unwrap(); + assert_eq!(expected, &String::from_utf8_lossy(&**bytes)) + }, + "3260D85554", + 0xffff, + r#"{"depth":1,"gas":"0xffff","op":50,"opName":"ORIGIN","pc":0,"stack":[],"storage":null} +{"depth":1,"gas":"0xfffd","op":96,"opName":"PUSH1","pc":1,"stack":["0x0"],"storage":null} +{"depth":1,"gas":"0xfffa","op":85,"opName":"SSTORE","pc":3,"stack":["0x0","0xd8"],"storage":null} +{"depth":1,"gas":"0xec72","op":84,"opName":"SLOAD","pc":4,"stack":[],"storage":null} "#, ) } diff --git a/bin/evmbin/src/info.rs b/bin/evmbin/src/info.rs index 9ebe02a4e..8519e1d4b 100644 --- a/bin/evmbin/src/info.rs +++ b/bin/evmbin/src/info.rs @@ -287,9 +287,9 @@ pub mod tests { #[test] fn should_call_account_from_spec() { - use display::std_json::tests::informant; + use display::{config::Config, std_json::tests::informant}; - let (inf, res) = informant(); + let (inf, res) = informant(Config::default()); let mut params = ActionParams::default(); params.code_address = 0x20.into(); params.gas = 0xffff.into();