2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2016-07-11 09:42:41 +02:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2016-07-11 09:42:41 +02:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2016-07-11 09:42:41 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-07-11 09:42:41 +02:00
|
|
|
|
2019-05-21 19:42:18 +02:00
|
|
|
//! Parity EVM Interpreter Binary.
|
|
|
|
//!
|
|
|
|
//! ## Overview
|
|
|
|
//!
|
|
|
|
//! The Parity EVM interpreter binary is a tool in the Parity
|
|
|
|
//! Ethereum toolchain. It is an EVM implementation for Parity Ethereum that
|
|
|
|
//! is used to run a standalone version of the EVM interpreter.
|
|
|
|
//!
|
|
|
|
//! ## Usage
|
|
|
|
//!
|
|
|
|
//! The evmbin tool is not distributed with regular Parity Ethereum releases
|
|
|
|
//! so you need to build it from source and run it like so:
|
|
|
|
//!
|
|
|
|
//! ```bash
|
|
|
|
//! cargo build -p evmbin --release
|
|
|
|
//! ./target/release/parity-evm --help
|
|
|
|
//! ```
|
2016-07-11 09:42:41 +02:00
|
|
|
|
|
|
|
#![warn(missing_docs)]
|
2017-08-28 14:25:16 +02:00
|
|
|
|
2019-08-07 16:51:08 +02:00
|
|
|
extern crate account_state;
|
2019-01-04 14:05:46 +01:00
|
|
|
extern crate common_types as types;
|
2019-08-07 16:51:08 +02:00
|
|
|
extern crate docopt;
|
|
|
|
extern crate env_logger;
|
2016-07-11 09:42:41 +02:00
|
|
|
extern crate ethcore;
|
2019-08-07 16:51:08 +02:00
|
|
|
extern crate ethereum_types;
|
2017-08-28 14:25:16 +02:00
|
|
|
extern crate ethjson;
|
2019-08-07 16:51:08 +02:00
|
|
|
extern crate evm;
|
|
|
|
extern crate panic_hook;
|
|
|
|
extern crate parity_bytes as bytes;
|
|
|
|
extern crate pod;
|
2017-07-06 11:36:15 +02:00
|
|
|
extern crate rustc_hex;
|
|
|
|
extern crate serde;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
2018-09-11 11:36:38 +02:00
|
|
|
extern crate serde_json;
|
2019-07-08 18:17:48 +02:00
|
|
|
extern crate trace;
|
2019-08-07 16:51:08 +02:00
|
|
|
extern crate vm;
|
2016-07-11 09:42:41 +02:00
|
|
|
|
2017-10-20 15:40:25 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
#[macro_use]
|
|
|
|
extern crate pretty_assertions;
|
|
|
|
|
2018-03-14 12:26:20 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
extern crate tempdir;
|
|
|
|
|
2016-10-17 11:55:47 +02:00
|
|
|
use std::sync::Arc;
|
2017-05-26 11:06:48 +02:00
|
|
|
use std::{fmt, fs};
|
2017-08-28 14:25:16 +02:00
|
|
|
use std::path::PathBuf;
|
2016-07-11 09:42:41 +02:00
|
|
|
use docopt::Docopt;
|
2017-07-06 11:36:15 +02:00
|
|
|
use rustc_hex::FromHex;
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::{U256, Address};
|
2017-09-06 20:47:45 +02:00
|
|
|
use bytes::Bytes;
|
2018-11-25 20:12:59 +01:00
|
|
|
use ethcore::{spec, json_tests, TrieSpec};
|
2017-08-10 18:47:23 +02:00
|
|
|
use vm::{ActionParams, CallType};
|
2016-07-11 09:42:41 +02:00
|
|
|
|
2017-08-01 16:41:33 +02:00
|
|
|
mod info;
|
2017-05-26 11:06:48 +02:00
|
|
|
mod display;
|
|
|
|
|
2019-08-07 16:51:08 +02:00
|
|
|
use info::{Informant, TxInput};
|
2017-05-26 11:06:48 +02:00
|
|
|
|
2016-07-11 09:42:41 +02:00
|
|
|
const USAGE: &'static str = r#"
|
|
|
|
EVM implementation for Parity.
|
2019-01-23 10:26:36 +01:00
|
|
|
Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
2016-07-11 09:42:41 +02:00
|
|
|
|
|
|
|
Usage:
|
2019-08-07 16:51:08 +02:00
|
|
|
parity-evm state-test <file> [--chain CHAIN --only NAME --json --std-json --std-dump-json --std-out-only --std-err-only]
|
2017-08-18 17:44:40 +02:00
|
|
|
parity-evm stats [options]
|
2018-06-25 11:21:45 +02:00
|
|
|
parity-evm stats-jsontests-vm <file>
|
2017-08-18 17:44:40 +02:00
|
|
|
parity-evm [options]
|
|
|
|
parity-evm [-h | --help]
|
2016-07-11 09:42:41 +02:00
|
|
|
|
2018-06-25 11:21:45 +02:00
|
|
|
Commands:
|
2019-08-07 16:51:08 +02:00
|
|
|
state-test Run a state test on a provided state test JSON file.
|
2018-06-25 11:21:45 +02:00
|
|
|
stats Execute EVM runtime code and return the statistics.
|
2019-08-07 16:51:08 +02:00
|
|
|
stats-jsontests-vm Execute standard json-tests on a provided state test JSON
|
|
|
|
file path, format VMTests, and return timing statistics
|
|
|
|
in tsv format.
|
2018-06-25 11:21:45 +02:00
|
|
|
|
2016-07-11 09:42:41 +02:00
|
|
|
Transaction options:
|
2017-05-26 11:06:48 +02:00
|
|
|
--code CODE Contract code as hex (without 0x).
|
2017-08-10 18:47:23 +02:00
|
|
|
--to ADDRESS Recipient address (without 0x).
|
2017-05-26 11:06:48 +02:00
|
|
|
--from ADDRESS Sender address (without 0x).
|
|
|
|
--input DATA Input data as hex (without 0x).
|
|
|
|
--gas GAS Supplied gas as hex (without 0x).
|
2017-08-10 18:47:23 +02:00
|
|
|
--gas-price WEI Supplied gas price as hex (without 0x).
|
2016-07-11 09:42:41 +02:00
|
|
|
|
2017-08-28 14:25:16 +02:00
|
|
|
State test options:
|
2019-08-07 16:51:08 +02:00
|
|
|
--chain CHAIN Run only from specific chain name (i.e. one of EIP150, EIP158,
|
|
|
|
Frontier, Homestead, Byzantium, Constantinople,
|
|
|
|
ConstantinopleFix, EIP158ToByzantiumAt5, FrontierToHomesteadAt5,
|
|
|
|
HomesteadToDaoAt5, HomesteadToEIP150At5).
|
2017-08-28 14:25:16 +02:00
|
|
|
--only NAME Runs only a single test matching the name.
|
|
|
|
|
2016-07-11 09:42:41 +02:00
|
|
|
General options:
|
2019-08-07 16:51:08 +02:00
|
|
|
--chain PATH Path to chain spec file.
|
2017-05-26 11:06:48 +02:00
|
|
|
--json Display verbose results in JSON.
|
2018-08-30 19:57:27 +02:00
|
|
|
--std-json Display results in standardized JSON format.
|
2018-11-25 20:12:59 +01:00
|
|
|
--std-dump-json Display results in standardized JSON format
|
|
|
|
with additional state dump.
|
2019-08-07 16:51:08 +02:00
|
|
|
--std-err-only With --std-json redirect to err output only.
|
|
|
|
--std-out-only With --std-json redirect to out output only.
|
2016-07-11 09:42:41 +02:00
|
|
|
-h, --help Display this message and exit.
|
|
|
|
"#;
|
|
|
|
|
|
|
|
fn main() {
|
2018-07-02 11:53:50 +02:00
|
|
|
panic_hook::set_abort();
|
2018-08-20 14:05:01 +02:00
|
|
|
env_logger::init();
|
2017-07-03 07:31:29 +02:00
|
|
|
|
2017-07-06 11:36:15 +02:00
|
|
|
let args: Args = Docopt::new(USAGE).and_then(|d| d.deserialize()).unwrap_or_else(|e| e.exit());
|
2016-07-11 09:42:41 +02:00
|
|
|
|
2017-08-28 14:25:16 +02:00
|
|
|
if args.cmd_state_test {
|
|
|
|
run_state_test(args)
|
2018-06-25 11:21:45 +02:00
|
|
|
} else if args.cmd_stats_jsontests_vm {
|
|
|
|
run_stats_jsontests_vm(args)
|
2017-08-28 14:25:16 +02:00
|
|
|
} else if args.flag_json {
|
|
|
|
run_call(args, display::json::Informant::default())
|
2018-11-25 20:12:59 +01:00
|
|
|
} else if args.flag_std_dump_json || args.flag_std_json {
|
|
|
|
if args.flag_std_err_only {
|
|
|
|
run_call(args, display::std_json::Informant::err_only())
|
|
|
|
} else if args.flag_std_out_only {
|
|
|
|
run_call(args, display::std_json::Informant::out_only())
|
|
|
|
} else {
|
|
|
|
run_call(args, display::std_json::Informant::default())
|
|
|
|
};
|
2017-05-26 11:06:48 +02:00
|
|
|
} else {
|
2017-08-28 14:25:16 +02:00
|
|
|
run_call(args, display::simple::Informant::default())
|
2016-10-18 12:13:49 +02:00
|
|
|
}
|
2016-07-12 09:49:16 +02:00
|
|
|
}
|
|
|
|
|
2017-08-28 14:25:16 +02:00
|
|
|
fn run_state_test(args: Args) {
|
|
|
|
use ethjson::state::test::Test;
|
|
|
|
|
2019-08-07 16:51:08 +02:00
|
|
|
// Parse the specified state test JSON file provided to the command `state-test <file>`.
|
|
|
|
let file = args.arg_file.expect("PATH to a state test JSON file is required");
|
2017-08-28 14:25:16 +02:00
|
|
|
let mut file = match fs::File::open(&file) {
|
2019-08-07 16:51:08 +02:00
|
|
|
Err(err) => die(format!("Unable to open path: {:?}: {}", file, err)),
|
2017-08-28 14:25:16 +02:00
|
|
|
Ok(file) => file,
|
|
|
|
};
|
|
|
|
let state_test = match Test::load(&mut file) {
|
|
|
|
Err(err) => die(format!("Unable to load the test file: {}", err)),
|
|
|
|
Ok(test) => test,
|
|
|
|
};
|
2019-08-07 16:51:08 +02:00
|
|
|
// Parse the name CLI option `--only NAME`.
|
2017-08-28 14:25:16 +02:00
|
|
|
let only_test = args.flag_only.map(|s| s.to_lowercase());
|
2019-08-07 16:51:08 +02:00
|
|
|
// Parse the chain `--chain CHAIN`
|
2017-08-28 14:25:16 +02:00
|
|
|
let only_chain = args.flag_chain.map(|s| s.to_lowercase());
|
|
|
|
|
2019-08-07 16:51:08 +02:00
|
|
|
// Iterate over 1st level (outer) key-value pair of the state test JSON file.
|
|
|
|
// Skip to next iteration if CLI option `--only NAME` was parsed into `only_test` and does not match
|
|
|
|
// the current key `state_test_name` (i.e. add11, create2callPrecompiles).
|
|
|
|
for (state_test_name, test) in state_test {
|
|
|
|
if let Some(false) = only_test.as_ref().map(|only_test| {
|
|
|
|
&state_test_name.to_lowercase() == only_test
|
|
|
|
}) {
|
2017-08-28 14:25:16 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-08-07 16:51:08 +02:00
|
|
|
// Assign from 2nd level key-value pairs of the state test JSON file (i.e. env, post, pre, transaction).
|
2017-08-28 14:25:16 +02:00
|
|
|
let multitransaction = test.transaction;
|
|
|
|
let env_info = test.env.into();
|
|
|
|
let pre = test.pre_state.into();
|
|
|
|
|
2019-08-07 16:51:08 +02:00
|
|
|
// Iterate over remaining "post" key of the 2nd level key-value pairs in the state test JSON file.
|
|
|
|
// Skip to next iteration if CLI option `--chain CHAIN` was parsed into `only_chain` and does not match
|
|
|
|
// the current key `fork_spec_name` (i.e. Constantinople, EIP150, EIP158).
|
|
|
|
for (fork_spec_name, states) in test.post_states {
|
|
|
|
if let Some(false) = only_chain.as_ref().map(|only_chain| {
|
|
|
|
&format!("{:?}", fork_spec_name).to_lowercase() == only_chain
|
|
|
|
}) {
|
2017-08-28 14:25:16 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-08-07 16:51:08 +02:00
|
|
|
// Iterate over the 3rd level key-value pairs of the state test JSON file
|
|
|
|
// (i.e. list of transactions and associated state roots hashes corresponding each chain).
|
|
|
|
for (tx_index, state) in states.into_iter().enumerate() {
|
2017-08-28 14:25:16 +02:00
|
|
|
let post_root = state.hash.into();
|
|
|
|
let transaction = multitransaction.select(&state.indexes).into();
|
|
|
|
|
2019-08-07 16:51:08 +02:00
|
|
|
// Determine the type of trie with state root to create in the database.
|
|
|
|
// The database is a key-value datastore implemented as a database-backend
|
|
|
|
// modified Merkle tree.
|
|
|
|
// Use a secure trie database specification when CLI option `--std-dump-json`
|
|
|
|
// is specified, otherwise use secure trie with fat trie database.
|
2018-11-25 20:12:59 +01:00
|
|
|
let trie_spec = if args.flag_std_dump_json {
|
|
|
|
TrieSpec::Fat
|
|
|
|
} else {
|
|
|
|
TrieSpec::Secure
|
|
|
|
};
|
2019-08-07 16:51:08 +02:00
|
|
|
|
|
|
|
// Execute the given transaction and verify resulting state root
|
|
|
|
// for CLI option `--std-dump-json` or `--std-json`.
|
|
|
|
if args.flag_std_dump_json || args.flag_std_json {
|
2018-11-25 20:12:59 +01:00
|
|
|
if args.flag_std_err_only {
|
2019-08-07 16:51:08 +02:00
|
|
|
let tx_input = TxInput {
|
|
|
|
state_test_name: &state_test_name,
|
|
|
|
tx_index,
|
|
|
|
fork_spec_name: &fork_spec_name,
|
|
|
|
pre_state: &pre,
|
|
|
|
post_root,
|
|
|
|
env_info: &env_info,
|
|
|
|
transaction,
|
|
|
|
informant: display::std_json::Informant::err_only(),
|
|
|
|
trie_spec,
|
|
|
|
};
|
|
|
|
// Use Standard JSON informant with err only
|
|
|
|
info::run_transaction(tx_input);
|
2018-11-25 20:12:59 +01:00
|
|
|
} else if args.flag_std_out_only {
|
2019-08-07 16:51:08 +02:00
|
|
|
let tx_input = TxInput {
|
|
|
|
state_test_name: &state_test_name,
|
|
|
|
tx_index,
|
|
|
|
fork_spec_name: &fork_spec_name,
|
|
|
|
pre_state: &pre,
|
|
|
|
post_root,
|
|
|
|
env_info: &env_info,
|
|
|
|
transaction,
|
|
|
|
informant: display::std_json::Informant::out_only(),
|
|
|
|
trie_spec,
|
|
|
|
};
|
|
|
|
// Use Standard JSON informant with out only
|
|
|
|
info::run_transaction(tx_input);
|
2018-11-25 20:12:59 +01:00
|
|
|
} else {
|
2019-08-07 16:51:08 +02:00
|
|
|
let tx_input = TxInput {
|
|
|
|
state_test_name: &state_test_name,
|
|
|
|
tx_index,
|
|
|
|
fork_spec_name: &fork_spec_name,
|
|
|
|
pre_state: &pre,
|
|
|
|
post_root,
|
|
|
|
env_info: &env_info,
|
|
|
|
transaction,
|
|
|
|
informant: display::std_json::Informant::default(),
|
|
|
|
trie_spec,
|
|
|
|
};
|
|
|
|
// Use Standard JSON informant default
|
|
|
|
info::run_transaction(tx_input);
|
2018-11-25 20:12:59 +01:00
|
|
|
}
|
2017-08-28 14:25:16 +02:00
|
|
|
} else {
|
2019-08-07 16:51:08 +02:00
|
|
|
// Execute the given transaction and verify resulting state root
|
|
|
|
// for CLI option `--json`.
|
|
|
|
if args.flag_json {
|
|
|
|
let tx_input = TxInput {
|
|
|
|
state_test_name: &state_test_name,
|
|
|
|
tx_index,
|
|
|
|
fork_spec_name: &fork_spec_name,
|
|
|
|
pre_state: &pre,
|
|
|
|
post_root,
|
|
|
|
env_info: &env_info,
|
|
|
|
transaction,
|
|
|
|
informant: display::json::Informant::default(),
|
|
|
|
trie_spec,
|
|
|
|
};
|
|
|
|
// Use JSON informant
|
|
|
|
info::run_transaction(tx_input);
|
|
|
|
} else {
|
|
|
|
let tx_input = TxInput {
|
|
|
|
state_test_name: &state_test_name,
|
|
|
|
tx_index,
|
|
|
|
fork_spec_name: &fork_spec_name,
|
|
|
|
pre_state: &pre,
|
|
|
|
post_root,
|
|
|
|
env_info: &env_info,
|
|
|
|
transaction,
|
|
|
|
informant: display::simple::Informant::default(),
|
|
|
|
trie_spec,
|
|
|
|
};
|
|
|
|
// Use Simple informant
|
|
|
|
info::run_transaction(tx_input);
|
|
|
|
}
|
2017-08-28 14:25:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 16:51:08 +02:00
|
|
|
fn run_stats_jsontests_vm(args: Args) {
|
|
|
|
use json_tests::HookType;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::time::{Instant, Duration};
|
|
|
|
|
|
|
|
let file = args.arg_file.expect("PATH to a state test JSON file is required");
|
|
|
|
|
|
|
|
let mut timings: HashMap<String, (Instant, Option<Duration>)> = HashMap::new();
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut record_time = |name: &str, typ: HookType| {
|
|
|
|
match typ {
|
|
|
|
HookType::OnStart => {
|
|
|
|
timings.insert(name.to_string(), (Instant::now(), None));
|
|
|
|
},
|
|
|
|
HookType::OnStop => {
|
|
|
|
timings.entry(name.to_string()).and_modify(|v| {
|
|
|
|
v.1 = Some(v.0.elapsed());
|
|
|
|
});
|
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if !file.is_file() {
|
|
|
|
json_tests::run_executive_test_path(&file, &[], &mut record_time);
|
|
|
|
} else {
|
|
|
|
json_tests::run_executive_test_file(&file, &mut record_time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (name, v) in timings {
|
|
|
|
println!("{}\t{}", name, display::as_micros(&v.1.expect("All hooks are called with OnStop; qed")));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CLI command `stats`
|
2017-10-20 15:40:25 +02:00
|
|
|
fn run_call<T: Informant>(args: Args, informant: T) {
|
2017-05-26 11:06:48 +02:00
|
|
|
let code = arg(args.code(), "--code");
|
2019-08-07 16:51:08 +02:00
|
|
|
let to = arg(args.to(), "--to");
|
|
|
|
let from = arg(args.from(), "--from");
|
|
|
|
let data = arg(args.data(), "--input");
|
2017-05-26 11:06:48 +02:00
|
|
|
let gas = arg(args.gas(), "--gas");
|
2017-08-28 14:25:16 +02:00
|
|
|
let gas_price = arg(args.gas_price(), "--gas-price");
|
2019-08-07 16:51:08 +02:00
|
|
|
let spec = arg(args.spec(), "--chain");
|
2016-07-12 09:49:16 +02:00
|
|
|
|
2019-06-03 15:36:21 +02:00
|
|
|
if code.is_none() && to == Address::zero() {
|
2017-08-10 18:47:23 +02:00
|
|
|
die("Either --code or --to is required.");
|
|
|
|
}
|
|
|
|
|
2017-05-26 11:06:48 +02:00
|
|
|
let mut params = ActionParams::default();
|
2017-08-10 18:47:23 +02:00
|
|
|
params.call_type = if code.is_none() { CallType::Call } else { CallType::None };
|
2019-08-07 16:51:08 +02:00
|
|
|
params.code = code.map(Arc::new);
|
2017-08-10 18:47:23 +02:00
|
|
|
params.code_address = to;
|
|
|
|
params.address = to;
|
2017-05-26 11:06:48 +02:00
|
|
|
params.sender = from;
|
|
|
|
params.origin = from;
|
2019-08-07 16:51:08 +02:00
|
|
|
params.data = data;
|
2017-05-26 11:06:48 +02:00
|
|
|
params.gas = gas;
|
2017-08-10 18:47:23 +02:00
|
|
|
params.gas_price = gas_price;
|
2017-05-26 11:06:48 +02:00
|
|
|
|
2018-11-25 20:12:59 +01:00
|
|
|
let mut sink = informant.clone_sink();
|
|
|
|
let result = if args.flag_std_dump_json {
|
|
|
|
info::run_action(&spec, params, informant, TrieSpec::Fat)
|
|
|
|
} else {
|
|
|
|
info::run_action(&spec, params, informant, TrieSpec::Secure)
|
|
|
|
};
|
|
|
|
T::finish(result, &mut sink);
|
2016-07-11 09:42:41 +02:00
|
|
|
}
|
|
|
|
|
2017-07-06 11:36:15 +02:00
|
|
|
#[derive(Debug, Deserialize)]
|
2016-07-11 09:42:41 +02:00
|
|
|
struct Args {
|
|
|
|
cmd_stats: bool,
|
2017-08-28 14:25:16 +02:00
|
|
|
cmd_state_test: bool,
|
2018-06-25 11:21:45 +02:00
|
|
|
cmd_stats_jsontests_vm: bool,
|
2017-08-28 14:25:16 +02:00
|
|
|
arg_file: Option<PathBuf>,
|
2016-07-11 09:42:41 +02:00
|
|
|
flag_code: Option<String>,
|
2019-08-07 16:51:08 +02:00
|
|
|
flag_to: Option<String>,
|
|
|
|
flag_from: Option<String>,
|
|
|
|
flag_input: Option<String>,
|
2016-07-11 09:42:41 +02:00
|
|
|
flag_gas: Option<String>,
|
2017-08-10 18:47:23 +02:00
|
|
|
flag_gas_price: Option<String>,
|
2019-08-07 16:51:08 +02:00
|
|
|
flag_only: Option<String>,
|
2017-08-18 17:44:40 +02:00
|
|
|
flag_chain: Option<String>,
|
2017-05-26 11:06:48 +02:00
|
|
|
flag_json: bool,
|
2018-01-18 10:32:22 +01:00
|
|
|
flag_std_json: bool,
|
2018-11-25 20:12:59 +01:00
|
|
|
flag_std_dump_json: bool,
|
|
|
|
flag_std_err_only: bool,
|
|
|
|
flag_std_out_only: bool,
|
2016-07-11 09:42:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Args {
|
2019-08-07 16:51:08 +02:00
|
|
|
// CLI option `--code CODE`
|
|
|
|
/// Set the contract code in hex. Only send to either a contract code or a recipient address.
|
|
|
|
pub fn code(&self) -> Result<Option<Bytes>, String> {
|
|
|
|
match self.flag_code {
|
|
|
|
Some(ref code) => code.from_hex().map(Some).map_err(to_string),
|
|
|
|
None => Ok(None),
|
2017-05-26 11:06:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 16:51:08 +02:00
|
|
|
// CLI option `--to ADDRESS`
|
|
|
|
/// Set the recipient address in hex. Only send to either a contract code or a recipient address.
|
|
|
|
pub fn to(&self) -> Result<Address, String> {
|
|
|
|
match self.flag_to {
|
|
|
|
Some(ref to) => to.parse().map_err(to_string),
|
|
|
|
None => Ok(Address::zero()),
|
2017-08-10 18:47:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 16:51:08 +02:00
|
|
|
// CLI option `--from ADDRESS`
|
|
|
|
/// Set the sender address.
|
2017-05-26 11:06:48 +02:00
|
|
|
pub fn from(&self) -> Result<Address, String> {
|
|
|
|
match self.flag_from {
|
|
|
|
Some(ref from) => from.parse().map_err(to_string),
|
2019-06-03 15:36:21 +02:00
|
|
|
None => Ok(Address::zero()),
|
2017-05-26 11:06:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 16:51:08 +02:00
|
|
|
// CLI option `--input DATA`
|
|
|
|
/// Set the input data in hex.
|
|
|
|
pub fn data(&self) -> Result<Option<Bytes>, String> {
|
|
|
|
match self.flag_input {
|
|
|
|
Some(ref input) => input.from_hex().map_err(to_string).map(Some),
|
|
|
|
None => Ok(None),
|
2017-08-10 18:47:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 16:51:08 +02:00
|
|
|
// CLI option `--gas GAS`
|
|
|
|
/// Set the gas limit in units of gas. Defaults to max value to allow code to run for whatever time is required.
|
|
|
|
pub fn gas(&self) -> Result<U256, String> {
|
|
|
|
match self.flag_gas {
|
|
|
|
Some(ref gas) => gas.parse().map_err(to_string),
|
|
|
|
None => Ok(U256::from(u64::max_value())),
|
2017-05-26 11:06:48 +02:00
|
|
|
}
|
2016-07-11 09:42:41 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 16:51:08 +02:00
|
|
|
// CLI option `--gas-price WEI`
|
|
|
|
/// Set the gas price. Defaults to zero to allow the code to run even if an account with no balance
|
|
|
|
/// is used, otherwise such accounts would not have sufficient funds to pay the transaction fee.
|
|
|
|
/// Defaulting to zero also makes testing easier since it is not necessary to specify a special configuration file.
|
|
|
|
pub fn gas_price(&self) -> Result<U256, String> {
|
|
|
|
match self.flag_gas_price {
|
|
|
|
Some(ref gas_price) => gas_price.parse().map_err(to_string),
|
|
|
|
None => Ok(U256::zero()),
|
2017-05-26 11:06:48 +02:00
|
|
|
}
|
2016-07-11 09:42:41 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 16:51:08 +02:00
|
|
|
// CLI option `--chain PATH`
|
|
|
|
/// Set the path of the chain specification JSON file.
|
2017-05-26 11:06:48 +02:00
|
|
|
pub fn spec(&self) -> Result<spec::Spec, String> {
|
2017-08-18 17:44:40 +02:00
|
|
|
Ok(match self.flag_chain {
|
2018-11-25 20:12:59 +01:00
|
|
|
Some(ref filename) => {
|
2017-05-26 11:06:48 +02:00
|
|
|
let file = fs::File::open(filename).map_err(|e| format!("{}", e))?;
|
2017-09-25 19:45:33 +02:00
|
|
|
spec::Spec::load(&::std::env::temp_dir(), file)?
|
2017-05-26 11:06:48 +02:00
|
|
|
},
|
|
|
|
None => {
|
2017-07-11 10:21:23 +02:00
|
|
|
ethcore::ethereum::new_foundation(&::std::env::temp_dir())
|
2017-05-26 11:06:48 +02:00
|
|
|
},
|
|
|
|
})
|
2016-07-11 09:42:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-26 11:06:48 +02:00
|
|
|
fn arg<T>(v: Result<T, String>, param: &str) -> T {
|
|
|
|
v.unwrap_or_else(|e| die(format!("Invalid {}: {}", param, e)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_string<T: fmt::Display>(msg: T) -> String {
|
|
|
|
format!("{}", msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn die<T: fmt::Display>(msg: T) -> ! {
|
2016-07-11 09:42:41 +02:00
|
|
|
println!("{}", msg);
|
|
|
|
::std::process::exit(-1)
|
|
|
|
}
|
2017-08-18 17:44:40 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-08-07 16:51:08 +02:00
|
|
|
use std::str::FromStr;
|
2017-08-18 17:44:40 +02:00
|
|
|
use docopt::Docopt;
|
2019-06-03 15:36:21 +02:00
|
|
|
use super::{Args, USAGE, Address};
|
2019-08-07 16:51:08 +02:00
|
|
|
use ethjson::state::test::{State};
|
|
|
|
use ethcore::{TrieSpec};
|
|
|
|
use ethereum_types::{H256};
|
|
|
|
use types::transaction;
|
|
|
|
|
|
|
|
use info;
|
|
|
|
use info::{TxInput};
|
|
|
|
use display;
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Deserialize)]
|
|
|
|
pub struct SampleStateTests {
|
|
|
|
pub add11: State,
|
|
|
|
pub add12: State,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct ConstantinopleStateTests {
|
|
|
|
pub create2call_precompiles: State,
|
|
|
|
}
|
2017-08-18 17:44:40 +02:00
|
|
|
|
|
|
|
fn run<T: AsRef<str>>(args: &[T]) -> Args {
|
|
|
|
Docopt::new(USAGE).and_then(|d| d.argv(args.into_iter()).deserialize()).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_parse_all_the_options() {
|
|
|
|
let args = run(&[
|
|
|
|
"parity-evm",
|
2019-08-07 16:51:08 +02:00
|
|
|
"--code", "05",
|
|
|
|
"--to", "0000000000000000000000000000000000000004",
|
|
|
|
"--from", "0000000000000000000000000000000000000003",
|
|
|
|
"--input", "06",
|
|
|
|
"--gas", "1",
|
|
|
|
"--gas-price", "2",
|
|
|
|
"--chain", "./testfile.json",
|
2017-08-18 17:44:40 +02:00
|
|
|
"--json",
|
2018-01-18 10:32:22 +01:00
|
|
|
"--std-json",
|
2018-11-25 20:12:59 +01:00
|
|
|
"--std-dump-json",
|
2019-08-07 16:51:08 +02:00
|
|
|
"--std-err-only",
|
|
|
|
"--std-out-only",
|
2017-08-18 17:44:40 +02:00
|
|
|
]);
|
|
|
|
|
2019-08-07 16:51:08 +02:00
|
|
|
assert_eq!(args.code(), Ok(Some(vec![05])));
|
|
|
|
assert_eq!(args.to(), Ok(Address::from_low_u64_be(4)));
|
|
|
|
assert_eq!(args.from(), Ok(Address::from_low_u64_be(3)));
|
|
|
|
assert_eq!(args.data(), Ok(Some(vec![06]))); // input data
|
|
|
|
assert_eq!(args.gas(), Ok(1.into()));
|
|
|
|
assert_eq!(args.gas_price(), Ok(2.into()));
|
|
|
|
assert_eq!(args.flag_chain, Some("./testfile.json".to_owned()));
|
2017-08-18 17:44:40 +02:00
|
|
|
assert_eq!(args.flag_json, true);
|
2018-01-18 10:32:22 +01:00
|
|
|
assert_eq!(args.flag_std_json, true);
|
2018-11-25 20:12:59 +01:00
|
|
|
assert_eq!(args.flag_std_dump_json, true);
|
|
|
|
assert_eq!(args.flag_std_err_only, true);
|
|
|
|
assert_eq!(args.flag_std_out_only, true);
|
2017-08-18 17:44:40 +02:00
|
|
|
}
|
2017-08-28 14:25:16 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_parse_state_test_command() {
|
|
|
|
let args = run(&[
|
|
|
|
"parity-evm",
|
|
|
|
"state-test",
|
|
|
|
"./file.json",
|
|
|
|
"--chain", "homestead",
|
|
|
|
"--only=add11",
|
|
|
|
"--json",
|
2018-11-25 20:12:59 +01:00
|
|
|
"--std-json",
|
2019-08-07 16:51:08 +02:00
|
|
|
"--std-dump-json",
|
|
|
|
"--std-out-only",
|
|
|
|
"--std-err-only",
|
2017-08-28 14:25:16 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
assert_eq!(args.cmd_state_test, true);
|
|
|
|
assert!(args.arg_file.is_some());
|
2019-08-07 16:51:08 +02:00
|
|
|
assert_eq!(args.flag_chain, Some("homestead".to_owned()));
|
|
|
|
assert_eq!(args.flag_only, Some("add11".to_owned()));
|
2017-08-28 14:25:16 +02:00
|
|
|
assert_eq!(args.flag_json, true);
|
2018-01-18 10:32:22 +01:00
|
|
|
assert_eq!(args.flag_std_json, true);
|
2018-11-25 20:12:59 +01:00
|
|
|
assert_eq!(args.flag_std_dump_json, true);
|
2019-08-07 16:51:08 +02:00
|
|
|
assert_eq!(args.flag_std_out_only, true);
|
|
|
|
assert_eq!(args.flag_std_err_only, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_verify_state_root_using_sample_state_test_json_file() {
|
|
|
|
let state_tests = include_str!("../res/teststate.json");
|
|
|
|
// Parse the specified state test JSON file to simulate the CLI command `state-test <file>`.
|
|
|
|
let deserialized_state_tests: SampleStateTests = serde_json::from_str(state_tests)
|
|
|
|
.expect("Serialization cannot fail; qed");
|
|
|
|
|
|
|
|
// Simulate the name CLI option `--only NAME`
|
|
|
|
let state_test_name = "add11";
|
|
|
|
// Simulate the chain `--chain CHAIN`
|
|
|
|
let pre = deserialized_state_tests.add11.pre_state.into();
|
|
|
|
let env_info = deserialized_state_tests.add11.env.into();
|
|
|
|
let multitransaction = deserialized_state_tests.add11.transaction;
|
|
|
|
|
|
|
|
let post_roots = [
|
|
|
|
// EIP-150
|
|
|
|
[
|
|
|
|
H256::from_str("f4455d9332a9e171fc41b48350457147c21fc0a92364d9925913f7421e15aa95").unwrap(),
|
|
|
|
H256::from_str("a0bc824c4186c4c1543851894fbf707b5b1cf771d15e74f3517daf0a3415fe5b").unwrap(),
|
|
|
|
],
|
|
|
|
// EIP-158
|
|
|
|
[
|
|
|
|
H256::from_str("f4455d9332a9e171fc41b48350457147c21fc0a92364d9925913f7421e15aa95").unwrap(),
|
|
|
|
H256::from_str("27682055e1899031c92d253ee1d22c40f70a6943724168c0b694a1a503664e0a").unwrap(),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
for (fork_index, (fork_spec_name, tx_states)) in deserialized_state_tests.add11.post_states.iter().enumerate() {
|
|
|
|
for (tx_index, tx_state) in tx_states.into_iter().enumerate() {
|
|
|
|
let post_root = post_roots[fork_index][tx_index];
|
|
|
|
let informant = display::json::Informant::default();
|
|
|
|
let trie_spec = TrieSpec::Secure; // TrieSpec::Fat for --std_dump_json
|
|
|
|
let transaction: transaction::SignedTransaction = multitransaction.select(&tx_state.indexes).into();
|
|
|
|
let tx_input = TxInput {
|
|
|
|
state_test_name: &state_test_name,
|
|
|
|
tx_index,
|
|
|
|
fork_spec_name: &fork_spec_name,
|
|
|
|
pre_state: &pre,
|
|
|
|
post_root,
|
|
|
|
env_info: &env_info,
|
|
|
|
transaction,
|
|
|
|
informant,
|
|
|
|
trie_spec,
|
|
|
|
};
|
|
|
|
assert!(info::run_transaction(tx_input));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_verify_state_root_using_constantinople_state_test_json_file() {
|
|
|
|
let state_tests = include_str!("../res/create2callPrecompiles.json");
|
|
|
|
// Parse the specified state test JSON file to simulate the CLI command `state-test <file>`.
|
|
|
|
let deserialized_state_tests: ConstantinopleStateTests = serde_json::from_str(state_tests)
|
|
|
|
.expect("Serialization cannot fail; qed");
|
|
|
|
|
|
|
|
// Simulate the name CLI option `--only NAME`
|
|
|
|
let state_test_name = "create2callPrecompiles";
|
|
|
|
let post_roots = [
|
|
|
|
// Constantinople
|
|
|
|
[
|
|
|
|
H256::from_str("3dfdcd1d19badbbba8b0c953504e8b4685270ee5b86e155350b6ef1042c9ce43").unwrap(),
|
|
|
|
H256::from_str("88803085d3420aec76078e215f67fc5f7b6f297fbe19d85c2236ad685d0fc7fc").unwrap(),
|
|
|
|
H256::from_str("57181dda5c067cb31f084c4118791b40d5028c39071e83e60e7f7403d683527e").unwrap(),
|
|
|
|
H256::from_str("f04c1039893eb6959354c3c16e9fe025d4b9dc3981362f79c56cc427dca0d544").unwrap(),
|
|
|
|
H256::from_str("5d5db3d6c4377b34b74ecf8638f684acb220cc7ce286ae5f000ffa74faf38bae").unwrap(),
|
|
|
|
H256::from_str("f8343b2e05ae120bf25947de840cedf1ca2c1bcda1cdb89d218427d8a84d4798").unwrap(),
|
|
|
|
H256::from_str("305a8a8a7d9da97d14ed2259503d9373d803ea4b7fbf8c360f50b1b30a3d04ed").unwrap(),
|
|
|
|
H256::from_str("de1d3953b508913c6e3e9bd412cd50daf60bb177517e5d1e8ccb0dab193aed03").unwrap(),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
let pre = deserialized_state_tests.create2call_precompiles.pre_state.into();
|
|
|
|
let env_info = deserialized_state_tests.create2call_precompiles.env.into();
|
|
|
|
let multitransaction = deserialized_state_tests.create2call_precompiles.transaction;
|
|
|
|
for (fork_index, (fork_spec_name, tx_states)) in
|
|
|
|
deserialized_state_tests.create2call_precompiles.post_states.iter().enumerate() {
|
|
|
|
for (tx_index, tx_state) in tx_states.into_iter().enumerate() {
|
|
|
|
let informant = display::json::Informant::default();
|
|
|
|
// Hash of latest transaction index in the chain
|
|
|
|
let post_root = post_roots[fork_index][tx_index];
|
|
|
|
let trie_spec = TrieSpec::Secure; // TrieSpec::Fat for --std_dump_json
|
|
|
|
let transaction: transaction::SignedTransaction = multitransaction.select(&tx_state.indexes).into();
|
|
|
|
let tx_input = TxInput {
|
|
|
|
state_test_name: &state_test_name,
|
|
|
|
tx_index,
|
|
|
|
fork_spec_name: &fork_spec_name,
|
|
|
|
pre_state: &pre,
|
|
|
|
post_root,
|
|
|
|
env_info: &env_info,
|
|
|
|
transaction,
|
|
|
|
informant,
|
|
|
|
trie_spec,
|
|
|
|
};
|
|
|
|
assert!(info::run_transaction(tx_input));
|
|
|
|
}
|
|
|
|
}
|
2017-08-28 14:25:16 +02:00
|
|
|
}
|
2017-08-18 17:44:40 +02:00
|
|
|
}
|