Merge branch 'master' into bettermining

This commit is contained in:
Gav Wood
2016-03-26 13:45:38 +01:00
53 changed files with 1471 additions and 816 deletions

View File

@@ -16,6 +16,7 @@
//! Evm input params.
use common::*;
use ethjson;
/// Transaction value
#[derive(Clone, Debug)]
@@ -67,3 +68,19 @@ impl Default for ActionParams {
}
}
}
impl From<ethjson::vm::Transaction> for ActionParams {
fn from(t: ethjson::vm::Transaction) -> Self {
ActionParams {
code_address: Address::new(),
address: t.address.into(),
sender: t.sender.into(),
origin: t.origin.into(),
code: Some(t.code.into()),
data: Some(t.data.into()),
gas: t.gas.into(),
gas_price: t.gas_price.into(),
value: ActionValue::Transfer(t.value.into()),
}
}
}

View File

@@ -16,6 +16,7 @@
use util::*;
use header::BlockNumber;
use ethjson;
/// Simple vector of hashes, should be at most 256 items large, can be smaller if being used
/// for a block whose number is less than 257.
@@ -69,6 +70,21 @@ impl FromJson for EnvInfo {
}
}
impl From<ethjson::vm::Env> for EnvInfo {
fn from(e: ethjson::vm::Env) -> Self {
let number = e.number.into();
EnvInfo {
number: number,
author: e.author.into(),
difficulty: e.difficulty.into(),
gas_limit: e.gas_limit.into(),
timestamp: e.timestamp.into(),
last_hashes: (1..cmp::min(number + 1, 257)).map(|i| format!("{}", number - i).as_bytes().sha3()).collect(),
gas_used: U256::zero(),
}
}
}
#[cfg(test)]
mod tests {
extern crate rustc_serialize;

View File

@@ -137,6 +137,7 @@ impl InstructionInfo {
}
}
#[cfg_attr(rustfmt, rustfmt_skip)]
/// Return details about specific instruction
pub fn get_info (instruction: Instruction) -> InstructionInfo {
match instruction {
@@ -301,7 +302,7 @@ pub const EXP: Instruction = 0x0a;
pub const SIGNEXTEND: Instruction = 0x0b;
/// less-than comparision
pub const LT: Instruction = 0x10;
pub const LT: Instruction = 0x10;
/// greater-than comparision
pub const GT: Instruction = 0x11;
/// signed less-than comparision
@@ -324,10 +325,10 @@ pub const NOT: Instruction = 0x19;
pub const BYTE: Instruction = 0x1a;
/// compute SHA3-256 hash
pub const SHA3: Instruction = 0x20;
pub const SHA3: Instruction = 0x20;
/// get address of currently executing account
pub const ADDRESS: Instruction = 0x30;
pub const ADDRESS: Instruction = 0x30;
/// get balance of the given account
pub const BALANCE: Instruction = 0x31;
/// get execution origination address
@@ -367,7 +368,7 @@ pub const DIFFICULTY: Instruction = 0x44;
pub const GASLIMIT: Instruction = 0x45;
/// remove item from stack
pub const POP: Instruction = 0x50;
pub const POP: Instruction = 0x50;
/// load word from memory
pub const MLOAD: Instruction = 0x51;
/// save word to memory
@@ -392,7 +393,7 @@ pub const GAS: Instruction = 0x5a;
pub const JUMPDEST: Instruction = 0x5b;
/// place 1 byte item on stack
pub const PUSH1: Instruction = 0x60;
pub const PUSH1: Instruction = 0x60;
/// place 2 byte item on stack
pub const PUSH2: Instruction = 0x61;
/// place 3 byte item on stack
@@ -457,7 +458,7 @@ pub const PUSH31: Instruction = 0x7e;
pub const PUSH32: Instruction = 0x7f;
/// copies the highest item in the stack to the top of the stack
pub const DUP1: Instruction = 0x80;
pub const DUP1: Instruction = 0x80;
/// copies the second highest item in the stack to the top of the stack
pub const DUP2: Instruction = 0x81;
/// copies the third highest item in the stack to the top of the stack
@@ -490,7 +491,7 @@ pub const DUP15: Instruction = 0x8e;
pub const DUP16: Instruction = 0x8f;
/// swaps the highest and second highest value on the stack
pub const SWAP1: Instruction = 0x90;
pub const SWAP1: Instruction = 0x90;
/// swaps the highest and third highest value on the stack
pub const SWAP2: Instruction = 0x91;
/// swaps the highest and 4th highest value on the stack
@@ -523,7 +524,7 @@ pub const SWAP15: Instruction = 0x9e;
pub const SWAP16: Instruction = 0x9f;
/// Makes a log entry; no topics.
pub const LOG0: Instruction = 0xa0;
pub const LOG0: Instruction = 0xa0;
/// Makes a log entry; 1 topic.
pub const LOG1: Instruction = 0xa1;
/// Makes a log entry; 2 topics.
@@ -536,7 +537,7 @@ pub const LOG4: Instruction = 0xa4;
pub const MAX_NO_OF_TOPICS : usize = 4;
/// create a new account with associated code
pub const CREATE: Instruction = 0xf0;
pub const CREATE: Instruction = 0xf0;
/// message-call into an account
pub const CALL: Instruction = 0xf1;
/// message-call with another account's code only
@@ -546,5 +547,5 @@ pub const RETURN: Instruction = 0xf3;
/// like CALLCODE but keeps caller's value and sender
pub const DELEGATECALL: Instruction = 0xf4;
/// halt execution and register account for later deletion
pub const SUICIDE: Instruction = 0xff;
pub const SUICIDE: Instruction = 0xff;

View File

@@ -28,7 +28,7 @@ pub fn json_chain_test(json_data: &[u8], era: ChainEra) -> Vec<String> {
let tests = ethjson::blockchain::Test::load(json_data).unwrap();
let mut failed = Vec::new();
for (name, blockchain) in tests.deref() {
for (name, blockchain) in tests.into_iter() {
let mut fail = false;
{
let mut fail_unless = |cond: bool| if !cond && !fail {
@@ -61,7 +61,7 @@ pub fn json_chain_test(json_data: &[u8], era: ChainEra) -> Vec<String> {
client.import_verified_blocks(&IoChannel::disconnected());
}
}
fail_unless(client.chain_info().best_block_hash == blockchain.best_block.clone().into());
fail_unless(client.chain_info().best_block_hash == blockchain.best_block.into());
}
}

View File

@@ -25,6 +25,7 @@ use ethereum;
use externalities::*;
use substate::*;
use tests::helpers::*;
use ethjson;
struct TestEngineFrontier {
vm_factory: Factory,
@@ -53,6 +54,7 @@ impl Engine for TestEngineFrontier {
}
}
#[derive(Debug, PartialEq)]
struct CallCreate {
data: Bytes,
destination: Option<Address>,
@@ -60,6 +62,18 @@ struct CallCreate {
value: U256
}
impl From<ethjson::vm::Call> for CallCreate {
fn from(c: ethjson::vm::Call) -> Self {
let dst: Option<_> = c.destination.into();
CallCreate {
data: c.data.into(),
destination: dst.map(Into::into),
gas_limit: c.gas_limit.into(),
value: c.value.into()
}
}
}
/// Tiny wrapper around executive externalities.
/// Stores callcreates.
struct TestExt<'a> {
@@ -174,58 +188,26 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
.collect()
}
fn do_json_test_for(vm: &VMType, json_data: &[u8]) -> Vec<String> {
let json = Json::from_str(::std::str::from_utf8(json_data).unwrap()).expect("Json is invalid");
fn do_json_test_for(vm_type: &VMType, json_data: &[u8]) -> Vec<String> {
let tests = ethjson::vm::Test::load(json_data).unwrap();
let mut failed = Vec::new();
for (name, test) in json.as_object().unwrap() {
for (name, vm) in tests.into_iter() {
println!("name: {:?}", name);
// sync io is usefull when something crashes in jit
// ::std::io::stdout().write(&name.as_bytes());
// ::std::io::stdout().write(b"\n");
// ::std::io::stdout().flush();
let mut fail = false;
//let mut fail_unless = |cond: bool| if !cond && !fail { failed.push(name.to_string()); fail = true };
let mut fail_unless = |cond: bool, s: &str | if !cond && !fail {
failed.push(format!("[{}] {}: {}", vm, name, s));
failed.push(format!("[{}] {}: {}", vm_type, name, s));
fail = true
};
// test env
let out_of_gas = vm.out_of_gas();
let mut state_result = get_temp_state();
let mut state = state_result.reference_mut();
test.find("pre").map(|pre| for (addr, s) in pre.as_object().unwrap() {
let address = Address::from(addr.as_ref());
let balance = xjson!(&s["balance"]);
let code = xjson!(&s["code"]);
let _nonce: U256 = xjson!(&s["nonce"]);
state.new_contract(&address, balance);
state.init_code(&address, code);
BTreeMap::from_json(&s["storage"]).into_iter().foreach(|(k, v)| state.set_storage(&address, k, v));
});
let info = test.find("env").map(|env| {
EnvInfo::from_json(env)
}).unwrap_or_default();
let engine = TestEngineFrontier::new(1, vm.clone());
// params
let mut params = ActionParams::default();
test.find("exec").map(|exec| {
params.address = xjson!(&exec["address"]);
params.sender = xjson!(&exec["caller"]);
params.origin = xjson!(&exec["origin"]);
params.code = xjson!(&exec["code"]);
params.data = xjson!(&exec["data"]);
params.gas = xjson!(&exec["gas"]);
params.gas_price = xjson!(&exec["gasPrice"]);
params.value = ActionValue::Transfer(xjson!(&exec["value"]));
});
let out_of_gas = test.find("callcreates").map(|_calls| {
}).is_none();
state.populate_from(From::from(vm.pre_state.clone()));
let info = From::from(vm.env);
let engine = TestEngineFrontier::new(1, vm_type.clone());
let params = ActionParams::from(vm.transaction);
let mut substate = Substate::new(false);
let mut output = vec![];
@@ -247,44 +229,37 @@ fn do_json_test_for(vm: &VMType, json_data: &[u8]) -> Vec<String> {
(res, ex.callcreates)
};
// then validate
match res {
Err(_) => fail_unless(out_of_gas, "didn't expect to run out of gas."),
Ok(gas_left) => {
// println!("name: {}, gas_left : {:?}", name, gas_left);
fail_unless(!out_of_gas, "expected to run out of gas.");
fail_unless(gas_left == xjson!(&test["gas"]), "gas_left is incorrect");
fail_unless(output == Bytes::from_json(&test["out"]), "output is incorrect");
fail_unless(Some(gas_left) == vm.gas_left.map(Into::into), "gas_left is incorrect");
let vm_output: Option<Vec<u8>> = vm.output.map(Into::into);
fail_unless(Some(output) == vm_output, "output is incorrect");
test.find("post").map(|pre| for (addr, s) in pre.as_object().unwrap() {
let address = Address::from(addr.as_ref());
fail_unless(state.code(&address).unwrap_or_else(|| vec![]) == Bytes::from_json(&s["code"]), "code is incorrect");
fail_unless(state.balance(&address) == xjson!(&s["balance"]), "balance 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"));
});
let cc = test["callcreates"].as_array().unwrap();
fail_unless(callcreates.len() == cc.len(), "callcreates does not match");
for i in 0..cc.len() {
let callcreate = &callcreates[i];
let expected = &cc[i];
fail_unless(callcreate.data == Bytes::from_json(&expected["data"]), "callcreates data is incorrect");
fail_unless(callcreate.destination == xjson!(&expected["destination"]), "callcreates destination is incorrect");
fail_unless(callcreate.value == xjson!(&expected["value"]), "callcreates value is incorrect");
fail_unless(callcreate.gas_limit == xjson!(&expected["gasLimit"]), "callcreates gas_limit is incorrect");
for (address, account) in vm.post_state.unwrap().into_iter() {
let address = address.into();
let code: Vec<u8> = account.code.into();
fail_unless(state.code(&address).unwrap_or_else(Vec::new) == code, "code is incorrect");
fail_unless(state.balance(&address) == account.balance.into(), "balance is incorrect");
fail_unless(state.nonce(&address) == account.nonce.into(), "nonce is incorrect");
account.storage.into_iter().foreach(|(k, v)| {
let key: U256 = k.into();
let value: U256 = v.into();
fail_unless(state.storage_at(&address, &From::from(key)) == From::from(value), "storage is incorrect");
});
}
}
}
}
let calls: Option<Vec<CallCreate>> = vm.calls.map(|c| c.into_iter().map(From::from).collect());
fail_unless(Some(callcreates) == calls, "callcreates does not match");
}
};
}
for f in &failed {
println!("FAILED: {:?}", f);
}
//assert!(false);
failed
}

View File

@@ -19,7 +19,7 @@ use account::*;
use account_db::*;
use ethjson;
#[derive(Debug,Clone,PartialEq,Eq)]
#[derive(Debug, Clone, PartialEq, Eq)]
/// An account, expressed as Plain-Old-Data (hence the name).
/// Does not have a DB overlay cache, code hash or anything like that.
pub struct PodAccount {

View File

@@ -20,7 +20,7 @@ use pod_state::*;
use account_diff::*;
#[derive(Debug,Clone,PartialEq,Eq)]
/// Expression for the delta between two system states. Encoded the
/// Expression for the delta between two system states. Encoded the
/// delta of every altered account.
pub struct StateDiff (BTreeMap<Address, AccountDiff>);
@@ -41,6 +41,14 @@ impl fmt::Display for StateDiff {
}
}
impl Deref for StateDiff {
type Target = BTreeMap<Address, AccountDiff>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[cfg(test)]
mod test {
use common::*;