2016-07-11 09:42:41 +02:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// 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;
|
|
|
|
extern crate rustc_serialize;
|
|
|
|
extern crate docopt;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate ethcore_util as util;
|
|
|
|
|
|
|
|
mod ext;
|
|
|
|
|
2016-10-17 11:55:47 +02:00
|
|
|
use std::sync::Arc;
|
2016-07-12 09:49:16 +02:00
|
|
|
use std::time::{Instant, Duration};
|
2016-10-18 12:13:49 +02:00
|
|
|
use std::fmt;
|
2016-07-11 09:42:41 +02:00
|
|
|
use std::str::FromStr;
|
|
|
|
use docopt::Docopt;
|
|
|
|
use util::{U256, FromHex, Uint, Bytes};
|
2016-10-18 12:13:49 +02:00
|
|
|
use ethcore::evm::{self, Factory, VMType, Finalize};
|
2016-07-11 09:42:41 +02:00
|
|
|
use ethcore::action_params::ActionParams;
|
|
|
|
|
|
|
|
const USAGE: &'static str = r#"
|
|
|
|
EVM implementation for Parity.
|
|
|
|
Copyright 2016 Ethcore (UK) Limited
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
evmbin stats [options]
|
|
|
|
evmbin [-h | --help]
|
|
|
|
|
|
|
|
Transaction options:
|
2016-10-17 11:55:47 +02:00
|
|
|
--code CODE Contract code as hex (without 0x)
|
|
|
|
--input DATA Input data as hex (without 0x)
|
|
|
|
--gas GAS Supplied gas as hex (without 0x)
|
2016-07-11 09:42:41 +02:00
|
|
|
|
|
|
|
General options:
|
|
|
|
-h, --help Display this message and exit.
|
|
|
|
"#;
|
|
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let args: Args = Docopt::new(USAGE).and_then(|d| d.decode()).unwrap_or_else(|e| e.exit());
|
|
|
|
|
|
|
|
let mut params = ActionParams::default();
|
|
|
|
params.gas = args.gas();
|
2016-10-17 11:55:47 +02:00
|
|
|
params.code = Some(Arc::new(args.code()));
|
2016-07-11 09:42:41 +02:00
|
|
|
params.data = args.data();
|
|
|
|
|
2016-07-12 09:49:16 +02:00
|
|
|
let result = run_vm(params);
|
2016-10-18 12:13:49 +02:00
|
|
|
match result {
|
|
|
|
Ok(success) => println!("{}", success),
|
|
|
|
Err(failure) => println!("{}", failure),
|
|
|
|
}
|
2016-07-12 09:49:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Execute VM with given `ActionParams`
|
2016-10-18 12:13:49 +02:00
|
|
|
pub fn run_vm(params: ActionParams) -> Result<Success, Failure> {
|
2016-07-12 09:49:16 +02:00
|
|
|
let initial_gas = params.gas;
|
2016-10-17 11:55:47 +02:00
|
|
|
let factory = Factory::new(VMType::Interpreter, 1024);
|
2016-07-11 09:42:41 +02:00
|
|
|
let mut vm = factory.create(params.gas);
|
|
|
|
let mut ext = ext::FakeExt::default();
|
|
|
|
|
|
|
|
let start = Instant::now();
|
2016-10-18 12:13:49 +02:00
|
|
|
let gas_left = vm.exec(params, &mut ext).finalize(ext);
|
2016-07-11 09:42:41 +02:00
|
|
|
let duration = start.elapsed();
|
|
|
|
|
2016-10-18 12:13:49 +02:00
|
|
|
match gas_left {
|
|
|
|
Ok(gas_left) => Ok(Success {
|
|
|
|
gas_used: initial_gas - gas_left,
|
|
|
|
// TODO [ToDr] get output from ext
|
|
|
|
output: Vec::new(),
|
|
|
|
time: duration,
|
|
|
|
}),
|
|
|
|
Err(e) => Err(Failure {
|
|
|
|
error: e,
|
|
|
|
time: duration,
|
|
|
|
}),
|
2016-07-12 09:49:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-18 12:13:49 +02:00
|
|
|
/// Execution finished correctly
|
|
|
|
pub struct Success {
|
2016-07-12 09:49:16 +02:00
|
|
|
/// Used gas
|
2016-10-18 12:13:49 +02:00
|
|
|
gas_used: U256,
|
2016-07-12 09:49:16 +02:00
|
|
|
/// Output as bytes
|
2016-10-18 12:13:49 +02:00
|
|
|
output: Vec<u8>,
|
2016-07-12 09:49:16 +02:00
|
|
|
/// Time Taken
|
2016-10-18 12:13:49 +02:00
|
|
|
time: Duration,
|
|
|
|
}
|
|
|
|
impl fmt::Display for Success {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
try!(writeln!(f, "Gas used: {:?}", self.gas_used));
|
|
|
|
try!(writeln!(f, "Output: {:?}", self.output));
|
|
|
|
try!(writeln!(f, "Time: {}.{:.9}s", self.time.as_secs(), self.time.subsec_nanos()));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Execution failed
|
|
|
|
pub struct Failure {
|
|
|
|
/// Internal error
|
|
|
|
error: evm::Error,
|
|
|
|
/// Duration
|
|
|
|
time: Duration,
|
|
|
|
}
|
|
|
|
impl fmt::Display for Failure {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
try!(writeln!(f, "Error: {:?}", self.error));
|
|
|
|
try!(writeln!(f, "Time: {}.{:.9}s", self.time.as_secs(), self.time.subsec_nanos()));
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-07-11 09:42:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, RustcDecodable)]
|
|
|
|
struct Args {
|
|
|
|
cmd_stats: bool,
|
|
|
|
flag_code: Option<String>,
|
|
|
|
flag_gas: Option<String>,
|
|
|
|
flag_input: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Args {
|
|
|
|
pub fn gas(&self) -> U256 {
|
|
|
|
self.flag_gas
|
|
|
|
.clone()
|
|
|
|
.and_then(|g| U256::from_str(&g).ok())
|
|
|
|
.unwrap_or_else(|| !U256::zero())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn code(&self) -> Bytes {
|
|
|
|
self.flag_code
|
|
|
|
.clone()
|
|
|
|
.and_then(|c| c.from_hex().ok())
|
|
|
|
.unwrap_or_else(|| die("Code is required."))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn data(&self) -> Option<Bytes> {
|
|
|
|
self.flag_input
|
|
|
|
.clone()
|
|
|
|
.and_then(|d| d.from_hex().ok())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn die(msg: &'static str) -> ! {
|
|
|
|
println!("{}", msg);
|
|
|
|
::std::process::exit(-1)
|
|
|
|
}
|