From b4849d1c5870db13020f356bdffe4cee7c6a7161 Mon Sep 17 00:00:00 2001 From: debris Date: Tue, 15 Mar 2016 18:23:59 +0100 Subject: [PATCH] finished blockchain test deserialization --- json/src/blockchain/header.rs | 22 ++++----- json/src/blockchain/state.rs | 6 +-- json/src/bytes.rs | 40 +++++++-------- json/src/hash.rs | 91 ++++++++++++++++++++--------------- 4 files changed, 86 insertions(+), 73 deletions(-) diff --git a/json/src/blockchain/header.rs b/json/src/blockchain/header.rs index ea58c76a1..fc27fc1f5 100644 --- a/json/src/blockchain/header.rs +++ b/json/src/blockchain/header.rs @@ -16,15 +16,15 @@ //! Blockchain test header deserializer. -use hash::Hash; +use hash::{H64, H256, Bloom}; use uint::Uint; use bytes::Bytes; /// Blockchain test header deserializer. #[derive(Debug, PartialEq, Deserialize)] pub struct Header { - bloom: Hash, // TODO Bloom - coinbase: Hash, + bloom: Bloom, + coinbase: H256, difficulty: Uint, #[serde(rename="extraData")] extra_data: Bytes, @@ -32,20 +32,20 @@ pub struct Header { gas_limit: Uint, #[serde(rename="gasUsed")] gas_used: Uint, - hash: Hash, + hash: H256, #[serde(rename="mixHash")] - mix_hash: Hash, - nonce: Uint, // TODO fix parsing + mix_hash: H256, + nonce: H64, number: Uint, #[serde(rename="parentHash")] - parent_hash: Hash, + parent_hash: H256, #[serde(rename="receiptTrie")] - receipt_trie: Hash, + receipt_trie: H256, #[serde(rename="stateRoot")] - state_root: Hash, + state_root: H256, timestamp: Uint, #[serde(rename="transactionsTrie")] - transactions_trie: Hash, + transactions_trie: H256, #[serde(rename="uncleHash")] - uncle_hash: Hash, + uncle_hash: H256, } diff --git a/json/src/blockchain/state.rs b/json/src/blockchain/state.rs index c934d7c0a..2af0dbf21 100644 --- a/json/src/blockchain/state.rs +++ b/json/src/blockchain/state.rs @@ -18,15 +18,15 @@ use std::collections::BTreeMap; use std::ops::Deref; -use hash::Hash; +use hash::Address; use blockchain::account::Account; /// Blockchain test state deserializer. #[derive(Debug, PartialEq, Deserialize)] -pub struct State(BTreeMap); +pub struct State(BTreeMap); impl Deref for State { - type Target = BTreeMap; + type Target = BTreeMap; fn deref(&self) -> &Self::Target { &self.0 diff --git a/json/src/bytes.rs b/json/src/bytes.rs index 034c90d62..c9e31e888 100644 --- a/json/src/bytes.rs +++ b/json/src/bytes.rs @@ -58,24 +58,26 @@ impl Visitor for BytesVisitor { } #[cfg(test)] -//mod test { - //use std::str::FromStr; - //use serde_json; - //use util::hash::H256; - //use hash::Hash; +mod test { + use serde_json; + use bytes::Bytes; - //#[test] - //fn uint_deserialization() { - //let s = r#"["", "5a39ed1020c04d4d84539975b893a4e7c53eab6c2965db8bc3468093a31bc5ae"]"#; - //let deserialized: Vec = serde_json::from_str(s).unwrap(); - //assert_eq!(deserialized, vec![ - //Hash(H256::from(0)), - //Hash(H256::from_str("5a39ed1020c04d4d84539975b893a4e7c53eab6c2965db8bc3468093a31bc5ae").unwrap()) - //]); - //} + #[test] + fn bytes_deserialization() { + let s = r#"["", "0x", "0x12", "1234"]"#; + let deserialized: Vec = serde_json::from_str(s).unwrap(); + assert_eq!(deserialized, vec![ + Bytes(vec![]), + Bytes(vec![]), + Bytes(vec![0x12]), + Bytes(vec![0x12, 0x34]) + ]); + } - //#[test] - //fn uint_into() { - //assert_eq!(H256::from(0), Hash(H256::from(0)).into()); - //} -//} + #[test] + fn bytes_into() { + let bytes = Bytes(vec![0xff, 0x11]); + let v: Vec = bytes.into(); + assert_eq!(vec![0xff, 0x11], v); + } +} diff --git a/json/src/hash.rs b/json/src/hash.rs index e5bbedbfa..8ed28c33c 100644 --- a/json/src/hash.rs +++ b/json/src/hash.rs @@ -19,63 +19,74 @@ use std::str::FromStr; use serde::{Deserialize, Deserializer, Error}; use serde::de::Visitor; -use util::hash::H256; +use util::hash::{H64 as Hash64, Address as Hash160, H256 as Hash256, H2048 as Hash2048}; -/// Lenient hash json deserialization for test json files. -#[derive(Default, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] -pub struct Hash(H256); -impl Into for Hash { - fn into(self) -> H256 { - self.0 +macro_rules! impl_hash { + ($name: ident, $inner: ident) => { + /// Lenient hash json deserialization for test json files. + #[derive(Default, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] + pub struct $name($inner); + + impl Into<$inner> for $name { + fn into(self) -> $inner { + self.0 + } + } + + impl Deserialize for $name { + fn deserialize(deserializer: &mut D) -> Result + where D: Deserializer { + + struct HashVisitor; + + impl Visitor for HashVisitor { + type Value = $name; + + fn visit_str(&mut self, value: &str) -> Result where E: Error { + let value = match value.len() { + 0 => $inner::from(0), + _ => try!($inner::from_str(value).map_err(|_| Error::custom("invalid hex value."))) + }; + + Ok($name(value)) + } + + fn visit_string(&mut self, value: String) -> Result where E: Error { + self.visit_str(value.as_ref()) + } + } + + deserializer.deserialize(HashVisitor) + } + } } } -impl Deserialize for Hash { - fn deserialize(deserializer: &mut D) -> Result - where D: Deserializer { - deserializer.deserialize(HashVisitor) - } -} - -struct HashVisitor; - -impl Visitor for HashVisitor { - type Value = Hash; - - fn visit_str(&mut self, value: &str) -> Result where E: Error { - let value = match value.len() { - 0 => H256::from(0), - _ => try!(H256::from_str(value).map_err(|_| Error::custom("invalid hex value."))) - }; - - Ok(Hash(value)) - } - - fn visit_string(&mut self, value: String) -> Result where E: Error { - self.visit_str(value.as_ref()) - } -} +impl_hash!(H64, Hash64); +impl_hash!(Address, Hash160); +impl_hash!(H256, Hash256); +impl_hash!(Bloom, Hash2048); #[cfg(test)] mod test { use std::str::FromStr; use serde_json; - use util::hash::H256; - use hash::Hash; + use util::hash; + use hash::H256; #[test] - fn uint_deserialization() { + fn hash_deserialization() { let s = r#"["", "5a39ed1020c04d4d84539975b893a4e7c53eab6c2965db8bc3468093a31bc5ae"]"#; - let deserialized: Vec = serde_json::from_str(s).unwrap(); + let deserialized: Vec = serde_json::from_str(s).unwrap(); assert_eq!(deserialized, vec![ - Hash(H256::from(0)), - Hash(H256::from_str("5a39ed1020c04d4d84539975b893a4e7c53eab6c2965db8bc3468093a31bc5ae").unwrap()) + H256(hash::H256::from(0)), + H256(hash::H256::from_str("5a39ed1020c04d4d84539975b893a4e7c53eab6c2965db8bc3468093a31bc5ae").unwrap()) ]); } #[test] - fn uint_into() { - assert_eq!(H256::from(0), Hash(H256::from(0)).into()); + fn hash_into() { + assert_eq!(hash::H256::from(0), H256(hash::H256::from(0)).into()); } }