2016-01-11 02:42:02 +01:00
|
|
|
//! Transaction Execution environment.
|
2016-01-11 16:33:08 +01:00
|
|
|
use common::*;
|
2016-01-07 19:05:44 +01:00
|
|
|
use state::*;
|
|
|
|
use engine::*;
|
2016-01-11 19:01:42 +01:00
|
|
|
use evm::{self, Schedule, Factory, Ext};
|
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.
|
2016-01-13 01:23:01 +01:00
|
|
|
pub struct Substate {
|
2016-01-09 21:39:38 +01:00
|
|
|
/// 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-12 16:05:54 +01:00
|
|
|
/// Created contracts.
|
|
|
|
contracts_created: Vec<Address>
|
2016-01-09 21:39:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Substate {
|
|
|
|
/// Creates new substate.
|
2016-01-13 01:23:01 +01:00
|
|
|
pub fn new() -> Self {
|
2016-01-09 21:39:38 +01:00
|
|
|
Substate {
|
|
|
|
suicides: HashSet::new(),
|
|
|
|
logs: vec![],
|
2016-01-09 22:54:16 +01:00
|
|
|
refunds_count: U256::zero(),
|
2016-01-12 16:05:54 +01:00
|
|
|
contracts_created: vec![]
|
2016-01-09 21:39:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-11 19:25:37 +01:00
|
|
|
/// Transaction execution receipt.
|
2016-01-12 13:39:12 +01:00
|
|
|
#[derive(Debug)]
|
2016-01-11 02:17:29 +01:00
|
|
|
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.
|
|
|
|
///
|
2016-01-11 23:40:52 +01:00
|
|
|
/// `cumulative_gas_used = gas_used(t0) + gas_used(t1) + ... gas_used(tn)`
|
2016-01-11 02:17:29 +01:00
|
|
|
///
|
|
|
|
/// where `tn` is current transaction.
|
|
|
|
pub cumulative_gas_used: U256,
|
|
|
|
/// Vector of logs generated by transaction.
|
2016-01-11 17:37:22 +01:00
|
|
|
pub logs: Vec<LogEntry>,
|
|
|
|
/// Execution ended running out of gas.
|
|
|
|
pub out_of_gas: bool,
|
2016-01-12 16:05:54 +01:00
|
|
|
/// Addresses of contracts created during execution of transaction.
|
|
|
|
/// Ordered from earliest creation.
|
|
|
|
///
|
|
|
|
/// eg. sender creates contract A and A in constructor creates contract B
|
|
|
|
///
|
|
|
|
/// B creation ends first, and it will be the first element of the vector.
|
|
|
|
pub contracts_created: Vec<Address>
|
2016-01-07 19:05:44 +01:00
|
|
|
}
|
|
|
|
|
2016-01-11 19:25:37 +01:00
|
|
|
/// Transaction execution result.
|
2016-01-11 02:17:29 +01:00
|
|
|
pub type ExecutionResult = Result<Executed, ExecutionError>;
|
|
|
|
|
2016-01-11 19:25:37 +01:00
|
|
|
/// Transaction executor.
|
2016-01-07 19:05:44 +01:00
|
|
|
pub struct Executive<'a> {
|
|
|
|
state: &'a mut State,
|
|
|
|
info: &'a EnvInfo,
|
|
|
|
engine: &'a Engine,
|
2016-01-12 15:12:03 +01:00
|
|
|
depth: usize
|
2016-01-07 19:05:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Executive<'a> {
|
2016-01-11 19:25:37 +01:00
|
|
|
/// Basic constructor.
|
2016-01-09 17:55:47 +01:00
|
|
|
pub fn new(state: &'a mut State, info: &'a EnvInfo, engine: &'a Engine) -> Self {
|
2016-01-12 15:12:03 +01:00
|
|
|
Executive::new_with_depth(state, info, engine, 0)
|
2016-01-09 17:55:47 +01:00
|
|
|
}
|
2016-01-09 00:51:09 +01:00
|
|
|
|
2016-01-11 14:08:03 +01:00
|
|
|
/// Populates executive from parent properties. Increments executive depth.
|
2016-01-12 15:12:03 +01:00
|
|
|
fn from_parent(state: &'a mut State, info: &'a EnvInfo, engine: &'a Engine, depth: usize) -> Self {
|
|
|
|
Executive::new_with_depth(state, info, engine, 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-12 15:12:03 +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-12 15:12:03 +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 20:47:19 +01:00
|
|
|
pub fn transact(&mut self, t: &Transaction) -> Result<Executed, Error> {
|
|
|
|
let sender = try!(t.sender());
|
2016-01-11 02:42:02 +01:00
|
|
|
let nonce = self.state.nonce(&sender);
|
2016-01-10 12:29:35 +01:00
|
|
|
|
|
|
|
// validate transaction nonce
|
|
|
|
if t.nonce != nonce {
|
2016-01-11 20:47:19 +01:00
|
|
|
return Err(From::from(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
|
2016-01-11 02:42:02 +01:00
|
|
|
if self.info.gas_used + t.gas > self.info.gas_limit {
|
2016-01-11 20:47:19 +01:00
|
|
|
return Err(From::from(ExecutionError::BlockGasLimitReached {
|
2016-01-11 02:42:02 +01:00
|
|
|
gas_limit: self.info.gas_limit,
|
|
|
|
gas_used: self.info.gas_used,
|
2016-01-11 02:17:29 +01:00
|
|
|
gas: t.gas
|
2016-01-11 20:47:19 +01:00
|
|
|
}));
|
2016-01-11 02:17:29 +01:00
|
|
|
}
|
|
|
|
|
2016-01-10 12:29:35 +01:00
|
|
|
// TODO: we might need bigints here, or at least check overflows.
|
2016-01-11 02:42:02 +01:00
|
|
|
let balance = self.state.balance(&sender);
|
2016-01-12 17:40:34 +01:00
|
|
|
let gas_cost = U512::from(t.gas) * U512::from(t.gas_price);
|
|
|
|
let total_cost = U512::from(t.value) + gas_cost;
|
2016-01-10 12:29:35 +01:00
|
|
|
|
|
|
|
// avoid unaffordable transactions
|
2016-01-12 17:40:34 +01:00
|
|
|
if U512::from(balance) < total_cost {
|
|
|
|
return Err(From::from(ExecutionError::NotEnoughCash { required: total_cost, is: U512::from(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-11 02:42:02 +01:00
|
|
|
self.state.inc_nonce(&sender);
|
2016-01-12 17:40:34 +01:00
|
|
|
self.state.sub_balance(&sender, &U256::from(gas_cost));
|
2016-01-09 21:39:38 +01:00
|
|
|
|
2016-01-12 13:39:12 +01:00
|
|
|
let mut substate = Substate::new();
|
2016-01-11 15:55:54 +01:00
|
|
|
let backup = self.state.clone();
|
|
|
|
|
2016-01-11 15:23:27 +01:00
|
|
|
let res = match t.action() {
|
|
|
|
&Action::Create => {
|
2016-01-11 16:33:08 +01:00
|
|
|
let params = ActionParams {
|
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 15:23:27 +01:00
|
|
|
self.create(¶ms, &mut substate)
|
2016-01-09 00:51:09 +01:00
|
|
|
},
|
2016-01-11 15:23:27 +01:00
|
|
|
&Action::Call(ref address) => {
|
2016-01-11 16:33:08 +01:00
|
|
|
let params = ActionParams {
|
2016-01-11 15:23:27 +01:00
|
|
|
address: address.clone(),
|
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,
|
2016-01-11 15:23:27 +01:00
|
|
|
code: self.state.code(address).unwrap_or(vec![]),
|
2016-01-09 17:55:47 +01:00
|
|
|
data: t.data.clone(),
|
|
|
|
};
|
2016-01-11 15:23:27 +01:00
|
|
|
self.call(¶ms, &mut substate, &mut [])
|
2016-01-09 17:55:47 +01:00
|
|
|
}
|
2016-01-09 21:39:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// finalize here!
|
2016-01-11 20:47:19 +01:00
|
|
|
Ok(try!(self.finalize(t, substate, backup, res)))
|
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.
|
2016-01-11 17:01:42 +01:00
|
|
|
/// Returns either gas_left or `evm::Error`.
|
2016-01-13 01:23:01 +01:00
|
|
|
pub fn call(&mut self, params: &ActionParams, substate: &mut Substate, output: &mut [u8]) -> evm::Result {
|
2016-01-09 22:54:16 +01:00
|
|
|
// at first, transfer value to destination
|
2016-01-11 02:42:02 +01:00
|
|
|
self.state.transfer_balance(¶ms.sender, ¶ms.address, ¶ms.value);
|
2016-01-09 22:54:16 +01:00
|
|
|
|
2016-01-11 02:42:02 +01:00
|
|
|
if self.engine.is_builtin(¶ms.address) {
|
2016-01-11 02:17:29 +01:00
|
|
|
// if destination is builtin, try to execute it
|
2016-01-11 02:42:02 +01:00
|
|
|
let cost = self.engine.cost_of_builtin(¶ms.address, ¶ms.data);
|
2016-01-11 02:17:29 +01:00
|
|
|
match cost <= params.gas {
|
|
|
|
true => {
|
2016-01-11 02:42:02 +01:00
|
|
|
self.engine.execute_builtin(¶ms.address, ¶ms.data, output);
|
2016-01-11 02:17:29 +01:00
|
|
|
Ok(params.gas - cost)
|
2016-01-09 22:54:16 +01:00
|
|
|
},
|
2016-01-11 17:01:42 +01:00
|
|
|
false => Err(evm::Error::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
|
2016-01-11 03:26:17 +01:00
|
|
|
let mut ext = Externalities::from_executive(self, params, substate, OutputPolicy::Return(output));
|
2016-01-11 19:01:42 +01:00
|
|
|
let evm = Factory::create();
|
2016-01-11 02:17:29 +01:00
|
|
|
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.
|
2016-01-11 17:01:42 +01:00
|
|
|
fn create(&mut self, params: &ActionParams, substate: &mut Substate) -> evm::Result {
|
2016-01-09 22:54:16 +01:00
|
|
|
// at first create new contract
|
2016-01-11 02:42:02 +01:00
|
|
|
self.state.new_contract(¶ms.address);
|
2016-01-09 22:54:16 +01:00
|
|
|
// then transfer value to it
|
2016-01-11 02:42:02 +01:00
|
|
|
self.state.transfer_balance(¶ms.sender, ¶ms.address, ¶ms.value);
|
2016-01-09 17:55:47 +01:00
|
|
|
|
2016-01-11 03:26:17 +01:00
|
|
|
let mut ext = Externalities::from_executive(self, params, substate, OutputPolicy::InitContract);
|
2016-01-11 19:01:42 +01:00
|
|
|
let evm = Factory::create();
|
2016-01-11 02:17:29 +01:00
|
|
|
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-11 17:01:42 +01:00
|
|
|
fn finalize(&mut self, t: &Transaction, substate: Substate, backup: State, result: evm::Result) -> ExecutionResult {
|
2016-01-11 15:55:54 +01:00
|
|
|
match result {
|
2016-01-11 17:01:42 +01:00
|
|
|
Err(evm::Error::Internal) => Err(ExecutionError::Internal),
|
|
|
|
Err(evm::Error::OutOfGas) => {
|
2016-01-11 15:55:54 +01:00
|
|
|
*self.state = backup;
|
2016-01-11 17:37:22 +01:00
|
|
|
Ok(Executed {
|
|
|
|
gas: t.gas,
|
|
|
|
gas_used: t.gas,
|
|
|
|
refunded: U256::zero(),
|
|
|
|
cumulative_gas_used: self.info.gas_used + t.gas,
|
|
|
|
logs: vec![],
|
|
|
|
out_of_gas: true,
|
2016-01-12 16:05:54 +01:00
|
|
|
contracts_created: vec![]
|
2016-01-11 17:37:22 +01:00
|
|
|
})
|
2016-01-11 15:55:54 +01:00
|
|
|
},
|
|
|
|
Ok(gas_left) => {
|
2016-01-11 16:28:30 +01:00
|
|
|
let schedule = self.engine.schedule(self.info);
|
2016-01-11 15:55:54 +01:00
|
|
|
|
|
|
|
// 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());
|
|
|
|
|
|
|
|
// real ammount to refund
|
|
|
|
let refund = cmp::min(sstore_refunds + suicide_refunds, (t.gas - gas_left) / U256::from(2)) + gas_left;
|
|
|
|
let refund_value = refund * t.gas_price;
|
2016-01-11 20:47:19 +01:00
|
|
|
self.state.add_balance(&t.sender().unwrap(), &refund_value);
|
2016-01-11 15:55:54 +01:00
|
|
|
|
|
|
|
// fees earned by author
|
2016-01-12 18:31:47 +01:00
|
|
|
let fees = t.gas - refund;
|
|
|
|
let fees_value = fees * t.gas_price;
|
2016-01-11 15:55:54 +01:00
|
|
|
let author = &self.info.author;
|
2016-01-12 18:31:47 +01:00
|
|
|
self.state.add_balance(author, &fees_value);
|
2016-01-11 15:55:54 +01:00
|
|
|
|
|
|
|
// perform suicides
|
|
|
|
for address in substate.suicides.iter() {
|
|
|
|
self.state.kill_account(address);
|
|
|
|
}
|
|
|
|
|
2016-01-11 16:05:21 +01:00
|
|
|
let gas_used = t.gas - gas_left;
|
|
|
|
Ok(Executed {
|
|
|
|
gas: t.gas,
|
|
|
|
gas_used: gas_used,
|
|
|
|
refunded: refund,
|
|
|
|
cumulative_gas_used: self.info.gas_used + gas_used,
|
2016-01-11 17:37:22 +01:00
|
|
|
logs: substate.logs,
|
|
|
|
out_of_gas: false,
|
2016-01-12 16:05:54 +01:00
|
|
|
contracts_created: substate.contracts_created
|
2016-01-11 16:05:21 +01:00
|
|
|
})
|
2016-01-11 15:55:54 +01:00
|
|
|
}
|
2016-01-09 23:24:01 +01:00
|
|
|
}
|
2016-01-09 21:39:38 +01:00
|
|
|
}
|
2016-01-09 17:55:47 +01:00
|
|
|
}
|
|
|
|
|
2016-01-11 02:47:45 +01:00
|
|
|
/// Policy for handling output data on `RETURN` opcode.
|
2016-01-13 13:16:53 +01:00
|
|
|
pub enum OutputPolicy<'a> {
|
2016-01-11 02:47:45 +01:00
|
|
|
/// Return reference to fixed sized output.
|
|
|
|
/// Used for message calls.
|
2016-01-11 02:17:29 +01:00
|
|
|
Return(&'a mut [u8]),
|
2016-01-11 02:47:45 +01:00
|
|
|
/// Init new contract as soon as `RETURN` is called.
|
2016-01-11 02:17:29 +01:00
|
|
|
InitContract
|
|
|
|
}
|
|
|
|
|
2016-01-09 21:39:38 +01:00
|
|
|
/// Implementation of evm Externalities.
|
2016-01-13 13:16:53 +01:00
|
|
|
pub struct Externalities<'a> {
|
|
|
|
pub state: &'a mut State,
|
2016-01-09 17:55:47 +01:00
|
|
|
info: &'a EnvInfo,
|
|
|
|
engine: &'a Engine,
|
|
|
|
depth: usize,
|
2016-01-13 13:16:53 +01:00
|
|
|
pub params: &'a ActionParams,
|
2016-01-11 02:17:29 +01:00
|
|
|
substate: &'a mut Substate,
|
2016-01-11 16:28:30 +01:00
|
|
|
schedule: Schedule,
|
2016-01-11 02:17:29 +01:00
|
|
|
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-13 13:16:53 +01:00
|
|
|
pub fn new(state: &'a mut State,
|
2016-01-11 02:17:29 +01:00
|
|
|
info: &'a EnvInfo,
|
|
|
|
engine: &'a Engine,
|
|
|
|
depth: usize,
|
2016-01-11 16:33:08 +01:00
|
|
|
params: &'a ActionParams,
|
2016-01-11 02:17:29 +01:00
|
|
|
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,
|
2016-01-11 16:28:30 +01:00
|
|
|
schedule: engine.schedule(info),
|
2016-01-11 02:17:29 +01:00
|
|
|
output: output
|
2016-01-09 17:55:47 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-11 03:26:17 +01:00
|
|
|
|
|
|
|
/// Creates `Externalities` from `Executive`.
|
2016-01-11 19:25:37 +01:00
|
|
|
fn from_executive(e: &'a mut Executive, params: &'a ActionParams, substate: &'a mut Substate, output: OutputPolicy<'a>) -> Self {
|
2016-01-12 15:12:03 +01:00
|
|
|
Self::new(e.state, e.info, e.engine, e.depth, params, substate, output)
|
2016-01-11 03:26:17 +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 {
|
2016-01-13 01:23:01 +01:00
|
|
|
match *number < U256::from(self.info.number) && number.low_u64() >= cmp::max(256, self.info.number) - 256 {
|
2016-01-09 00:51:09 +01:00
|
|
|
true => {
|
2016-01-13 01:23:01 +01:00
|
|
|
let index = self.info.number - number.low_u64() - 1;
|
|
|
|
self.info.last_hashes[index as usize].clone()
|
|
|
|
},
|
|
|
|
false => H256::from(&U256::zero()),
|
2016-01-09 00:51:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-11 17:01:42 +01:00
|
|
|
fn create(&mut self, gas: u64, value: &U256, code: &[u8]) -> Result<(u64, Option<Address>), evm::Error> {
|
2016-01-10 16:21:01 +01:00
|
|
|
// if balance is insufficient or we are to deep, return
|
2016-01-12 15:12:03 +01:00
|
|
|
if self.state.balance(&self.params.address) < *value || self.depth >= self.schedule.stack_limit {
|
2016-01-11 14:08:03 +01:00
|
|
|
return Ok((gas, None));
|
2016-01-10 16:21:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// create new contract address
|
|
|
|
let address = contract_address(&self.params.address, &self.state.nonce(&self.params.address));
|
|
|
|
|
|
|
|
// prepare the params
|
2016-01-11 16:33:08 +01:00
|
|
|
let params = ActionParams {
|
2016-01-10 16:21:01 +01:00
|
|
|
address: address.clone(),
|
|
|
|
sender: self.params.address.clone(),
|
|
|
|
origin: self.params.origin.clone(),
|
|
|
|
gas: U256::from(gas),
|
|
|
|
gas_price: self.params.gas_price.clone(),
|
2016-01-11 14:08:03 +01:00
|
|
|
value: value.clone(),
|
2016-01-10 16:21:01 +01:00
|
|
|
code: code.to_vec(),
|
|
|
|
data: vec![],
|
|
|
|
};
|
|
|
|
|
2016-01-12 15:12:03 +01:00
|
|
|
let mut ex = Executive::from_parent(self.state, self.info, self.engine, self.depth);
|
2016-01-11 14:08:03 +01:00
|
|
|
ex.state.inc_nonce(&self.params.address);
|
|
|
|
ex.create(¶ms, self.substate).map(|gas_left| (gas_left.low_u64(), Some(address)))
|
2016-01-09 00:51:09 +01:00
|
|
|
}
|
|
|
|
|
2016-01-11 17:01:42 +01:00
|
|
|
fn call(&mut self, gas: u64, call_gas: u64, receive_address: &Address, value: &U256, data: &[u8], code_address: &Address, output: &mut [u8]) -> Result<u64, evm::Error> {
|
2016-01-10 16:21:01 +01:00
|
|
|
let mut gas_cost = call_gas;
|
|
|
|
let mut call_gas = call_gas;
|
|
|
|
|
|
|
|
let is_call = receive_address == code_address;
|
2016-01-12 13:54:54 +01:00
|
|
|
if is_call && !self.state.exists(&code_address) {
|
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 {
|
2016-01-11 17:01:42 +01:00
|
|
|
return Err(evm::Error::OutOfGas)
|
2016-01-10 16:21:01 +01:00
|
|
|
}
|
|
|
|
|
2016-01-11 15:55:54 +01:00
|
|
|
let gas = gas - gas_cost;
|
2016-01-11 14:08:03 +01:00
|
|
|
|
|
|
|
// if balance is insufficient or we are to deep, return
|
2016-01-12 15:12:03 +01:00
|
|
|
if self.state.balance(&self.params.address) < *value || self.depth >= self.schedule.stack_limit {
|
2016-01-11 14:08:03 +01:00
|
|
|
return Ok(gas + call_gas)
|
2016-01-10 16:21:01 +01:00
|
|
|
}
|
|
|
|
|
2016-01-11 16:33:08 +01:00
|
|
|
let params = ActionParams {
|
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-12 15:12:03 +01:00
|
|
|
let mut ex = Executive::from_parent(self.state, self.info, self.engine, self.depth);
|
2016-01-12 13:39:12 +01:00
|
|
|
ex.call(¶ms, self.substate, output).map(|gas_left| {
|
|
|
|
gas + gas_left.low_u64()
|
|
|
|
})
|
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 17:01:42 +01:00
|
|
|
fn ret(&mut self, gas: u64, data: &[u8]) -> Result<u64, evm::Error> {
|
2016-01-11 02:17:29 +01:00
|
|
|
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);
|
2016-01-11 14:14:35 +01:00
|
|
|
Ok(gas)
|
2016-01-11 02:17:29 +01:00
|
|
|
},
|
|
|
|
&mut OutputPolicy::InitContract => {
|
|
|
|
let return_cost = data.len() as u64 * self.schedule.create_data_gas as u64;
|
|
|
|
if return_cost > gas {
|
2016-01-12 16:32:51 +01:00
|
|
|
return match self.schedule.exceptional_failed_code_deposit {
|
|
|
|
true => Err(evm::Error::OutOfGas),
|
|
|
|
false => Ok(gas)
|
2016-01-12 14:56:19 +01:00
|
|
|
}
|
2016-01-11 02:17:29 +01:00
|
|
|
}
|
|
|
|
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);
|
2016-01-12 16:05:54 +01:00
|
|
|
self.substate.contracts_created.push(address.clone());
|
2016-01-11 14:14:35 +01:00
|
|
|
Ok(gas - return_cost)
|
2016-01-11 02:17:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-01-11 16:28:30 +01:00
|
|
|
fn schedule(&self) -> &Schedule {
|
2016-01-11 02:17:29 +01:00
|
|
|
&self.schedule
|
|
|
|
}
|
2016-01-11 22:32:01 +01:00
|
|
|
|
|
|
|
fn env_info(&self) -> &EnvInfo {
|
|
|
|
&self.info
|
|
|
|
}
|
2016-01-07 19:05:44 +01:00
|
|
|
}
|
2016-01-07 23:33:54 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2016-01-11 17:49:49 +01:00
|
|
|
use super::*;
|
|
|
|
use common::*;
|
2016-01-09 01:33:50 +01:00
|
|
|
use state::*;
|
2016-01-10 16:21:01 +01:00
|
|
|
use ethereum;
|
2016-01-12 15:12:03 +01:00
|
|
|
use engine::*;
|
|
|
|
use spec::*;
|
|
|
|
use evm::Schedule;
|
2016-01-13 01:23:01 +01:00
|
|
|
//use super::Substate;
|
2016-01-09 01:33:50 +01:00
|
|
|
|
2016-01-12 15:12:03 +01:00
|
|
|
struct TestEngine {
|
2016-01-12 16:05:54 +01:00
|
|
|
spec: Spec,
|
|
|
|
stack_limit: usize
|
2016-01-12 15:12:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TestEngine {
|
2016-01-12 16:05:54 +01:00
|
|
|
fn new(stack_limit: usize) -> TestEngine {
|
2016-01-12 15:12:03 +01:00
|
|
|
TestEngine {
|
2016-01-13 01:23:01 +01:00
|
|
|
spec: ethereum::new_frontier_test(),
|
2016-01-12 16:05:54 +01:00
|
|
|
stack_limit: stack_limit
|
2016-01-12 15:12:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Engine for TestEngine {
|
|
|
|
fn name(&self) -> &str { "TestEngine" }
|
|
|
|
fn spec(&self) -> &Spec { &self.spec }
|
|
|
|
fn schedule(&self, _env_info: &EnvInfo) -> Schedule {
|
|
|
|
let mut schedule = Schedule::new_frontier();
|
2016-01-12 16:05:54 +01:00
|
|
|
schedule.stack_limit = self.stack_limit;
|
2016-01-12 15:12:03 +01:00
|
|
|
schedule
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-12 13:39:12 +01:00
|
|
|
fn test_sender_balance() {
|
2016-01-09 02:12:17 +01:00
|
|
|
let sender = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap();
|
|
|
|
let address = contract_address(&sender, &U256::zero());
|
2016-01-11 16:33:08 +01:00
|
|
|
let mut params = ActionParams::new();
|
2016-01-09 02:12:17 +01:00
|
|
|
params.address = address.clone();
|
|
|
|
params.sender = sender.clone();
|
2016-01-12 13:39:12 +01:00
|
|
|
params.gas = U256::from(100_000);
|
2016-01-09 02:12:17 +01:00
|
|
|
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();
|
2016-01-12 16:05:54 +01:00
|
|
|
let engine = TestEngine::new(0);
|
2016-01-09 21:39:38 +01:00
|
|
|
let mut substate = Substate::new();
|
2016-01-09 02:12:17 +01:00
|
|
|
|
2016-01-12 13:39:12 +01:00
|
|
|
let gas_left = {
|
2016-01-12 15:12:03 +01:00
|
|
|
let mut ex = Executive::new(&mut state, &info, &engine);
|
2016-01-12 13:39:12 +01:00
|
|
|
ex.create(¶ms, &mut substate).unwrap()
|
|
|
|
};
|
2016-01-09 02:12:17 +01:00
|
|
|
|
2016-01-12 13:39:12 +01:00
|
|
|
assert_eq!(gas_left, U256::from(79_975));
|
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-12 16:05:54 +01:00
|
|
|
// 0 cause contract hasn't returned
|
|
|
|
assert_eq!(substate.contracts_created.len(), 0);
|
2016-01-11 17:37:22 +01:00
|
|
|
|
|
|
|
// TODO: just test state root.
|
2016-01-09 01:33:50 +01:00
|
|
|
}
|
2016-01-09 13:51:59 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_create_contract() {
|
2016-01-12 16:05:54 +01:00
|
|
|
// code:
|
|
|
|
//
|
2016-01-12 13:39:12 +01:00
|
|
|
// 7c 601080600c6000396000f3006000355415600957005b60203560003555 - push 29 bytes?
|
|
|
|
// 60 00 - push 0
|
|
|
|
// 52
|
|
|
|
// 60 1d - push 29
|
|
|
|
// 60 03 - push 3
|
|
|
|
// 60 17 - push 17
|
|
|
|
// f0 - create
|
|
|
|
// 60 00 - push 0
|
|
|
|
// 55 sstore
|
2016-01-12 16:05:54 +01:00
|
|
|
//
|
|
|
|
// other code:
|
|
|
|
//
|
|
|
|
// 60 10 - push 16
|
|
|
|
// 80 - duplicate first stack item
|
|
|
|
// 60 0c - push 12
|
|
|
|
// 60 00 - push 0
|
|
|
|
// 39 - copy current code to memory
|
|
|
|
// 60 00 - push 0
|
|
|
|
// f3 - return
|
2016-01-12 13:39:12 +01:00
|
|
|
|
|
|
|
let code = "7c601080600c6000396000f3006000355415600957005b60203560003555600052601d60036017f0600055".from_hex().unwrap();
|
|
|
|
|
2016-01-09 13:51:59 +01:00
|
|
|
let sender = Address::from_str("cd1722f3947def4cf144679da39c4c32bdc35681").unwrap();
|
|
|
|
let address = contract_address(&sender, &U256::zero());
|
2016-01-12 13:39:12 +01:00
|
|
|
// TODO: add tests for 'callcreate'
|
|
|
|
//let next_address = contract_address(&address, &U256::zero());
|
2016-01-11 16:33:08 +01:00
|
|
|
let mut params = ActionParams::new();
|
2016-01-09 13:51:59 +01:00
|
|
|
params.address = address.clone();
|
|
|
|
params.sender = sender.clone();
|
|
|
|
params.origin = sender.clone();
|
2016-01-12 13:39:12 +01:00
|
|
|
params.gas = U256::from(100_000);
|
|
|
|
params.code = code.clone();
|
2016-01-12 16:05:54 +01:00
|
|
|
params.value = U256::from(100);
|
2016-01-09 13:51:59 +01:00
|
|
|
let mut state = State::new_temp();
|
2016-01-12 13:39:12 +01:00
|
|
|
state.add_balance(&sender, &U256::from(100));
|
2016-01-09 13:51:59 +01:00
|
|
|
let info = EnvInfo::new();
|
2016-01-12 16:05:54 +01:00
|
|
|
let engine = TestEngine::new(0);
|
2016-01-09 21:39:38 +01:00
|
|
|
let mut substate = Substate::new();
|
2016-01-09 13:51:59 +01:00
|
|
|
|
2016-01-12 13:39:12 +01:00
|
|
|
let gas_left = {
|
2016-01-12 15:12:03 +01:00
|
|
|
let mut ex = Executive::new(&mut state, &info, &engine);
|
2016-01-12 13:39:12 +01:00
|
|
|
ex.create(¶ms, &mut substate).unwrap()
|
|
|
|
};
|
2016-01-09 13:51:59 +01:00
|
|
|
|
2016-01-13 13:16:53 +01:00
|
|
|
assert_eq!(gas_left, U256::from(62_976));
|
|
|
|
// ended with max depth
|
|
|
|
assert_eq!(substate.contracts_created.len(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_create_contract_value_too_high() {
|
|
|
|
// code:
|
|
|
|
//
|
|
|
|
// 7c 601080600c6000396000f3006000355415600957005b60203560003555 - push 29 bytes?
|
|
|
|
// 60 00 - push 0
|
|
|
|
// 52
|
|
|
|
// 60 1d - push 29
|
|
|
|
// 60 03 - push 3
|
|
|
|
// 60 e6 - push 230
|
|
|
|
// f0 - create a contract trying to send 230.
|
|
|
|
// 60 00 - push 0
|
|
|
|
// 55 sstore
|
|
|
|
//
|
|
|
|
// other code:
|
|
|
|
//
|
|
|
|
// 60 10 - push 16
|
|
|
|
// 80 - duplicate first stack item
|
|
|
|
// 60 0c - push 12
|
|
|
|
// 60 00 - push 0
|
|
|
|
// 39 - copy current code to memory
|
|
|
|
// 60 00 - push 0
|
|
|
|
// f3 - return
|
|
|
|
|
|
|
|
let code = "7c601080600c6000396000f3006000355415600957005b60203560003555600052601d600360e6f0600055".from_hex().unwrap();
|
|
|
|
|
|
|
|
let sender = Address::from_str("cd1722f3947def4cf144679da39c4c32bdc35681").unwrap();
|
|
|
|
let address = contract_address(&sender, &U256::zero());
|
|
|
|
// TODO: add tests for 'callcreate'
|
|
|
|
//let next_address = contract_address(&address, &U256::zero());
|
|
|
|
let mut params = ActionParams::new();
|
|
|
|
params.address = address.clone();
|
|
|
|
params.sender = sender.clone();
|
|
|
|
params.origin = sender.clone();
|
|
|
|
params.gas = U256::from(100_000);
|
|
|
|
params.code = code.clone();
|
|
|
|
params.value = U256::from(100);
|
|
|
|
let mut state = State::new_temp();
|
|
|
|
state.add_balance(&sender, &U256::from(100));
|
|
|
|
let info = EnvInfo::new();
|
|
|
|
let engine = TestEngine::new(0);
|
|
|
|
let mut substate = Substate::new();
|
|
|
|
|
|
|
|
let gas_left = {
|
|
|
|
let mut ex = Executive::new(&mut state, &info, &engine);
|
|
|
|
ex.create(¶ms, &mut substate).unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(gas_left, U256::from(62_976));
|
2016-01-12 16:05:54 +01:00
|
|
|
assert_eq!(substate.contracts_created.len(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_create_contract_without_stack_limit() {
|
|
|
|
// code:
|
|
|
|
//
|
|
|
|
// 7c 601080600c6000396000f3006000355415600957005b60203560003555 - push 29 bytes?
|
|
|
|
// 60 00 - push 0
|
|
|
|
// 52
|
|
|
|
// 60 1d - push 29
|
|
|
|
// 60 03 - push 3
|
|
|
|
// 60 17 - push 17
|
|
|
|
// f0 - create
|
|
|
|
// 60 00 - push 0
|
|
|
|
// 55 sstore
|
|
|
|
//
|
|
|
|
// other code:
|
|
|
|
//
|
|
|
|
// 60 10 - push 16
|
|
|
|
// 80 - duplicate first stack item
|
|
|
|
// 60 0c - push 12
|
|
|
|
// 60 00 - push 0
|
|
|
|
// 39 - copy current code to memory
|
|
|
|
// 60 00 - push 0
|
|
|
|
// f3 - return
|
|
|
|
|
|
|
|
let code = "7c601080600c6000396000f3006000355415600957005b60203560003555600052601d60036017f0600055".from_hex().unwrap();
|
|
|
|
|
|
|
|
let sender = Address::from_str("cd1722f3947def4cf144679da39c4c32bdc35681").unwrap();
|
|
|
|
let address = contract_address(&sender, &U256::zero());
|
|
|
|
let next_address = contract_address(&address, &U256::zero());
|
|
|
|
let mut params = ActionParams::new();
|
|
|
|
params.address = address.clone();
|
|
|
|
params.sender = sender.clone();
|
|
|
|
params.origin = sender.clone();
|
|
|
|
params.gas = U256::from(100_000);
|
|
|
|
params.code = code.clone();
|
|
|
|
params.value = U256::from(100);
|
|
|
|
let mut state = State::new_temp();
|
|
|
|
state.add_balance(&sender, &U256::from(100));
|
|
|
|
let info = EnvInfo::new();
|
|
|
|
let engine = TestEngine::new(1024);
|
|
|
|
let mut substate = Substate::new();
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut ex = Executive::new(&mut state, &info, &engine);
|
|
|
|
ex.create(¶ms, &mut substate).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(substate.contracts_created.len(), 1);
|
|
|
|
assert_eq!(substate.contracts_created[0], next_address);
|
2016-01-12 13:39:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_aba_calls() {
|
|
|
|
// 60 00 - push 0
|
|
|
|
// 60 00 - push 0
|
|
|
|
// 60 00 - push 0
|
|
|
|
// 60 00 - push 0
|
|
|
|
// 60 18 - push 18
|
|
|
|
// 73 945304eb96065b2a98b57a48a06ae28d285a71b5 - push this address
|
|
|
|
// 61 03e8 - push 1000
|
|
|
|
// f1 - message call
|
|
|
|
// 58 - get PC
|
|
|
|
// 55 - sstore
|
|
|
|
|
|
|
|
let code_a = "6000600060006000601873945304eb96065b2a98b57a48a06ae28d285a71b56103e8f15855".from_hex().unwrap();
|
|
|
|
|
|
|
|
// 60 00 - push 0
|
|
|
|
// 60 00 - push 0
|
|
|
|
// 60 00 - push 0
|
|
|
|
// 60 00 - push 0
|
|
|
|
// 60 17 - push 17
|
|
|
|
// 73 0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6 - push this address
|
|
|
|
// 61 0x01f4 - push 500
|
|
|
|
// f1 - message call
|
|
|
|
// 60 01 - push 1
|
|
|
|
// 01 - add
|
|
|
|
// 58 - get PC
|
|
|
|
// 55 - sstore
|
|
|
|
let code_b = "60006000600060006017730f572e5295c57f15886f9b263e2f6d2d6c7b5ec66101f4f16001015855".from_hex().unwrap();
|
|
|
|
|
|
|
|
let address_a = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap();
|
|
|
|
let address_b = Address::from_str("945304eb96065b2a98b57a48a06ae28d285a71b5" ).unwrap();
|
|
|
|
let sender = Address::from_str("cd1722f3947def4cf144679da39c4c32bdc35681").unwrap();
|
|
|
|
|
|
|
|
let mut params = ActionParams::new();
|
|
|
|
params.address = address_a.clone();
|
|
|
|
params.sender = sender.clone();
|
|
|
|
params.gas = U256::from(100_000);
|
|
|
|
params.code = code_a.clone();
|
|
|
|
params.value = U256::from(100_000);
|
|
|
|
|
|
|
|
let mut state = State::new_temp();
|
|
|
|
state.init_code(&address_a, code_a.clone());
|
|
|
|
state.init_code(&address_b, code_b.clone());
|
|
|
|
state.add_balance(&sender, &U256::from(100_000));
|
|
|
|
|
|
|
|
let info = EnvInfo::new();
|
2016-01-12 16:05:54 +01:00
|
|
|
let engine = TestEngine::new(0);
|
2016-01-12 13:39:12 +01:00
|
|
|
let mut substate = Substate::new();
|
|
|
|
|
|
|
|
let gas_left = {
|
2016-01-12 15:12:03 +01:00
|
|
|
let mut ex = Executive::new(&mut state, &info, &engine);
|
2016-01-12 13:39:12 +01:00
|
|
|
ex.call(¶ms, &mut substate, &mut []).unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(gas_left, U256::from(73_237));
|
|
|
|
assert_eq!(state.storage_at(&address_a, &H256::from(&U256::from(0x23))), H256::from(&U256::from(1)));
|
2016-01-10 16:21:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
2016-01-12 13:39:12 +01:00
|
|
|
// 55 - sstore
|
2016-01-10 16:21:01 +01:00
|
|
|
let sender = Address::from_str("cd1722f3947def4cf144679da39c4c32bdc35681").unwrap();
|
2016-01-11 14:08:03 +01:00
|
|
|
let code = "600160005401600055600060006000600060003060e05a03f1600155".from_hex().unwrap();
|
2016-01-10 16:21:01 +01:00
|
|
|
let address = contract_address(&sender, &U256::zero());
|
2016-01-11 16:33:08 +01:00
|
|
|
let mut params = ActionParams::new();
|
2016-01-10 16:21:01 +01:00
|
|
|
params.address = address.clone();
|
2016-01-12 13:39:12 +01:00
|
|
|
params.gas = U256::from(100_000);
|
2016-01-10 16:21:01 +01:00
|
|
|
params.code = code.clone();
|
|
|
|
let mut state = State::new_temp();
|
|
|
|
state.init_code(&address, code.clone());
|
|
|
|
let info = EnvInfo::new();
|
2016-01-12 16:05:54 +01:00
|
|
|
let engine = TestEngine::new(0);
|
2016-01-10 16:21:01 +01:00
|
|
|
let mut substate = Substate::new();
|
|
|
|
|
2016-01-12 13:39:12 +01:00
|
|
|
let gas_left = {
|
2016-01-12 15:12:03 +01:00
|
|
|
let mut ex = Executive::new(&mut state, &info, &engine);
|
2016-01-12 13:39:12 +01:00
|
|
|
ex.call(¶ms, &mut substate, &mut []).unwrap()
|
|
|
|
};
|
2016-01-10 16:21:01 +01:00
|
|
|
|
2016-01-12 13:39:12 +01:00
|
|
|
assert_eq!(gas_left, U256::from(59_870));
|
|
|
|
assert_eq!(state.storage_at(&address, &H256::from(&U256::zero())), H256::from(&U256::from(1)));
|
|
|
|
assert_eq!(state.storage_at(&address, &H256::from(&U256::one())), H256::from(&U256::from(1)));
|
2016-01-09 13:51:59 +01:00
|
|
|
}
|
2016-01-12 18:31:47 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transact_simple() {
|
2016-01-12 19:11:05 +01:00
|
|
|
let mut t = Transaction::new_create(U256::from(17), "3331600055".from_hex().unwrap(), U256::from(100_000), U256::zero(), U256::zero());
|
|
|
|
let keypair = KeyPair::create().unwrap();
|
|
|
|
t.sign(&keypair.secret());
|
2016-01-12 18:31:47 +01:00
|
|
|
let sender = t.sender().unwrap();
|
2016-01-12 19:43:26 +01:00
|
|
|
let contract = contract_address(&sender, &U256::zero());
|
2016-01-12 18:31:47 +01:00
|
|
|
|
|
|
|
let mut state = State::new_temp();
|
2016-01-12 19:43:26 +01:00
|
|
|
state.add_balance(&sender, &U256::from(18));
|
2016-01-12 18:31:47 +01:00
|
|
|
let mut info = EnvInfo::new();
|
|
|
|
info.gas_limit = U256::from(100_000);
|
|
|
|
let engine = TestEngine::new(0);
|
|
|
|
|
|
|
|
let executed = {
|
|
|
|
let mut ex = Executive::new(&mut state, &info, &engine);
|
|
|
|
ex.transact(&t).unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(executed.gas, U256::from(100_000));
|
2016-01-12 19:43:26 +01:00
|
|
|
assert_eq!(executed.gas_used, U256::from(20_025));
|
|
|
|
assert_eq!(executed.refunded, U256::from(79_975));
|
|
|
|
assert_eq!(executed.cumulative_gas_used, U256::from(20_025));
|
2016-01-12 18:31:47 +01:00
|
|
|
assert_eq!(executed.logs.len(), 0);
|
|
|
|
assert_eq!(executed.out_of_gas, false);
|
|
|
|
assert_eq!(executed.contracts_created.len(), 0);
|
2016-01-12 19:43:26 +01:00
|
|
|
assert_eq!(state.balance(&sender), U256::from(1));
|
|
|
|
assert_eq!(state.balance(&contract), U256::from(17));
|
|
|
|
assert_eq!(state.nonce(&sender), U256::from(1));
|
|
|
|
assert_eq!(state.storage_at(&contract, &H256::new()), H256::from(&U256::from(1)));
|
2016-01-12 18:31:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transact_invalid_sender() {
|
2016-01-12 19:43:26 +01:00
|
|
|
let t = Transaction::new_create(U256::from(17), "3331600055".from_hex().unwrap(), U256::from(100_000), U256::zero(), U256::zero());
|
2016-01-12 18:31:47 +01:00
|
|
|
|
|
|
|
let mut state = State::new_temp();
|
2016-01-12 19:43:26 +01:00
|
|
|
let mut info = EnvInfo::new();
|
|
|
|
info.gas_limit = U256::from(100_000);
|
2016-01-12 18:31:47 +01:00
|
|
|
let engine = TestEngine::new(0);
|
|
|
|
|
|
|
|
let res = {
|
|
|
|
let mut ex = Executive::new(&mut state, &info, &engine);
|
|
|
|
ex.transact(&t)
|
|
|
|
};
|
|
|
|
|
|
|
|
match res {
|
|
|
|
Err(Error::Util(UtilError::Crypto(CryptoError::InvalidSignature))) => (),
|
2016-01-12 19:43:26 +01:00
|
|
|
_ => assert!(false, "Expected invalid signature error.")
|
2016-01-12 18:31:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transact_invalid_nonce() {
|
2016-01-12 19:11:05 +01:00
|
|
|
let mut t = Transaction::new_create(U256::from(17), "3331600055".from_hex().unwrap(), U256::from(100_000), U256::zero(), U256::one());
|
|
|
|
let keypair = KeyPair::create().unwrap();
|
|
|
|
t.sign(&keypair.secret());
|
2016-01-12 19:43:26 +01:00
|
|
|
let sender = t.sender().unwrap();
|
2016-01-12 19:11:05 +01:00
|
|
|
|
2016-01-12 18:31:47 +01:00
|
|
|
let mut state = State::new_temp();
|
2016-01-12 19:43:26 +01:00
|
|
|
state.add_balance(&sender, &U256::from(17));
|
|
|
|
let mut info = EnvInfo::new();
|
|
|
|
info.gas_limit = U256::from(100_000);
|
|
|
|
let engine = TestEngine::new(0);
|
|
|
|
|
|
|
|
let res = {
|
|
|
|
let mut ex = Executive::new(&mut state, &info, &engine);
|
|
|
|
ex.transact(&t)
|
|
|
|
};
|
|
|
|
|
|
|
|
match res {
|
|
|
|
Err(Error::Execution(ExecutionError::InvalidNonce { expected, is }))
|
|
|
|
if expected == U256::zero() && is == U256::one() => (),
|
|
|
|
_ => assert!(false, "Expected invalid nonce error.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transact_gas_limit_reached() {
|
|
|
|
let mut t = Transaction::new_create(U256::from(17), "3331600055".from_hex().unwrap(), U256::from(80_001), U256::zero(), U256::zero());
|
|
|
|
let keypair = KeyPair::create().unwrap();
|
|
|
|
t.sign(&keypair.secret());
|
|
|
|
let sender = t.sender().unwrap();
|
|
|
|
|
|
|
|
let mut state = State::new_temp();
|
|
|
|
state.add_balance(&sender, &U256::from(17));
|
|
|
|
let mut info = EnvInfo::new();
|
|
|
|
info.gas_used = U256::from(20_000);
|
|
|
|
info.gas_limit = U256::from(100_000);
|
|
|
|
let engine = TestEngine::new(0);
|
|
|
|
|
|
|
|
let res = {
|
|
|
|
let mut ex = Executive::new(&mut state, &info, &engine);
|
|
|
|
ex.transact(&t)
|
|
|
|
};
|
|
|
|
|
|
|
|
match res {
|
|
|
|
Err(Error::Execution(ExecutionError::BlockGasLimitReached { gas_limit, gas_used, gas }))
|
|
|
|
if gas_limit == U256::from(100_000) && gas_used == U256::from(20_000) && gas == U256::from(80_001) => (),
|
|
|
|
_ => assert!(false, "Expected block gas limit error.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_not_enough_cash() {
|
|
|
|
let mut t = Transaction::new_create(U256::from(18), "3331600055".from_hex().unwrap(), U256::from(100_000), U256::one(), U256::zero());
|
|
|
|
let keypair = KeyPair::create().unwrap();
|
|
|
|
t.sign(&keypair.secret());
|
|
|
|
let sender = t.sender().unwrap();
|
|
|
|
|
|
|
|
let mut state = State::new_temp();
|
|
|
|
state.add_balance(&sender, &U256::from(100_017));
|
|
|
|
let mut info = EnvInfo::new();
|
|
|
|
info.gas_limit = U256::from(100_000);
|
2016-01-12 18:31:47 +01:00
|
|
|
let engine = TestEngine::new(0);
|
|
|
|
|
|
|
|
let res = {
|
|
|
|
let mut ex = Executive::new(&mut state, &info, &engine);
|
|
|
|
ex.transact(&t)
|
|
|
|
};
|
|
|
|
|
|
|
|
match res {
|
2016-01-12 19:43:26 +01:00
|
|
|
Err(Error::Execution(ExecutionError::NotEnoughCash { required , is }))
|
2016-01-13 13:16:53 +01:00
|
|
|
if required == U512::from(100_018) && is == U512::from(100_017) => (),
|
|
|
|
_ => assert!(false, "Expected not enough cash error. {:?}", res)
|
2016-01-12 18:31:47 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-07 23:33:54 +01:00
|
|
|
}
|