2016-01-09 21:39:38 +01:00
|
|
|
use std::collections::HashSet;
|
2016-01-09 22:54:16 +01:00
|
|
|
use std::cmp;
|
2016-01-11 02:17:29 +01:00
|
|
|
use std::ptr;
|
2016-01-07 21:29:36 +01:00
|
|
|
use util::hash::*;
|
|
|
|
use util::uint::*;
|
2016-01-07 23:33:54 +01:00
|
|
|
use util::rlp::*;
|
|
|
|
use util::sha3::*;
|
2016-01-09 00:51:09 +01:00
|
|
|
use util::bytes::*;
|
2016-01-07 19:05:44 +01:00
|
|
|
use state::*;
|
|
|
|
use env_info::*;
|
2016-01-11 02:17:29 +01:00
|
|
|
use evm_schedule::*;
|
2016-01-07 19:05:44 +01:00
|
|
|
use engine::*;
|
|
|
|
use transaction::*;
|
2016-01-11 02:17:29 +01:00
|
|
|
use evm::{VmFactory, Ext, LogEntry, EvmParams, EvmResult, EvmError};
|
2016-01-07 23:33:54 +01:00
|
|
|
|
2016-01-09 00:51:09 +01:00
|
|
|
/// Returns new address created from address and given nonce.
|
2016-01-08 00:16:15 +01:00
|
|
|
pub fn contract_address(address: &Address, nonce: &U256) -> Address {
|
2016-01-07 23:33:54 +01:00
|
|
|
let mut stream = RlpStream::new_list(2);
|
|
|
|
stream.append(address);
|
|
|
|
stream.append(nonce);
|
|
|
|
From::from(stream.out().sha3())
|
|
|
|
}
|
2016-01-07 19:05:44 +01:00
|
|
|
|
2016-01-09 21:39:38 +01:00
|
|
|
/// State changes which should be applied in finalize,
|
|
|
|
/// after transaction is fully executed.
|
|
|
|
pub struct Substate {
|
|
|
|
/// Any accounts that have suicided.
|
|
|
|
suicides: HashSet<Address>,
|
|
|
|
/// Any logs.
|
|
|
|
logs: Vec<LogEntry>,
|
|
|
|
/// Refund counter of SSTORE nonzero->zero.
|
2016-01-09 22:54:16 +01:00
|
|
|
refunds_count: U256,
|
2016-01-09 21:39:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Substate {
|
|
|
|
/// Creates new substate.
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Substate {
|
|
|
|
suicides: HashSet::new(),
|
|
|
|
logs: vec![],
|
2016-01-09 22:54:16 +01:00
|
|
|
refunds_count: U256::zero(),
|
2016-01-09 21:39:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-11 02:17:29 +01:00
|
|
|
// TODO: remove
|
2016-01-09 23:24:01 +01:00
|
|
|
pub fn logs(&self) -> &[LogEntry] {
|
|
|
|
&self.logs
|
|
|
|
}
|
|
|
|
|
2016-01-09 21:39:38 +01:00
|
|
|
/// Appends another substate to this substate.
|
|
|
|
fn accrue(&mut self, s: Substate) {
|
|
|
|
self.suicides.extend(s.suicides.into_iter());
|
|
|
|
self.logs.extend(s.logs.into_iter());
|
2016-01-09 22:54:16 +01:00
|
|
|
self.refunds_count = self.refunds_count + s.refunds_count;
|
2016-01-09 21:39:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-11 02:17:29 +01:00
|
|
|
/// Transaction execution result.
|
|
|
|
pub struct Executed {
|
|
|
|
/// Gas paid up front for execution of transaction.
|
|
|
|
pub gas: U256,
|
|
|
|
/// Gas used during execution of transaction.
|
|
|
|
pub gas_used: U256,
|
|
|
|
/// Gas refunded after the execution of transaction.
|
|
|
|
/// To get gas that was required up front, add `refunded` and `gas_used`.
|
|
|
|
pub refunded: U256,
|
|
|
|
/// Cumulative gas used in current block so far.
|
|
|
|
///
|
|
|
|
/// cumulative_gas_used = gas_used(t0) + gas_used(t1) + ... gas_used(tn)
|
|
|
|
///
|
|
|
|
/// where `tn` is current transaction.
|
|
|
|
pub cumulative_gas_used: U256,
|
|
|
|
/// Transaction output.
|
|
|
|
pub output: Bytes,
|
|
|
|
/// Vector of logs generated by transaction.
|
|
|
|
pub logs: Vec<LogEntry>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Executed {
|
|
|
|
fn new() -> Executed {
|
|
|
|
Executed {
|
|
|
|
gas: U256::zero(),
|
|
|
|
gas_used: U256::zero(),
|
|
|
|
refunded: U256::zero(),
|
|
|
|
cumulative_gas_used: U256::zero(),
|
|
|
|
output: vec![],
|
|
|
|
logs: vec![]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-10 16:21:01 +01:00
|
|
|
/// Result of executing the transaction.
|
2016-01-09 13:51:59 +01:00
|
|
|
#[derive(PartialEq, Debug)]
|
2016-01-11 02:17:29 +01:00
|
|
|
pub enum ExecutionError {
|
|
|
|
/// Returned when block (gas_used + gas) > gas_limit.
|
|
|
|
///
|
|
|
|
/// If gas =< gas_limit, upstream may try to execute the transaction
|
|
|
|
/// in next block.
|
2016-01-10 12:29:35 +01:00
|
|
|
BlockGasLimitReached { gas_limit: U256, gas_used: U256, gas: U256 },
|
2016-01-11 02:17:29 +01:00
|
|
|
/// Returned when transaction nonce does not match state nonce.
|
2016-01-10 12:29:35 +01:00
|
|
|
InvalidNonce { expected: U256, is: U256 },
|
2016-01-11 02:17:29 +01:00
|
|
|
/// Returned when cost of transaction (value + gas_price * gas) exceeds
|
|
|
|
/// current sender balance.
|
2016-01-10 12:29:35 +01:00
|
|
|
NotEnoughCash { required: U256, is: U256 },
|
2016-01-11 02:17:29 +01:00
|
|
|
/// Returned when internal evm error occurs.
|
|
|
|
Internal
|
2016-01-07 19:05:44 +01:00
|
|
|
}
|
|
|
|
|
2016-01-11 02:17:29 +01:00
|
|
|
pub type ExecutionResult = Result<Executed, ExecutionError>;
|
|
|
|
|
2016-01-09 22:54:16 +01:00
|
|
|
/// Message-call/contract-creation executor; useful for executing transactions.
|
2016-01-07 19:05:44 +01:00
|
|
|
pub struct Executive<'a> {
|
|
|
|
state: &'a mut State,
|
|
|
|
info: &'a EnvInfo,
|
|
|
|
engine: &'a Engine,
|
2016-01-09 00:51:09 +01:00
|
|
|
depth: usize,
|
2016-01-07 19:05:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Executive<'a> {
|
2016-01-09 21:39:38 +01:00
|
|
|
/// Creates new executive with depth equal 0.
|
2016-01-09 17:55:47 +01:00
|
|
|
pub fn new(state: &'a mut State, info: &'a EnvInfo, engine: &'a Engine) -> Self {
|
|
|
|
Executive::new_with_depth(state, info, engine, 0)
|
|
|
|
}
|
2016-01-09 00:51:09 +01:00
|
|
|
|
2016-01-09 21:39:38 +01:00
|
|
|
/// Populates executive from parent externalities. Increments executive depth.
|
2016-01-09 17:55:47 +01:00
|
|
|
fn from_parent(e: &'a mut Externalities) -> Self {
|
|
|
|
Executive::new_with_depth(e.state, e.info, e.engine, e.depth + 1)
|
2016-01-09 00:51:09 +01:00
|
|
|
}
|
|
|
|
|
2016-01-09 21:39:38 +01:00
|
|
|
/// Helper constructor. Should be used to create `Executive` with desired depth.
|
|
|
|
/// Private.
|
2016-01-09 17:55:47 +01:00
|
|
|
fn new_with_depth(state: &'a mut State, info: &'a EnvInfo, engine: &'a Engine, depth: usize) -> Self {
|
2016-01-07 19:05:44 +01:00
|
|
|
Executive {
|
|
|
|
state: state,
|
|
|
|
info: info,
|
|
|
|
engine: engine,
|
2016-01-09 17:55:47 +01:00
|
|
|
depth: depth,
|
2016-01-07 19:05:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-09 21:39:38 +01:00
|
|
|
/// This funtion should be used to execute transaction.
|
2016-01-11 02:17:29 +01:00
|
|
|
pub fn transact(e: &mut Executive<'a>, t: &Transaction) -> ExecutionResult {
|
|
|
|
// TODO: validate transaction signature ?/ sender
|
2016-01-10 12:29:35 +01:00
|
|
|
|
2016-01-09 17:55:47 +01:00
|
|
|
let sender = t.sender();
|
2016-01-10 12:29:35 +01:00
|
|
|
let nonce = e.state.nonce(&sender);
|
|
|
|
|
|
|
|
// validate transaction nonce
|
|
|
|
if t.nonce != nonce {
|
2016-01-11 02:17:29 +01:00
|
|
|
return Err(ExecutionError::InvalidNonce { expected: nonce, is: t.nonce });
|
2016-01-10 12:29:35 +01:00
|
|
|
}
|
|
|
|
|
2016-01-11 02:17:29 +01:00
|
|
|
// validate if transaction fits into given block
|
|
|
|
if e.info.gas_used + t.gas > e.info.gas_limit {
|
|
|
|
return Err(ExecutionError::BlockGasLimitReached {
|
|
|
|
gas_limit: e.info.gas_limit,
|
|
|
|
gas_used: e.info.gas_used,
|
|
|
|
gas: t.gas
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-01-10 12:29:35 +01:00
|
|
|
// TODO: we might need bigints here, or at least check overflows.
|
|
|
|
let balance = e.state.balance(&sender);
|
|
|
|
let gas_cost = t.gas * t.gas_price;
|
|
|
|
let total_cost = t.value + gas_cost;
|
|
|
|
|
|
|
|
// avoid unaffordable transactions
|
|
|
|
if balance < total_cost {
|
2016-01-11 02:17:29 +01:00
|
|
|
return Err(ExecutionError::NotEnoughCash { required: total_cost, is: balance });
|
2016-01-10 12:29:35 +01:00
|
|
|
}
|
2016-01-07 21:29:36 +01:00
|
|
|
|
2016-01-11 02:17:29 +01:00
|
|
|
// NOTE: there can be no invalid transactions from this point.
|
2016-01-10 12:29:35 +01:00
|
|
|
e.state.inc_nonce(&sender);
|
2016-01-09 21:39:38 +01:00
|
|
|
let mut substate = Substate::new();
|
|
|
|
|
|
|
|
let res = match t.kind() {
|
2016-01-09 17:55:47 +01:00
|
|
|
TransactionKind::ContractCreation => {
|
|
|
|
let params = EvmParams {
|
2016-01-10 12:29:35 +01:00
|
|
|
address: contract_address(&sender, &nonce),
|
2016-01-09 17:55:47 +01:00
|
|
|
sender: sender.clone(),
|
|
|
|
origin: sender.clone(),
|
|
|
|
gas: t.gas,
|
|
|
|
gas_price: t.gas_price,
|
|
|
|
value: t.value,
|
|
|
|
code: t.data.clone(),
|
|
|
|
data: vec![],
|
|
|
|
};
|
2016-01-11 02:17:29 +01:00
|
|
|
Executive::call(e, ¶ms, &mut substate, &mut [])
|
2016-01-09 00:51:09 +01:00
|
|
|
},
|
2016-01-09 17:55:47 +01:00
|
|
|
TransactionKind::MessageCall => {
|
|
|
|
let params = EvmParams {
|
|
|
|
address: t.to.clone().unwrap(),
|
|
|
|
sender: sender.clone(),
|
|
|
|
origin: sender.clone(),
|
|
|
|
gas: t.gas,
|
|
|
|
gas_price: t.gas_price,
|
|
|
|
value: t.value,
|
|
|
|
code: e.state.code(&t.to.clone().unwrap()).unwrap_or(vec![]),
|
|
|
|
data: t.data.clone(),
|
|
|
|
};
|
2016-01-09 21:39:38 +01:00
|
|
|
Executive::create(e, ¶ms, &mut substate)
|
2016-01-09 17:55:47 +01:00
|
|
|
}
|
2016-01-09 21:39:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// finalize here!
|
2016-01-10 12:29:35 +01:00
|
|
|
e.finalize(substate, &sender, U256::zero(), U256::zero(), t.gas_price);
|
2016-01-11 02:17:29 +01:00
|
|
|
//res
|
|
|
|
Ok(Executed::new())
|
2016-01-07 19:05:44 +01:00
|
|
|
}
|
|
|
|
|
2016-01-09 21:39:38 +01:00
|
|
|
/// Calls contract function with given contract params.
|
2016-01-11 02:17:29 +01:00
|
|
|
/// NOTE. It does not finalize the transaction (doesn't do refunds, nor suicides).
|
|
|
|
/// Modifies the substate and the output.
|
|
|
|
/// Returns either gas_left or `EvmError`.
|
|
|
|
fn call(e: &mut Executive<'a>, params: &EvmParams, substate: &mut Substate, output: &mut [u8]) -> EvmResult {
|
2016-01-09 22:54:16 +01:00
|
|
|
// at first, transfer value to destination
|
|
|
|
e.state.transfer_balance(¶ms.sender, ¶ms.address, ¶ms.value);
|
|
|
|
|
|
|
|
if e.engine.is_builtin(¶ms.address) {
|
2016-01-11 02:17:29 +01:00
|
|
|
// if destination is builtin, try to execute it
|
|
|
|
let cost = e.engine.cost_of_builtin(¶ms.address, ¶ms.data);
|
|
|
|
match cost <= params.gas {
|
|
|
|
true => {
|
|
|
|
e.engine.execute_builtin(¶ms.address, ¶ms.data, output);
|
|
|
|
Ok(params.gas - cost)
|
2016-01-09 22:54:16 +01:00
|
|
|
},
|
2016-01-11 02:17:29 +01:00
|
|
|
false => Err(EvmError::OutOfGas)
|
2016-01-09 22:54:16 +01:00
|
|
|
}
|
2016-01-11 02:17:29 +01:00
|
|
|
} else if params.code.len() > 0 {
|
|
|
|
// if destination is a contract, do normal message call
|
|
|
|
let mut ext = Externalities::new(e.state, e.info, e.engine, e.depth, params, substate, OutputPolicy::Return(output));
|
|
|
|
let evm = VmFactory::create();
|
|
|
|
evm.exec(¶ms, &mut ext)
|
|
|
|
} else {
|
|
|
|
// otherwise, nothing
|
|
|
|
Ok(params.gas)
|
2016-01-09 22:54:16 +01:00
|
|
|
}
|
2016-01-07 19:05:44 +01:00
|
|
|
}
|
2016-01-09 02:12:17 +01:00
|
|
|
|
2016-01-09 21:39:38 +01:00
|
|
|
/// Creates contract with given contract params.
|
2016-01-11 02:17:29 +01:00
|
|
|
/// NOTE. It does not finalize the transaction (doesn't do refunds, nor suicides).
|
|
|
|
/// Modifies the substate.
|
|
|
|
fn create(e: &mut Executive<'a>, params: &EvmParams, substate: &mut Substate) -> EvmResult {
|
2016-01-09 22:54:16 +01:00
|
|
|
// at first create new contract
|
2016-01-09 18:41:12 +01:00
|
|
|
e.state.new_contract(¶ms.address);
|
2016-01-09 22:54:16 +01:00
|
|
|
// then transfer value to it
|
2016-01-09 17:55:47 +01:00
|
|
|
e.state.transfer_balance(¶ms.sender, ¶ms.address, ¶ms.value);
|
|
|
|
|
2016-01-11 02:17:29 +01:00
|
|
|
let mut ext = Externalities::new(e.state, e.info, e.engine, e.depth, params, substate, OutputPolicy::InitContract);
|
|
|
|
let evm = VmFactory::create();
|
|
|
|
evm.exec(¶ms, &mut ext)
|
2016-01-07 19:05:44 +01:00
|
|
|
}
|
2016-01-09 21:39:38 +01:00
|
|
|
|
2016-01-09 22:54:16 +01:00
|
|
|
/// Finalizes the transaction (does refunds and suicides).
|
2016-01-10 12:29:35 +01:00
|
|
|
fn finalize(&mut self, substate: Substate, sender: &Address, gas: U256, gas_left: U256, gas_price: U256) {
|
2016-01-09 22:54:16 +01:00
|
|
|
let schedule = self.engine.evm_schedule(self.info);
|
|
|
|
|
|
|
|
// refunds from SSTORE nonzero -> zero
|
|
|
|
let sstore_refunds = U256::from(schedule.sstore_refund_gas) * substate.refunds_count;
|
|
|
|
// refunds from contract suicides
|
|
|
|
let suicide_refunds = U256::from(schedule.suicide_refund_gas) * U256::from(substate.suicides.len());
|
2016-01-10 12:29:35 +01:00
|
|
|
|
2016-01-09 22:54:16 +01:00
|
|
|
// real ammount to refund
|
2016-01-10 12:29:35 +01:00
|
|
|
let refund = cmp::min(sstore_refunds + suicide_refunds, (gas - gas_left) / U256::from(2)) + gas_left;
|
|
|
|
let refund_value = refund * gas_price;
|
|
|
|
self.state.add_balance(sender, &refund_value);
|
|
|
|
|
|
|
|
// fees earned by author
|
|
|
|
let fees = (gas - refund) * gas_price;
|
|
|
|
let author = &self.info.author;
|
|
|
|
self.state.add_balance(author, &fees);
|
2016-01-09 22:54:16 +01:00
|
|
|
|
|
|
|
// perform suicides
|
2016-01-09 23:24:01 +01:00
|
|
|
for address in substate.suicides.iter() {
|
|
|
|
self.state.kill_account(address);
|
|
|
|
}
|
2016-01-09 21:39:38 +01:00
|
|
|
}
|
2016-01-09 17:55:47 +01:00
|
|
|
}
|
|
|
|
|
2016-01-11 02:17:29 +01:00
|
|
|
pub enum ExtMode {
|
|
|
|
Call,
|
|
|
|
Create
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wrapper structure for evm return data to avoid unnecessary copying.
|
|
|
|
pub enum OutputPolicy<'a> {
|
|
|
|
/// Reference to fixed sized output of a message call.
|
|
|
|
Return(&'a mut [u8]),
|
|
|
|
/// Use it, if you want return code to initialize contract.
|
|
|
|
InitContract
|
|
|
|
}
|
|
|
|
|
2016-01-09 21:39:38 +01:00
|
|
|
/// Implementation of evm Externalities.
|
2016-01-09 17:55:47 +01:00
|
|
|
pub struct Externalities<'a> {
|
|
|
|
state: &'a mut State,
|
|
|
|
info: &'a EnvInfo,
|
|
|
|
engine: &'a Engine,
|
|
|
|
depth: usize,
|
|
|
|
params: &'a EvmParams,
|
2016-01-11 02:17:29 +01:00
|
|
|
substate: &'a mut Substate,
|
|
|
|
schedule: EvmSchedule,
|
|
|
|
output: OutputPolicy<'a>
|
2016-01-09 17:55:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Externalities<'a> {
|
2016-01-09 21:39:38 +01:00
|
|
|
/// Basic `Externalities` constructor.
|
2016-01-11 02:17:29 +01:00
|
|
|
pub fn new(state: &'a mut State,
|
|
|
|
info: &'a EnvInfo,
|
|
|
|
engine: &'a Engine,
|
|
|
|
depth: usize,
|
|
|
|
params: &'a EvmParams,
|
|
|
|
substate: &'a mut Substate,
|
|
|
|
output: OutputPolicy<'a>) -> Self {
|
2016-01-09 17:55:47 +01:00
|
|
|
Externalities {
|
|
|
|
state: state,
|
|
|
|
info: info,
|
|
|
|
engine: engine,
|
|
|
|
depth: depth,
|
|
|
|
params: params,
|
2016-01-11 02:17:29 +01:00
|
|
|
substate: substate,
|
|
|
|
schedule: engine.evm_schedule(info),
|
|
|
|
output: output
|
2016-01-09 17:55:47 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-09 00:51:09 +01:00
|
|
|
}
|
|
|
|
|
2016-01-09 17:55:47 +01:00
|
|
|
impl<'a> Ext for Externalities<'a> {
|
2016-01-09 00:51:09 +01:00
|
|
|
fn sload(&self, key: &H256) -> H256 {
|
|
|
|
self.state.storage_at(&self.params.address, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sstore(&mut self, key: H256, value: H256) {
|
2016-01-09 23:24:01 +01:00
|
|
|
// if SSTORE nonzero -> zero, increment refund count
|
2016-01-09 00:51:09 +01:00
|
|
|
if value == H256::new() && self.state.storage_at(&self.params.address, &key) != H256::new() {
|
2016-01-09 22:54:16 +01:00
|
|
|
self.substate.refunds_count = self.substate.refunds_count + U256::one();
|
2016-01-09 00:51:09 +01:00
|
|
|
}
|
|
|
|
self.state.set_storage(&self.params.address, key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn balance(&self, address: &Address) -> U256 {
|
|
|
|
self.state.balance(address)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn blockhash(&self, number: &U256) -> H256 {
|
|
|
|
match *number < self.info.number {
|
|
|
|
false => H256::from(&U256::zero()),
|
|
|
|
true => {
|
|
|
|
let index = self.info.number - *number - U256::one();
|
|
|
|
self.info.last_hashes[index.low_u32() as usize].clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-09 23:24:01 +01:00
|
|
|
fn create(&mut self, gas: u64, endowment: &U256, code: &[u8]) -> Option<(Address, u64)> {
|
2016-01-10 16:21:01 +01:00
|
|
|
// if balance is insufficient or we are to deep, return
|
|
|
|
if self.state.balance(&self.params.address) < *endowment && self.depth >= 1024 {
|
|
|
|
return None
|
|
|
|
}
|
|
|
|
|
|
|
|
// create new contract address
|
|
|
|
let address = contract_address(&self.params.address, &self.state.nonce(&self.params.address));
|
|
|
|
|
|
|
|
// prepare the params
|
|
|
|
let params = EvmParams {
|
|
|
|
address: address.clone(),
|
|
|
|
sender: self.params.address.clone(),
|
|
|
|
origin: self.params.origin.clone(),
|
|
|
|
gas: U256::from(gas),
|
|
|
|
gas_price: self.params.gas_price.clone(),
|
|
|
|
value: endowment.clone(),
|
|
|
|
code: code.to_vec(),
|
|
|
|
data: vec![],
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut substate = Substate::new();
|
|
|
|
{
|
|
|
|
let mut ex = Executive::from_parent(self);
|
|
|
|
ex.state.inc_nonce(&address);
|
|
|
|
let res = Executive::create(&mut ex, ¶ms, &mut substate);
|
2016-01-09 00:51:09 +01:00
|
|
|
}
|
2016-01-10 16:21:01 +01:00
|
|
|
|
|
|
|
self.substate.accrue(substate);
|
|
|
|
Some((address, gas))
|
2016-01-09 00:51:09 +01:00
|
|
|
}
|
|
|
|
|
2016-01-10 16:21:01 +01:00
|
|
|
fn call(&mut self, gas: u64, call_gas: u64, receive_address: &Address, value: &U256, data: &[u8], code_address: &Address) -> Option<(Vec<u8>, u64)> {
|
2016-01-09 00:51:09 +01:00
|
|
|
// TODO: validation of the call
|
|
|
|
|
2016-01-10 16:21:01 +01:00
|
|
|
println!("gas: {:?}", gas);
|
|
|
|
println!("call_gas: {:?}", call_gas);
|
|
|
|
|
|
|
|
let mut gas_cost = call_gas;
|
|
|
|
let mut call_gas = call_gas;
|
|
|
|
|
|
|
|
let is_call = receive_address == code_address;
|
|
|
|
if is_call && self.state.code(&code_address).is_none() {
|
2016-01-11 02:17:29 +01:00
|
|
|
gas_cost = gas_cost + self.schedule.call_new_account_gas as u64;
|
2016-01-10 16:21:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if *value > U256::zero() {
|
2016-01-11 02:17:29 +01:00
|
|
|
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;
|
2016-01-10 16:21:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if gas_cost > gas {
|
|
|
|
// TODO: maybe gas should always be updated?
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we are too deep, return
|
|
|
|
// TODO: replace with >= 1024
|
|
|
|
if self.depth == 1 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2016-01-09 00:51:09 +01:00
|
|
|
let params = EvmParams {
|
2016-01-10 16:21:01 +01:00
|
|
|
address: receive_address.clone(),
|
|
|
|
sender: self.params.address.clone(),
|
2016-01-09 00:51:09 +01:00
|
|
|
origin: self.params.origin.clone(),
|
2016-01-10 16:21:01 +01:00
|
|
|
gas: U256::from(call_gas),
|
2016-01-09 00:51:09 +01:00
|
|
|
gas_price: self.params.gas_price.clone(),
|
|
|
|
value: value.clone(),
|
|
|
|
code: self.state.code(code_address).unwrap_or(vec![]),
|
|
|
|
data: data.to_vec(),
|
|
|
|
};
|
|
|
|
|
2016-01-10 16:21:01 +01:00
|
|
|
println!("params: {:?}", params);
|
|
|
|
|
2016-01-09 21:39:38 +01:00
|
|
|
let mut substate = Substate::new();
|
2016-01-09 00:51:09 +01:00
|
|
|
{
|
2016-01-09 17:55:47 +01:00
|
|
|
let mut ex = Executive::from_parent(self);
|
2016-01-11 02:17:29 +01:00
|
|
|
// TODO: take output into account
|
|
|
|
Executive::call(&mut ex, ¶ms, &mut substate, &mut []);
|
2016-01-09 00:51:09 +01:00
|
|
|
}
|
2016-01-10 16:21:01 +01:00
|
|
|
|
2016-01-11 02:17:29 +01:00
|
|
|
self.substate.accrue(substate);
|
2016-01-10 16:21:01 +01:00
|
|
|
// TODO: replace call_gas with what's actually left
|
|
|
|
Some((vec![], gas - gas_cost + call_gas))
|
2016-01-09 00:51:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn extcode(&self, address: &Address) -> Vec<u8> {
|
|
|
|
self.state.code(address).unwrap_or(vec![])
|
|
|
|
}
|
|
|
|
|
2016-01-11 02:17:29 +01:00
|
|
|
fn ret(&mut self, gas: u64, data: &[u8]) -> Option<u64> {
|
|
|
|
match &mut self.output {
|
|
|
|
&mut OutputPolicy::Return(ref mut slice) => unsafe {
|
|
|
|
let len = cmp::min(slice.len(), data.len());
|
|
|
|
ptr::copy(data.as_ptr(), slice.as_mut_ptr(), len);
|
|
|
|
Some(gas)
|
|
|
|
},
|
|
|
|
&mut OutputPolicy::InitContract => {
|
|
|
|
let return_cost = data.len() as u64 * self.schedule.create_data_gas as u64;
|
|
|
|
if return_cost > gas {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let mut code = vec![];
|
|
|
|
code.reserve(data.len());
|
|
|
|
unsafe {
|
|
|
|
ptr::copy(data.as_ptr(), code.as_mut_ptr(), data.len());
|
|
|
|
code.set_len(data.len());
|
|
|
|
}
|
|
|
|
let address = &self.params.address;
|
|
|
|
self.state.init_code(address, code);
|
|
|
|
Some(gas - return_cost)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-09 00:51:09 +01:00
|
|
|
fn log(&mut self, topics: Vec<H256>, data: Bytes) {
|
|
|
|
let address = self.params.address.clone();
|
2016-01-09 21:39:38 +01:00
|
|
|
self.substate.logs.push(LogEntry::new(address, topics, data));
|
2016-01-09 00:51:09 +01:00
|
|
|
}
|
2016-01-11 02:17:29 +01:00
|
|
|
|
|
|
|
fn suicide(&mut self) {
|
|
|
|
let address = self.params.address.clone();
|
|
|
|
self.substate.suicides.insert(address);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn schedule(&self) -> &EvmSchedule {
|
|
|
|
&self.schedule
|
|
|
|
}
|
2016-01-07 19:05:44 +01:00
|
|
|
}
|
2016-01-07 23:33:54 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2016-01-09 01:33:50 +01:00
|
|
|
use rustc_serialize::hex::FromHex;
|
2016-01-07 23:33:54 +01:00
|
|
|
use std::str::FromStr;
|
|
|
|
use util::hash::*;
|
|
|
|
use util::uint::*;
|
2016-01-09 01:33:50 +01:00
|
|
|
use evm::*;
|
|
|
|
use transaction::*;
|
|
|
|
use env_info::*;
|
|
|
|
use state::*;
|
|
|
|
use spec::*;
|
|
|
|
use engine::*;
|
|
|
|
use evm_schedule::*;
|
2016-01-09 02:12:17 +01:00
|
|
|
use super::contract_address;
|
2016-01-10 16:21:01 +01:00
|
|
|
use ethereum;
|
|
|
|
use null_engine::*;
|
|
|
|
use std::ops::*;
|
2016-01-09 01:33:50 +01:00
|
|
|
|
2016-01-09 18:41:12 +01:00
|
|
|
struct TestEngine;
|
2016-01-09 01:33:50 +01:00
|
|
|
|
|
|
|
impl TestEngine {
|
|
|
|
fn new() -> Self {
|
2016-01-09 18:41:12 +01:00
|
|
|
TestEngine
|
2016-01-09 01:33:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Engine for TestEngine {
|
|
|
|
fn name(&self) -> &str { "TestEngine" }
|
2016-01-09 18:41:12 +01:00
|
|
|
fn spec(&self) -> &Spec { unimplemented!() }
|
2016-01-09 01:33:50 +01:00
|
|
|
fn evm_schedule(&self, _env_info: &EnvInfo) -> EvmSchedule { EvmSchedule::new_frontier() }
|
|
|
|
}
|
2016-01-07 23:33:54 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_contract_address() {
|
|
|
|
let address = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap();
|
2016-01-09 02:12:17 +01:00
|
|
|
let expected_address = Address::from_str("3f09c73a5ed19289fb9bdc72f1742566df146f56").unwrap();
|
|
|
|
assert_eq!(expected_address, contract_address(&address, &U256::from(88)));
|
2016-01-07 23:33:54 +01:00
|
|
|
}
|
2016-01-09 01:33:50 +01:00
|
|
|
|
|
|
|
#[test]
|
2016-01-09 13:51:59 +01:00
|
|
|
// TODO: replace params with transactions!
|
2016-01-09 01:33:50 +01:00
|
|
|
fn test_executive() {
|
2016-01-09 02:12:17 +01:00
|
|
|
let sender = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap();
|
|
|
|
let address = contract_address(&sender, &U256::zero());
|
2016-01-09 17:55:47 +01:00
|
|
|
let mut params = EvmParams::new();
|
2016-01-09 02:12:17 +01:00
|
|
|
params.address = address.clone();
|
|
|
|
params.sender = sender.clone();
|
|
|
|
params.gas = U256::from(0x174876e800u64);
|
|
|
|
params.code = "3331600055".from_hex().unwrap();
|
|
|
|
params.value = U256::from(0x7);
|
|
|
|
let mut state = State::new_temp();
|
|
|
|
state.add_balance(&sender, &U256::from(0x100u64));
|
|
|
|
let info = EnvInfo::new();
|
|
|
|
let engine = TestEngine::new();
|
2016-01-09 21:39:38 +01:00
|
|
|
let mut substate = Substate::new();
|
2016-01-09 02:12:17 +01:00
|
|
|
|
|
|
|
{
|
2016-01-09 17:55:47 +01:00
|
|
|
let mut ex = Executive::new(&mut state, &info, &engine);
|
2016-01-11 02:17:29 +01:00
|
|
|
assert_eq!(Executive::create(&mut ex, ¶ms, &mut substate), ExecutionResult::Ok);
|
2016-01-09 02:12:17 +01:00
|
|
|
}
|
|
|
|
|
2016-01-09 13:51:59 +01:00
|
|
|
assert_eq!(state.storage_at(&address, &H256::new()), H256::from(&U256::from(0xf9u64)));
|
2016-01-09 02:12:17 +01:00
|
|
|
assert_eq!(state.balance(&sender), U256::from(0xf9));
|
|
|
|
assert_eq!(state.balance(&address), U256::from(0x7));
|
2016-01-09 01:33:50 +01:00
|
|
|
}
|
2016-01-09 13:51:59 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_create_contract() {
|
|
|
|
let sender = Address::from_str("cd1722f3947def4cf144679da39c4c32bdc35681").unwrap();
|
|
|
|
let address = contract_address(&sender, &U256::zero());
|
|
|
|
let next_address = contract_address(&address, &U256::zero());
|
2016-01-09 17:55:47 +01:00
|
|
|
let mut params = EvmParams::new();
|
2016-01-09 13:51:59 +01:00
|
|
|
params.address = address.clone();
|
|
|
|
params.sender = sender.clone();
|
|
|
|
params.origin = sender.clone();
|
|
|
|
params.gas = U256::from(0x174876e800u64);
|
|
|
|
params.code = "7c601080600c6000396000f3006000355415600957005b60203560003555600052601d60036000f0600055".from_hex().unwrap();
|
|
|
|
let mut state = State::new_temp();
|
|
|
|
state.add_balance(&sender, &U256::from(0x100u64));
|
|
|
|
let info = EnvInfo::new();
|
|
|
|
let engine = TestEngine::new();
|
2016-01-09 21:39:38 +01:00
|
|
|
let mut substate = Substate::new();
|
2016-01-09 13:51:59 +01:00
|
|
|
|
|
|
|
{
|
2016-01-09 17:55:47 +01:00
|
|
|
let mut ex = Executive::new(&mut state, &info, &engine);
|
2016-01-11 02:17:29 +01:00
|
|
|
assert_eq!(Executive::create(&mut ex, ¶ms, &mut substate), ExecutionResult::Ok);
|
2016-01-09 13:51:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(state.storage_at(&address, &H256::new()), H256::from(next_address.clone()));
|
2016-01-09 18:41:12 +01:00
|
|
|
assert_eq!(state.code(&next_address).unwrap(), "6000355415600957005b602035600035".from_hex().unwrap());
|
2016-01-10 16:21:01 +01:00
|
|
|
//assert!(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_recursive_bomb1() {
|
|
|
|
// 60 01 - push 1
|
|
|
|
// 60 00 - push 0
|
|
|
|
// 54 - sload
|
|
|
|
// 01 - add
|
|
|
|
// 60 00 - push 0
|
|
|
|
// 55 - sstore
|
|
|
|
// 60 00 - push 0
|
|
|
|
// 60 00 - push 0
|
|
|
|
// 60 00 - push 0
|
|
|
|
// 60 00 - push 0
|
|
|
|
// 60 00 - push 0
|
|
|
|
// 30 - load address
|
|
|
|
// 60 e0 - push e0
|
|
|
|
// 5a - get gas
|
|
|
|
// 03 - sub
|
|
|
|
// f1 - message call (self in this case)
|
|
|
|
// 60 01 - push 1
|
|
|
|
// 55 - store
|
|
|
|
let sender = Address::from_str("cd1722f3947def4cf144679da39c4c32bdc35681").unwrap();
|
|
|
|
let code = "600160005401600055600060006000600060003360e05a03f1600155".from_hex().unwrap();
|
|
|
|
let address = contract_address(&sender, &U256::zero());
|
|
|
|
let mut params = EvmParams::new();
|
|
|
|
params.address = address.clone();
|
|
|
|
params.sender = sender.clone();
|
|
|
|
params.origin = sender.clone();
|
|
|
|
params.gas = U256::from(0x590b3);
|
|
|
|
params.gas_price = U256::one();
|
|
|
|
params.code = code.clone();
|
|
|
|
println!("init gas: {:?}", params.gas.low_u64());
|
|
|
|
let mut state = State::new_temp();
|
|
|
|
state.init_code(&address, code.clone());
|
|
|
|
let info = EnvInfo::new();
|
|
|
|
//let engine = TestEngine::new();
|
|
|
|
let engine = NullEngine::new_boxed(ethereum::new_frontier());
|
|
|
|
let mut substate = Substate::new();
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut ex = Executive::new(&mut state, &info, engine.deref());
|
2016-01-11 02:17:29 +01:00
|
|
|
assert_eq!(Executive::call(&mut ex, ¶ms, &mut substate), ExecutionResult::Ok);
|
2016-01-10 16:21:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert!(false);
|
|
|
|
|
2016-01-09 13:51:59 +01:00
|
|
|
}
|
2016-01-07 23:33:54 +01:00
|
|
|
}
|