2016-02-05 13:40:41 +01:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-01-15 14:22:46 +01:00
|
|
|
//! Transaction Execution environment.
|
|
|
|
use common::*;
|
|
|
|
use state::*;
|
|
|
|
use engine::*;
|
|
|
|
use executive::*;
|
2016-01-16 20:11:12 +01:00
|
|
|
use evm::{self, Schedule, Ext, ContractCreateResult, MessageCallResult};
|
2016-01-15 14:22:46 +01:00
|
|
|
use substate::*;
|
|
|
|
|
|
|
|
/// Policy for handling output data on `RETURN` opcode.
|
|
|
|
pub enum OutputPolicy<'a> {
|
|
|
|
/// Return reference to fixed sized output.
|
|
|
|
/// Used for message calls.
|
|
|
|
Return(BytesRef<'a>),
|
|
|
|
/// Init new contract as soon as `RETURN` is called.
|
|
|
|
InitContract
|
|
|
|
}
|
|
|
|
|
2016-01-16 20:11:12 +01:00
|
|
|
/// Transaction properties that externalities need to know about.
|
|
|
|
pub struct OriginInfo {
|
|
|
|
address: Address,
|
|
|
|
origin: Address,
|
2016-01-23 10:41:13 +01:00
|
|
|
gas_price: U256,
|
|
|
|
value: U256
|
2016-01-16 20:11:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl OriginInfo {
|
|
|
|
/// Populates origin info from action params.
|
|
|
|
pub fn from(params: &ActionParams) -> Self {
|
|
|
|
OriginInfo {
|
|
|
|
address: params.address.clone(),
|
|
|
|
origin: params.origin.clone(),
|
2016-01-20 17:27:33 +01:00
|
|
|
gas_price: params.gas_price.clone(),
|
|
|
|
value: match params.value {
|
|
|
|
ActionValue::Transfer(val) => val,
|
|
|
|
ActionValue::Apparent(val) => val,
|
|
|
|
}
|
2016-01-16 20:11:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 14:22:46 +01:00
|
|
|
/// Implementation of evm Externalities.
|
|
|
|
pub struct Externalities<'a> {
|
|
|
|
state: &'a mut State,
|
2016-01-16 20:11:12 +01:00
|
|
|
env_info: &'a EnvInfo,
|
2016-01-15 14:22:46 +01:00
|
|
|
engine: &'a Engine,
|
|
|
|
depth: usize,
|
2016-01-16 20:11:12 +01:00
|
|
|
origin_info: OriginInfo,
|
2016-01-15 14:22:46 +01:00
|
|
|
substate: &'a mut Substate,
|
|
|
|
schedule: Schedule,
|
|
|
|
output: OutputPolicy<'a>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Externalities<'a> {
|
|
|
|
/// Basic `Externalities` constructor.
|
|
|
|
pub fn new(state: &'a mut State,
|
2016-01-16 20:11:12 +01:00
|
|
|
env_info: &'a EnvInfo,
|
2016-01-15 14:22:46 +01:00
|
|
|
engine: &'a Engine,
|
|
|
|
depth: usize,
|
2016-01-16 20:11:12 +01:00
|
|
|
origin_info: OriginInfo,
|
2016-01-15 14:22:46 +01:00
|
|
|
substate: &'a mut Substate,
|
|
|
|
output: OutputPolicy<'a>) -> Self {
|
|
|
|
Externalities {
|
|
|
|
state: state,
|
2016-01-16 20:11:12 +01:00
|
|
|
env_info: env_info,
|
2016-01-15 14:22:46 +01:00
|
|
|
engine: engine,
|
|
|
|
depth: depth,
|
2016-01-16 20:11:12 +01:00
|
|
|
origin_info: origin_info,
|
2016-01-15 14:22:46 +01:00
|
|
|
substate: substate,
|
2016-01-16 20:11:12 +01:00
|
|
|
schedule: engine.schedule(env_info),
|
2016-01-15 14:22:46 +01:00
|
|
|
output: output
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Ext for Externalities<'a> {
|
2016-01-15 18:56:28 +01:00
|
|
|
fn storage_at(&self, key: &H256) -> H256 {
|
2016-01-16 20:11:12 +01:00
|
|
|
self.state.storage_at(&self.origin_info.address, key)
|
2016-01-15 14:22:46 +01:00
|
|
|
}
|
|
|
|
|
2016-01-16 20:11:12 +01:00
|
|
|
fn set_storage(&mut self, key: H256, value: H256) {
|
|
|
|
self.state.set_storage(&self.origin_info.address, key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exists(&self, address: &Address) -> bool {
|
|
|
|
self.state.exists(address)
|
2016-01-15 14:22:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn balance(&self, address: &Address) -> U256 {
|
|
|
|
self.state.balance(address)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn blockhash(&self, number: &U256) -> H256 {
|
2016-01-16 20:11:12 +01:00
|
|
|
match *number < U256::from(self.env_info.number) && number.low_u64() >= cmp::max(256, self.env_info.number) - 256 {
|
2016-01-15 14:22:46 +01:00
|
|
|
true => {
|
2016-01-16 20:11:12 +01:00
|
|
|
let index = self.env_info.number - number.low_u64() - 1;
|
|
|
|
let r = self.env_info.last_hashes[index as usize].clone();
|
|
|
|
trace!("ext: blockhash({}) -> {} self.env_info.number={}\n", number, r, self.env_info.number);
|
2016-01-15 22:55:04 +01:00
|
|
|
r
|
|
|
|
},
|
|
|
|
false => {
|
2016-01-16 20:11:12 +01:00
|
|
|
trace!("ext: blockhash({}) -> null self.env_info.number={}\n", number, self.env_info.number);
|
2016-01-15 22:55:04 +01:00
|
|
|
H256::from(&U256::zero())
|
2016-01-15 14:22:46 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-16 20:11:12 +01:00
|
|
|
fn create(&mut self, gas: &U256, value: &U256, code: &[u8]) -> ContractCreateResult {
|
2016-01-15 14:22:46 +01:00
|
|
|
// create new contract address
|
2016-01-16 20:11:12 +01:00
|
|
|
let address = contract_address(&self.origin_info.address, &self.state.nonce(&self.origin_info.address));
|
2016-01-15 14:22:46 +01:00
|
|
|
|
|
|
|
// prepare the params
|
|
|
|
let params = ActionParams {
|
2016-01-15 15:14:41 +01:00
|
|
|
code_address: address.clone(),
|
2016-01-15 14:22:46 +01:00
|
|
|
address: address.clone(),
|
2016-01-16 20:11:12 +01:00
|
|
|
sender: self.origin_info.address.clone(),
|
|
|
|
origin: self.origin_info.origin.clone(),
|
2016-01-15 14:22:46 +01:00
|
|
|
gas: *gas,
|
2016-01-16 20:11:12 +01:00
|
|
|
gas_price: self.origin_info.gas_price.clone(),
|
2016-01-20 17:27:33 +01:00
|
|
|
value: ActionValue::Transfer(value.clone()),
|
2016-01-15 15:14:41 +01:00
|
|
|
code: Some(code.to_vec()),
|
|
|
|
data: None,
|
2016-01-15 14:22:46 +01:00
|
|
|
};
|
|
|
|
|
2016-01-16 20:11:12 +01:00
|
|
|
self.state.inc_nonce(&self.origin_info.address);
|
|
|
|
let mut ex = Executive::from_parent(self.state, self.env_info, self.engine, self.depth);
|
|
|
|
|
|
|
|
// TODO: handle internal error separately
|
|
|
|
match ex.create(params, self.substate) {
|
|
|
|
Ok(gas_left) => {
|
|
|
|
self.substate.contracts_created.push(address.clone());
|
|
|
|
ContractCreateResult::Created(address, gas_left)
|
|
|
|
},
|
|
|
|
_ => ContractCreateResult::Failed
|
2016-01-15 14:22:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-23 10:41:13 +01:00
|
|
|
fn call(&mut self,
|
2016-01-20 16:52:22 +01:00
|
|
|
gas: &U256,
|
2016-01-23 10:41:13 +01:00
|
|
|
sender_address: &Address,
|
|
|
|
receive_address: &Address,
|
2016-01-25 23:59:50 +01:00
|
|
|
value: Option<U256>,
|
2016-01-20 16:52:22 +01:00
|
|
|
data: &[u8],
|
|
|
|
code_address: &Address,
|
|
|
|
output: &mut [u8]) -> MessageCallResult {
|
|
|
|
|
2016-01-23 10:41:13 +01:00
|
|
|
let mut params = ActionParams {
|
|
|
|
sender: sender_address.clone(),
|
|
|
|
address: receive_address.clone(),
|
|
|
|
value: ActionValue::Apparent(self.origin_info.value.clone()),
|
2016-01-20 16:52:22 +01:00
|
|
|
code_address: code_address.clone(),
|
|
|
|
origin: self.origin_info.origin.clone(),
|
|
|
|
gas: *gas,
|
|
|
|
gas_price: self.origin_info.gas_price.clone(),
|
|
|
|
code: self.state.code(code_address),
|
|
|
|
data: Some(data.to_vec()),
|
|
|
|
};
|
|
|
|
|
2016-01-23 10:41:13 +01:00
|
|
|
if let Some(value) = value {
|
2016-01-25 23:59:50 +01:00
|
|
|
params.value = ActionValue::Transfer(value);
|
2016-01-20 16:52:22 +01:00
|
|
|
}
|
2016-01-15 14:22:46 +01:00
|
|
|
|
2016-01-16 20:11:12 +01:00
|
|
|
let mut ex = Executive::from_parent(self.state, self.env_info, self.engine, self.depth);
|
2016-01-15 22:55:04 +01:00
|
|
|
|
2016-01-16 20:11:12 +01:00
|
|
|
match ex.call(params, self.substate, BytesRef::Fixed(output)) {
|
|
|
|
Ok(gas_left) => MessageCallResult::Success(gas_left),
|
|
|
|
_ => MessageCallResult::Failed
|
2016-01-15 14:22:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-16 17:17:43 +01:00
|
|
|
fn extcode(&self, address: &Address) -> Bytes {
|
2016-01-19 13:47:30 +01:00
|
|
|
self.state.code(address).unwrap_or_else(|| vec![])
|
2016-01-15 14:22:46 +01:00
|
|
|
}
|
|
|
|
|
2016-01-17 15:56:09 +01:00
|
|
|
#[allow(match_ref_pats)]
|
2016-01-15 14:22:46 +01:00
|
|
|
fn ret(&mut self, gas: &U256, data: &[u8]) -> Result<U256, evm::Error> {
|
|
|
|
match &mut self.output {
|
|
|
|
&mut OutputPolicy::Return(BytesRef::Fixed(ref mut slice)) => unsafe {
|
|
|
|
let len = cmp::min(slice.len(), data.len());
|
|
|
|
ptr::copy(data.as_ptr(), slice.as_mut_ptr(), len);
|
|
|
|
Ok(*gas)
|
|
|
|
},
|
|
|
|
&mut OutputPolicy::Return(BytesRef::Flexible(ref mut vec)) => unsafe {
|
|
|
|
vec.clear();
|
|
|
|
vec.reserve(data.len());
|
|
|
|
ptr::copy(data.as_ptr(), vec.as_mut_ptr(), data.len());
|
|
|
|
vec.set_len(data.len());
|
|
|
|
Ok(*gas)
|
|
|
|
},
|
|
|
|
&mut OutputPolicy::InitContract => {
|
|
|
|
let return_cost = U256::from(data.len()) * U256::from(self.schedule.create_data_gas);
|
|
|
|
if return_cost > *gas {
|
|
|
|
return match self.schedule.exceptional_failed_code_deposit {
|
|
|
|
true => Err(evm::Error::OutOfGas),
|
|
|
|
false => Ok(*gas)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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());
|
|
|
|
}
|
2016-01-16 20:11:12 +01:00
|
|
|
let address = &self.origin_info.address;
|
2016-01-15 14:22:46 +01:00
|
|
|
self.state.init_code(address, code);
|
|
|
|
Ok(*gas - return_cost)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-16 17:17:43 +01:00
|
|
|
fn log(&mut self, topics: Vec<H256>, data: &[u8]) {
|
2016-01-16 20:11:12 +01:00
|
|
|
let address = self.origin_info.address.clone();
|
2016-01-16 17:17:43 +01:00
|
|
|
self.substate.logs.push(LogEntry::new(address, topics, data.to_vec()));
|
2016-01-15 14:22:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn suicide(&mut self, refund_address: &Address) {
|
2016-01-16 20:11:12 +01:00
|
|
|
let address = self.origin_info.address.clone();
|
2016-01-15 14:22:46 +01:00
|
|
|
let balance = self.balance(&address);
|
2016-01-27 17:25:19 +01:00
|
|
|
if &address == refund_address {
|
|
|
|
// TODO [todr] To be consisted with CPP client we set balance to 0 in that case.
|
|
|
|
self.state.sub_balance(&address, &balance);
|
|
|
|
} else {
|
|
|
|
trace!("Suiciding {} -> {} (xfer: {})", address, refund_address, balance);
|
|
|
|
self.state.transfer_balance(&address, refund_address, &balance);
|
|
|
|
}
|
2016-01-15 14:22:46 +01:00
|
|
|
self.substate.suicides.insert(address);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn schedule(&self) -> &Schedule {
|
|
|
|
&self.schedule
|
|
|
|
}
|
|
|
|
|
|
|
|
fn env_info(&self) -> &EnvInfo {
|
2016-01-16 20:11:12 +01:00
|
|
|
&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();
|
2016-01-15 14:22:46 +01:00
|
|
|
}
|
|
|
|
}
|