Use new json trait for conversion of some types.

This commit is contained in:
Gav Wood 2016-01-14 21:23:46 +01:00
parent c6a1d1a286
commit d3091b95c3
6 changed files with 49 additions and 49 deletions

View File

@ -42,8 +42,8 @@ impl EnvInfo {
EnvInfo { EnvInfo {
number: current_number, number: current_number,
author: address_from_json(&json["currentCoinbase"]), author: address_from_json(&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: u64_from_json(&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

@ -47,8 +47,8 @@ impl fmt::Display for PodAccount {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use common::*; use common::*;
use super::*;
use account_diff::*; use account_diff::*;
use super::*;
#[test] #[test]
fn existence() { fn existence() {

View File

@ -11,8 +11,8 @@ impl PodState {
/// Translate the JSON object into a hash map of account information ready for insertion into State. /// Translate the JSON object into a hash map of account information ready for insertion into State.
pub fn from_json(json: &Json) -> PodState { pub fn from_json(json: &Json) -> PodState {
PodState(json.as_object().unwrap().iter().fold(BTreeMap::new(), |mut state, (address, acc)| { PodState(json.as_object().unwrap().iter().fold(BTreeMap::new(), |mut state, (address, acc)| {
let balance = acc.find("balance").map(&u256_from_json); let balance = acc.find("balance").map(&U256::from_json);
let nonce = acc.find("nonce").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 storage = acc.find("storage").map(&map_h256_h256_from_json);;
let code = acc.find("code").map(&bytes_from_json); let code = acc.find("code").map(&bytes_from_json);
if balance.is_some() || nonce.is_some() || storage.is_some() || code.is_some() { if balance.is_some() || nonce.is_some() || storage.is_some() || code.is_some() {

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,9 +169,9 @@ 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 = bytes_from_json(&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);
@ -179,7 +179,7 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
for (k, v) in s["storage"].as_object().unwrap() { for (k, v) in s["storage"].as_object().unwrap() {
let key = H256::from(&u256_from_str(k)); let key = H256::from(&u256_from_str(k));
let val = H256::from(&u256_from_json(v)); let val = H256::from(&U256::from_json(v));
state.set_storage(&address, key, val); state.set_storage(&address, key, val);
} }
}); });
@ -188,10 +188,10 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
test.find("env").map(|env| { test.find("env").map(|env| {
info.author = address_from_json(&env["currentCoinbase"]); info.author = address_from_json(&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);
@ -204,9 +204,9 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
params.origin = address_from_json(&exec["origin"]); params.origin = address_from_json(&exec["origin"]);
params.code = bytes_from_json(&exec["code"]); params.code = bytes_from_json(&exec["code"]);
params.data = bytes_from_json(&exec["data"]); params.data = bytes_from_json(&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,23 +228,23 @@ 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"]); //let balance = U256::from(&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");
for (k, v) in s["storage"].as_object().unwrap() { for (k, v) in s["storage"].as_object().unwrap() {
let key = H256::from(&u256_from_str(k)); let key = H256::from(&u256_from_str(k));
let val = H256::from(&u256_from_json(v)); let val = H256::from(&U256::from_json(v));
fail_unless(state.storage_at(&address, &key) == val, "storage is incorrect"); fail_unless(state.storage_at(&address, &key) == val, "storage is incorrect");
} }
@ -257,12 +257,12 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
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 == address_from_json(&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

@ -21,10 +21,10 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
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 == &address_from_json(&tx["to"]));

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,8 +72,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),
} }
@ -81,18 +81,18 @@ impl Transaction {
pub fn from_json(json: &Json) -> Transaction { pub fn from_json(json: &Json) -> Transaction {
let mut r = Transaction { let mut r = Transaction {
nonce: u256_from_json(&json["nonce"]), nonce: xjson!(&json["nonce"]),
gas_price: u256_from_json(&json["gasPrice"]), gas_price: xjson!(&json["gasPrice"]),
gas: u256_from_json(&json["gasLimit"]), gas: xjson!(&json["gasLimit"]),
action: match bytes_from_json(&json["to"]) { action: match bytes_from_json(&json["to"]) {
ref x if x.len() == 0 => Action::Create, ref x if x.len() == 0 => Action::Create,
ref x => Action::Call(Address::from_slice(x)), ref x => Action::Call(Address::from_slice(x)),
}, },
value: u256_from_json(&json["value"]), value: xjson!(&json["value"]),
data: bytes_from_json(&json["data"]), data: bytes_from_json(&json["data"]),
v: match json.find("v") { Some(ref j) => u8_from_json(j), None => 0 }, 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() }, r: match json.find("r") { Some(j) => xjson!(j), None => x!(0) },
s: match json.find("s") { Some(ref j) => u256_from_json(j), None => U256::zero() }, s: match json.find("s") { Some(j) => xjson!(j), None => x!(0) },
hash: RefCell::new(None), hash: RefCell::new(None),
sender: match json.find("sender") { sender: match json.find("sender") {
Some(&Json::String(ref sender)) => RefCell::new(Some(address_from_hex(clean(sender)))), Some(&Json::String(ref sender)) => RefCell::new(Some(address_from_hex(clean(sender)))),