From 4a92668c566e362c9847886fcc39e564c49b3a09 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sun, 13 Nov 2016 13:58:42 +0100 Subject: [PATCH] Fix chainspec storage field. (#3406) Add a test. --- ethcore/src/pod_account.rs | 12 ++++++++---- ethcore/src/spec/spec.rs | 2 +- json/src/blockchain/account.rs | 2 +- json/src/spec/account.rs | 25 +++++++++++++++++++++++-- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/ethcore/src/pod_account.rs b/ethcore/src/pod_account.rs index afee32f94..0882b688c 100644 --- a/ethcore/src/pod_account.rs +++ b/ethcore/src/pod_account.rs @@ -89,7 +89,7 @@ impl From for PodAccount { let key: U256 = key.into(); let value: U256 = value.into(); (H256::from(key), H256::from(value)) - }).collect() + }).collect(), } } } @@ -99,8 +99,12 @@ impl From for PodAccount { PodAccount { balance: a.balance.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())), - storage: BTreeMap::new() + code: Some(a.code.map_or_else(Vec::new, Into::into)), + 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.code.as_ref().map_or(0, |c| c.len()), self.code.as_ref().map_or_else(H256::new, |c| c.sha3()), - self.storage.len() + self.storage.len(), ) } } diff --git a/ethcore/src/spec/spec.rs b/ethcore/src/spec/spec.rs index 2babfb708..6bfb1bcc1 100644 --- a/ethcore/src/spec/spec.rs +++ b/ethcore/src/spec/spec.rs @@ -162,7 +162,7 @@ impl Spec { /// Get the configured 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() } /// Get the configured network fork block. diff --git a/json/src/blockchain/account.rs b/json/src/blockchain/account.rs index ca69409fc..649cd170f 100644 --- a/json/src/blockchain/account.rs +++ b/json/src/blockchain/account.rs @@ -48,7 +48,7 @@ mod tests { "0x01" : "0x9a10c2b5bb8f3c602e674006d9b21f09167df57c87a78a5ce96d4159ecb76520" } }"#; - let _deserialized: Account= serde_json::from_str(s).unwrap(); + let _deserialized: Account = serde_json::from_str(s).unwrap(); // TODO: validate all fields } } diff --git a/json/src/spec/account.rs b/json/src/spec/account.rs index eafb60931..90ca8a658 100644 --- a/json/src/spec/account.rs +++ b/json/src/spec/account.rs @@ -16,6 +16,7 @@ //! Spec account deserialization. +use std::collections::BTreeMap; use uint::Uint; use bytes::Bytes; use spec::builtin::Builtin; @@ -30,18 +31,21 @@ pub struct Account { /// Nonce. pub nonce: Option, /// Code. - pub code: Option + pub code: Option, + /// Storage + pub storage: Option>, } impl Account { /// Returns true if account does not have nonce and balance. 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)] mod tests { + use std::collections::BTreeMap; use serde_json; use spec::account::Account; use util::U256; @@ -62,4 +66,21 @@ mod tests { assert_eq!(deserialized.code.unwrap(), Bytes::new(vec![0x12, 0x34])); 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); + } }