Fix chainspec storage field. (#3406)

Add a test.
This commit is contained in:
Gav Wood 2016-11-13 13:58:42 +01:00 committed by GitHub
parent 37f49aac1b
commit 4a92668c56
4 changed files with 33 additions and 8 deletions

View File

@ -89,7 +89,7 @@ impl From<ethjson::blockchain::Account> for PodAccount {
let key: U256 = key.into(); let key: U256 = key.into();
let value: U256 = value.into(); let value: U256 = value.into();
(H256::from(key), H256::from(value)) (H256::from(key), H256::from(value))
}).collect() }).collect(),
} }
} }
} }
@ -99,8 +99,12 @@ impl From<ethjson::spec::Account> for PodAccount {
PodAccount { PodAccount {
balance: a.balance.map_or_else(U256::zero, Into::into), balance: a.balance.map_or_else(U256::zero, Into::into),
nonce: a.nonce.map_or_else(U256::zero, Into::into), nonce: a.nonce.map_or_else(U256::zero, Into::into),
code: a.code.map(Into::into).or_else(|| Some(Vec::new())), code: Some(a.code.map_or_else(Vec::new, Into::into)),
storage: BTreeMap::new() storage: a.storage.map_or_else(BTreeMap::new, |s| s.into_iter().map(|(key, value)| {
let key: U256 = key.into();
let value: U256 = value.into();
(H256::from(key), H256::from(value))
}).collect()),
} }
} }
} }
@ -112,7 +116,7 @@ impl fmt::Display for PodAccount {
self.nonce, self.nonce,
self.code.as_ref().map_or(0, |c| c.len()), self.code.as_ref().map_or(0, |c| c.len()),
self.code.as_ref().map_or_else(H256::new, |c| c.sha3()), self.code.as_ref().map_or_else(H256::new, |c| c.sha3()),
self.storage.len() self.storage.len(),
) )
} }
} }

View File

@ -162,7 +162,7 @@ impl Spec {
/// Get the configured Network ID. /// Get the configured Network ID.
pub fn network_id(&self) -> usize { self.params.network_id } pub fn network_id(&self) -> usize { self.params.network_id }
/// Get the configured Network ID. /// Get the configured subprotocol name.
pub fn subprotocol_name(&self) -> String { self.params.subprotocol_name.clone() } pub fn subprotocol_name(&self) -> String { self.params.subprotocol_name.clone() }
/// Get the configured network fork block. /// Get the configured network fork block.

View File

@ -16,6 +16,7 @@
//! Spec account deserialization. //! Spec account deserialization.
use std::collections::BTreeMap;
use uint::Uint; use uint::Uint;
use bytes::Bytes; use bytes::Bytes;
use spec::builtin::Builtin; use spec::builtin::Builtin;
@ -30,18 +31,21 @@ pub struct Account {
/// Nonce. /// Nonce.
pub nonce: Option<Uint>, pub nonce: Option<Uint>,
/// Code. /// Code.
pub code: Option<Bytes> pub code: Option<Bytes>,
/// Storage
pub storage: Option<BTreeMap<Uint, Uint>>,
} }
impl Account { impl Account {
/// Returns true if account does not have nonce and balance. /// Returns true if account does not have nonce and balance.
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.balance.is_none() && self.nonce.is_none() self.balance.is_none() && self.nonce.is_none() && self.code.is_none() && self.storage.is_none()
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::collections::BTreeMap;
use serde_json; use serde_json;
use spec::account::Account; use spec::account::Account;
use util::U256; use util::U256;
@ -62,4 +66,21 @@ mod tests {
assert_eq!(deserialized.code.unwrap(), Bytes::new(vec![0x12, 0x34])); assert_eq!(deserialized.code.unwrap(), Bytes::new(vec![0x12, 0x34]));
assert!(deserialized.builtin.is_some()); // Further tested in builtin.rs assert!(deserialized.builtin.is_some()); // Further tested in builtin.rs
} }
#[test]
fn account_storage_deserialization() {
let s = r#"{
"balance": "1",
"nonce": "0",
"code": "1234",
"storage": { "0x7fffffffffffffff7fffffffffffffff": "0x1" }
}"#;
let deserialized: Account = serde_json::from_str(s).unwrap();
assert_eq!(deserialized.balance.unwrap(), Uint(U256::from(1)));
assert_eq!(deserialized.nonce.unwrap(), Uint(U256::from(0)));
assert_eq!(deserialized.code.unwrap(), Bytes::new(vec![0x12, 0x34]));
let mut storage = BTreeMap::new();
storage.insert(Uint(U256::from("7fffffffffffffff7fffffffffffffff")), Uint(U256::from(1)));
assert_eq!(deserialized.storage.unwrap(), storage);
}
} }