finished blockchain test deserialization

This commit is contained in:
debris 2016-03-15 18:23:59 +01:00
parent d96858d38c
commit b4849d1c58
4 changed files with 86 additions and 73 deletions

View File

@ -16,15 +16,15 @@
//! Blockchain test header deserializer. //! Blockchain test header deserializer.
use hash::Hash; use hash::{H64, H256, Bloom};
use uint::Uint; use uint::Uint;
use bytes::Bytes; use bytes::Bytes;
/// Blockchain test header deserializer. /// Blockchain test header deserializer.
#[derive(Debug, PartialEq, Deserialize)] #[derive(Debug, PartialEq, Deserialize)]
pub struct Header { pub struct Header {
bloom: Hash, // TODO Bloom bloom: Bloom,
coinbase: Hash, coinbase: H256,
difficulty: Uint, difficulty: Uint,
#[serde(rename="extraData")] #[serde(rename="extraData")]
extra_data: Bytes, extra_data: Bytes,
@ -32,20 +32,20 @@ pub struct Header {
gas_limit: Uint, gas_limit: Uint,
#[serde(rename="gasUsed")] #[serde(rename="gasUsed")]
gas_used: Uint, gas_used: Uint,
hash: Hash, hash: H256,
#[serde(rename="mixHash")] #[serde(rename="mixHash")]
mix_hash: Hash, mix_hash: H256,
nonce: Uint, // TODO fix parsing nonce: H64,
number: Uint, number: Uint,
#[serde(rename="parentHash")] #[serde(rename="parentHash")]
parent_hash: Hash, parent_hash: H256,
#[serde(rename="receiptTrie")] #[serde(rename="receiptTrie")]
receipt_trie: Hash, receipt_trie: H256,
#[serde(rename="stateRoot")] #[serde(rename="stateRoot")]
state_root: Hash, state_root: H256,
timestamp: Uint, timestamp: Uint,
#[serde(rename="transactionsTrie")] #[serde(rename="transactionsTrie")]
transactions_trie: Hash, transactions_trie: H256,
#[serde(rename="uncleHash")] #[serde(rename="uncleHash")]
uncle_hash: Hash, uncle_hash: H256,
} }

View File

@ -18,15 +18,15 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::ops::Deref; use std::ops::Deref;
use hash::Hash; use hash::Address;
use blockchain::account::Account; use blockchain::account::Account;
/// Blockchain test state deserializer. /// Blockchain test state deserializer.
#[derive(Debug, PartialEq, Deserialize)] #[derive(Debug, PartialEq, Deserialize)]
pub struct State(BTreeMap<Hash, Account>); pub struct State(BTreeMap<Address, Account>);
impl Deref for State { impl Deref for State {
type Target = BTreeMap<Hash, Account>; type Target = BTreeMap<Address, Account>;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.0 &self.0

View File

@ -58,24 +58,26 @@ impl Visitor for BytesVisitor {
} }
#[cfg(test)] #[cfg(test)]
//mod test { mod test {
//use std::str::FromStr; use serde_json;
//use serde_json; use bytes::Bytes;
//use util::hash::H256;
//use hash::Hash;
//#[test] #[test]
//fn uint_deserialization() { fn bytes_deserialization() {
//let s = r#"["", "5a39ed1020c04d4d84539975b893a4e7c53eab6c2965db8bc3468093a31bc5ae"]"#; let s = r#"["", "0x", "0x12", "1234"]"#;
//let deserialized: Vec<Hash> = serde_json::from_str(s).unwrap(); let deserialized: Vec<Bytes> = serde_json::from_str(s).unwrap();
//assert_eq!(deserialized, vec![ assert_eq!(deserialized, vec![
//Hash(H256::from(0)), Bytes(vec![]),
//Hash(H256::from_str("5a39ed1020c04d4d84539975b893a4e7c53eab6c2965db8bc3468093a31bc5ae").unwrap()) Bytes(vec![]),
//]); Bytes(vec![0x12]),
//} Bytes(vec![0x12, 0x34])
]);
}
//#[test] #[test]
//fn uint_into() { fn bytes_into() {
//assert_eq!(H256::from(0), Hash(H256::from(0)).into()); let bytes = Bytes(vec![0xff, 0x11]);
//} let v: Vec<u8> = bytes.into();
//} assert_eq!(vec![0xff, 0x11], v);
}
}

View File

@ -19,63 +19,74 @@
use std::str::FromStr; use std::str::FromStr;
use serde::{Deserialize, Deserializer, Error}; use serde::{Deserialize, Deserializer, Error};
use serde::de::Visitor; 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<H256> for Hash { macro_rules! impl_hash {
fn into(self) -> H256 { ($name: ident, $inner: ident) => {
self.0 /// 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<D>(deserializer: &mut D) -> Result<Self, D::Error>
where D: Deserializer {
struct HashVisitor;
impl Visitor for HashVisitor {
type Value = $name;
fn visit_str<E>(&mut self, value: &str) -> Result<Self::Value, E> 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<E>(&mut self, value: String) -> Result<Self::Value, E> where E: Error {
self.visit_str(value.as_ref())
}
}
deserializer.deserialize(HashVisitor)
}
}
} }
} }
impl Deserialize for Hash { impl_hash!(H64, Hash64);
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error> impl_hash!(Address, Hash160);
where D: Deserializer { impl_hash!(H256, Hash256);
deserializer.deserialize(HashVisitor) impl_hash!(Bloom, Hash2048);
}
}
struct HashVisitor;
impl Visitor for HashVisitor {
type Value = Hash;
fn visit_str<E>(&mut self, value: &str) -> Result<Self::Value, E> 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<E>(&mut self, value: String) -> Result<Self::Value, E> where E: Error {
self.visit_str(value.as_ref())
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use std::str::FromStr; use std::str::FromStr;
use serde_json; use serde_json;
use util::hash::H256; use util::hash;
use hash::Hash; use hash::H256;
#[test] #[test]
fn uint_deserialization() { fn hash_deserialization() {
let s = r#"["", "5a39ed1020c04d4d84539975b893a4e7c53eab6c2965db8bc3468093a31bc5ae"]"#; let s = r#"["", "5a39ed1020c04d4d84539975b893a4e7c53eab6c2965db8bc3468093a31bc5ae"]"#;
let deserialized: Vec<Hash> = serde_json::from_str(s).unwrap(); let deserialized: Vec<H256> = serde_json::from_str(s).unwrap();
assert_eq!(deserialized, vec![ assert_eq!(deserialized, vec![
Hash(H256::from(0)), H256(hash::H256::from(0)),
Hash(H256::from_str("5a39ed1020c04d4d84539975b893a4e7c53eab6c2965db8bc3468093a31bc5ae").unwrap()) H256(hash::H256::from_str("5a39ed1020c04d4d84539975b893a4e7c53eab6c2965db8bc3468093a31bc5ae").unwrap())
]); ]);
} }
#[test] #[test]
fn uint_into() { fn hash_into() {
assert_eq!(H256::from(0), Hash(H256::from(0)).into()); assert_eq!(hash::H256::from(0), H256(hash::H256::from(0)).into());
} }
} }