merged with mk

This commit is contained in:
Tomusdrw 2016-01-16 20:11:12 +01:00
parent 1d53d806b2
commit 3979b787f7
12 changed files with 484 additions and 422 deletions

View File

@ -21,3 +21,4 @@ evmjit = { path = "rust-evmjit", optional = true }
[features] [features]
jit = ["evmjit"] jit = ["evmjit"]
evm_debug = []

View File

@ -47,5 +47,5 @@ pub type Result = result::Result<U256, Error>;
/// Evm interface. /// Evm interface.
pub trait Evm { pub trait Evm {
/// This function should be used to execute transaction. /// This function should be used to execute transaction.
fn exec(&self, params: &ActionParams, ext: &mut Ext) -> Result; fn exec(&self, params: ActionParams, ext: &mut Ext) -> Result;
} }

View File

@ -6,12 +6,35 @@ use util::uint::*;
use evm::{Schedule, Error}; use evm::{Schedule, Error};
use env_info::*; use env_info::*;
/// Result of externalities create function.
pub enum ContractCreateResult {
/// Returned when creation was successfull.
/// Contains an address of newly created contract and gas left.
Created(Address, U256),
/// Returned when contract creation failed.
/// VM doesn't have to know the reason.
Failed
}
/// Result of externalities call function.
pub enum MessageCallResult {
/// Returned when message call was successfull.
/// Contains gas left.
Success(U256),
/// Returned when message call failed.
/// VM doesn't have to know the reason.
Failed
}
pub trait Ext { pub trait Ext {
/// Returns a value for given key. /// Returns a value for given key.
fn storage_at(&self, key: &H256) -> H256; fn storage_at(&self, key: &H256) -> H256;
/// Stores a value for given key. /// Stores a value for given key.
fn set_storage_at(&mut self, key: H256, value: H256); fn set_storage(&mut self, key: H256, value: H256);
/// Determine whether an account exists.
fn exists(&self, address: &Address) -> bool;
/// Returns address balance. /// Returns address balance.
fn balance(&self, address: &Address) -> U256; fn balance(&self, address: &Address) -> U256;
@ -22,7 +45,7 @@ pub trait Ext {
/// Creates new contract. /// Creates new contract.
/// ///
/// Returns gas_left and contract address if contract creation was succesfull. /// Returns gas_left and contract address if contract creation was succesfull.
fn create(&mut self, gas: &U256, value: &U256, code: &[u8]) -> (U256, Option<Address>); fn create(&mut self, gas: &U256, value: &U256, code: &[u8]) -> ContractCreateResult;
/// Message call. /// Message call.
/// ///
@ -31,12 +54,11 @@ pub trait Ext {
/// and true if subcall was successfull. /// and true if subcall was successfull.
fn call(&mut self, fn call(&mut self,
gas: &U256, gas: &U256,
call_gas: &U256, address: &Address,
receive_address: &Address,
value: &U256, value: &U256,
data: &[u8], data: &[u8],
code_address: &Address, code_address: &Address,
output: &mut [u8]) -> Result<(U256, bool), Error>; output: &mut [u8]) -> MessageCallResult;
/// Returns code at given address /// Returns code at given address
fn extcode(&self, address: &Address) -> Bytes; fn extcode(&self, address: &Address) -> Bytes;
@ -57,4 +79,13 @@ pub trait Ext {
/// Returns environment info. /// Returns environment info.
fn env_info(&self) -> &EnvInfo; fn env_info(&self) -> &EnvInfo;
/// Returns current depth of execution.
///
/// If contract A calls contract B, and contract B calls C,
/// then A depth is 0, B is 1, C is 2 and so on.
fn depth(&self) -> usize;
/// Increments sstore refunds count by 1.
fn inc_sstore_clears(&mut self);
} }

View File

@ -6,11 +6,17 @@ use super::instructions as instructions;
use super::instructions::Instruction; use super::instructions::Instruction;
use std::num::wrapping::OverflowingOps; use std::num::wrapping::OverflowingOps;
use std::marker::Copy; use std::marker::Copy;
use evm::{MessageCallResult, ContractCreateResult};
#[cfg(not(feature = "evm_debug"))]
macro_rules! evm_debug {
($x: expr) => {}
}
#[cfg(feature = "evm_debug")] #[cfg(feature = "evm_debug")]
macro_rules! evm_debug { macro_rules! evm_debug {
($x: expr) => { ($x: expr) => {
println!($x); $x
} }
} }
@ -21,11 +27,6 @@ fn color(instruction: Instruction, name: &'static str) -> String {
format!("\x1B[1;{}m{}\x1B[0m", colors[c], name) format!("\x1B[1;{}m{}\x1B[0m", colors[c], name)
} }
#[cfg(not(feature = "evm_debug"))]
macro_rules! evm_debug {
($x: expr) => {}
}
type CodePosition = usize; type CodePosition = usize;
type Gas = U256; type Gas = U256;
type ProgramCounter = usize; type ProgramCounter = usize;
@ -81,7 +82,7 @@ impl<S : fmt::Display> Stack<S> for VecStack<S> {
match val { match val {
Some(x) => { Some(x) => {
evm_debug!({ evm_debug!({
format!(" POP: {}", x) println!(" POP: {}", x)
}); });
x x
}, },
@ -100,7 +101,7 @@ impl<S : fmt::Display> Stack<S> for VecStack<S> {
fn push(&mut self, elem: S) { fn push(&mut self, elem: S) {
evm_debug!({ evm_debug!({
format!(" PUSH: {}", elem) println!(" PUSH: {}", elem)
}); });
self.stack.push(elem); self.stack.push(elem);
} }
@ -246,7 +247,9 @@ enum InstructionCost {
} }
enum InstructionResult { enum InstructionResult {
AdditionalGasCost(U256), Ok,
UseAllGas,
UnusedGas(U256),
JumpToPosition(U256), JumpToPosition(U256),
StopExecutionWithGasCost(U256), StopExecutionWithGasCost(U256),
StopExecution StopExecution
@ -256,7 +259,7 @@ enum InstructionResult {
pub struct Interpreter; pub struct Interpreter;
impl evm::Evm for Interpreter { impl evm::Evm for Interpreter {
fn exec(&self, params: &ActionParams, ext: &mut evm::Ext) -> evm::Result { fn exec(&self, params: ActionParams, ext: &mut evm::Ext) -> evm::Result {
let code = &params.code.clone().unwrap(); let code = &params.code.clone().unwrap();
let valid_jump_destinations = self.find_jump_destinations(&code); let valid_jump_destinations = self.find_jump_destinations(&code);
@ -279,29 +282,33 @@ impl evm::Evm for Interpreter {
current_gas = current_gas - gas_cost; current_gas = current_gas - gas_cost;
evm_debug!({ evm_debug!({
format!("[0x{:x}][{}(0x{:x}) Gas: {}\n Gas Before: {}", println!("[0x{:x}][{}(0x{:x}) Gas: {:x}\n Gas Before: {:x}",
reader.position, reader.position,
color(instruction, instructions::get_info(instruction).name), color(instruction, instructions::get_info(instruction).name),
instruction, instruction,
gas_cost, gas_cost,
current_gas + gas_cost current_gas + gas_cost
) );
}); });
// Execute instruction // Execute instruction
let result = try!(self.exec_instruction( let result = try!(self.exec_instruction(
current_gas, params, ext, instruction, &mut reader, &mut mem, &mut stack current_gas, &params, ext, instruction, &mut reader, &mut mem, &mut stack
)); ));
// Advance // Advance
match result { match result {
InstructionResult::Ok => {},
InstructionResult::UnusedGas(gas) => {
current_gas = current_gas + gas;
},
InstructionResult::UseAllGas => {
current_gas = U256::zero();
},
InstructionResult::JumpToPosition(position) => { InstructionResult::JumpToPosition(position) => {
let pos = try!(self.verify_jump(position, &valid_jump_destinations)); let pos = try!(self.verify_jump(position, &valid_jump_destinations));
reader.position = pos; reader.position = pos;
}, },
InstructionResult::AdditionalGasCost(gas_cost) => {
current_gas = current_gas - gas_cost;
},
InstructionResult::StopExecutionWithGasCost(gas_cost) => { InstructionResult::StopExecutionWithGasCost(gas_cost) => {
current_gas = current_gas - gas_cost; current_gas = current_gas - gas_cost;
reader.position = code.len(); reader.position = code.len();
@ -351,6 +358,7 @@ impl Interpreter {
let gas = if self.is_zero(&val) && !self.is_zero(newval) { let gas = if self.is_zero(&val) && !self.is_zero(newval) {
schedule.sstore_set_gas schedule.sstore_set_gas
} else if !self.is_zero(&val) && self.is_zero(newval) { } else if !self.is_zero(&val) && self.is_zero(newval) {
// Refund is added when actually executing sstore
schedule.sstore_reset_gas schedule.sstore_reset_gas
} else { } else {
schedule.sstore_reset_gas schedule.sstore_reset_gas
@ -397,18 +405,34 @@ impl Interpreter {
instructions::LOG0...instructions::LOG4 => { instructions::LOG0...instructions::LOG4 => {
let no_of_topics = instructions::get_log_topics(instruction); let no_of_topics = instructions::get_log_topics(instruction);
let log_gas = schedule.log_gas + schedule.log_topic_gas * no_of_topics; let log_gas = schedule.log_gas + schedule.log_topic_gas * no_of_topics;
// TODO [todr] potential overflow of datagass
let data_gas = stack.peek(1).clone() * U256::from(schedule.log_data_gas); let data_gas = stack.peek(1).clone() * U256::from(schedule.log_data_gas);
let gas = try!(self.gas_add(data_gas, U256::from(log_gas))); let gas = try!(self.gas_add(data_gas, U256::from(log_gas)));
InstructionCost::GasMem(gas, self.mem_needed(stack.peek(0), stack.peek(1))) InstructionCost::GasMem(gas, self.mem_needed(stack.peek(0), stack.peek(1)))
}, },
instructions::CALL | instructions::CALLCODE => { instructions::CALL | instructions::CALLCODE => {
// [todr] we actuall call gas_cost is calculated in ext match add_u256_usize(stack.peek(0), schedule.call_gas) {
let gas = U256::from(schedule.call_gas); (_gas, true) => InstructionCost::GasMem(U256::zero(), RequiredMem::OutOfMemory),
let mem = self.mem_max( (mut gas, false) => {
self.mem_needed(stack.peek(5), stack.peek(6)), let mem = self.mem_max(
self.mem_needed(stack.peek(3), stack.peek(4)) self.mem_needed(stack.peek(5), stack.peek(6)),
); self.mem_needed(stack.peek(3), stack.peek(4))
InstructionCost::GasMem(gas, mem) );
let address = u256_to_address(stack.peek(1));
// TODO [todr] Potential overflows
if instruction == instructions::CALL && !ext.exists(&address) {
gas = gas + U256::from(schedule.call_new_account_gas);
};
if stack.peek(2).clone() > U256::zero() {
gas = gas + U256::from(schedule.call_value_transfer_gas)
};
InstructionCost::GasMem(gas,mem)
}
}
}, },
instructions::DELEGATECALL => { instructions::DELEGATECALL => {
match add_u256_usize(stack.peek(0), schedule.call_gas) { match add_u256_usize(stack.peek(0), schedule.call_gas) {
@ -554,30 +578,41 @@ impl Interpreter {
let init_size = stack.pop_back(); let init_size = stack.pop_back();
let contract_code = mem.read_slice(init_off, init_size); let contract_code = mem.read_slice(init_off, init_size);
let (gas_left, maybe_address) = ext.create(&gas, &endowment, &contract_code); let can_create = ext.balance(&params.address) >= endowment && ext.depth() < ext.schedule().max_depth;
match maybe_address {
Some(address) => stack.push(address_to_u256(address)), if !can_create {
None => stack.push(U256::zero()) stack.push(U256::zero());
return Ok(InstructionResult::Ok);
} }
return Ok(InstructionResult::AdditionalGasCost(
gas - gas_left let create_result = ext.create(&gas, &endowment, &contract_code);
)); return match create_result {
ContractCreateResult::Created(address, gas_left) => {
stack.push(address_to_u256(address));
Ok(InstructionResult::UnusedGas(gas - gas_left))
},
ContractCreateResult::Failed => {
stack.push(U256::zero());
// TODO [todr] Should we just StopExecution here?
Ok(InstructionResult::UseAllGas)
}
};
}, },
instructions::CALL | instructions::CALLCODE | instructions::DELEGATECALL => { instructions::CALL | instructions::CALLCODE | instructions::DELEGATECALL => {
assert!(ext.schedule().call_value_transfer_gas > ext.schedule().call_stipend, "overflow possible");
let call_gas = stack.pop_back(); let call_gas = stack.pop_back();
let code_address = stack.pop_back(); let code_address = stack.pop_back();
let code_address = u256_to_address(&code_address); let code_address = u256_to_address(&code_address);
let is_delegatecall = instruction == instructions::DELEGATECALL;
let value = if instruction == instructions::DELEGATECALL { let value = match is_delegatecall {
params.value true => params.value,
} else { false => stack.pop_back()
stack.pop_back()
}; };
let address = if instruction == instructions::CALL { let address = match instruction == instructions::CALL {
&code_address true => &code_address,
} else { false => &params.address
&params.address
}; };
let in_off = stack.pop_back(); let in_off = stack.pop_back();
@ -585,23 +620,37 @@ impl Interpreter {
let out_off = stack.pop_back(); let out_off = stack.pop_back();
let out_size = stack.pop_back(); let out_size = stack.pop_back();
let (gas_left, call_successful) = { let call_gas = call_gas + match !is_delegatecall && value > U256::zero() {
true => U256::from(ext.schedule().call_stipend),
false => U256::zero()
};
let can_call = (is_delegatecall || ext.balance(&params.address) >= value) && ext.depth() < ext.schedule().max_depth;
if !can_call {
stack.push(U256::zero());
return Ok(InstructionResult::UnusedGas(call_gas));
}
let call_result = {
// we need to write and read from memory in the same time // we need to write and read from memory in the same time
// and we don't want to copy // and we don't want to copy
let input = unsafe { ::std::mem::transmute(mem.read_slice(in_off, in_size)) }; let input = unsafe { ::std::mem::transmute(mem.read_slice(in_off, in_size)) };
let output = mem.writeable_slice(out_off, out_size); let output = mem.writeable_slice(out_off, out_size);
try!( ext.call(&call_gas, address, &value, input, &code_address, output)
ext.call(&gas, &call_gas, address, &value, input, &code_address, output) };
)
return match call_result {
MessageCallResult::Success(gas_left) => {
println!("Unused: {}", gas_left);
stack.push(U256::one());
Ok(InstructionResult::UnusedGas(gas_left))
},
MessageCallResult::Failed => {
stack.push(U256::zero());
Ok(InstructionResult::Ok)
}
}; };
if call_successful {
stack.push(U256::one());
} else {
stack.push(U256::zero());
}
return Ok(InstructionResult::AdditionalGasCost(
gas - gas_left
));
}, },
instructions::RETURN => { instructions::RETURN => {
let init_off = stack.pop_back(); let init_off = stack.pop_back();
@ -665,9 +714,15 @@ impl Interpreter {
stack.push(word); stack.push(word);
}, },
instructions::SSTORE => { instructions::SSTORE => {
let key = H256::from(&stack.pop_back()); let address = H256::from(&stack.pop_back());
let word = H256::from(&stack.pop_back()); let val = stack.pop_back();
ext.set_storage_at(key, word);
let current_val = U256::from(ext.storage_at(&address).as_slice());
// Increase refund for clear
if !self.is_zero(&current_val) && self.is_zero(&val) {
ext.inc_sstore_clears();
}
ext.set_storage(address, H256::from(&val));
}, },
instructions::PC => { instructions::PC => {
stack.push(U256::from(code.position - 1)); stack.push(U256::from(code.position - 1));
@ -755,7 +810,7 @@ impl Interpreter {
try!(self.exec_stack_instruction(instruction, stack)); try!(self.exec_stack_instruction(instruction, stack));
} }
}; };
Ok(InstructionResult::AdditionalGasCost(U256::zero())) Ok(InstructionResult::Ok)
} }
fn copy_data_to_memory(&self, fn copy_data_to_memory(&self,
@ -769,10 +824,9 @@ impl Interpreter {
if index < U256::from(data_size) { if index < U256::from(data_size) {
let u_index = index.low_u64() as usize; let u_index = index.low_u64() as usize;
let bound_size = if size + index > U256::from(data_size) { let bound_size = match size + index > U256::from(data_size) {
data_size true => data_size,
} else { false => size.low_u64() as usize + u_index
size.low_u64() as usize + u_index
}; };
mem.write_slice(offset, &data[u_index..bound_size]); mem.write_slice(offset, &data[u_index..bound_size]);
@ -801,10 +855,9 @@ impl Interpreter {
} }
fn verify_gas(&self, current_gas: &U256, gas_cost: &U256) -> Result<(), evm::Error> { fn verify_gas(&self, current_gas: &U256, gas_cost: &U256) -> Result<(), evm::Error> {
if current_gas < gas_cost { match current_gas < gas_cost {
Err(evm::Error::OutOfGas) true => Err(evm::Error::OutOfGas),
} else { false => Ok(())
Ok(())
} }
} }
@ -867,21 +920,23 @@ impl Interpreter {
instructions::DIV => { instructions::DIV => {
let a = stack.pop_back(); let a = stack.pop_back();
let b = stack.pop_back(); let b = stack.pop_back();
stack.push(if !self.is_zero(&b) { stack.push(match !self.is_zero(&b) {
let (c, _overflow) = a.overflowing_div(b); true => {
c let (c, _overflow) = a.overflowing_div(b);
} else { c
U256::zero() },
false => U256::zero()
}); });
}, },
instructions::MOD => { instructions::MOD => {
let a = stack.pop_back(); let a = stack.pop_back();
let b = stack.pop_back(); let b = stack.pop_back();
stack.push(if !self.is_zero(&b) { stack.push(match !self.is_zero(&b) {
let (c, _overflow) = a.overflowing_rem(b); true => {
c let (c, _overflow) = a.overflowing_rem(b);
} else { c
U256::zero() },
false => U256::zero()
}); });
}, },
instructions::SDIV => { instructions::SDIV => {
@ -979,10 +1034,9 @@ impl Interpreter {
instructions::BYTE => { instructions::BYTE => {
let word = stack.pop_back(); let word = stack.pop_back();
let val = stack.pop_back(); let val = stack.pop_back();
let byte = if word < U256::from(32) { let byte = match word < U256::from(32) {
(val >> (8 * (31 - word.low_u64() as usize))) & U256::from(0xff) true => (val >> (8 * (31 - word.low_u64() as usize))) & U256::from(0xff),
} else { false => U256::zero()
U256::zero()
}; };
stack.push(byte); stack.push(byte);
}, },

View File

@ -3,43 +3,6 @@ use common::*;
use evmjit; use evmjit;
use evm; use evm;
/// Ethcore representation of evmjit runtime data.
struct RuntimeData {
gas: U256,
gas_price: U256,
call_data: Bytes,
address: Address,
caller: Address,
origin: Address,
call_value: U256,
author: Address,
difficulty: U256,
gas_limit: U256,
number: u64,
timestamp: u64,
code: Bytes
}
impl RuntimeData {
fn new() -> RuntimeData {
RuntimeData {
gas: U256::zero(),
gas_price: U256::zero(),
call_data: vec![],
address: Address::new(),
caller: Address::new(),
origin: Address::new(),
call_value: U256::zero(),
author: Address::new(),
difficulty: U256::zero(),
gas_limit: U256::zero(),
number: 0,
timestamp: 0,
code: vec![]
}
}
}
/// Should be used to convert jit types to ethcore /// Should be used to convert jit types to ethcore
trait FromJit<T>: Sized { trait FromJit<T>: Sized {
fn from_jit(input: T) -> Self; fn from_jit(input: T) -> Self;
@ -126,64 +89,42 @@ impl IntoJit<evmjit::H256> for Address {
} }
} }
impl IntoJit<evmjit::RuntimeDataHandle> for RuntimeData {
fn into_jit(self) -> evmjit::RuntimeDataHandle {
let mut data = evmjit::RuntimeDataHandle::new();
assert!(self.gas <= U256::from(u64::max_value()), "evmjit gas must be lower than 2 ^ 64");
assert!(self.gas_price <= U256::from(u64::max_value()), "evmjit gas_price must be lower than 2 ^ 64");
data.gas = self.gas.low_u64() as i64;
data.gas_price = self.gas_price.low_u64() as i64;
data.call_data = self.call_data.as_ptr();
data.call_data_size = self.call_data.len() as u64;
mem::forget(self.call_data);
data.address = self.address.into_jit();
data.caller = self.caller.into_jit();
data.origin = self.origin.into_jit();
data.call_value = self.call_value.into_jit();
data.author = self.author.into_jit();
data.difficulty = self.difficulty.into_jit();
data.gas_limit = self.gas_limit.into_jit();
data.number = self.number;
data.timestamp = self.timestamp as i64;
data.code = self.code.as_ptr();
data.code_size = self.code.len() as u64;
data.code_hash = self.code.sha3().into_jit();
mem::forget(self.code);
data
}
}
/// Externalities adapter. Maps callbacks from evmjit to externalities trait. /// Externalities adapter. Maps callbacks from evmjit to externalities trait.
/// ///
/// Evmjit doesn't have to know about children execution failures. /// Evmjit doesn't have to know about children execution failures.
/// This adapter 'catches' them and moves upstream. /// This adapter 'catches' them and moves upstream.
struct ExtAdapter<'a> { struct ExtAdapter<'a> {
ext: &'a mut evm::Ext, ext: &'a mut evm::Ext,
err: &'a mut Option<evm::Error> address: Address
} }
impl<'a> ExtAdapter<'a> { impl<'a> ExtAdapter<'a> {
fn new(ext: &'a mut evm::Ext, err: &'a mut Option<evm::Error>) -> Self { fn new(ext: &'a mut evm::Ext, address: Address) -> Self {
ExtAdapter { ExtAdapter {
ext: ext, ext: ext,
err: err address: address
} }
} }
} }
impl<'a> evmjit::Ext for ExtAdapter<'a> { impl<'a> evmjit::Ext for ExtAdapter<'a> {
fn sload(&self, index: *const evmjit::I256, out_value: *mut evmjit::I256) { fn sload(&self, key: *const evmjit::I256, out_value: *mut evmjit::I256) {
unsafe { unsafe {
let i = H256::from_jit(&*index); let i = H256::from_jit(&*key);
let o = self.ext.storage_at(&i); let o = self.ext.storage_at(&i);
*out_value = o.into_jit(); *out_value = o.into_jit();
} }
} }
fn sstore(&mut self, index: *const evmjit::I256, value: *const evmjit::I256) { fn sstore(&mut self, key: *const evmjit::I256, value: *const evmjit::I256) {
unsafe { let key = unsafe { H256::from_jit(&*key) };
self.ext.set_storage_at(H256::from_jit(&*index), H256::from_jit(&*value)); let value = unsafe { H256::from_jit(&*value) };
let old_value = self.ext.storage_at(&key);
// if SSTORE nonzero -> zero, increment refund count
if !old_value.is_zero() && value.is_zero() {
self.ext.inc_sstore_clears();
} }
self.ext.set_storage(key, value);
} }
fn balance(&self, address: *const evmjit::H256, out_value: *mut evmjit::I256) { fn balance(&self, address: *const evmjit::H256, out_value: *mut evmjit::I256) {
@ -204,17 +145,29 @@ impl<'a> evmjit::Ext for ExtAdapter<'a> {
fn create(&mut self, fn create(&mut self,
io_gas: *mut u64, io_gas: *mut u64,
endowment: *const evmjit::I256, value: *const evmjit::I256,
init_beg: *const u8, init_beg: *const u8,
init_size: u64, init_size: u64,
address: *mut evmjit::H256) { address: *mut evmjit::H256) {
unsafe {
let (gas_left, opt_addr) = self.ext.create(&U256::from(*io_gas), &U256::from_jit(&*endowment), slice::from_raw_parts(init_beg, init_size as usize)); let gas = unsafe { U256::from(*io_gas) };
*io_gas = gas_left.low_u64(); let value = unsafe { U256::from_jit(&*value) };
*address = match opt_addr { let code = unsafe { slice::from_raw_parts(init_beg, init_size as usize) };
Some(addr) => addr.into_jit(),
_ => Address::new().into_jit() // check if balance is sufficient and we are not too deep
}; if self.ext.balance(&self.address) >= value && self.ext.depth() < self.ext.schedule().max_depth {
match self.ext.create(&gas, &value, code) {
evm::ContractCreateResult::Created(new_address, gas_left) => unsafe {
*address = new_address.into_jit();
*io_gas = gas_left.low_u64();
},
evm::ContractCreateResult::Failed => unsafe {
*address = Address::new().into_jit();
*io_gas = 0;
}
}
} else {
unsafe { *address = Address::new().into_jit(); }
} }
} }
@ -228,31 +181,56 @@ impl<'a> evmjit::Ext for ExtAdapter<'a> {
out_beg: *mut u8, out_beg: *mut u8,
out_size: u64, out_size: u64,
code_address: *const evmjit::H256) -> bool { code_address: *const evmjit::H256) -> bool {
unsafe {
let res = self.ext.call(&U256::from(*io_gas), let mut gas = unsafe { U256::from(*io_gas) };
&U256::from(call_gas), let mut call_gas = U256::from(call_gas);
&Address::from_jit(&*receive_address), let mut gas_cost = call_gas;
&U256::from_jit(&*value), let receive_address = unsafe { Address::from_jit(&*receive_address) };
slice::from_raw_parts(in_beg, in_size as usize), let code_address = unsafe { Address::from_jit(&*code_address) };
&Address::from_jit(&*code_address), let value = unsafe { U256::from_jit(&*value) };
slice::from_raw_parts_mut(out_beg, out_size as usize));
match res { // receive address and code address are the same in normal calls
Ok((gas_left, ok)) => { let is_callcode = receive_address != code_address;
*io_gas = gas_left.low_u64(); if !is_callcode && !self.ext.exists(&code_address) {
ok gas_cost = gas_cost + U256::from(self.ext.schedule().call_new_account_gas);
} }
Err(evm::Error::OutOfGas) => {
// hack to propagate out_of_gas to evmjit. if value > U256::zero() {
// must be negative assert!(self.ext.schedule().call_value_transfer_gas > self.ext.schedule().call_stipend, "overflow possible");
*io_gas = -1i64 as u64; gas_cost = gas_cost + U256::from(self.ext.schedule().call_value_transfer_gas);
false call_gas = call_gas + U256::from(self.ext.schedule().call_stipend);
}, }
Err(err) => {
// internal error. if gas_cost > gas {
*self.err = Some(err); unsafe {
*io_gas = -1i64 as u64; *io_gas = -1i64 as u64;
false return false;
} }
}
gas = gas - gas_cost;
// check if balance is sufficient and we are not too deep
if self.ext.balance(&self.address) < value || self.ext.depth() >= self.ext.schedule().max_depth {
unsafe {
*io_gas = (gas + call_gas).low_u64();
return false;
}
}
match self.ext.call(&call_gas,
&receive_address,
&value,
unsafe { slice::from_raw_parts(in_beg, in_size as usize) },
&code_address,
unsafe { slice::from_raw_parts_mut(out_beg, out_size as usize) }) {
evm::MessageCallResult::Success(gas_left) => unsafe {
*io_gas = (gas + gas_left).low_u64();
true
},
evm::MessageCallResult::Failed => unsafe {
*io_gas = gas.low_u64();
false
} }
} }
} }
@ -302,35 +280,41 @@ impl<'a> evmjit::Ext for ExtAdapter<'a> {
pub struct JitEvm; pub struct JitEvm;
impl evm::Evm for JitEvm { impl evm::Evm for JitEvm {
fn exec(&self, params: &ActionParams, ext: &mut evm::Ext) -> evm::Result { fn exec(&self, params: ActionParams, ext: &mut evm::Ext) -> evm::Result {
let mut optional_err = None;
// Dirty hack. This is unsafe, but we interact with ffi, so it's justified. // Dirty hack. This is unsafe, but we interact with ffi, so it's justified.
let ext_adapter: ExtAdapter<'static> = unsafe { ::std::mem::transmute(ExtAdapter::new(ext, &mut optional_err)) }; let ext_adapter: ExtAdapter<'static> = unsafe { ::std::mem::transmute(ExtAdapter::new(ext, params.address.clone())) };
let mut ext_handle = evmjit::ExtHandle::new(ext_adapter); let mut ext_handle = evmjit::ExtHandle::new(ext_adapter);
let mut data = RuntimeData::new(); assert!(params.gas <= U256::from(i64::max_value() as u64), "evmjit max gas is 2 ^ 63");
data.gas = params.gas; assert!(params.gas_price <= U256::from(i64::max_value() as u64), "evmjit max gas is 2 ^ 63");
data.gas_price = params.gas_price;
data.call_data = params.data.clone().unwrap_or(vec![]);
data.address = params.address.clone();
data.caller = params.sender.clone();
data.origin = params.origin.clone();
data.call_value = params.value;
data.code = params.code.clone().unwrap_or(vec![]);
data.author = ext.env_info().author.clone(); let call_data = params.data.unwrap_or(vec![]);
data.difficulty = ext.env_info().difficulty; let code = params.code.unwrap_or(vec![]);
data.gas_limit = ext.env_info().gas_limit;
let mut data = evmjit::RuntimeDataHandle::new();
data.gas = params.gas.low_u64() as i64;
data.gas_price = params.gas_price.low_u64() as i64;
data.call_data = call_data.as_ptr();
data.call_data_size = call_data.len() as u64;
mem::forget(call_data);
data.code = code.as_ptr();
data.code_size = code.len() as u64;
data.code_hash = code.sha3().into_jit();
mem::forget(code);
data.address = params.address.into_jit();
data.caller = params.sender.into_jit();
data.origin = params.origin.into_jit();
data.call_value = params.value.into_jit();
data.author = ext.env_info().author.clone().into_jit();
data.difficulty = ext.env_info().difficulty.into_jit();
data.gas_limit = ext.env_info().gas_limit.into_jit();
data.number = ext.env_info().number; data.number = ext.env_info().number;
data.timestamp = ext.env_info().timestamp; // don't really know why jit timestamp is int..
data.timestamp = ext.env_info().timestamp as i64;
let mut context = unsafe { evmjit::ContextHandle::new(data.into_jit(), &mut ext_handle) };
let mut context = unsafe { evmjit::ContextHandle::new(data, &mut ext_handle) };
let res = context.exec(); let res = context.exec();
// check in adapter if execution of children contracts failed.
if let Some(err) = optional_err {
return Err(err);
}
match res { match res {
evmjit::ReturnCode::Stop => Ok(U256::from(context.gas_left())), evmjit::ReturnCode::Stop => Ok(U256::from(context.gas_left())),
evmjit::ReturnCode::Return => ext.ret(&U256::from(context.gas_left()), context.output_data()), evmjit::ReturnCode::Return => ext.ret(&U256::from(context.gas_left()), context.output_data()),

View File

@ -14,7 +14,7 @@ mod jit;
mod tests; mod tests;
pub use self::evm::{Evm, Error, Result}; pub use self::evm::{Evm, Error, Result};
pub use self::ext::{Ext}; pub use self::ext::{Ext, ContractCreateResult, MessageCallResult};
pub use self::factory::Factory; pub use self::factory::Factory;
pub use self::schedule::Schedule; pub use self::schedule::Schedule;
pub use self::factory::VMType; pub use self::factory::VMType;

View File

@ -1,6 +1,6 @@
use common::*; use common::*;
use evm; use evm;
use evm::{Ext, Schedule, Factory, VMType}; use evm::{Ext, Schedule, Factory, VMType, ContractCreateResult, MessageCallResult};
struct FakeLogEntry { struct FakeLogEntry {
topics: Vec<H256>, topics: Vec<H256>,
@ -39,10 +39,14 @@ impl Ext for FakeExt {
self.store.get(key).unwrap_or(&H256::new()).clone() self.store.get(key).unwrap_or(&H256::new()).clone()
} }
fn set_storage_at(&mut self, key: H256, value: H256) { fn set_storage(&mut self, key: H256, value: H256) {
self.store.insert(key, value); self.store.insert(key, value);
} }
fn exists(&self, _address: &Address) -> bool {
unimplemented!();
}
fn balance(&self, _address: &Address) -> U256 { fn balance(&self, _address: &Address) -> U256 {
unimplemented!(); unimplemented!();
} }
@ -51,18 +55,17 @@ impl Ext for FakeExt {
self.blockhashes.get(number).unwrap_or(&H256::new()).clone() self.blockhashes.get(number).unwrap_or(&H256::new()).clone()
} }
fn create(&mut self, _gas: &U256, _value: &U256, _code: &[u8]) -> (U256, Option<Address>) { fn create(&mut self, _gas: &U256, _value: &U256, _code: &[u8]) -> ContractCreateResult {
unimplemented!(); unimplemented!();
} }
fn call(&mut self, fn call(&mut self,
_gas: &U256, _gas: &U256,
_call_gas: &U256, _address: &Address,
_receive_address: &Address,
_value: &U256, _value: &U256,
_data: &[u8], _data: &[u8],
_code_address: &Address, _code_address: &Address,
_output: &mut [u8]) -> result::Result<(U256, bool), evm::Error> { _output: &mut [u8]) -> MessageCallResult {
unimplemented!(); unimplemented!();
} }
@ -92,10 +95,17 @@ impl Ext for FakeExt {
fn env_info(&self) -> &EnvInfo { fn env_info(&self) -> &EnvInfo {
&self.info &self.info
} }
fn depth(&self) -> usize {
unimplemented!();
}
fn inc_sstore_clears(&mut self) {
unimplemented!();
}
} }
#[test] #[test]
fn test_stack_underflow() { fn test_stack_underflow() {
let address = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap(); let address = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap();
let code = "01600055".from_hex().unwrap(); let code = "01600055".from_hex().unwrap();
@ -108,7 +118,7 @@ fn test_stack_underflow() {
let err = { let err = {
let vm : Box<evm::Evm> = Box::new(super::interpreter::Interpreter); let vm : Box<evm::Evm> = Box::new(super::interpreter::Interpreter);
vm.exec(&params, &mut ext).unwrap_err() vm.exec(params, &mut ext).unwrap_err()
}; };
match err { match err {
@ -135,7 +145,7 @@ fn test_add(factory: super::Factory) {
let gas_left = { let gas_left = {
let vm = factory.create(); let vm = factory.create();
vm.exec(&params, &mut ext).unwrap() vm.exec(params, &mut ext).unwrap()
}; };
assert_eq!(gas_left, U256::from(79_988)); assert_eq!(gas_left, U256::from(79_988));
@ -155,7 +165,7 @@ fn test_sha3(factory: super::Factory) {
let gas_left = { let gas_left = {
let vm = factory.create(); let vm = factory.create();
vm.exec(&params, &mut ext).unwrap() vm.exec(params, &mut ext).unwrap()
}; };
assert_eq!(gas_left, U256::from(79_961)); assert_eq!(gas_left, U256::from(79_961));
@ -175,7 +185,7 @@ fn test_address(factory: super::Factory) {
let gas_left = { let gas_left = {
let vm = factory.create(); let vm = factory.create();
vm.exec(&params, &mut ext).unwrap() vm.exec(params, &mut ext).unwrap()
}; };
assert_eq!(gas_left, U256::from(79_995)); assert_eq!(gas_left, U256::from(79_995));
@ -197,7 +207,7 @@ fn test_origin(factory: super::Factory) {
let gas_left = { let gas_left = {
let vm = factory.create(); let vm = factory.create();
vm.exec(&params, &mut ext).unwrap() vm.exec(params, &mut ext).unwrap()
}; };
assert_eq!(gas_left, U256::from(79_995)); assert_eq!(gas_left, U256::from(79_995));
@ -219,7 +229,7 @@ fn test_sender(factory: super::Factory) {
let gas_left = { let gas_left = {
let vm = factory.create(); let vm = factory.create();
vm.exec(&params, &mut ext).unwrap() vm.exec(params, &mut ext).unwrap()
}; };
assert_eq!(gas_left, U256::from(79_995)); assert_eq!(gas_left, U256::from(79_995));
@ -254,7 +264,7 @@ fn test_extcodecopy(factory: super::Factory) {
let gas_left = { let gas_left = {
let vm = factory.create(); let vm = factory.create();
vm.exec(&params, &mut ext).unwrap() vm.exec(params, &mut ext).unwrap()
}; };
assert_eq!(gas_left, U256::from(79_935)); assert_eq!(gas_left, U256::from(79_935));
@ -274,7 +284,7 @@ fn test_log_empty(factory: super::Factory) {
let gas_left = { let gas_left = {
let vm = factory.create(); let vm = factory.create();
vm.exec(&params, &mut ext).unwrap() vm.exec(params, &mut ext).unwrap()
}; };
assert_eq!(gas_left, U256::from(99_619)); assert_eq!(gas_left, U256::from(99_619));
@ -306,7 +316,7 @@ fn test_log_sender(factory: super::Factory) {
let gas_left = { let gas_left = {
let vm = factory.create(); let vm = factory.create();
vm.exec(&params, &mut ext).unwrap() vm.exec(params, &mut ext).unwrap()
}; };
assert_eq!(gas_left, U256::from(98_974)); assert_eq!(gas_left, U256::from(98_974));
@ -331,7 +341,7 @@ fn test_blockhash(factory: super::Factory) {
let gas_left = { let gas_left = {
let vm = factory.create(); let vm = factory.create();
vm.exec(&params, &mut ext).unwrap() vm.exec(params, &mut ext).unwrap()
}; };
assert_eq!(gas_left, U256::from(79_974)); assert_eq!(gas_left, U256::from(79_974));
@ -353,7 +363,7 @@ fn test_calldataload(factory: super::Factory) {
let gas_left = { let gas_left = {
let vm = factory.create(); let vm = factory.create();
vm.exec(&params, &mut ext).unwrap() vm.exec(params, &mut ext).unwrap()
}; };
assert_eq!(gas_left, U256::from(79_991)); assert_eq!(gas_left, U256::from(79_991));
@ -374,7 +384,7 @@ fn test_author(factory: super::Factory) {
let gas_left = { let gas_left = {
let vm = factory.create(); let vm = factory.create();
vm.exec(&params, &mut ext).unwrap() vm.exec(params, &mut ext).unwrap()
}; };
assert_eq!(gas_left, U256::from(79_995)); assert_eq!(gas_left, U256::from(79_995));
@ -394,7 +404,7 @@ fn test_timestamp(factory: super::Factory) {
let gas_left = { let gas_left = {
let vm = factory.create(); let vm = factory.create();
vm.exec(&params, &mut ext).unwrap() vm.exec(params, &mut ext).unwrap()
}; };
assert_eq!(gas_left, U256::from(79_995)); assert_eq!(gas_left, U256::from(79_995));
@ -414,7 +424,7 @@ fn test_number(factory: super::Factory) {
let gas_left = { let gas_left = {
let vm = factory.create(); let vm = factory.create();
vm.exec(&params, &mut ext).unwrap() vm.exec(params, &mut ext).unwrap()
}; };
assert_eq!(gas_left, U256::from(79_995)); assert_eq!(gas_left, U256::from(79_995));
@ -434,7 +444,7 @@ fn test_difficulty(factory: super::Factory) {
let gas_left = { let gas_left = {
let vm = factory.create(); let vm = factory.create();
vm.exec(&params, &mut ext).unwrap() vm.exec(params, &mut ext).unwrap()
}; };
assert_eq!(gas_left, U256::from(79_995)); assert_eq!(gas_left, U256::from(79_995));
@ -454,7 +464,7 @@ fn test_gas_limit(factory: super::Factory) {
let gas_left = { let gas_left = {
let vm = factory.create(); let vm = factory.create();
vm.exec(&params, &mut ext).unwrap() vm.exec(params, &mut ext).unwrap()
}; };
assert_eq!(gas_left, U256::from(79_995)); assert_eq!(gas_left, U256::from(79_995));

View File

@ -75,8 +75,8 @@ impl<'a> Executive<'a> {
} }
/// Creates `Externalities` from `Executive`. /// Creates `Externalities` from `Executive`.
pub fn to_externalities<'_>(&'_ mut self, params: &'_ ActionParams, substate: &'_ mut Substate, output: OutputPolicy<'_>) -> Externalities { pub fn to_externalities<'_>(&'_ mut self, origin_info: OriginInfo, substate: &'_ mut Substate, output: OutputPolicy<'_>) -> Externalities {
Externalities::new(self.state, self.info, self.engine, self.depth, params, substate, output) Externalities::new(self.state, self.info, self.engine, self.depth, origin_info, substate, output)
} }
/// This funtion should be used to execute transaction. /// This funtion should be used to execute transaction.
@ -137,7 +137,7 @@ impl<'a> Executive<'a> {
code: Some(t.data.clone()), code: Some(t.data.clone()),
data: None, data: None,
}; };
self.create(&params, &mut substate) self.create(params, &mut substate)
}, },
&Action::Call(ref address) => { &Action::Call(ref address) => {
let params = ActionParams { let params = ActionParams {
@ -153,7 +153,7 @@ impl<'a> Executive<'a> {
}; };
// TODO: move output upstream // TODO: move output upstream
let mut out = vec![]; let mut out = vec![];
self.call(&params, &mut substate, BytesRef::Flexible(&mut out)) self.call(params, &mut substate, BytesRef::Flexible(&mut out))
} }
}; };
@ -165,7 +165,7 @@ impl<'a> Executive<'a> {
/// NOTE. It does not finalize the transaction (doesn't do refunds, nor suicides). /// NOTE. It does not finalize the transaction (doesn't do refunds, nor suicides).
/// Modifies the substate and the output. /// Modifies the substate and the output.
/// Returns either gas_left or `evm::Error`. /// Returns either gas_left or `evm::Error`.
pub fn call(&mut self, params: &ActionParams, substate: &mut Substate, mut output: BytesRef) -> evm::Result { pub fn call(&mut self, params: ActionParams, substate: &mut Substate, mut output: BytesRef) -> evm::Result {
// backup used in case of running out of gas // backup used in case of running out of gas
let backup = self.state.clone(); let backup = self.state.clone();
@ -198,11 +198,11 @@ impl<'a> Executive<'a> {
let mut unconfirmed_substate = Substate::new(); let mut unconfirmed_substate = Substate::new();
let res = { let res = {
let mut ext = self.to_externalities(params, &mut unconfirmed_substate, OutputPolicy::Return(output)); let mut ext = self.to_externalities(OriginInfo::from(&params), &mut unconfirmed_substate, OutputPolicy::Return(output));
self.engine.vm_factory().create().exec(&params, &mut ext) self.engine.vm_factory().create().exec(params, &mut ext)
}; };
trace!("exec: sstore-clears={}\n", unconfirmed_substate.refunds_count); trace!("exec: sstore-clears={}\n", unconfirmed_substate.sstore_clears_count);
trace!("exec: substate={:?}; unconfirmed_substate={:?}\n", substate, unconfirmed_substate); trace!("exec: substate={:?}; unconfirmed_substate={:?}\n", substate, unconfirmed_substate);
self.enact_result(&res, substate, unconfirmed_substate, backup); self.enact_result(&res, substate, unconfirmed_substate, backup);
trace!("exec: new substate={:?}\n", substate); trace!("exec: new substate={:?}\n", substate);
@ -216,7 +216,7 @@ impl<'a> Executive<'a> {
/// Creates contract with given contract params. /// Creates contract with given contract params.
/// NOTE. It does not finalize the transaction (doesn't do refunds, nor suicides). /// NOTE. It does not finalize the transaction (doesn't do refunds, nor suicides).
/// Modifies the substate. /// Modifies the substate.
pub fn create(&mut self, params: &ActionParams, substate: &mut Substate) -> evm::Result { pub fn create(&mut self, params: ActionParams, substate: &mut Substate) -> evm::Result {
// backup used in case of running out of gas // backup used in case of running out of gas
let backup = self.state.clone(); let backup = self.state.clone();
@ -230,8 +230,8 @@ impl<'a> Executive<'a> {
self.state.transfer_balance(&params.sender, &params.address, &params.value); self.state.transfer_balance(&params.sender, &params.address, &params.value);
let res = { let res = {
let mut ext = self.to_externalities(params, &mut unconfirmed_substate, OutputPolicy::InitContract); let mut ext = self.to_externalities(OriginInfo::from(&params), &mut unconfirmed_substate, OutputPolicy::InitContract);
self.engine.vm_factory().create().exec(&params, &mut ext) self.engine.vm_factory().create().exec(params, &mut ext)
}; };
self.enact_result(&res, substate, unconfirmed_substate, backup); self.enact_result(&res, substate, unconfirmed_substate, backup);
res res
@ -242,7 +242,7 @@ impl<'a> Executive<'a> {
let schedule = self.engine.schedule(self.info); let schedule = self.engine.schedule(self.info);
// refunds from SSTORE nonzero -> zero // refunds from SSTORE nonzero -> zero
let sstore_refunds = U256::from(schedule.sstore_refund_gas) * substate.refunds_count; let sstore_refunds = U256::from(schedule.sstore_refund_gas) * substate.sstore_clears_count;
// refunds from contract suicides // refunds from contract suicides
let suicide_refunds = U256::from(schedule.suicide_refund_gas) * U256::from(substate.suicides.len()); let suicide_refunds = U256::from(schedule.suicide_refund_gas) * U256::from(substate.suicides.len());
let refunds_bound = sstore_refunds + suicide_refunds; let refunds_bound = sstore_refunds + suicide_refunds;
@ -381,7 +381,7 @@ mod tests {
let gas_left = { let gas_left = {
let mut ex = Executive::new(&mut state, &info, &engine); let mut ex = Executive::new(&mut state, &info, &engine);
ex.create(&params, &mut substate).unwrap() ex.create(params, &mut substate).unwrap()
}; };
assert_eq!(gas_left, U256::from(79_975)); assert_eq!(gas_left, U256::from(79_975));
@ -439,7 +439,7 @@ mod tests {
let gas_left = { let gas_left = {
let mut ex = Executive::new(&mut state, &info, &engine); let mut ex = Executive::new(&mut state, &info, &engine);
ex.create(&params, &mut substate).unwrap() ex.create(params, &mut substate).unwrap()
}; };
assert_eq!(gas_left, U256::from(62_976)); assert_eq!(gas_left, U256::from(62_976));
@ -492,7 +492,7 @@ mod tests {
let gas_left = { let gas_left = {
let mut ex = Executive::new(&mut state, &info, &engine); let mut ex = Executive::new(&mut state, &info, &engine);
ex.create(&params, &mut substate).unwrap() ex.create(params, &mut substate).unwrap()
}; };
assert_eq!(gas_left, U256::from(62_976)); assert_eq!(gas_left, U256::from(62_976));
@ -543,7 +543,7 @@ mod tests {
{ {
let mut ex = Executive::new(&mut state, &info, &engine); let mut ex = Executive::new(&mut state, &info, &engine);
ex.create(&params, &mut substate).unwrap(); ex.create(params, &mut substate).unwrap();
} }
assert_eq!(substate.contracts_created.len(), 1); assert_eq!(substate.contracts_created.len(), 1);
@ -602,7 +602,7 @@ mod tests {
let gas_left = { let gas_left = {
let mut ex = Executive::new(&mut state, &info, &engine); let mut ex = Executive::new(&mut state, &info, &engine);
ex.call(&params, &mut substate, BytesRef::Fixed(&mut [])).unwrap() ex.call(params, &mut substate, BytesRef::Fixed(&mut [])).unwrap()
}; };
assert_eq!(gas_left, U256::from(73_237)); assert_eq!(gas_left, U256::from(73_237));
@ -645,7 +645,7 @@ mod tests {
let gas_left = { let gas_left = {
let mut ex = Executive::new(&mut state, &info, &engine); let mut ex = Executive::new(&mut state, &info, &engine);
ex.call(&params, &mut substate, BytesRef::Fixed(&mut [])).unwrap() ex.call(params, &mut substate, BytesRef::Fixed(&mut [])).unwrap()
}; };
assert_eq!(gas_left, U256::from(59_870)); assert_eq!(gas_left, U256::from(59_870));
@ -804,7 +804,7 @@ mod tests {
let result = { let result = {
let mut ex = Executive::new(&mut state, &info, &engine); let mut ex = Executive::new(&mut state, &info, &engine);
ex.create(&params, &mut substate) ex.create(params, &mut substate)
}; };
match result { match result {

View File

@ -3,7 +3,7 @@ use common::*;
use state::*; use state::*;
use engine::*; use engine::*;
use executive::*; use executive::*;
use evm::{self, Schedule, Ext}; use evm::{self, Schedule, Ext, ContractCreateResult, MessageCallResult};
use substate::*; use substate::*;
/// Policy for handling output data on `RETURN` opcode. /// Policy for handling output data on `RETURN` opcode.
@ -15,23 +15,31 @@ pub enum OutputPolicy<'a> {
InitContract InitContract
} }
/// Transaction properties that externalities need to know about.
pub struct OriginInfo {
address: Address,
origin: Address,
gas_price: U256
}
impl OriginInfo {
/// Populates origin info from action params.
pub fn from(params: &ActionParams) -> Self {
OriginInfo {
address: params.address.clone(),
origin: params.origin.clone(),
gas_price: params.gas_price.clone()
}
}
}
/// Implementation of evm Externalities. /// Implementation of evm Externalities.
pub struct Externalities<'a> { pub struct Externalities<'a> {
#[cfg(test)]
pub state: &'a mut State,
#[cfg(not(test))]
state: &'a mut State, state: &'a mut State,
env_info: &'a EnvInfo,
info: &'a EnvInfo,
engine: &'a Engine, engine: &'a Engine,
depth: usize, depth: usize,
origin_info: OriginInfo,
#[cfg(test)]
pub params: &'a ActionParams,
#[cfg(not(test))]
params: &'a ActionParams,
substate: &'a mut Substate, substate: &'a mut Substate,
schedule: Schedule, schedule: Schedule,
output: OutputPolicy<'a> output: OutputPolicy<'a>
@ -40,20 +48,20 @@ pub struct Externalities<'a> {
impl<'a> Externalities<'a> { impl<'a> Externalities<'a> {
/// Basic `Externalities` constructor. /// Basic `Externalities` constructor.
pub fn new(state: &'a mut State, pub fn new(state: &'a mut State,
info: &'a EnvInfo, env_info: &'a EnvInfo,
engine: &'a Engine, engine: &'a Engine,
depth: usize, depth: usize,
params: &'a ActionParams, origin_info: OriginInfo,
substate: &'a mut Substate, substate: &'a mut Substate,
output: OutputPolicy<'a>) -> Self { output: OutputPolicy<'a>) -> Self {
Externalities { Externalities {
state: state, state: state,
info: info, env_info: env_info,
engine: engine, engine: engine,
depth: depth, depth: depth,
params: params, origin_info: origin_info,
substate: substate, substate: substate,
schedule: engine.schedule(info), schedule: engine.schedule(env_info),
output: output output: output
} }
} }
@ -61,19 +69,15 @@ impl<'a> Externalities<'a> {
impl<'a> Ext for Externalities<'a> { impl<'a> Ext for Externalities<'a> {
fn storage_at(&self, key: &H256) -> H256 { fn storage_at(&self, key: &H256) -> H256 {
trace!("ext: storage_at({}, {}) == {}\n", self.params.address, key, U256::from(self.state.storage_at(&self.params.address, key).as_slice())); self.state.storage_at(&self.origin_info.address, key)
self.state.storage_at(&self.params.address, key)
} }
fn set_storage_at(&mut self, key: H256, value: H256) { fn set_storage(&mut self, key: H256, value: H256) {
let old = self.state.storage_at(&self.params.address, &key); self.state.set_storage(&self.origin_info.address, key, value)
// if SSTORE nonzero -> zero, increment refund count }
if value.is_zero() && !old.is_zero() {
trace!("ext: additional refund. {} -> {}\n", self.substate.refunds_count, self.substate.refunds_count + x!(1)); fn exists(&self, address: &Address) -> bool {
self.substate.refunds_count = self.substate.refunds_count + U256::one(); self.state.exists(address)
}
trace!("ext: set_storage_at({}, {}): {} -> {}\n", self.params.address, key, U256::from(old.as_slice()), U256::from(value.as_slice()));
self.state.set_storage(&self.params.address, key, value)
} }
fn balance(&self, address: &Address) -> U256 { fn balance(&self, address: &Address) -> U256 {
@ -81,109 +85,75 @@ impl<'a> Ext for Externalities<'a> {
} }
fn blockhash(&self, number: &U256) -> H256 { fn blockhash(&self, number: &U256) -> H256 {
match *number < U256::from(self.info.number) && number.low_u64() >= cmp::max(256, self.info.number) - 256 { match *number < U256::from(self.env_info.number) && number.low_u64() >= cmp::max(256, self.env_info.number) - 256 {
true => { true => {
let index = self.info.number - number.low_u64() - 1; let index = self.env_info.number - number.low_u64() - 1;
let r = self.info.last_hashes[index as usize].clone(); let r = self.env_info.last_hashes[index as usize].clone();
trace!("ext: blockhash({}) -> {} self.info.number={}\n", number, r, self.info.number); trace!("ext: blockhash({}) -> {} self.env_info.number={}\n", number, r, self.env_info.number);
r r
}, },
false => { false => {
trace!("ext: blockhash({}) -> null self.info.number={}\n", number, self.info.number); trace!("ext: blockhash({}) -> null self.env_info.number={}\n", number, self.env_info.number);
H256::from(&U256::zero()) H256::from(&U256::zero())
}, },
} }
} }
fn create(&mut self, gas: &U256, value: &U256, code: &[u8]) -> (U256, Option<Address>) { fn create(&mut self, gas: &U256, value: &U256, code: &[u8]) -> ContractCreateResult {
// if balance is insufficient or we are to deep, return
if self.state.balance(&self.params.address) < *value || self.depth >= self.schedule.max_depth {
return (*gas, None);
}
// create new contract address // create new contract address
let address = contract_address(&self.params.address, &self.state.nonce(&self.params.address)); let address = contract_address(&self.origin_info.address, &self.state.nonce(&self.origin_info.address));
// prepare the params // prepare the params
let params = ActionParams { let params = ActionParams {
code_address: address.clone(), code_address: address.clone(),
address: address.clone(), address: address.clone(),
sender: self.params.address.clone(), sender: self.origin_info.address.clone(),
origin: self.params.origin.clone(), origin: self.origin_info.origin.clone(),
gas: *gas, gas: *gas,
gas_price: self.params.gas_price.clone(), gas_price: self.origin_info.gas_price.clone(),
value: value.clone(), value: value.clone(),
code: Some(code.to_vec()), code: Some(code.to_vec()),
data: None, data: None,
}; };
self.state.inc_nonce(&self.params.address); self.state.inc_nonce(&self.origin_info.address);
let mut ex = Executive::from_parent(self.state, self.info, self.engine, self.depth); let mut ex = Executive::from_parent(self.state, self.env_info, self.engine, self.depth);
match ex.create(&params, self.substate) {
Ok(gas_left) => (gas_left, Some(address)), // TODO: handle internal error separately
_ => (U256::zero(), None) match ex.create(params, self.substate) {
Ok(gas_left) => {
self.substate.contracts_created.push(address.clone());
ContractCreateResult::Created(address, gas_left)
},
_ => ContractCreateResult::Failed
} }
} }
fn call(&mut self, fn call(&mut self,
gas: &U256, gas: &U256,
call_gas: &U256, address: &Address,
receive_address: &Address,
value: &U256, value: &U256,
data: &[u8], data: &[u8],
code_address: &Address, code_address: &Address,
output: &mut [u8]) -> Result<(U256, bool), evm::Error> { output: &mut [u8]) -> MessageCallResult {
let mut gas_cost = *call_gas;
let mut call_gas = *call_gas;
let is_call = receive_address == code_address;
if is_call && !self.state.exists(&code_address) {
gas_cost = gas_cost + U256::from(self.schedule.call_new_account_gas);
}
if *value > U256::zero() {
assert!(self.schedule.call_value_transfer_gas > self.schedule.call_stipend, "overflow possible");
gas_cost = gas_cost + U256::from(self.schedule.call_value_transfer_gas);
call_gas = call_gas + U256::from(self.schedule.call_stipend);
}
debug!("Externalities::call(gas={}, call_gas={}, recv={}, value={}, data={}, code={})\n", gas, call_gas, receive_address, value, data.pretty(), code_address);
if gas_cost > *gas {
debug!("Externalities::call: OutOfGas gas_cost={}, gas={}", gas_cost, gas);
return Err(evm::Error::OutOfGas);
}
let gas = *gas - gas_cost;
// if balance is insufficient or we are too deep, return
if self.state.balance(&self.params.address) < *value || self.depth >= self.schedule.max_depth {
debug!("Externalities::call: OutOfCash bal({})={}, value={}", self.params.address, self.state.balance(&self.params.address), value);
return Ok((gas + call_gas, false));
}
let params = ActionParams { let params = ActionParams {
code_address: code_address.clone(), code_address: code_address.clone(),
address: receive_address.clone(), address: address.clone(),
sender: self.params.address.clone(), sender: self.origin_info.address.clone(),
origin: self.params.origin.clone(), origin: self.origin_info.origin.clone(),
gas: call_gas, gas: *gas,
gas_price: self.params.gas_price.clone(), gas_price: self.origin_info.gas_price.clone(),
value: value.clone(), value: value.clone(),
code: self.state.code(code_address), code: self.state.code(code_address),
data: Some(data.to_vec()), data: Some(data.to_vec()),
}; };
let mut ex = Executive::from_parent(self.state, self.env_info, self.engine, self.depth);
trace!("Externalities::call: BEFORE: bal({})={}, bal({})={}\n", params.sender, self.state.balance(&params.sender), params.address, self.state.balance(&params.address)); match ex.call(params, self.substate, BytesRef::Fixed(output)) {
trace!("Externalities::call: CALLING: params={:?}\n", params); Ok(gas_left) => MessageCallResult::Success(gas_left),
let r = Executive::from_parent(self.state, self.info, self.engine, self.depth).call(&params, self.substate, BytesRef::Fixed(output)); _ => MessageCallResult::Failed
trace!("Externalities::call: AFTER: bal({})={}, bal({})={}\n", params.sender, self.state.balance(&params.sender), params.address, self.state.balance(&params.address));
match r {
Ok(gas_left) => Ok((gas + gas_left, true)),
_ => Ok((gas, false))
} }
} }
@ -219,21 +189,20 @@ impl<'a> Ext for Externalities<'a> {
ptr::copy(data.as_ptr(), code.as_mut_ptr(), data.len()); ptr::copy(data.as_ptr(), code.as_mut_ptr(), data.len());
code.set_len(data.len()); code.set_len(data.len());
} }
let address = &self.params.address; let address = &self.origin_info.address;
self.state.init_code(address, code); self.state.init_code(address, code);
self.substate.contracts_created.push(address.clone());
Ok(*gas - return_cost) Ok(*gas - return_cost)
} }
} }
} }
fn log(&mut self, topics: Vec<H256>, data: &[u8]) { fn log(&mut self, topics: Vec<H256>, data: &[u8]) {
let address = self.params.address.clone(); let address = self.origin_info.address.clone();
self.substate.logs.push(LogEntry::new(address, topics, data.to_vec())); self.substate.logs.push(LogEntry::new(address, topics, data.to_vec()));
} }
fn suicide(&mut self, refund_address: &Address) { fn suicide(&mut self, refund_address: &Address) {
let address = self.params.address.clone(); let address = self.origin_info.address.clone();
let balance = self.balance(&address); let balance = self.balance(&address);
self.state.transfer_balance(&address, refund_address, &balance); self.state.transfer_balance(&address, refund_address, &balance);
self.substate.suicides.insert(address); self.substate.suicides.insert(address);
@ -244,6 +213,14 @@ impl<'a> Ext for Externalities<'a> {
} }
fn env_info(&self) -> &EnvInfo { fn env_info(&self) -> &EnvInfo {
&self.info &self.env_info
}
fn depth(&self) -> usize {
self.depth
}
fn inc_sstore_clears(&mut self) {
self.substate.sstore_clears_count = self.substate.sstore_clears_count + U256::one();
} }
} }

View File

@ -8,8 +8,8 @@ pub struct Substate {
pub suicides: HashSet<Address>, pub suicides: HashSet<Address>,
/// Any logs. /// Any logs.
pub logs: Vec<LogEntry>, pub logs: Vec<LogEntry>,
/// Refund counter of SSTORE nonzero->zero. /// Refund counter of SSTORE nonzero -> zero.
pub refunds_count: U256, pub sstore_clears_count: U256,
/// Created contracts. /// Created contracts.
pub contracts_created: Vec<Address> pub contracts_created: Vec<Address>
} }
@ -20,7 +20,7 @@ impl Substate {
Substate { Substate {
suicides: HashSet::new(), suicides: HashSet::new(),
logs: vec![], logs: vec![],
refunds_count: U256::zero(), sstore_clears_count: U256::zero(),
contracts_created: vec![] contracts_created: vec![]
} }
} }
@ -28,7 +28,7 @@ impl Substate {
pub fn accrue(&mut self, s: Substate) { pub fn accrue(&mut self, s: Substate) {
self.suicides.extend(s.suicides.into_iter()); self.suicides.extend(s.suicides.into_iter());
self.logs.extend(s.logs.into_iter()); self.logs.extend(s.logs.into_iter());
self.refunds_count = self.refunds_count + s.refunds_count; self.sstore_clears_count = self.sstore_clears_count + s.sstore_clears_count;
self.contracts_created.extend(s.contracts_created.into_iter()); self.contracts_created.extend(s.contracts_created.into_iter());
} }
} }

View File

@ -4,7 +4,7 @@ use executive::*;
use spec::*; use spec::*;
use engine::*; use engine::*;
use evm; use evm;
use evm::{Schedule, Ext, Factory, VMType}; use evm::{Schedule, Ext, Factory, VMType, ContractCreateResult, MessageCallResult};
use ethereum; use ethereum;
use externalities::*; use externalities::*;
use substate::*; use substate::*;
@ -39,7 +39,7 @@ impl Engine for TestEngine {
struct CallCreate { struct CallCreate {
data: Bytes, data: Bytes,
destination: Option<Address>, destination: Option<Address>,
_gas_limit: U256, gas_limit: U256,
value: U256 value: U256
} }
@ -47,13 +47,22 @@ struct CallCreate {
/// Stores callcreates. /// Stores callcreates.
struct TestExt<'a> { struct TestExt<'a> {
ext: Externalities<'a>, ext: Externalities<'a>,
callcreates: Vec<CallCreate> callcreates: Vec<CallCreate>,
contract_address: Address
} }
impl<'a> TestExt<'a> { impl<'a> TestExt<'a> {
fn new(ext: Externalities<'a>) -> TestExt { fn new(state: &'a mut State,
info: &'a EnvInfo,
engine: &'a Engine,
depth: usize,
origin_info: OriginInfo,
substate: &'a mut Substate,
output: OutputPolicy<'a>,
address: Address) -> Self {
TestExt { TestExt {
ext: ext, contract_address: contract_address(&address, &state.nonce(&address)),
ext: Externalities::new(state, info, engine, depth, origin_info, substate, output),
callcreates: vec![] callcreates: vec![]
} }
} }
@ -64,8 +73,12 @@ impl<'a> Ext for TestExt<'a> {
self.ext.storage_at(key) self.ext.storage_at(key)
} }
fn set_storage_at(&mut self, key: H256, value: H256) { fn set_storage(&mut self, key: H256, value: H256) {
self.ext.set_storage_at(key, value) self.ext.set_storage(key, value)
}
fn exists(&self, address: &Address) -> bool {
self.ext.exists(address)
} }
fn balance(&self, address: &Address) -> U256 { fn balance(&self, address: &Address) -> U256 {
@ -76,60 +89,30 @@ impl<'a> Ext for TestExt<'a> {
self.ext.blockhash(number) self.ext.blockhash(number)
} }
fn create(&mut self, gas: &U256, value: &U256, code: &[u8]) -> (U256, Option<Address>) { fn create(&mut self, gas: &U256, value: &U256, code: &[u8]) -> ContractCreateResult {
// in call and create we need to check if we exited with insufficient balance or max limit reached. self.callcreates.push(CallCreate {
// in case of reaching max depth, we should store callcreates. Otherwise, ignore. data: code.to_vec(),
let res = self.ext.create(gas, value, code); destination: None,
let ext = &self.ext; gas_limit: *gas,
match res { value: *value
// just record call create });
(gas_left, Some(address)) => { ContractCreateResult::Created(self.contract_address.clone(), *gas)
self.callcreates.push(CallCreate {
data: code.to_vec(),
destination: Some(address.clone()),
_gas_limit: *gas,
value: *value
});
(gas_left, Some(address))
},
// creation failed only due to reaching max_depth
(gas_left, None) if ext.state.balance(&ext.params.address) >= *value => {
self.callcreates.push(CallCreate {
data: code.to_vec(),
// callcreate test does not need an address
destination: None,
_gas_limit: *gas,
value: *value
});
let address = contract_address(&ext.params.address, &ext.state.nonce(&ext.params.address));
(gas_left, Some(address))
},
other => other
}
} }
fn call(&mut self, fn call(&mut self,
gas: &U256, gas: &U256,
call_gas: &U256,
receive_address: &Address, receive_address: &Address,
value: &U256, value: &U256,
data: &[u8], data: &[u8],
code_address: &Address, _code_address: &Address,
output: &mut [u8]) -> Result<(U256, bool), evm::Error> { _output: &mut [u8]) -> MessageCallResult {
let res = self.ext.call(gas, call_gas, receive_address, value, data, code_address, output); self.callcreates.push(CallCreate {
let ext = &self.ext; data: data.to_vec(),
if let &Ok((gas_left, _)) = &res { destination: Some(receive_address.clone()),
if ext.state.balance(&ext.params.address) >= *value { gas_limit: *gas,
self.callcreates.push(CallCreate { value: *value
data: data.to_vec(), });
destination: Some(receive_address.clone()), MessageCallResult::Success(*gas)
_gas_limit: *call_gas,
value: *value
});
return Ok((gas_left, true))
}
}
res
} }
fn extcode(&self, address: &Address) -> Bytes { fn extcode(&self, address: &Address) -> Bytes {
@ -155,6 +138,14 @@ impl<'a> Ext for TestExt<'a> {
fn env_info(&self) -> &EnvInfo { fn env_info(&self) -> &EnvInfo {
self.ext.env_info() self.ext.env_info()
} }
fn depth(&self) -> usize {
0
}
fn inc_sstore_clears(&mut self) {
self.ext.inc_sstore_clears()
}
} }
fn do_json_test(json_data: &[u8]) -> Vec<String> { fn do_json_test(json_data: &[u8]) -> Vec<String> {
@ -206,7 +197,7 @@ fn do_json_test_for(vm: &VMType, json_data: &[u8]) -> Vec<String> {
info.timestamp = xjson!(&env["currentTimestamp"]); info.timestamp = xjson!(&env["currentTimestamp"]);
}); });
let engine = TestEngine::new(0, vm.clone()); let engine = TestEngine::new(1, vm.clone());
// params // params
let mut params = ActionParams::new(); let mut params = ActionParams::new();
@ -229,11 +220,17 @@ fn do_json_test_for(vm: &VMType, json_data: &[u8]) -> Vec<String> {
// execute // execute
let (res, callcreates) = { let (res, callcreates) = {
let ex = Externalities::new(&mut state, &info, &engine, 0, &params, &mut substate, OutputPolicy::Return(BytesRef::Flexible(&mut output))); let mut ex = TestExt::new(&mut state,
let mut test_ext = TestExt::new(ex); &info,
&engine,
0,
OriginInfo::from(&params),
&mut substate,
OutputPolicy::Return(BytesRef::Flexible(&mut output)),
params.address.clone());
let evm = engine.vm_factory().create(); let evm = engine.vm_factory().create();
let res = evm.exec(&params, &mut test_ext); let res = evm.exec(params, &mut ex);
(res, test_ext.callcreates) (res, ex.callcreates)
}; };
// then validate // then validate
@ -262,11 +259,7 @@ fn do_json_test_for(vm: &VMType, json_data: &[u8]) -> Vec<String> {
fail_unless(callcreate.data == Bytes::from_json(&expected["data"]), "callcreates data is incorrect"); 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.destination == xjson!(&expected["destination"]), "callcreates destination is incorrect");
fail_unless(callcreate.value == xjson!(&expected["value"]), "callcreates value 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");
// TODO: call_gas is calculated in externalities and is not exposed to TestExt.
// maybe move it to it's own function to simplify calculation?
//println!("name: {:?}, callcreate {:?}, expected: {:?}", name, callcreate.gas_limit, U256::from(&expected["gasLimit"]));
//fail_unless(callcreate.gas_limit == U256::from(&expected["gasLimit"]), "callcreates gas_limit is incorrect");
} }
} }
} }

View File

@ -4,15 +4,26 @@ use pod_state::*;
use state_diff::*; use state_diff::*;
use ethereum; use ethereum;
const HOMESTEAD_BLOCK : u64 = 0x0dbba0;
fn do_json_test(json_data: &[u8]) -> Vec<String> { fn do_json_test(json_data: &[u8]) -> Vec<String> {
let json = Json::from_str(::std::str::from_utf8(json_data).unwrap()).expect("Json is invalid"); let json = Json::from_str(::std::str::from_utf8(json_data).unwrap()).expect("Json is invalid");
let mut failed = Vec::new(); let mut failed = Vec::new();
let engine = ethereum::new_frontier_test().to_engine().unwrap(); let engine = |block_number: u64| {
if block_number >= HOMESTEAD_BLOCK {
ethereum::new_homestead_test().to_engine().unwrap()
} else {
ethereum::new_frontier_test().to_engine().unwrap()
}
};
flush(format!("\n")); flush(format!("\n"));
for (name, test) in json.as_object().unwrap() { for (name, test) in json.as_object().unwrap() {
if name != "createNameRegistratorPerTxsAt" {
continue;
}
let mut fail = false; let mut fail = false;
{ {
let mut fail_unless = |cond: bool| if !cond && !fail { let mut fail_unless = |cond: bool| if !cond && !fail {
@ -24,8 +35,9 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
flush(format!(" - {}...", name)); flush(format!(" - {}...", name));
let t = Transaction::from_json(&test["transaction"]);
let env = EnvInfo::from_json(&test["env"]); let env = EnvInfo::from_json(&test["env"]);
let engine = engine(env.number);
let t = Transaction::from_json(&test["transaction"]);
let _out = Bytes::from_json(&test["out"]); let _out = Bytes::from_json(&test["out"]);
let post_state_root = xjson!(&test["postStateRoot"]); let post_state_root = xjson!(&test["postStateRoot"]);
let pre = PodState::from_json(&test["pre"]); let pre = PodState::from_json(&test["pre"]);