From d3091b95c39834414fcf6fb70b0fd122cde1ab7e Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 14 Jan 2016 21:23:46 +0100 Subject: [PATCH] Use new json trait for conversion of some types. --- src/env_info.rs | 4 ++-- src/pod_account.rs | 2 +- src/pod_state.rs | 4 ++-- src/tests/executive.rs | 48 ++++++++++++++++++++-------------------- src/tests/transaction.rs | 8 +++---- src/transaction.rs | 32 +++++++++++++-------------- 6 files changed, 49 insertions(+), 49 deletions(-) diff --git a/src/env_info.rs b/src/env_info.rs index 88367fca9..c806f228d 100644 --- a/src/env_info.rs +++ b/src/env_info.rs @@ -42,8 +42,8 @@ impl EnvInfo { EnvInfo { number: current_number, author: address_from_json(&json["currentCoinbase"]), - difficulty: u256_from_json(&json["currentDifficulty"]), - gas_limit: u256_from_json(&json["currentGasLimit"]), + difficulty: xjson!(&json["currentDifficulty"]), + gas_limit: xjson!(&json["currentGasLimit"]), timestamp: u64_from_json(&json["currentTimestamp"]), last_hashes: (1..257).map(|i| format!("{}", current_number - i).as_bytes().sha3()).collect(), gas_used: x!(0), diff --git a/src/pod_account.rs b/src/pod_account.rs index ecbccb038..a39bf1fa3 100644 --- a/src/pod_account.rs +++ b/src/pod_account.rs @@ -47,8 +47,8 @@ impl fmt::Display for PodAccount { #[cfg(test)] mod test { use common::*; - use super::*; use account_diff::*; + use super::*; #[test] fn existence() { diff --git a/src/pod_state.rs b/src/pod_state.rs index f3f7a2f39..ff456bda3 100644 --- a/src/pod_state.rs +++ b/src/pod_state.rs @@ -11,8 +11,8 @@ impl PodState { /// 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 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() { diff --git a/src/tests/executive.rs b/src/tests/executive.rs index 25166dc04..1e95082ae 100644 --- a/src/tests/executive.rs +++ b/src/tests/executive.rs @@ -9,14 +9,14 @@ use ethereum; struct TestEngine { spec: Spec, - max_depth: usize + stack_limit: usize } impl TestEngine { - fn new(max_depth: usize) -> TestEngine { + fn new(stack_limit: usize) -> TestEngine { TestEngine { 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 schedule(&self, _env_info: &EnvInfo) -> Schedule { let mut schedule = Schedule::new_frontier(); - schedule.max_depth = self.max_depth; + schedule.stack_limit = self.stack_limit; schedule } } @@ -87,7 +87,7 @@ impl<'a> Ext for TestExt<'a> { }); 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 => { let address = contract_address(&ext.params.address, &ext.state.nonce(&ext.params.address)); self.callcreates.push(CallCreate { @@ -169,9 +169,9 @@ fn do_json_test(json_data: &[u8]) -> Vec { test.find("pre").map(|pre| for (addr, s) in pre.as_object().unwrap() { 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 _nonce = u256_from_json(&s["nonce"]); + let _nonce: U256 = xjson!(&s["nonce"]); state.new_contract(&address); state.add_balance(&address, &balance); @@ -179,7 +179,7 @@ fn do_json_test(json_data: &[u8]) -> Vec { 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)); + let val = H256::from(&U256::from_json(v)); state.set_storage(&address, key, val); } }); @@ -188,10 +188,10 @@ fn do_json_test(json_data: &[u8]) -> Vec { test.find("env").map(|env| { info.author = address_from_json(&env["currentCoinbase"]); - info.difficulty = u256_from_json(&env["currentDifficulty"]); - info.gas_limit = u256_from_json(&env["currentGasLimit"]); - info.number = u256_from_json(&env["currentNumber"]).low_u64(); - info.timestamp = u256_from_json(&env["currentTimestamp"]).low_u64(); + info.difficulty = xjson!(&env["currentDifficulty"]); + info.gas_limit = xjson!(&env["currentGasLimit"]); + info.number = xjson!(&env["currentNumber"]); + info.timestamp = xjson!(&env["currentTimestamp"]); }); let engine = TestEngine::new(0); @@ -204,9 +204,9 @@ fn do_json_test(json_data: &[u8]) -> Vec { params.origin = address_from_json(&exec["origin"]); params.code = bytes_from_json(&exec["code"]); params.data = bytes_from_json(&exec["data"]); - params.gas = u256_from_json(&exec["gas"]); - params.gas_price = u256_from_json(&exec["gasPrice"]); - params.value = u256_from_json(&exec["value"]); + params.gas = xjson!(&exec["gas"]); + params.gas_price = xjson!(&exec["gasPrice"]); + params.value = xjson!(&exec["value"]); }); let out_of_gas = test.find("callcreates").map(|_calls| { @@ -228,23 +228,23 @@ fn do_json_test(json_data: &[u8]) -> Vec { match res { Err(_) => fail_unless(out_of_gas, "didn't expect to run out of gas."), 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(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"); test.find("post").map(|pre| for (addr, s) in pre.as_object().unwrap() { 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.balance(&address) == u256_from_json(&s["balance"]), "balance is incorrect"); - fail_unless(state.nonce(&address) == u256_from_json(&s["nonce"]), "nonce is incorrect"); + fail_unless(state.balance(&address) == xjson!(&s["balance"]), "balance is incorrect"); + fail_unless(state.nonce(&address) == xjson!(&s["nonce"]), "nonce 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)); + let val = H256::from(&U256::from_json(v)); fail_unless(state.storage_at(&address, &key) == val, "storage is incorrect"); } @@ -257,12 +257,12 @@ fn do_json_test(json_data: &[u8]) -> Vec { let expected = &cc[i]; 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.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. // maybe move it to it's own function to simplify calculation? - //println!("name: {:?}, is {:?}, expected: {:?}", name, is.gas_limit, u256_from_json(&expected["gasLimit"])); - //fail_unless(is.gas_limit == u256_from_json(&expected["gasLimit"]), "callcreates gas_limit is incorrect"); + //println!("name: {:?}, is {:?}, expected: {:?}", name, is.gas_limit, U256::from(&expected["gasLimit"])); + //fail_unless(is.gas_limit == U256::from(&expected["gasLimit"]), "callcreates gas_limit is incorrect"); } } } diff --git a/src/tests/transaction.rs b/src/tests/transaction.rs index b3c62b883..0660b2345 100644 --- a/src/tests/transaction.rs +++ b/src/tests/transaction.rs @@ -21,10 +21,10 @@ fn do_json_test(json_data: &[u8]) -> Vec { let t = res.unwrap(); fail_unless(t.sender().unwrap() == address_from_hex(clean(expect_sender))); fail_unless(t.data == bytes_from_json(&tx["data"])); - fail_unless(t.gas == u256_from_json(&tx["gasLimit"])); - fail_unless(t.gas_price == u256_from_json(&tx["gasPrice"])); - fail_unless(t.nonce == u256_from_json(&tx["nonce"])); - fail_unless(t.value == u256_from_json(&tx["value"])); + fail_unless(t.gas == xjson!(&tx["gasLimit"])); + fail_unless(t.gas_price == xjson!(&tx["gasPrice"])); + fail_unless(t.nonce == xjson!(&tx["nonce"])); + fail_unless(t.value == xjson!(&tx["value"])); if let Action::Call(ref to) = t.action { *ot.borrow_mut() = t.clone(); fail_unless(to == &address_from_json(&tx["to"])); diff --git a/src/transaction.rs b/src/transaction.rs index 7bc882d6d..f0441d774 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -32,15 +32,15 @@ pub struct Transaction { impl Transaction { pub fn new() -> Self { Transaction { - nonce: U256::zero(), - gas_price: U256::zero(), - gas: U256::zero(), + nonce: x!(0), + gas_price: x!(0), + gas: x!(0), action: Action::Create, - value: U256::zero(), + value: x!(0), data: vec![], v: 0, - r: U256::zero(), - s: U256::zero(), + r: x!(0), + s: x!(0), hash: RefCell::new(None), sender: RefCell::new(None), } @@ -55,8 +55,8 @@ impl Transaction { value: value, data: data, v: 0, - r: U256::zero(), - s: U256::zero(), + r: x!(0), + s: x!(0), hash: RefCell::new(None), sender: RefCell::new(None), } @@ -72,8 +72,8 @@ impl Transaction { value: value, data: data, v: 0, - r: U256::zero(), - s: U256::zero(), + r: x!(0), + s: x!(0), hash: RefCell::new(None), sender: RefCell::new(None), } @@ -81,18 +81,18 @@ impl Transaction { 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"]), + 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: u256_from_json(&json["value"]), + value: xjson!(&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() }, + 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)))),