From 7dbf23829ef5f6aac7ec72fd478ea1a643de3a55 Mon Sep 17 00:00:00 2001 From: keorn Date: Mon, 1 Aug 2016 12:33:21 +0200 Subject: [PATCH 1/3] improve ethjson tests, add code deserialization --- json/src/spec/account.rs | 17 ++++++++++++++--- json/src/spec/builtin.rs | 7 ++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/json/src/spec/account.rs b/json/src/spec/account.rs index 6ad142df3..3487906ae 100644 --- a/json/src/spec/account.rs +++ b/json/src/spec/account.rs @@ -17,6 +17,7 @@ //! Spec account deserialization. use uint::Uint; +use bytes::Bytes; use spec::builtin::Builtin; /// Spec account. @@ -28,6 +29,8 @@ pub struct Account { pub balance: Option, /// Nonce. pub nonce: Option, + /// Code. + pub code: Option } impl Account { @@ -41,14 +44,22 @@ impl Account { mod tests { use serde_json; use spec::account::Account; + use util::numbers::U256; + use uint::Uint; + use bytes::Bytes; #[test] fn account_deserialization() { let s = r#"{ "balance": "1", - "builtin": { "name": "ecrecover", "pricing": { "linear": { "base": 3000, "word": 0 } } } + "nonce": "0", + "builtin": { "name": "ecrecover", "pricing": { "linear": { "base": 3000, "word": 0 } } }, + "code": "1234" }"#; - let _deserialized: Account = serde_json::from_str(s).unwrap(); - // TODO: validate all fields + 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])); + assert!(deserialized.builtin.is_some()); // Further tested in builtin.rs } } diff --git a/json/src/spec/builtin.rs b/json/src/spec/builtin.rs index 454199883..d56c4a4c7 100644 --- a/json/src/spec/builtin.rs +++ b/json/src/spec/builtin.rs @@ -45,7 +45,7 @@ pub struct Builtin { #[cfg(test)] mod tests { use serde_json; - use spec::builtin::Builtin; + use spec::builtin::{Builtin, Pricing, Linear}; #[test] fn builtin_deserialization() { @@ -53,7 +53,8 @@ mod tests { "name": "ecrecover", "pricing": { "linear": { "base": 3000, "word": 0 } } }"#; - let _deserialized: Builtin = serde_json::from_str(s).unwrap(); - // TODO: validate all fields + let deserialized: Builtin = serde_json::from_str(s).unwrap(); + assert_eq!(deserialized.name, "ecrecover"); + assert_eq!(deserialized.pricing, Pricing::Linear(Linear { base: 3000, word: 0 })); } } From e99753d6e35bf88286abbdbedb884431eb17bc6a Mon Sep 17 00:00:00 2001 From: keorn Date: Mon, 1 Aug 2016 13:35:45 +0200 Subject: [PATCH 2/3] deserialized to pod_account conversion --- ethcore/src/pod_account.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethcore/src/pod_account.rs b/ethcore/src/pod_account.rs index 833ae9b6b..4e7d494d4 100644 --- a/ethcore/src/pod_account.rs +++ b/ethcore/src/pod_account.rs @@ -96,7 +96,7 @@ 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: Some(vec![]), + code: a.code.map(|c| c.into()).or(Some(Vec::new())), storage: BTreeMap::new() } } From de531c9a1e463c996be777b998fc695c59119c5f Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Mon, 1 Aug 2016 10:14:12 -0700 Subject: [PATCH 3/3] Minor simplification --- ethcore/src/pod_account.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethcore/src/pod_account.rs b/ethcore/src/pod_account.rs index 4e7d494d4..af7ef2ebb 100644 --- a/ethcore/src/pod_account.rs +++ b/ethcore/src/pod_account.rs @@ -96,7 +96,7 @@ 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(|c| c.into()).or(Some(Vec::new())), + code: a.code.map(Into::into).or(Some(Vec::new())), storage: BTreeMap::new() } }