From 5a072d1b4ba67e62a4f3d54f4be05b0c1edcf00b Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 13 Jan 2016 22:36:59 +0100 Subject: [PATCH] vm ext call function uses u256 instead of u64, ext works entirely on u256 --- src/evm/ext.rs | 7 +++---- src/evm/jit.rs | 6 +++--- src/evm/tests.rs | 6 +++--- src/executive.rs | 22 ++++++++++------------ src/tests/executive.rs | 8 ++++---- 5 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/evm/ext.rs b/src/evm/ext.rs index 71fcf2696..52b8af24f 100644 --- a/src/evm/ext.rs +++ b/src/evm/ext.rs @@ -6,7 +6,6 @@ use util::bytes::*; use evm::{Schedule, Error}; use env_info::*; -// TODO: replace all u64 with u256 pub trait Ext { /// Returns a value for given key. fn sload(&self, key: &H256) -> H256; @@ -32,13 +31,13 @@ pub trait Ext { /// If call is successfull, returns gas left. /// otherwise `Error`. fn call(&mut self, - gas: u64, - call_gas: u64, + gas: &U256, + call_gas: &U256, receive_address: &Address, value: &U256, data: &[u8], code_address: &Address, - output: &mut [u8]) -> Result; + output: &mut [u8]) -> Result; /// Returns code at given address fn extcode(&self, address: &Address) -> Vec; diff --git a/src/evm/jit.rs b/src/evm/jit.rs index 71789ef2a..f907c4be8 100644 --- a/src/evm/jit.rs +++ b/src/evm/jit.rs @@ -240,8 +240,8 @@ impl<'a> evmjit::Ext for ExtAdapter<'a> { out_size: u64, code_address: *const evmjit::H256) -> bool { unsafe { - let res = self.ext.call(*io_gas, - call_gas, + let res = self.ext.call(&U256::from(*io_gas), + &U256::from(call_gas), &Address::from_jit(&*receive_address), &U256::from_jit(&*value), slice::from_raw_parts(in_beg, in_size as usize), @@ -250,7 +250,7 @@ impl<'a> evmjit::Ext for ExtAdapter<'a> { match res { Ok(gas_left) => { - *io_gas = gas_left; + *io_gas = gas_left.low_u64(); true }, Err(err @ evm::Error::OutOfGas) => { diff --git a/src/evm/tests.rs b/src/evm/tests.rs index 4de68bc37..7aff9f407 100644 --- a/src/evm/tests.rs +++ b/src/evm/tests.rs @@ -47,13 +47,13 @@ impl Ext for FakeExt { } fn call(&mut self, - _gas: u64, - _call_gas: u64, + _gas: &U256, + _call_gas: &U256, _receive_address: &Address, _value: &U256, _data: &[u8], _code_address: &Address, - _output: &mut [u8]) -> result::Result { + _output: &mut [u8]) -> result::Result { unimplemented!(); } diff --git a/src/executive.rs b/src/executive.rs index b10baa795..ea135194f 100644 --- a/src/executive.rs +++ b/src/executive.rs @@ -375,26 +375,26 @@ impl<'a> Ext for Externalities<'a> { ex.create(¶ms, self.substate).map(|gas_left| (gas_left, Some(address))) } - fn call(&mut self, gas: u64, call_gas: u64, receive_address: &Address, value: &U256, data: &[u8], code_address: &Address, output: &mut [u8]) -> Result { - let mut gas_cost = call_gas; - let mut call_gas = call_gas; + fn call(&mut self, gas: &U256, call_gas: &U256, receive_address: &Address, value: &U256, data: &[u8], code_address: &Address, output: &mut [u8]) -> Result { + 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 + self.schedule.call_new_account_gas as u64; + 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 + self.schedule.call_value_transfer_gas as u64; - call_gas = call_gas + self.schedule.call_stipend as u64; + gas_cost = gas_cost + U256::from(self.schedule.call_value_transfer_gas); + call_gas = call_gas + U256::from(self.schedule.call_stipend); } - if gas_cost > gas { + if gas_cost > *gas { return Err(evm::Error::OutOfGas) } - let gas = gas - gas_cost; + let gas = *gas - gas_cost; // if balance is insufficient or we are to deep, return if self.state.balance(&self.params.address) < *value || self.depth >= self.schedule.stack_limit { @@ -405,7 +405,7 @@ impl<'a> Ext for Externalities<'a> { address: receive_address.clone(), sender: self.params.address.clone(), origin: self.params.origin.clone(), - gas: U256::from(call_gas), + gas: call_gas, gas_price: self.params.gas_price.clone(), value: value.clone(), code: self.state.code(code_address).unwrap_or(vec![]), @@ -413,9 +413,7 @@ impl<'a> Ext for Externalities<'a> { }; let mut ex = Executive::from_parent(self.state, self.info, self.engine, self.depth); - ex.call(¶ms, self.substate, BytesRef::Fixed(output)).map(|gas_left| { - gas + gas_left.low_u64() - }) + ex.call(¶ms, self.substate, BytesRef::Fixed(output)) } fn extcode(&self, address: &Address) -> Vec { diff --git a/src/tests/executive.rs b/src/tests/executive.rs index 7e2b7ecfd..cfe80dfc1 100644 --- a/src/tests/executive.rs +++ b/src/tests/executive.rs @@ -104,13 +104,13 @@ impl<'a> Ext for TestExt<'a> { } fn call(&mut self, - gas: u64, - call_gas: u64, + gas: &U256, + call_gas: &U256, receive_address: &Address, value: &U256, data: &[u8], code_address: &Address, - output: &mut [u8]) -> Result { + output: &mut [u8]) -> Result { let res = self.ext.call(gas, call_gas, receive_address, value, data, code_address, output); let ext = &self.ext; match res { @@ -118,7 +118,7 @@ impl<'a> Ext for TestExt<'a> { self.callcreates.push(CallCreate { data: data.to_vec(), destination: receive_address.clone(), - _gas_limit: U256::from(call_gas), + _gas_limit: *call_gas, value: *value }); Ok(gas_left)