[evmbin] Omit storage output, now for std-json (#311)

* [evmbin] Omit storage output, now for std-json
* fix tests
This commit is contained in:
rakita 2021-03-10 16:39:32 +01:00 committed by GitHub
parent eca8fb74ae
commit d7a958129f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 13 deletions

View File

@ -16,7 +16,7 @@
//! Config used by display informants //! Config used by display informants
#[derive(Default, Copy, Clone)] #[derive(Default, Copy, Clone, Debug)]
pub struct Config { pub struct Config {
omit_storage_output: bool, omit_storage_output: bool,
omit_memory_output: bool, omit_memory_output: bool,

View File

@ -284,7 +284,7 @@ mod tests {
gas_cost: U256, gas_cost: U256,
memory: String, memory: String,
stack: Vec<U256>, stack: Vec<U256>,
storage: HashMap<H256, H256>, storage: Option<HashMap<H256, H256>>,
depth: usize, 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":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":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} {"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}
"#, "#,
) )
} }

View File

@ -203,6 +203,11 @@ impl<Trace: Writer, Out: Writer> trace::VMTracer for Informant<Trace, Out> {
fn trace_next_instruction(&mut self, pc: usize, instruction: u8, current_gas: U256) -> bool { fn trace_next_instruction(&mut self, pc: usize, instruction: u8, current_gas: U256) -> bool {
let subdepth = self.subdepth; let subdepth = self.subdepth;
Self::with_informant_in_depth(self, subdepth, |informant: &mut Informant<Trace, Out>| { Self::with_informant_in_depth(self, subdepth, |informant: &mut Informant<Trace, Out>| {
let storage = if informant.config.omit_storage_output() {
None
} else {
Some(&informant.storage)
};
let info = ::evm::Instruction::from_u8(instruction).map(|i| i.info()); let info = ::evm::Instruction::from_u8(instruction).map(|i| i.info());
informant.instruction = instruction; informant.instruction = instruction;
let trace_data = json!({ let trace_data = json!({
@ -211,7 +216,7 @@ impl<Trace: Writer, Out: Writer> trace::VMTracer for Informant<Trace, Out> {
"opName": info.map(|i| i.name).unwrap_or(""), "opName": info.map(|i| i.name).unwrap_or(""),
"gas": format!("{:#x}", current_gas), "gas": format!("{:#x}", current_gas),
"stack": informant.stack, "stack": informant.stack,
"storage": informant.storage, "storage": storage,
"depth": informant.depth, "depth": informant.depth,
}); });
@ -307,19 +312,16 @@ pub mod tests {
} }
} }
pub fn informant() -> (Informant<TestWriter, TestWriter>, Arc<Mutex<Vec<u8>>>) { pub fn informant(config: Config) -> (Informant<TestWriter, TestWriter>, Arc<Mutex<Vec<u8>>>) {
let trace_writer: TestWriter = Default::default(); let trace_writer: TestWriter = Default::default();
let out_writer: TestWriter = Default::default(); let out_writer: TestWriter = Default::default();
let res = trace_writer.0.clone(); let res = trace_writer.0.clone();
( (Informant::new(trace_writer, out_writer, config), res)
Informant::new(trace_writer, out_writer, Config::default()),
res,
)
} }
#[test] #[test]
fn should_trace_failure() { fn should_trace_failure() {
let (inf, res) = informant(); let (inf, res) = informant(Config::default());
run_test( run_test(
inf, inf,
move |_, expected| { move |_, expected| {
@ -333,7 +335,7 @@ pub mod tests {
"#, "#,
); );
let (inf, res) = informant(); let (inf, res) = informant(Config::default());
run_test( run_test(
inf, inf,
move |_, expected| { move |_, expected| {
@ -349,7 +351,7 @@ pub mod tests {
#[test] #[test]
fn should_trace_create_correctly() { fn should_trace_create_correctly() {
let (informant, res) = informant(); let (informant, res) = informant(Config::default());
run_test( run_test(
informant, informant,
move |_, expected| { 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":"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":"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":{}} {"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}
"#, "#,
) )
} }

View File

@ -287,9 +287,9 @@ pub mod tests {
#[test] #[test]
fn should_call_account_from_spec() { 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(); let mut params = ActionParams::default();
params.code_address = 0x20.into(); params.code_address = 0x20.into();
params.gas = 0xffff.into(); params.gas = 0xffff.into();