Merge pull request #1790 from ethcore/spec-code

Allow code from spec json
This commit is contained in:
Nikolay Volf 2016-08-01 21:41:46 +03:00 committed by GitHub
commit 21c65a99ea
3 changed files with 19 additions and 7 deletions

View File

@ -96,7 +96,7 @@ 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: Some(vec![]), code: a.code.map(Into::into).or(Some(Vec::new())),
storage: BTreeMap::new() storage: BTreeMap::new()
} }
} }

View File

@ -17,6 +17,7 @@
//! Spec account deserialization. //! Spec account deserialization.
use uint::Uint; use uint::Uint;
use bytes::Bytes;
use spec::builtin::Builtin; use spec::builtin::Builtin;
/// Spec account. /// Spec account.
@ -28,6 +29,8 @@ pub struct Account {
pub balance: Option<Uint>, pub balance: Option<Uint>,
/// Nonce. /// Nonce.
pub nonce: Option<Uint>, pub nonce: Option<Uint>,
/// Code.
pub code: Option<Bytes>
} }
impl Account { impl Account {
@ -41,14 +44,22 @@ impl Account {
mod tests { mod tests {
use serde_json; use serde_json;
use spec::account::Account; use spec::account::Account;
use util::numbers::U256;
use uint::Uint;
use bytes::Bytes;
#[test] #[test]
fn account_deserialization() { fn account_deserialization() {
let s = r#"{ let s = r#"{
"balance": "1", "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(); let deserialized: Account = serde_json::from_str(s).unwrap();
// TODO: validate all fields 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
} }
} }

View File

@ -45,7 +45,7 @@ pub struct Builtin {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use serde_json; use serde_json;
use spec::builtin::Builtin; use spec::builtin::{Builtin, Pricing, Linear};
#[test] #[test]
fn builtin_deserialization() { fn builtin_deserialization() {
@ -53,7 +53,8 @@ mod tests {
"name": "ecrecover", "name": "ecrecover",
"pricing": { "linear": { "base": 3000, "word": 0 } } "pricing": { "linear": { "base": 3000, "word": 0 } }
}"#; }"#;
let _deserialized: Builtin = serde_json::from_str(s).unwrap(); let deserialized: Builtin = serde_json::from_str(s).unwrap();
// TODO: validate all fields assert_eq!(deserialized.name, "ecrecover");
assert_eq!(deserialized.pricing, Pricing::Linear(Linear { base: 3000, word: 0 }));
} }
} }