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.
This commit is contained in:
Gav Wood 2016-06-08 12:40:55 -07:00
parent a72ee5c16a
commit 293d9f15d5
2 changed files with 66 additions and 16 deletions

View File

@ -38,23 +38,7 @@ pub struct Block {
impl Block { impl Block {
/// Returns true if the given bytes form a valid encoding of a block in RLP. /// 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 { 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::<Header>(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::<Transaction>().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::<Header>().is_err()).is_some() {
return false;
}
true*/
UntrustedRlp::new(b).as_val::<Block>().is_ok() UntrustedRlp::new(b).as_val::<Block>().is_ok()
} }
} }

View File

@ -417,6 +417,7 @@ impl From<EthTrace> for Trace {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use serde_json; use serde_json;
use std::collections::BTreeMap;
use util::{U256, H256, Address}; use util::{U256, H256, Address};
use v1::types::Bytes; use v1::types::Bytes;
use super::*; 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"}"#); 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] #[test]
fn test_action_serialize() { fn test_action_serialize() {
let actions = vec![Action::Call(Call { let actions = vec![Action::Call(Call {