finished blockchain test deserialization
This commit is contained in:
parent
d96858d38c
commit
b4849d1c58
@ -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,
|
||||
}
|
||||
|
@ -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<Hash, Account>);
|
||||
pub struct State(BTreeMap<Address, Account>);
|
||||
|
||||
impl Deref for State {
|
||||
type Target = BTreeMap<Hash, Account>;
|
||||
type Target = BTreeMap<Address, Account>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
|
@ -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<Hash> = 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<Bytes> = 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<u8> = bytes.into();
|
||||
assert_eq!(vec![0xff, 0x11], v);
|
||||
}
|
||||
}
|
||||
|
@ -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<H256> 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<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 {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||
where D: Deserializer {
|
||||
deserializer.deserialize(HashVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
}
|
||||
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<Hash> = serde_json::from_str(s).unwrap();
|
||||
let deserialized: Vec<H256> = 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());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user