Very basic EVM binary. (#1574)

* EVM binary - initial version

* Adding missing documentation

* Fixing warnings

* Basic evmbin options

* EVMbin crate.
This commit is contained in:
Tomasz Drwięga 2016-07-11 09:42:41 +02:00 committed by Gav Wood
parent e15f631ec7
commit 2ed09de38e
7 changed files with 1299 additions and 3 deletions

View File

@ -95,11 +95,17 @@ impl<'a> Finalize for Result<GasLeft<'a>> {
}
}
/// Cost calculation type. For low-gas usage we calculate costs using usize instead of U256
pub trait CostType: ops::Mul<Output=Self> + ops::Div<Output=Self> + ops::Add<Output=Self> + ops::Sub<Output=Self> + ops::Shr<usize, Output=Self> + ops::Shl<usize, Output=Self> + cmp::Ord + Sized + From<usize> + Copy {
/// Converts this cost into `U256`
fn as_u256(&self) -> U256;
/// Tries to fit `U256` into this `Cost` type
fn from_u256(val: U256) -> Result<Self>;
/// Convert to usize (may panic)
fn as_usize(&self) -> usize;
/// Add with overflow
fn overflow_add(self, other: Self) -> (Self, bool);
/// Multiple with overflow
fn overflow_mul(self, other: Self) -> (Self, bool);
}

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
///! Rust VM implementation
//! Rust VM implementation
#[cfg(not(feature = "evm-debug"))]
macro_rules! evm_debug {
@ -182,6 +182,7 @@ impl<Cost: CostType> Interpreter<Cost> {
instruction: instruction
});
}
if info.tier == instructions::GasPriceTier::Invalid {
return Err(evm::Error::BadInstruction {
instruction: instruction

View File

@ -118,18 +118,18 @@ pub mod pod_state;
pub mod engine;
pub mod migrations;
pub mod miner;
#[macro_use] pub mod evm;
pub mod action_params;
mod blooms;
mod db;
mod common;
mod basic_types;
#[macro_use] mod evm;
mod env_info;
mod pod_account;
mod state;
mod account;
mod account_db;
mod action_params;
mod null_engine;
mod builtin;
mod substate;

1063
evmbin/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

11
evmbin/Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "evm"
description = "Parity's EVM implementation"
version = "0.1.0"
authors = ["Ethcore <admin@ethcore.io>"]
[dependencies]
rustc-serialize = "0.3"
docopt = { version = "0.6" }
ethcore = { path = "../ethcore" }
ethcore-util = { path = "../util" }

107
evmbin/src/ext.rs Normal file
View File

@ -0,0 +1,107 @@
// 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/>.
//! Externalities implementation.
use std::collections::HashMap;
use util::{U256, H256, Address, Bytes, FixedHash};
use ethcore::client::EnvInfo;
use ethcore::evm::{self, Ext, ContractCreateResult, MessageCallResult, Schedule};
pub struct FakeExt {
schedule: Schedule,
store: HashMap<H256, H256>,
}
impl Default for FakeExt {
fn default() -> Self {
FakeExt {
schedule: Schedule::new_homestead(),
store: HashMap::new(),
}
}
}
impl Ext for FakeExt {
fn storage_at(&self, key: &H256) -> H256 {
self.store.get(key).unwrap_or(&H256::new()).clone()
}
fn set_storage(&mut self, key: H256, value: H256) {
self.store.insert(key, value);
}
fn exists(&self, _address: &Address) -> bool {
unimplemented!();
}
fn balance(&self, _address: &Address) -> U256 {
unimplemented!();
}
fn blockhash(&self, _number: &U256) -> H256 {
unimplemented!();
}
fn create(&mut self, _gas: &U256, _value: &U256, _code: &[u8]) -> ContractCreateResult {
unimplemented!();
}
fn call(&mut self,
_gas: &U256,
_sender_address: &Address,
_receive_address: &Address,
_value: Option<U256>,
_data: &[u8],
_code_address: &Address,
_output: &mut [u8]) -> MessageCallResult {
unimplemented!();
}
fn extcode(&self, _address: &Address) -> Bytes {
unimplemented!();
}
fn log(&mut self, _topics: Vec<H256>, _data: &[u8]) {
unimplemented!();
}
fn ret(self, gas: &U256, _data: &[u8]) -> evm::Result<U256> {
Ok(*gas)
}
fn suicide(&mut self, _refund_address: &Address) {
unimplemented!();
}
fn schedule(&self) -> &Schedule {
&self.schedule
}
fn env_info(&self) -> &EnvInfo {
unimplemented!()
}
fn depth(&self) -> usize {
unimplemented!();
// self.depth
}
fn inc_sstore_clears(&mut self) {
unimplemented!();
// self.sstore_clears += 1;
}
}

108
evmbin/src/main.rs Normal file
View File

@ -0,0 +1,108 @@
// 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)]
extern crate ethcore;
extern crate rustc_serialize;
extern crate docopt;
#[macro_use]
extern crate ethcore_util as util;
mod ext;
use std::time::Instant;
use std::str::FromStr;
use docopt::Docopt;
use util::{U256, FromHex, Uint, Bytes};
use ethcore::evm::{Factory, VMType, Finalize};
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:
--code CODE Contract code.
--input DATA Input data.
--gas GAS Supplied gas.
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();
params.code = Some(args.code());
params.data = args.data();
let factory = Factory::new(VMType::Interpreter);
let mut vm = factory.create(params.gas);
let mut ext = ext::FakeExt::default();
let start = Instant::now();
let gas_left = vm.exec(params, &mut ext).finalize(ext).expect("OK");
let duration = start.elapsed();
println!("Gas used: {:?}", args.gas() - gas_left);
println!("Output: {:?}", "");
println!("Time: {}.{:.9}s", duration.as_secs(), duration.subsec_nanos());
}
#[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)
}