Merge pull request #113 from gavofyork/gav

Refactored Pod & Diff types into separate files, JSON infrastructure revamp.
This commit is contained in:
Gav Wood 2016-01-14 22:28:58 +01:00
commit 5426c3e911
16 changed files with 587 additions and 621 deletions

View File

@ -18,7 +18,6 @@ rust-crypto = "0.2.34"
time = "0.1" time = "0.1"
#interpolate_idents = { git = "https://github.com/SkylerLipthay/interpolate_idents" } #interpolate_idents = { git = "https://github.com/SkylerLipthay/interpolate_idents" }
evmjit = { path = "rust-evmjit", optional = true } evmjit = { path = "rust-evmjit", optional = true }
itertools = "0.4"
[features] [features]
default = ["jit"] default = ["jit"]

View File

@ -1,396 +1,5 @@
use util::*; use util::*;
use itertools::Itertools; use pod_account::*;
pub const SHA3_EMPTY: H256 = H256( [0xc5, 0xd2, 0x46, 0x01, 0x86, 0xf7, 0x23, 0x3c, 0x92, 0x7e, 0x7d, 0xb2, 0xdc, 0xc7, 0x03, 0xc0, 0xe5, 0x00, 0xb6, 0x53, 0xca, 0x82, 0x27, 0x3b, 0x7b, 0xfa, 0xd8, 0x04, 0x5d, 0x85, 0xa4, 0x70] );
#[derive(Debug,Clone,PartialEq,Eq)]
pub enum Diff<T> where T: Eq {
Same,
Born(T),
Changed(T, T),
Died(T),
}
#[derive(Debug,Clone,PartialEq,Eq)]
pub enum Existance {
Born,
Alive,
Died,
}
impl<T> Diff<T> where T: Eq {
pub fn new(pre: T, post: T) -> Self { if pre == post { Diff::Same } else { Diff::Changed(pre, post) } }
pub fn pre(&self) -> Option<&T> { match self { &Diff::Died(ref x) | &Diff::Changed(ref x, _) => Some(x), _ => None } }
pub fn post(&self) -> Option<&T> { match self { &Diff::Born(ref x) | &Diff::Changed(_, ref x) => Some(x), _ => None } }
pub fn is_same(&self) -> bool { match self { &Diff::Same => true, _ => false }}
}
#[derive(Debug,Clone,PartialEq,Eq)]
/// Genesis account data. Does not have a DB overlay cache.
pub struct PodAccount {
// Balance of the account.
pub balance: U256,
// Nonce of the account.
pub nonce: U256,
pub code: Bytes,
pub storage: BTreeMap<H256, H256>,
}
impl fmt::Display for PodAccount {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "(bal={}; nonce={}; code={} bytes, #{}; storage={} items)", self.balance, self.nonce, self.code.len(), self.code.sha3(), self.storage.len())
}
}
#[derive(Debug,Clone,PartialEq,Eq)]
pub struct PodState (BTreeMap<Address, PodAccount>);
pub fn map_h256_h256_from_json(json: &Json) -> BTreeMap<H256, H256> {
json.as_object().unwrap().iter().fold(BTreeMap::new(), |mut m, (key, value)| {
m.insert(H256::from(&u256_from_str(key)), H256::from(&u256_from_json(value)));
m
})
}
impl PodState {
/// Contruct a new object from the `m`.
pub fn from(m: BTreeMap<Address, PodAccount>) -> PodState { PodState(m) }
/// Translate the JSON object into a hash map of account information ready for insertion into State.
pub fn from_json(json: &Json) -> PodState {
PodState(json.as_object().unwrap().iter().fold(BTreeMap::new(), |mut state, (address, acc)| {
let balance = acc.find("balance").map(&u256_from_json);
let nonce = acc.find("nonce").map(&u256_from_json);
let storage = acc.find("storage").map(&map_h256_h256_from_json);;
let code = acc.find("code").map(&bytes_from_json);
if balance.is_some() || nonce.is_some() || storage.is_some() || code.is_some() {
state.insert(address_from_hex(address), PodAccount{
balance: balance.unwrap_or(U256::zero()),
nonce: nonce.unwrap_or(U256::zero()),
storage: storage.unwrap_or(BTreeMap::new()),
code: code.unwrap_or(Vec::new())
});
}
state
}))
}
/// Drain object to get the underlying map.
pub fn drain(self) -> BTreeMap<Address, PodAccount> { self.0 }
}
impl fmt::Display for PodState {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (add, acc) in &self.0 {
try!(writeln!(f, "{} => {}", add, acc));
}
Ok(())
}
}
#[derive(Debug,Clone,PartialEq,Eq)]
pub struct AccountDiff {
pub balance: Diff<U256>, // Allowed to be Same
pub nonce: Diff<U256>, // Allowed to be Same
pub code: Diff<Bytes>, // Allowed to be Same
pub storage: BTreeMap<H256, Diff<H256>>,// Not allowed to be Same
}
impl AccountDiff {
pub fn existance(&self) -> Existance {
match self.balance {
Diff::Born(_) => Existance::Born,
Diff::Died(_) => Existance::Died,
_ => Existance::Alive,
}
}
}
fn format(u: &H256) -> String {
if u <= &H256::from(0xffffffff) {
format!("{} = 0x{:x}", U256::from(u.as_slice()).low_u32(), U256::from(u.as_slice()).low_u32())
} else if u <= &H256::from(u64::max_value()) {
format!("{} = 0x{:x}", U256::from(u.as_slice()).low_u64(), U256::from(u.as_slice()).low_u64())
// } else if u <= &H256::from("0xffffffffffffffffffffffffffffffffffffffff") {
// format!("@{}", Address::from(u))
} else {
format!("#{}", u)
}
}
#[derive(Debug,Clone,PartialEq,Eq)]
pub struct StateDiff (BTreeMap<Address, AccountDiff>);
impl fmt::Display for AccountDiff {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.nonce {
Diff::Born(ref x) => try!(write!(f, " non {}", x)),
Diff::Changed(ref pre, ref post) => try!(write!(f, "#{} ({} {} {})", post, pre, if pre > post {"-"} else {"+"}, *max(pre, post) - * min(pre, post))),
_ => {},
}
match self.balance {
Diff::Born(ref x) => try!(write!(f, " bal {}", x)),
Diff::Changed(ref pre, ref post) => try!(write!(f, "${} ({} {} {})", post, pre, if pre > post {"-"} else {"+"}, *max(pre, post) - *min(pre, post))),
_ => {},
}
match self.code {
Diff::Born(ref x) => try!(write!(f, " code {}", x.pretty())),
_ => {},
}
try!(write!(f, "\n"));
for (k, dv) in self.storage.iter() {
match dv {
&Diff::Born(ref v) => try!(write!(f, " + {} => {}\n", format(k), format(v))),
&Diff::Changed(ref pre, ref post) => try!(write!(f, " * {} => {} (was {})\n", format(k), format(post), format(pre))),
&Diff::Died(_) => try!(write!(f, " X {}\n", format(k))),
_ => {},
}
}
Ok(())
}
}
impl fmt::Display for Existance {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&Existance::Born => try!(write!(f, "+++")),
&Existance::Alive => try!(write!(f, "***")),
&Existance::Died => try!(write!(f, "XXX")),
}
Ok(())
}
}
impl fmt::Display for StateDiff {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (add, acc) in self.0.iter() {
try!(write!(f, "{} {}: {}", acc.existance(), add, acc));
}
Ok(())
}
}
pub fn pod_diff(pre: Option<&PodAccount>, post: Option<&PodAccount>) -> Option<AccountDiff> {
match (pre, post) {
(None, Some(x)) => Some(AccountDiff {
balance: Diff::Born(x.balance.clone()),
nonce: Diff::Born(x.nonce.clone()),
code: Diff::Born(x.code.clone()),
storage: x.storage.iter().map(|(k, v)| (k.clone(), Diff::Born(v.clone()))).collect(),
}),
(Some(x), None) => Some(AccountDiff {
balance: Diff::Died(x.balance.clone()),
nonce: Diff::Died(x.nonce.clone()),
code: Diff::Died(x.code.clone()),
storage: x.storage.iter().map(|(k, v)| (k.clone(), Diff::Died(v.clone()))).collect(),
}),
(Some(pre), Some(post)) => {
let storage: Vec<_> = pre.storage.keys().merge(post.storage.keys())
.filter(|k| pre.storage.get(k).unwrap_or(&H256::new()) != post.storage.get(k).unwrap_or(&H256::new()))
.collect();
let r = AccountDiff {
balance: Diff::new(pre.balance.clone(), post.balance.clone()),
nonce: Diff::new(pre.nonce.clone(), post.nonce.clone()),
code: Diff::new(pre.code.clone(), post.code.clone()),
storage: storage.into_iter().map(|k|
(k.clone(), Diff::new(
pre.storage.get(&k).cloned().unwrap_or(H256::new()),
post.storage.get(&k).cloned().unwrap_or(H256::new())
))).collect(),
};
if r.balance.is_same() && r.nonce.is_same() && r.code.is_same() && r.storage.len() == 0 {
None
} else {
Some(r)
}
},
_ => None,
}
}
pub fn pod_state_diff(pre: &PodState, post: &PodState) -> StateDiff {
StateDiff(pre.0.keys().merge(post.0.keys()).filter_map(|acc| pod_diff(pre.0.get(acc), post.0.get(acc)).map(|d|(acc.clone(), d))).collect())
}
#[test]
fn state_diff_create_delete() {
let a = PodState(map![
x!(1) => PodAccount{
balance: x!(69),
nonce: x!(0),
code: vec![],
storage: map![]
}
]);
assert_eq!(pod_state_diff(&a, &PodState(map![])), StateDiff(map![
x!(1) => AccountDiff{
balance: Diff::Died(x!(69)),
nonce: Diff::Died(x!(0)),
code: Diff::Died(vec![]),
storage: map![],
}
]));
assert_eq!(pod_state_diff(&PodState(map![]), &a), StateDiff(map![
x!(1) => AccountDiff{
balance: Diff::Born(x!(69)),
nonce: Diff::Born(x!(0)),
code: Diff::Born(vec![]),
storage: map![],
}
]));
}
#[test]
fn state_diff_cretae_delete_with_unchanged() {
let a = PodState(map![
x!(1) => PodAccount{
balance: x!(69),
nonce: x!(0),
code: vec![],
storage: map![]
}
]);
let b = PodState(map![
x!(1) => PodAccount{
balance: x!(69),
nonce: x!(0),
code: vec![],
storage: map![]
},
x!(2) => PodAccount{
balance: x!(69),
nonce: x!(0),
code: vec![],
storage: map![]
}
]);
assert_eq!(pod_state_diff(&a, &b), StateDiff(map![
x!(2) => AccountDiff{
balance: Diff::Born(x!(69)),
nonce: Diff::Born(x!(0)),
code: Diff::Born(vec![]),
storage: map![],
}
]));
assert_eq!(pod_state_diff(&b, &a), StateDiff(map![
x!(2) => AccountDiff{
balance: Diff::Died(x!(69)),
nonce: Diff::Died(x!(0)),
code: Diff::Died(vec![]),
storage: map![],
}
]));
}
#[test]
fn state_diff_change_with_unchanged() {
let a = PodState(map![
x!(1) => PodAccount{
balance: x!(69),
nonce: x!(0),
code: vec![],
storage: map![]
},
x!(2) => PodAccount{
balance: x!(69),
nonce: x!(0),
code: vec![],
storage: map![]
}
]);
let b = PodState(map![
x!(1) => PodAccount{
balance: x!(69),
nonce: x!(1),
code: vec![],
storage: map![]
},
x!(2) => PodAccount{
balance: x!(69),
nonce: x!(0),
code: vec![],
storage: map![]
}
]);
assert_eq!(pod_state_diff(&a, &b), StateDiff(map![
x!(1) => AccountDiff{
balance: Diff::Same,
nonce: Diff::Changed(x!(0), x!(1)),
code: Diff::Same,
storage: map![],
}
]));
}
#[test]
fn account_diff_existence() {
let a = PodAccount{balance: x!(69), nonce: x!(0), code: vec![], storage: map![]};
assert_eq!(pod_diff(Some(&a), Some(&a)), None);
assert_eq!(pod_diff(None, Some(&a)), Some(AccountDiff{
balance: Diff::Born(x!(69)),
nonce: Diff::Born(x!(0)),
code: Diff::Born(vec![]),
storage: map![],
}));
}
#[test]
fn account_diff_basic() {
let a = PodAccount{balance: x!(69), nonce: x!(0), code: vec![], storage: map![]};
let b = PodAccount{balance: x!(42), nonce: x!(1), code: vec![], storage: map![]};
assert_eq!(pod_diff(Some(&a), Some(&b)), Some(AccountDiff {
balance: Diff::Changed(x!(69), x!(42)),
nonce: Diff::Changed(x!(0), x!(1)),
code: Diff::Same,
storage: map![],
}));
}
#[test]
fn account_diff_code() {
let a = PodAccount{balance: x!(0), nonce: x!(0), code: vec![], storage: map![]};
let b = PodAccount{balance: x!(0), nonce: x!(1), code: vec![0], storage: map![]};
assert_eq!(pod_diff(Some(&a), Some(&b)), Some(AccountDiff {
balance: Diff::Same,
nonce: Diff::Changed(x!(0), x!(1)),
code: Diff::Changed(vec![], vec![0]),
storage: map![],
}));
}
#[test]
fn account_diff_storage() {
let a = PodAccount {
balance: x!(0),
nonce: x!(0),
code: vec![],
storage: mapx![1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 0, 6 => 0, 7 => 0]
};
let b = PodAccount {
balance: x!(0),
nonce: x!(0),
code: vec![],
storage: mapx![1 => 1, 2 => 3, 3 => 0, 5 => 0, 7 => 7, 8 => 0, 9 => 9]
};
assert_eq!(pod_diff(Some(&a), Some(&b)), Some(AccountDiff {
balance: Diff::Same,
nonce: Diff::Same,
code: Diff::Same,
storage: map![
x!(2) => Diff::new(x!(2), x!(3)),
x!(3) => Diff::new(x!(3), x!(0)),
x!(4) => Diff::new(x!(4), x!(0)),
x!(7) => Diff::new(x!(0), x!(7)),
x!(9) => Diff::new(x!(0), x!(9))
],
}));
}
#[derive(PartialEq,Eq,Clone,Copy)]
pub enum Filth {
Clean,
Dirty,
}
/// Single account in the system. /// Single account in the system.
#[derive(Clone)] #[derive(Clone)]
@ -409,29 +18,6 @@ pub struct Account {
code_cache: Bytes, code_cache: Bytes,
} }
impl PodAccount {
/// Convert Account to a PodAccount.
/// NOTE: This will silently fail unless the account is fully cached.
pub fn from_account(acc: &Account) -> PodAccount {
PodAccount {
balance: acc.balance.clone(),
nonce: acc.nonce.clone(),
storage: acc.storage_overlay.borrow().iter().fold(BTreeMap::new(), |mut m, (k, &(_, ref v))| {m.insert(k.clone(), v.clone()); m}),
code: acc.code_cache.clone()
}
}
pub fn rlp(&self) -> Bytes {
let mut stream = RlpStream::new_list(4);
stream.append(&self.nonce);
stream.append(&self.balance);
// TODO.
stream.append(&SHA3_NULL_RLP);
stream.append(&self.code.sha3());
stream.out()
}
}
impl Account { impl Account {
/// General constructor. /// General constructor.
pub fn new(balance: U256, nonce: U256, storage: HashMap<H256, H256>, code: Bytes) -> Account { pub fn new(balance: U256, nonce: U256, storage: HashMap<H256, H256>, code: Bytes) -> Account {
@ -647,13 +233,8 @@ impl fmt::Debug for Account {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use util::*;
use super::*; use super::*;
use std::collections::HashMap;
use util::hash::*;
use util::bytes::*;
use util::rlp::*;
use util::uint::*;
use util::overlaydb::*;
#[test] #[test]
fn storage_at() { fn storage_at() {

121
src/account_diff.rs Normal file
View File

@ -0,0 +1,121 @@
use util::*;
use pod_account::*;
#[derive(Debug,Clone,PartialEq,Eq)]
/// Change in existance type.
// TODO: include other types of change.
pub enum Existance {
Born,
Alive,
Died,
}
impl fmt::Display for Existance {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&Existance::Born => try!(write!(f, "+++")),
&Existance::Alive => try!(write!(f, "***")),
&Existance::Died => try!(write!(f, "XXX")),
}
Ok(())
}
}
#[derive(Debug,Clone,PartialEq,Eq)]
pub struct AccountDiff {
pub balance: Diff<U256>, // Allowed to be Same
pub nonce: Diff<U256>, // Allowed to be Same
pub code: Diff<Bytes>, // Allowed to be Same
pub storage: BTreeMap<H256, Diff<H256>>,// Not allowed to be Same
}
impl AccountDiff {
pub fn existance(&self) -> Existance {
match self.balance {
Diff::Born(_) => Existance::Born,
Diff::Died(_) => Existance::Died,
_ => Existance::Alive,
}
}
pub fn diff_pod(pre: Option<&PodAccount>, post: Option<&PodAccount>) -> Option<AccountDiff> {
match (pre, post) {
(None, Some(x)) => Some(AccountDiff {
balance: Diff::Born(x.balance.clone()),
nonce: Diff::Born(x.nonce.clone()),
code: Diff::Born(x.code.clone()),
storage: x.storage.iter().map(|(k, v)| (k.clone(), Diff::Born(v.clone()))).collect(),
}),
(Some(x), None) => Some(AccountDiff {
balance: Diff::Died(x.balance.clone()),
nonce: Diff::Died(x.nonce.clone()),
code: Diff::Died(x.code.clone()),
storage: x.storage.iter().map(|(k, v)| (k.clone(), Diff::Died(v.clone()))).collect(),
}),
(Some(pre), Some(post)) => {
let storage: Vec<_> = pre.storage.keys().merge(post.storage.keys())
.filter(|k| pre.storage.get(k).unwrap_or(&H256::new()) != post.storage.get(k).unwrap_or(&H256::new()))
.collect();
let r = AccountDiff {
balance: Diff::new(pre.balance.clone(), post.balance.clone()),
nonce: Diff::new(pre.nonce.clone(), post.nonce.clone()),
code: Diff::new(pre.code.clone(), post.code.clone()),
storage: storage.into_iter().map(|k|
(k.clone(), Diff::new(
pre.storage.get(&k).cloned().unwrap_or(H256::new()),
post.storage.get(&k).cloned().unwrap_or(H256::new())
))).collect(),
};
if r.balance.is_same() && r.nonce.is_same() && r.code.is_same() && r.storage.len() == 0 {
None
} else {
Some(r)
}
},
_ => None,
}
}
}
// TODO: refactor into something nicer.
fn interpreted_hash(u: &H256) -> String {
if u <= &H256::from(0xffffffff) {
format!("{} = 0x{:x}", U256::from(u.as_slice()).low_u32(), U256::from(u.as_slice()).low_u32())
} else if u <= &H256::from(u64::max_value()) {
format!("{} = 0x{:x}", U256::from(u.as_slice()).low_u64(), U256::from(u.as_slice()).low_u64())
// } else if u <= &H256::from("0xffffffffffffffffffffffffffffffffffffffff") {
// format!("@{}", Address::from(u))
} else {
format!("#{}", u)
}
}
impl fmt::Display for AccountDiff {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.nonce {
Diff::Born(ref x) => try!(write!(f, " non {}", x)),
Diff::Changed(ref pre, ref post) => try!(write!(f, "#{} ({} {} {})", post, pre, if pre > post {"-"} else {"+"}, *max(pre, post) - * min(pre, post))),
_ => {},
}
match self.balance {
Diff::Born(ref x) => try!(write!(f, " bal {}", x)),
Diff::Changed(ref pre, ref post) => try!(write!(f, "${} ({} {} {})", post, pre, if pre > post {"-"} else {"+"}, *max(pre, post) - *min(pre, post))),
_ => {},
}
match self.code {
Diff::Born(ref x) => try!(write!(f, " code {}", x.pretty())),
_ => {},
}
try!(write!(f, "\n"));
for (k, dv) in self.storage.iter() {
match dv {
&Diff::Born(ref v) => try!(write!(f, " + {} => {}\n", interpreted_hash(k), interpreted_hash(v))),
&Diff::Changed(ref pre, ref post) => try!(write!(f, " * {} => {} (was {})\n", interpreted_hash(k), interpreted_hash(post), interpreted_hash(pre))),
&Diff::Died(_) => try!(write!(f, " X {}\n", interpreted_hash(k))),
_ => {},
}
}
Ok(())
}
}

View File

@ -39,6 +39,12 @@ impl Builtin {
}) })
} }
/// Simple forwarder for cost.
pub fn cost(&self, s: usize) -> U256 { (*self.cost)(s) }
/// Simple forwarder for execute.
pub fn execute(&self, input: &[u8], output: &mut[u8]) { (*self.execute)(input, output); }
/// Create a builtin from JSON. /// Create a builtin from JSON.
/// ///
/// JSON must be of the form `{ "name": "identity", "linear": {"base": 10, "word": 20} }`. /// JSON must be of the form `{ "name": "identity", "linear": {"base": 10, "word": 20} }`.
@ -55,12 +61,6 @@ impl Builtin {
} }
None None
} }
/// Simple forwarder for cost.
pub fn cost(&self, s: usize) -> U256 { (*self.cost)(s) }
/// Simple forwarder for execute.
pub fn execute(&self, input: &[u8], output: &mut[u8]) { (*self.execute)(input, output); }
} }
pub fn copy_to(src: &[u8], dest: &mut[u8]) { pub fn copy_to(src: &[u8], dest: &mut[u8]) {

View File

@ -36,15 +36,17 @@ impl EnvInfo {
gas_used: x!(0), gas_used: x!(0),
} }
} }
}
pub fn from_json(json: &Json) -> EnvInfo { impl FromJson for EnvInfo {
let current_number = u64_from_json(&json["currentNumber"]); fn from_json(json: &Json) -> EnvInfo {
let current_number: u64 = xjson!(&json["currentNumber"]);
EnvInfo { EnvInfo {
number: current_number, number: current_number,
author: address_from_json(&json["currentCoinbase"]), author: xjson!(&json["currentCoinbase"]),
difficulty: u256_from_json(&json["currentDifficulty"]), difficulty: xjson!(&json["currentDifficulty"]),
gas_limit: u256_from_json(&json["currentGasLimit"]), gas_limit: xjson!(&json["currentGasLimit"]),
timestamp: u64_from_json(&json["currentTimestamp"]), timestamp: xjson!(&json["currentTimestamp"]),
last_hashes: (1..257).map(|i| format!("{}", current_number - i).as_bytes().sha3()).collect(), last_hashes: (1..257).map(|i| format!("{}", current_number - i).as_bytes().sha3()).collect(),
gas_used: x!(0), gas_used: x!(0),
} }

View File

@ -73,18 +73,19 @@
//! sudo ldconfig //! sudo ldconfig
//! ``` //! ```
#[macro_use] extern crate log; #[macro_use]
extern crate log;
extern crate rustc_serialize; extern crate rustc_serialize;
#[macro_use] extern crate itertools;
extern crate flate2; extern crate flate2;
extern crate rocksdb; extern crate rocksdb;
extern crate heapsize; extern crate heapsize;
extern crate crypto; extern crate crypto;
extern crate time; extern crate time;
extern crate env_logger; extern crate env_logger;
#[cfg(feature = "jit" )] extern crate evmjit; #[cfg(feature = "jit" )]
extern crate evmjit;
#[macro_use] extern crate ethcore_util as util; #[macro_use]
extern crate ethcore_util as util;
pub mod common; pub mod common;
pub mod basic_types; pub mod basic_types;
@ -92,6 +93,10 @@ pub mod executive;
pub mod error; pub mod error;
pub mod log_entry; pub mod log_entry;
pub mod env_info; pub mod env_info;
pub mod pod_account;
pub mod pod_state;
pub mod account_diff;
pub mod state_diff;
pub mod engine; pub mod engine;
pub mod state; pub mod state;
pub mod account; pub mod account;

View File

@ -28,16 +28,6 @@ impl LogEntry {
} }
} }
/// Convert given JSON object to a LogEntry.
pub fn from_json(json: &Json) -> LogEntry {
// TODO: check bloom.
LogEntry {
address: address_from_json(&json["address"]),
topics: vec_h256_from_json(&json["topics"]),
data: bytes_from_json(&json["data"]),
}
}
/// Returns reference to address. /// Returns reference to address.
pub fn address(&self) -> &Address { pub fn address(&self) -> &Address {
&self.address &self.address
@ -59,6 +49,18 @@ impl LogEntry {
} }
} }
impl FromJson for LogEntry {
/// Convert given JSON object to a LogEntry.
fn from_json(json: &Json) -> LogEntry {
// TODO: check bloom.
LogEntry {
address: xjson!(&json["address"]),
topics: xjson!(&json["topics"]),
data: xjson!(&json["data"]),
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use util::*; use util::*;

116
src/pod_account.rs Normal file
View File

@ -0,0 +1,116 @@
use util::*;
use account::*;
#[derive(Debug,Clone,PartialEq,Eq)]
/// Genesis account data. Does not have a DB overlay cache.
pub struct PodAccount {
pub balance: U256,
pub nonce: U256,
pub code: Bytes,
pub storage: BTreeMap<H256, H256>,
}
impl PodAccount {
/// Construct new object.
pub fn new(balance: U256, nonce: U256, code: Bytes, storage: BTreeMap<H256, H256>) -> PodAccount {
PodAccount { balance: balance, nonce: nonce, code: code, storage: storage }
}
/// Convert Account to a PodAccount.
/// NOTE: This will silently fail unless the account is fully cached.
pub fn from_account(acc: &Account) -> PodAccount {
PodAccount {
balance: acc.balance().clone(),
nonce: acc.nonce().clone(),
storage: acc.storage_overlay().iter().fold(BTreeMap::new(), |mut m, (k, &(_, ref v))| {m.insert(k.clone(), v.clone()); m}),
code: acc.code().unwrap().to_vec(),
}
}
pub fn rlp(&self) -> Bytes {
let mut stream = RlpStream::new_list(4);
stream.append(&self.nonce);
stream.append(&self.balance);
// TODO.
stream.append(&SHA3_NULL_RLP);
stream.append(&self.code.sha3());
stream.out()
}
}
impl fmt::Display for PodAccount {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "(bal={}; nonce={}; code={} bytes, #{}; storage={} items)", self.balance, self.nonce, self.code.len(), self.code.sha3(), self.storage.len())
}
}
#[cfg(test)]
mod test {
use common::*;
use account_diff::*;
use super::*;
#[test]
fn existence() {
let a = PodAccount{balance: x!(69), nonce: x!(0), code: vec![], storage: map![]};
assert_eq!(AccountDiff::diff_pod(Some(&a), Some(&a)), None);
assert_eq!(AccountDiff::diff_pod(None, Some(&a)), Some(AccountDiff{
balance: Diff::Born(x!(69)),
nonce: Diff::Born(x!(0)),
code: Diff::Born(vec![]),
storage: map![],
}));
}
#[test]
fn basic() {
let a = PodAccount{balance: x!(69), nonce: x!(0), code: vec![], storage: map![]};
let b = PodAccount{balance: x!(42), nonce: x!(1), code: vec![], storage: map![]};
assert_eq!(AccountDiff::diff_pod(Some(&a), Some(&b)), Some(AccountDiff {
balance: Diff::Changed(x!(69), x!(42)),
nonce: Diff::Changed(x!(0), x!(1)),
code: Diff::Same,
storage: map![],
}));
}
#[test]
fn code() {
let a = PodAccount{balance: x!(0), nonce: x!(0), code: vec![], storage: map![]};
let b = PodAccount{balance: x!(0), nonce: x!(1), code: vec![0], storage: map![]};
assert_eq!(AccountDiff::diff_pod(Some(&a), Some(&b)), Some(AccountDiff {
balance: Diff::Same,
nonce: Diff::Changed(x!(0), x!(1)),
code: Diff::Changed(vec![], vec![0]),
storage: map![],
}));
}
#[test]
fn storage() {
let a = PodAccount {
balance: x!(0),
nonce: x!(0),
code: vec![],
storage: mapx![1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 0, 6 => 0, 7 => 0]
};
let b = PodAccount {
balance: x!(0),
nonce: x!(0),
code: vec![],
storage: mapx![1 => 1, 2 => 3, 3 => 0, 5 => 0, 7 => 7, 8 => 0, 9 => 9]
};
assert_eq!(AccountDiff::diff_pod(Some(&a), Some(&b)), Some(AccountDiff {
balance: Diff::Same,
nonce: Diff::Same,
code: Diff::Same,
storage: map![
x!(2) => Diff::new(x!(2), x!(3)),
x!(3) => Diff::new(x!(3), x!(0)),
x!(4) => Diff::new(x!(4), x!(0)),
x!(7) => Diff::new(x!(0), x!(7)),
x!(9) => Diff::new(x!(0), x!(9))
],
}));
}
}

46
src/pod_state.rs Normal file
View File

@ -0,0 +1,46 @@
use util::*;
use pod_account::*;
#[derive(Debug,Clone,PartialEq,Eq)]
pub struct PodState (BTreeMap<Address, PodAccount>);
impl PodState {
/// Contruct a new object from the `m`.
pub fn new(m: BTreeMap<Address, PodAccount>) -> PodState { PodState(m) }
/// Get the underlying map.
pub fn get(&self) -> &BTreeMap<Address, PodAccount> { &self.0 }
/// Drain object to get the underlying map.
pub fn drain(self) -> BTreeMap<Address, PodAccount> { self.0 }
}
impl FromJson for PodState {
/// Translate the JSON object into a hash map of account information ready for insertion into State.
fn from_json(json: &Json) -> PodState {
PodState(json.as_object().unwrap().iter().fold(BTreeMap::new(), |mut state, (address, acc)| {
let balance = acc.find("balance").map(&U256::from_json);
let nonce = acc.find("nonce").map(&U256::from_json);
let storage = acc.find("storage").map(&BTreeMap::from_json);
let code = acc.find("code").map(&Bytes::from_json);
if balance.is_some() || nonce.is_some() || storage.is_some() || code.is_some() {
state.insert(address_from_hex(address), PodAccount{
balance: balance.unwrap_or(U256::zero()),
nonce: nonce.unwrap_or(U256::zero()),
storage: storage.unwrap_or(BTreeMap::new()),
code: code.unwrap_or(Vec::new())
});
}
state
}))
}
}
impl fmt::Display for PodState {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (add, acc) in &self.0 {
try!(writeln!(f, "{} => {}", add, acc));
}
Ok(())
}
}

View File

@ -148,9 +148,11 @@ impl Spec {
ret.append_raw(&empty_list, 1); ret.append_raw(&empty_list, 1);
ret.out() ret.out()
} }
}
impl FromJson for Spec {
/// Loads a chain-specification from a json data structure /// Loads a chain-specification from a json data structure
pub fn from_json(json: Json) -> Spec { fn from_json(json: &Json) -> Spec {
// once we commit ourselves to some json parsing library (serde?) // once we commit ourselves to some json parsing library (serde?)
// move it to proper data structure // move it to proper data structure
let mut state = HashMap::new(); let mut state = HashMap::new();
@ -210,7 +212,9 @@ impl Spec {
state_root_memo: RwLock::new(genesis.find("stateRoot").and_then(|_| genesis["stateRoot"].as_string()).map(|s| H256::from_str(&s[2..]).unwrap())), state_root_memo: RwLock::new(genesis.find("stateRoot").and_then(|_| genesis["stateRoot"].as_string()).map(|s| H256::from_str(&s[2..]).unwrap())),
} }
} }
}
impl Spec {
/// Ensure that the given state DB has the trie nodes in for the genesis state. /// Ensure that the given state DB has the trie nodes in for the genesis state.
pub fn ensure_db_good(&self, db: &mut HashDB) { pub fn ensure_db_good(&self, db: &mut HashDB) {
if !db.contains(&self.state_root()) { if !db.contains(&self.state_root()) {
@ -229,8 +233,7 @@ impl Spec {
/// Create a new Spec from a JSON string. /// Create a new Spec from a JSON string.
pub fn from_json_str(s: &str) -> Spec { pub fn from_json_str(s: &str) -> Spec {
let json = Json::from_str(s).expect("Json is invalid"); Self::from_json(&Json::from_str(s).expect("Json is invalid"))
Self::from_json(json)
} }
/// Create a new Spec which conforms to the Morden chain except that it's a NullEngine consensus. /// Create a new Spec which conforms to the Morden chain except that it's a NullEngine consensus.

View File

@ -1,6 +1,8 @@
use common::*; use common::*;
use engine::Engine; use engine::Engine;
use executive::Executive; use executive::Executive;
use pod_account::*;
use pod_state::*;
pub type ApplyResult = Result<Receipt, Error>; pub type ApplyResult = Result<Receipt, Error>;
@ -201,7 +203,7 @@ impl State {
/// Populate a PodAccount map from this state. /// Populate a PodAccount map from this state.
pub fn to_pod(&self) -> PodState { pub fn to_pod(&self) -> PodState {
// TODO: handle database rather than just the cache. // TODO: handle database rather than just the cache.
PodState::from(self.cache.borrow().iter().fold(BTreeMap::new(), |mut m, (add, opt)| { PodState::new(self.cache.borrow().iter().fold(BTreeMap::new(), |mut m, (add, opt)| {
if let &Some(ref acc) = opt { if let &Some(ref acc) = opt {
m.insert(add.clone(), PodAccount::from_account(acc)); m.insert(add.clone(), PodAccount::from_account(acc));
} }

98
src/state_diff.rs Normal file
View File

@ -0,0 +1,98 @@
use util::*;
use pod_state::*;
use account_diff::*;
#[derive(Debug,Clone,PartialEq,Eq)]
pub struct StateDiff (BTreeMap<Address, AccountDiff>);
impl StateDiff {
/// Calculate and return diff between `pre` state and `post` state.
pub fn diff_pod(pre: &PodState, post: &PodState) -> StateDiff {
StateDiff(pre.get().keys().merge(post.get().keys()).filter_map(|acc| AccountDiff::diff_pod(pre.get().get(acc), post.get().get(acc)).map(|d|(acc.clone(), d))).collect())
}
}
impl fmt::Display for StateDiff {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (add, acc) in self.0.iter() {
try!(write!(f, "{} {}: {}", acc.existance(), add, acc));
}
Ok(())
}
}
#[cfg(test)]
mod test {
use common::*;
use pod_state::*;
use account_diff::*;
use pod_account::*;
use super::*;
#[test]
fn create_delete() {
let a = PodState::new(map![ x!(1) => PodAccount::new(x!(69), x!(0), vec![], map![]) ]);
assert_eq!(StateDiff::diff_pod(&a, &PodState::new(map![])), StateDiff(map![
x!(1) => AccountDiff{
balance: Diff::Died(x!(69)),
nonce: Diff::Died(x!(0)),
code: Diff::Died(vec![]),
storage: map![],
}
]));
assert_eq!(StateDiff::diff_pod(&PodState::new(map![]), &a), StateDiff(map![
x!(1) => AccountDiff{
balance: Diff::Born(x!(69)),
nonce: Diff::Born(x!(0)),
code: Diff::Born(vec![]),
storage: map![],
}
]));
}
#[test]
fn create_delete_with_unchanged() {
let a = PodState::new(map![ x!(1) => PodAccount::new(x!(69), x!(0), vec![], map![]) ]);
let b = PodState::new(map![
x!(1) => PodAccount::new(x!(69), x!(0), vec![], map![]),
x!(2) => PodAccount::new(x!(69), x!(0), vec![], map![])
]);
assert_eq!(StateDiff::diff_pod(&a, &b), StateDiff(map![
x!(2) => AccountDiff{
balance: Diff::Born(x!(69)),
nonce: Diff::Born(x!(0)),
code: Diff::Born(vec![]),
storage: map![],
}
]));
assert_eq!(StateDiff::diff_pod(&b, &a), StateDiff(map![
x!(2) => AccountDiff{
balance: Diff::Died(x!(69)),
nonce: Diff::Died(x!(0)),
code: Diff::Died(vec![]),
storage: map![],
}
]));
}
#[test]
fn change_with_unchanged() {
let a = PodState::new(map![
x!(1) => PodAccount::new(x!(69), x!(0), vec![], map![]),
x!(2) => PodAccount::new(x!(69), x!(0), vec![], map![])
]);
let b = PodState::new(map![
x!(1) => PodAccount::new(x!(69), x!(1), vec![], map![]),
x!(2) => PodAccount::new(x!(69), x!(0), vec![], map![])
]);
assert_eq!(StateDiff::diff_pod(&a, &b), StateDiff(map![
x!(1) => AccountDiff{
balance: Diff::Same,
nonce: Diff::Changed(x!(0), x!(1)),
code: Diff::Same,
storage: map![],
}
]));
}
}

View File

@ -9,14 +9,14 @@ use ethereum;
struct TestEngine { struct TestEngine {
spec: Spec, spec: Spec,
max_depth: usize stack_limit: usize
} }
impl TestEngine { impl TestEngine {
fn new(max_depth: usize) -> TestEngine { fn new(stack_limit: usize) -> TestEngine {
TestEngine { TestEngine {
spec: ethereum::new_frontier_test(), spec: ethereum::new_frontier_test(),
max_depth: max_depth stack_limit: stack_limit
} }
} }
} }
@ -26,7 +26,7 @@ impl Engine for TestEngine {
fn spec(&self) -> &Spec { &self.spec } fn spec(&self) -> &Spec { &self.spec }
fn schedule(&self, _env_info: &EnvInfo) -> Schedule { fn schedule(&self, _env_info: &EnvInfo) -> Schedule {
let mut schedule = Schedule::new_frontier(); let mut schedule = Schedule::new_frontier();
schedule.max_depth = self.max_depth; schedule.stack_limit = self.stack_limit;
schedule schedule
} }
} }
@ -87,7 +87,7 @@ impl<'a> Ext for TestExt<'a> {
}); });
Ok((gas_left, Some(address))) Ok((gas_left, Some(address)))
}, },
// creation failed only due to reaching max_depth // creation failed only due to reaching stack_limit
Ok((gas_left, None)) if ext.state.balance(&ext.params.address) >= *value => { Ok((gas_left, None)) if ext.state.balance(&ext.params.address) >= *value => {
let address = contract_address(&ext.params.address, &ext.state.nonce(&ext.params.address)); let address = contract_address(&ext.params.address, &ext.state.nonce(&ext.params.address));
self.callcreates.push(CallCreate { self.callcreates.push(CallCreate {
@ -169,29 +169,24 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
test.find("pre").map(|pre| for (addr, s) in pre.as_object().unwrap() { test.find("pre").map(|pre| for (addr, s) in pre.as_object().unwrap() {
let address = Address::from(addr.as_ref()); let address = Address::from(addr.as_ref());
let balance = u256_from_json(&s["balance"]); let balance = xjson!(&s["balance"]);
let code = bytes_from_json(&s["code"]); let code = xjson!(&s["code"]);
let _nonce = u256_from_json(&s["nonce"]); let _nonce: U256 = xjson!(&s["nonce"]);
state.new_contract(&address); state.new_contract(&address);
state.add_balance(&address, &balance); state.add_balance(&address, &balance);
state.init_code(&address, code); state.init_code(&address, code);
BTreeMap::from_json(&s["storage"]).into_iter().foreach(|(k, v)| state.set_storage(&address, k, v));
for (k, v) in s["storage"].as_object().unwrap() {
let key = H256::from(&u256_from_str(k));
let val = H256::from(&u256_from_json(v));
state.set_storage(&address, key, val);
}
}); });
let mut info = EnvInfo::new(); let mut info = EnvInfo::new();
test.find("env").map(|env| { test.find("env").map(|env| {
info.author = address_from_json(&env["currentCoinbase"]); info.author = xjson!(&env["currentCoinbase"]);
info.difficulty = u256_from_json(&env["currentDifficulty"]); info.difficulty = xjson!(&env["currentDifficulty"]);
info.gas_limit = u256_from_json(&env["currentGasLimit"]); info.gas_limit = xjson!(&env["currentGasLimit"]);
info.number = u256_from_json(&env["currentNumber"]).low_u64(); info.number = xjson!(&env["currentNumber"]);
info.timestamp = u256_from_json(&env["currentTimestamp"]).low_u64(); info.timestamp = xjson!(&env["currentTimestamp"]);
}); });
let engine = TestEngine::new(0); let engine = TestEngine::new(0);
@ -199,14 +194,14 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
// params // params
let mut params = ActionParams::new(); let mut params = ActionParams::new();
test.find("exec").map(|exec| { test.find("exec").map(|exec| {
params.address = address_from_json(&exec["address"]); params.address = xjson!(&exec["address"]);
params.sender = address_from_json(&exec["caller"]); params.sender = xjson!(&exec["caller"]);
params.origin = address_from_json(&exec["origin"]); params.origin = xjson!(&exec["origin"]);
params.code = bytes_from_json(&exec["code"]); params.code = xjson!(&exec["code"]);
params.data = bytes_from_json(&exec["data"]); params.data = xjson!(&exec["data"]);
params.gas = u256_from_json(&exec["gas"]); params.gas = xjson!(&exec["gas"]);
params.gas_price = u256_from_json(&exec["gasPrice"]); params.gas_price = xjson!(&exec["gasPrice"]);
params.value = u256_from_json(&exec["value"]); params.value = xjson!(&exec["value"]);
}); });
let out_of_gas = test.find("callcreates").map(|_calls| { let out_of_gas = test.find("callcreates").map(|_calls| {
@ -228,26 +223,19 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
match res { match res {
Err(_) => fail_unless(out_of_gas, "didn't expect to run out of gas."), Err(_) => fail_unless(out_of_gas, "didn't expect to run out of gas."),
Ok(gas_left) => { Ok(gas_left) => {
//println!("name: {}, gas_left : {:?}, expected: {:?}", name, gas_left, u256_from_json(&test["gas"])); //println!("name: {}, gas_left : {:?}, expected: {:?}", name, gas_left, U256::from(&test["gas"]));
fail_unless(!out_of_gas, "expected to run out of gas."); fail_unless(!out_of_gas, "expected to run out of gas.");
fail_unless(gas_left == u256_from_json(&test["gas"]), "gas_left is incorrect"); fail_unless(gas_left == xjson!(&test["gas"]), "gas_left is incorrect");
fail_unless(output == bytes_from_json(&test["out"]), "output is incorrect"); fail_unless(output == Bytes::from_json(&test["out"]), "output is incorrect");
test.find("post").map(|pre| for (addr, s) in pre.as_object().unwrap() { test.find("post").map(|pre| for (addr, s) in pre.as_object().unwrap() {
let address = Address::from(addr.as_ref()); let address = Address::from(addr.as_ref());
//let balance = u256_from_json(&s["balance"]);
fail_unless(state.code(&address).unwrap_or(vec![]) == bytes_from_json(&s["code"]), "code is incorrect"); fail_unless(state.code(&address).unwrap_or(vec![]) == Bytes::from_json(&s["code"]), "code is incorrect");
fail_unless(state.balance(&address) == u256_from_json(&s["balance"]), "balance is incorrect"); fail_unless(state.balance(&address) == xjson!(&s["balance"]), "balance is incorrect");
fail_unless(state.nonce(&address) == u256_from_json(&s["nonce"]), "nonce is incorrect"); fail_unless(state.nonce(&address) == xjson!(&s["nonce"]), "nonce is incorrect");
BTreeMap::from_json(&s["storage"]).iter().foreach(|(k, v)| fail_unless(&state.storage_at(&address, &k) == v, "storage is incorrect"));
for (k, v) in s["storage"].as_object().unwrap() {
let key = H256::from(&u256_from_str(k));
let val = H256::from(&u256_from_json(v));
fail_unless(state.storage_at(&address, &key) == val, "storage is incorrect");
}
}); });
let cc = test["callcreates"].as_array().unwrap(); let cc = test["callcreates"].as_array().unwrap();
@ -255,14 +243,14 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
for i in 0..cc.len() { for i in 0..cc.len() {
let is = &callcreates[i]; let is = &callcreates[i];
let expected = &cc[i]; let expected = &cc[i];
fail_unless(is.data == bytes_from_json(&expected["data"]), "callcreates data is incorrect"); fail_unless(is.data == Bytes::from_json(&expected["data"]), "callcreates data is incorrect");
fail_unless(is.destination == address_from_json(&expected["destination"]), "callcreates destination is incorrect"); fail_unless(is.destination == xjson!(&expected["destination"]), "callcreates destination is incorrect");
fail_unless(is.value == u256_from_json(&expected["value"]), "callcreates value is incorrect"); fail_unless(is.value == xjson!(&expected["value"]), "callcreates value is incorrect");
// TODO: call_gas is calculated in externalities and is not exposed to TestExt. // TODO: call_gas is calculated in externalities and is not exposed to TestExt.
// maybe move it to it's own function to simplify calculation? // maybe move it to it's own function to simplify calculation?
//println!("name: {:?}, is {:?}, expected: {:?}", name, is.gas_limit, u256_from_json(&expected["gasLimit"])); //println!("name: {:?}, is {:?}, expected: {:?}", name, is.gas_limit, U256::from(&expected["gasLimit"]));
//fail_unless(is.gas_limit == u256_from_json(&expected["gasLimit"]), "callcreates gas_limit is incorrect"); //fail_unless(is.gas_limit == U256::from(&expected["gasLimit"]), "callcreates gas_limit is incorrect");
} }
} }
} }

View File

@ -1,6 +1,7 @@
use super::test_common::*; use super::test_common::*;
use state::*; use state::*;
use executive::*; use pod_state::*;
use state_diff::*;
use ethereum; use ethereum;
fn do_json_test(json_data: &[u8]) -> Vec<String> { fn do_json_test(json_data: &[u8]) -> Vec<String> {
@ -15,8 +16,8 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
let t = Transaction::from_json(&test["transaction"]); let t = Transaction::from_json(&test["transaction"]);
let env = EnvInfo::from_json(&test["env"]); let env = EnvInfo::from_json(&test["env"]);
let _out = bytes_from_json(&test["out"]); let _out = Bytes::from_json(&test["out"]);
let post_state_root = h256_from_json(&test["postStateRoot"]); let post_state_root = xjson!(&test["postStateRoot"]);
let pre = PodState::from_json(&test["pre"]); let pre = PodState::from_json(&test["pre"]);
let post = PodState::from_json(&test["post"]); let post = PodState::from_json(&test["post"]);
let logs: Vec<_> = test["logs"].as_array().unwrap().iter().map(&LogEntry::from_json).collect(); let logs: Vec<_> = test["logs"].as_array().unwrap().iter().map(&LogEntry::from_json).collect();
@ -40,7 +41,7 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
let our_post = s.to_pod(); let our_post = s.to_pod();
println!("Got:\n{}", our_post); println!("Got:\n{}", our_post);
println!("Expect:\n{}", post); println!("Expect:\n{}", post);
println!("Diff ---expect -> +++got:\n{}", pod_state_diff(&post, &our_post)); println!("Diff ---expect -> +++got:\n{}", StateDiff::diff_pod(&post, &our_post));
} }
if fail_unless(logs == r.logs) { if fail_unless(logs == r.logs) {

View File

@ -14,23 +14,23 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
.and_then(|j| j.as_string()) .and_then(|j| j.as_string())
.and_then(|s| BlockNumber::from_str(s).ok()) .and_then(|s| BlockNumber::from_str(s).ok())
.unwrap_or(0) { x if x < 900000 => &old_schedule, _ => &new_schedule }; .unwrap_or(0) { x if x < 900000 => &old_schedule, _ => &new_schedule };
let rlp = bytes_from_json(&test["rlp"]); let rlp = Bytes::from_json(&test["rlp"]);
let res = UntrustedRlp::new(&rlp).as_val().map_err(|e| From::from(e)).and_then(|t: Transaction| t.validate(schedule, schedule.have_delegate_call)); let res = UntrustedRlp::new(&rlp).as_val().map_err(|e| From::from(e)).and_then(|t: Transaction| t.validate(schedule, schedule.have_delegate_call));
fail_unless(test.find("transaction").is_none() == res.is_err()); fail_unless(test.find("transaction").is_none() == res.is_err());
if let (Some(&Json::Object(ref tx)), Some(&Json::String(ref expect_sender))) = (test.find("transaction"), test.find("sender")) { if let (Some(&Json::Object(ref tx)), Some(&Json::String(ref expect_sender))) = (test.find("transaction"), test.find("sender")) {
let t = res.unwrap(); let t = res.unwrap();
fail_unless(t.sender().unwrap() == address_from_hex(clean(expect_sender))); fail_unless(t.sender().unwrap() == address_from_hex(clean(expect_sender)));
fail_unless(t.data == bytes_from_json(&tx["data"])); fail_unless(t.data == Bytes::from_json(&tx["data"]));
fail_unless(t.gas == u256_from_json(&tx["gasLimit"])); fail_unless(t.gas == xjson!(&tx["gasLimit"]));
fail_unless(t.gas_price == u256_from_json(&tx["gasPrice"])); fail_unless(t.gas_price == xjson!(&tx["gasPrice"]));
fail_unless(t.nonce == u256_from_json(&tx["nonce"])); fail_unless(t.nonce == xjson!(&tx["nonce"]));
fail_unless(t.value == u256_from_json(&tx["value"])); fail_unless(t.value == xjson!(&tx["value"]));
if let Action::Call(ref to) = t.action { if let Action::Call(ref to) = t.action {
*ot.borrow_mut() = t.clone(); *ot.borrow_mut() = t.clone();
fail_unless(to == &address_from_json(&tx["to"])); fail_unless(to == &xjson!(&tx["to"]));
} else { } else {
*ot.borrow_mut() = t.clone(); *ot.borrow_mut() = t.clone();
fail_unless(bytes_from_json(&tx["to"]).len() == 0); fail_unless(Bytes::from_json(&tx["to"]).len() == 0);
} }
} }
} }

View File

@ -32,15 +32,15 @@ pub struct Transaction {
impl Transaction { impl Transaction {
pub fn new() -> Self { pub fn new() -> Self {
Transaction { Transaction {
nonce: U256::zero(), nonce: x!(0),
gas_price: U256::zero(), gas_price: x!(0),
gas: U256::zero(), gas: x!(0),
action: Action::Create, action: Action::Create,
value: U256::zero(), value: x!(0),
data: vec![], data: vec![],
v: 0, v: 0,
r: U256::zero(), r: x!(0),
s: U256::zero(), s: x!(0),
hash: RefCell::new(None), hash: RefCell::new(None),
sender: RefCell::new(None), sender: RefCell::new(None),
} }
@ -55,8 +55,8 @@ impl Transaction {
value: value, value: value,
data: data, data: data,
v: 0, v: 0,
r: U256::zero(), r: x!(0),
s: U256::zero(), s: x!(0),
hash: RefCell::new(None), hash: RefCell::new(None),
sender: RefCell::new(None), sender: RefCell::new(None),
} }
@ -72,39 +72,13 @@ impl Transaction {
value: value, value: value,
data: data, data: data,
v: 0, v: 0,
r: U256::zero(), r: x!(0),
s: U256::zero(), s: x!(0),
hash: RefCell::new(None), hash: RefCell::new(None),
sender: RefCell::new(None), sender: RefCell::new(None),
} }
} }
pub fn from_json(json: &Json) -> Transaction {
let mut r = Transaction {
nonce: u256_from_json(&json["nonce"]),
gas_price: u256_from_json(&json["gasPrice"]),
gas: u256_from_json(&json["gasLimit"]),
action: match bytes_from_json(&json["to"]) {
ref x if x.len() == 0 => Action::Create,
ref x => Action::Call(Address::from_slice(x)),
},
value: u256_from_json(&json["value"]),
data: bytes_from_json(&json["data"]),
v: match json.find("v") { Some(ref j) => u8_from_json(j), None => 0 },
r: match json.find("r") { Some(ref j) => u256_from_json(j), None => U256::zero() },
s: match json.find("s") { Some(ref j) => u256_from_json(j), None => U256::zero() },
hash: RefCell::new(None),
sender: match json.find("sender") {
Some(&Json::String(ref sender)) => RefCell::new(Some(address_from_hex(clean(sender)))),
_ => RefCell::new(None),
},
};
if let Some(&Json::String(ref secret_key)) = json.find("secretKey") {
r.sign(&h256_from_hex(clean(secret_key)));
}
r
}
/// Get the nonce of the transaction. /// Get the nonce of the transaction.
pub fn nonce(&self) -> &U256 { &self.nonce } pub fn nonce(&self) -> &U256 { &self.nonce }
/// Get the gas price of the transaction. /// Get the gas price of the transaction.
@ -144,6 +118,34 @@ impl Transaction {
} }
} }
impl FromJson for Transaction {
fn from_json(json: &Json) -> Transaction {
let mut r = Transaction {
nonce: xjson!(&json["nonce"]),
gas_price: xjson!(&json["gasPrice"]),
gas: xjson!(&json["gasLimit"]),
action: match Bytes::from_json(&json["to"]) {
ref x if x.len() == 0 => Action::Create,
ref x => Action::Call(Address::from_slice(x)),
},
value: xjson!(&json["value"]),
data: xjson!(&json["data"]),
v: match json.find("v") { Some(ref j) => u16::from_json(j) as u8, None => 0 },
r: match json.find("r") { Some(j) => xjson!(j), None => x!(0) },
s: match json.find("s") { Some(j) => xjson!(j), None => x!(0) },
hash: RefCell::new(None),
sender: match json.find("sender") {
Some(&Json::String(ref sender)) => RefCell::new(Some(address_from_hex(clean(sender)))),
_ => RefCell::new(None),
},
};
if let Some(&Json::String(ref secret_key)) = json.find("secretKey") {
r.sign(&h256_from_hex(clean(secret_key)));
}
r
}
}
impl RlpStandard for Transaction { impl RlpStandard for Transaction {
fn rlp_append(&self, s: &mut RlpStream) { self.rlp_append_opt(s, Seal::With) } fn rlp_append(&self, s: &mut RlpStream) { self.rlp_append_opt(s, Seal::With) }
} }