From 293d9f15d5775622f50314dd03c56a9e7350fa13 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 8 Jun 2016 12:40:55 -0700 Subject: [PATCH] Tests for JSON serialisation of statediff/vmtrace (#1241) * Splitting RPC Apis into more fine-grained sets * Remove old code/comment. * Add test for VMTrace ser. * Add StateDiff ser test. --- ethcore/src/block.rs | 16 ---------- rpc/src/v1/types/trace.rs | 66 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 16 deletions(-) diff --git a/ethcore/src/block.rs b/ethcore/src/block.rs index 8f2dc263c..74f54a057 100644 --- a/ethcore/src/block.rs +++ b/ethcore/src/block.rs @@ -38,23 +38,7 @@ pub struct Block { impl Block { /// Returns true if the given bytes form a valid encoding of a block in RLP. - // TODO: implement Decoder for this and have this use that. pub fn is_good(b: &[u8]) -> bool { - /* - let urlp = UntrustedRlp::new(&b); - if !urlp.is_list() || urlp.item_count() != 3 || urlp.size() != b.len() { return false; } - if urlp.val_at::
(0).is_err() { return false; } - - if !urlp.at(1).unwrap().is_list() { return false; } - if urlp.at(1).unwrap().iter().find(|i| i.as_val::().is_err()).is_some() { - return false; - } - - if !urlp.at(2).unwrap().is_list() { return false; } - if urlp.at(2).unwrap().iter().find(|i| i.as_val::
().is_err()).is_some() { - return false; - } - true*/ UntrustedRlp::new(b).as_val::().is_ok() } } diff --git a/rpc/src/v1/types/trace.rs b/rpc/src/v1/types/trace.rs index 5b4192886..12bcfa1cc 100644 --- a/rpc/src/v1/types/trace.rs +++ b/rpc/src/v1/types/trace.rs @@ -417,6 +417,7 @@ impl From for Trace { #[cfg(test)] mod tests { use serde_json; + use std::collections::BTreeMap; use util::{U256, H256, Address}; use v1::types::Bytes; use super::*; @@ -446,6 +447,71 @@ mod tests { assert_eq!(serialized, r#"{"action":{"call":{"from":"0x0000000000000000000000000000000000000004","to":"0x0000000000000000000000000000000000000005","value":"0x06","gas":"0x07","input":"0x1234"}},"result":{"call":{"gasUsed":"0x08","output":"0x5678"}},"traceAddress":["0x0a"],"subtraces":"0x01","transactionPosition":"0x0b","transactionHash":"0x000000000000000000000000000000000000000000000000000000000000000c","blockNumber":"0x0d","blockHash":"0x000000000000000000000000000000000000000000000000000000000000000e"}"#); } + #[test] + fn test_vmtrace_serialize() { + let t = VMTrace { + code: vec![0, 1, 2, 3], + ops: vec![ + VMOperation { + pc: 0, + cost: 10, + ex: None, + sub: None, + }, + VMOperation { + pc: 1, + cost: 11, + ex: Some(VMExecutedOperation { + used: 10, + push: vec![69.into()], + mem: None, + store: None, + }), + sub: Some(VMTrace { + code: vec![0], + ops: vec![ + VMOperation { + pc: 0, + cost: 0, + ex: Some(VMExecutedOperation { + used: 10, + push: vec![42.into()], + mem: Some(MemoryDiff {off: 42, data: vec![1, 2, 3]}), + store: Some(StorageDiff {key: 69.into(), val: 42.into()}), + }), + sub: None, + } + ] + }), + } + ] + }; + let serialized = serde_json::to_string(&t).unwrap(); + assert_eq!(serialized, "{\"code\":[0,1,2,3],\"ops\":[{\"pc\":0,\"cost\":10,\"ex\":null,\"sub\":null},{\"pc\":1,\"cost\":11,\"ex\":{\"used\":10,\"push\":[\"0x45\"],\"mem\":null,\"store\":null},\"sub\":{\"code\":[0],\"ops\":[{\"pc\":0,\"cost\":0,\"ex\":{\"used\":10,\"push\":[\"0x2a\"],\"mem\":{\"off\":42,\"data\":[1,2,3]},\"store\":{\"key\":\"0x45\",\"val\":\"0x2a\"}},\"sub\":null}]}}]}"); + } + + #[test] + fn test_statediff_serialize() { + let t = StateDiff(map![ + 42.into() => AccountDiff { + balance: Diff::Same, + nonce: Diff::Born(1.into()), + code: Diff::Same, + storage: map![ + 42.into() => Diff::Same + ] + }, + 69.into() => AccountDiff { + balance: Diff::Same, + nonce: Diff::Changed(ChangedType { from: 1.into(), to: 0.into() }), + code: Diff::Died(vec![96].into()), + storage: map![], + } + ]); + let serialized = serde_json::to_string(&t).unwrap(); + assert_eq!(serialized, "{\"0x000000000000000000000000000000000000002a\":{\"balance\":{\"=\":[]},\"nonce\":{\"+\":\"0x01\"},\"code\":{\"=\":[]},\"storage\":{\"0x000000000000000000000000000000000000000000000000000000000000002a\":{\"=\":[]}}},\"0x0000000000000000000000000000000000000045\":{\"balance\":{\"=\":[]},\"nonce\":{\"*\":{\"from\":\"0x01\",\"to\":\"0x00\"}},\"code\":{\"-\":\"0x60\"},\"storage\":{}}}"); + } + #[test] fn test_action_serialize() { let actions = vec![Action::Call(Call {