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 {
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(Into::into).or(Some(Vec::new())),
storage: BTreeMap::new()
}
}

View File

@ -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<Uint>,
/// Nonce.
pub nonce: Option<Uint>,
/// Code.
pub code: Option<Bytes>
}
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
}
}

View File

@ -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 }));
}
}