2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-07-11 09:42:41 +02:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// 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
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
//! Parity EVM interpreter binary.
|
|
|
|
|
|
|
|
#![warn(missing_docs)]
|
2016-07-12 09:49:16 +02:00
|
|
|
#![allow(dead_code)]
|
2016-07-11 09:42:41 +02:00
|
|
|
extern crate ethcore;
|
2017-07-06 11:36:15 +02:00
|
|
|
extern crate rustc_hex;
|
|
|
|
extern crate serde;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
2016-07-11 09:42:41 +02:00
|
|
|
extern crate docopt;
|
|
|
|
extern crate ethcore_util as util;
|
2017-08-01 16:41:33 +02:00
|
|
|
extern crate vm;
|
2017-07-12 13:09:17 +02:00
|
|
|
extern crate evm;
|
2017-07-03 07:31:29 +02:00
|
|
|
extern crate panic_hook;
|
2016-07-11 09:42:41 +02:00
|
|
|
|
2016-10-17 11:55:47 +02:00
|
|
|
use std::sync::Arc;
|
2017-05-26 11:06:48 +02:00
|
|
|
use std::{fmt, fs};
|
2016-07-11 09:42:41 +02:00
|
|
|
use docopt::Docopt;
|
2017-07-06 11:36:15 +02:00
|
|
|
use rustc_hex::FromHex;
|
|
|
|
use util::{U256, Bytes, Address};
|
2017-05-26 11:06:48 +02:00
|
|
|
use ethcore::spec;
|
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;
|
|
|
|
|
2017-08-01 16:41:33 +02:00
|
|
|
use info::Informant;
|
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.
|
2017-01-25 18:51:41 +01:00
|
|
|
Copyright 2016, 2017 Parity Technologies (UK) Ltd
|
2016-07-11 09:42:41 +02:00
|
|
|
|
|
|
|
Usage:
|
|
|
|
evmbin stats [options]
|
2017-05-26 11:06:48 +02:00
|
|
|
evmbin [options]
|
2016-07-11 09:42:41 +02:00
|
|
|
evmbin [-h | --help]
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
General options:
|
2017-05-26 11:06:48 +02:00
|
|
|
--json Display verbose results in JSON.
|
|
|
|
--chain CHAIN Chain spec file path.
|
2016-07-11 09:42:41 +02:00
|
|
|
-h, --help Display this message and exit.
|
|
|
|
"#;
|
|
|
|
|
|
|
|
|
|
|
|
fn main() {
|
2017-07-03 07:31:29 +02:00
|
|
|
panic_hook::set();
|
|
|
|
|
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-05-26 11:06:48 +02:00
|
|
|
if args.flag_json {
|
|
|
|
run(args, display::json::Informant::default())
|
|
|
|
} else {
|
|
|
|
run(args, display::simple::Informant::default())
|
2016-10-18 12:13:49 +02:00
|
|
|
}
|
2016-07-12 09:49:16 +02:00
|
|
|
}
|
|
|
|
|
2017-05-26 11:06:48 +02:00
|
|
|
fn run<T: Informant>(args: Args, mut informant: T) {
|
|
|
|
let from = arg(args.from(), "--from");
|
2017-08-10 18:47:23 +02:00
|
|
|
let to = arg(args.to(), "--to");
|
2017-05-26 11:06:48 +02:00
|
|
|
let code = arg(args.code(), "--code");
|
|
|
|
let spec = arg(args.spec(), "--chain");
|
|
|
|
let gas = arg(args.gas(), "--gas");
|
2017-08-10 18:47:23 +02:00
|
|
|
let gas_price = arg(args.gas(), "--gas-price");
|
2017-05-26 11:06:48 +02:00
|
|
|
let data = arg(args.data(), "--input");
|
2016-07-12 09:49:16 +02:00
|
|
|
|
2017-08-10 18:47:23 +02:00
|
|
|
if code.is_none() && to == Address::default() {
|
|
|
|
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 };
|
|
|
|
params.code_address = to;
|
|
|
|
params.address = to;
|
2017-05-26 11:06:48 +02:00
|
|
|
params.sender = from;
|
|
|
|
params.origin = from;
|
|
|
|
params.gas = gas;
|
2017-08-10 18:47:23 +02:00
|
|
|
params.gas_price = gas_price;
|
|
|
|
params.code = code.map(Arc::new);
|
2017-05-26 11:06:48 +02:00
|
|
|
params.data = data;
|
|
|
|
|
2017-06-09 12:31:03 +02:00
|
|
|
informant.set_gas(gas);
|
2017-08-01 16:41:33 +02:00
|
|
|
let result = info::run(&mut informant, spec, params);
|
2017-05-26 11:06:48 +02:00
|
|
|
informant.finish(result);
|
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-05-26 11:06:48 +02:00
|
|
|
flag_from: Option<String>,
|
2017-08-10 18:47:23 +02:00
|
|
|
flag_to: Option<String>,
|
2016-07-11 09:42:41 +02:00
|
|
|
flag_code: Option<String>,
|
|
|
|
flag_gas: Option<String>,
|
2017-08-10 18:47:23 +02:00
|
|
|
flag_gas_price: Option<String>,
|
2016-07-11 09:42:41 +02:00
|
|
|
flag_input: Option<String>,
|
2017-05-26 11:06:48 +02:00
|
|
|
flag_spec: Option<String>,
|
|
|
|
flag_json: bool,
|
2016-07-11 09:42:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Args {
|
2017-05-26 11:06:48 +02:00
|
|
|
pub fn gas(&self) -> Result<U256, String> {
|
|
|
|
match self.flag_gas {
|
|
|
|
Some(ref gas) => gas.parse().map_err(to_string),
|
|
|
|
None => Ok(!U256::zero()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-10 18:47:23 +02:00
|
|
|
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
|
|
|
pub fn from(&self) -> Result<Address, String> {
|
|
|
|
match self.flag_from {
|
|
|
|
Some(ref from) => from.parse().map_err(to_string),
|
|
|
|
None => Ok(Address::default()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-10 18:47:23 +02:00
|
|
|
pub fn to(&self) -> Result<Address, String> {
|
|
|
|
match self.flag_to {
|
|
|
|
Some(ref to) => to.parse().map_err(to_string),
|
|
|
|
None => Ok(Address::default()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn code(&self) -> Result<Option<Bytes>, String> {
|
2017-05-26 11:06:48 +02:00
|
|
|
match self.flag_code {
|
2017-08-10 18:47:23 +02:00
|
|
|
Some(ref code) => code.from_hex().map(Some).map_err(to_string),
|
|
|
|
None => Ok(None),
|
2017-05-26 11:06:48 +02:00
|
|
|
}
|
2016-07-11 09:42:41 +02:00
|
|
|
}
|
|
|
|
|
2017-05-26 11:06:48 +02:00
|
|
|
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),
|
|
|
|
}
|
2016-07-11 09:42:41 +02:00
|
|
|
}
|
|
|
|
|
2017-05-26 11:06:48 +02:00
|
|
|
pub fn spec(&self) -> Result<spec::Spec, String> {
|
|
|
|
Ok(match self.flag_spec {
|
|
|
|
Some(ref filename) => {
|
|
|
|
let file = fs::File::open(filename).map_err(|e| format!("{}", e))?;
|
2017-07-11 10:21:23 +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)
|
|
|
|
}
|