2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-02-05 13:40:41 +01:00
|
|
|
// 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.
|
2017-07-29 17:12:07 +02:00
|
|
|
use std::cmp;
|
2017-07-29 21:56:42 +02:00
|
|
|
use std::sync::Arc;
|
2017-09-04 16:36:49 +02:00
|
|
|
use bigint::prelude::U256;
|
|
|
|
use bigint::hash::H256;
|
2016-10-24 18:35:25 +02:00
|
|
|
use util::*;
|
2017-09-06 20:47:45 +02:00
|
|
|
use bytes::{Bytes, BytesRef};
|
2017-06-28 09:10:57 +02:00
|
|
|
use state::{Backend as StateBackend, State, Substate, CleanupMode};
|
2017-09-26 14:19:08 +02:00
|
|
|
use machine::EthereumMachine as Machine;
|
2016-01-15 14:22:46 +01:00
|
|
|
use executive::*;
|
2017-08-01 12:37:57 +02:00
|
|
|
use vm::{
|
|
|
|
self, ActionParams, ActionValue, EnvInfo, CallType, Schedule,
|
|
|
|
Ext, ContractCreateResult, MessageCallResult, CreateContractAddress,
|
|
|
|
ReturnData
|
|
|
|
};
|
2017-09-15 21:07:54 +02:00
|
|
|
use evm::FinalizationResult;
|
2017-07-12 13:09:17 +02:00
|
|
|
use transaction::UNSIGNED_SENDER;
|
2016-06-02 12:40:31 +02:00
|
|
|
use trace::{Tracer, VMTracer};
|
2016-01-15 14:22:46 +01:00
|
|
|
|
|
|
|
/// Policy for handling output data on `RETURN` opcode.
|
2016-03-19 18:37:55 +01:00
|
|
|
pub enum OutputPolicy<'a, 'b> {
|
2016-01-15 14:22:46 +01:00
|
|
|
/// Return reference to fixed sized output.
|
|
|
|
/// Used for message calls.
|
2016-03-19 18:37:55 +01:00
|
|
|
Return(BytesRef<'a>, Option<&'b mut Bytes>),
|
2016-01-15 14:22:46 +01:00
|
|
|
/// Init new contract as soon as `RETURN` is called.
|
2016-03-19 18:46:41 +01:00
|
|
|
InitContract(Option<&'b mut Bytes>),
|
2016-01-15 14:22:46 +01:00
|
|
|
}
|
|
|
|
|
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,
|
2017-06-19 11:41:46 +02:00
|
|
|
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-02-14 12:54:27 +01:00
|
|
|
gas_price: params.gas_price,
|
2016-01-20 17:27:33 +01:00
|
|
|
value: match params.value {
|
2016-02-14 12:54:27 +01:00
|
|
|
ActionValue::Transfer(val) | ActionValue::Apparent(val) => val
|
2017-06-19 11:41:46 +02:00
|
|
|
},
|
2016-01-16 20:11:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 14:22:46 +01:00
|
|
|
/// Implementation of evm Externalities.
|
2017-09-26 14:19:08 +02:00
|
|
|
pub struct Externalities<'a, T: 'a, V: 'a, B: 'a>
|
2017-02-21 12:35:21 +01:00
|
|
|
where T: Tracer, V: VMTracer, B: StateBackend
|
|
|
|
{
|
|
|
|
state: &'a mut State<B>,
|
2016-01-16 20:11:12 +01:00
|
|
|
env_info: &'a EnvInfo,
|
2017-09-26 14:19:08 +02:00
|
|
|
machine: &'a Machine,
|
2016-01-15 14:22:46 +01:00
|
|
|
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,
|
2016-04-08 01:50:55 +02:00
|
|
|
output: OutputPolicy<'a, 'a>,
|
|
|
|
tracer: &'a mut T,
|
2016-06-02 12:40:31 +02:00
|
|
|
vm_tracer: &'a mut V,
|
2017-06-19 11:41:46 +02:00
|
|
|
static_flag: bool,
|
2016-01-15 14:22:46 +01:00
|
|
|
}
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
impl<'a, T: 'a, V: 'a, B: 'a> Externalities<'a, T, V, B>
|
|
|
|
where T: Tracer, V: VMTracer, B: StateBackend
|
2017-02-21 12:35:21 +01:00
|
|
|
{
|
2016-01-15 14:22:46 +01:00
|
|
|
/// Basic `Externalities` constructor.
|
2017-02-21 12:35:21 +01:00
|
|
|
pub fn new(state: &'a mut State<B>,
|
2016-03-19 18:37:55 +01:00
|
|
|
env_info: &'a EnvInfo,
|
2017-09-26 14:19:08 +02:00
|
|
|
machine: &'a Machine,
|
2016-03-19 18:37:55 +01:00
|
|
|
depth: usize,
|
|
|
|
origin_info: OriginInfo,
|
|
|
|
substate: &'a mut Substate,
|
2016-04-08 01:50:55 +02:00
|
|
|
output: OutputPolicy<'a, 'a>,
|
|
|
|
tracer: &'a mut T,
|
2016-06-02 12:40:31 +02:00
|
|
|
vm_tracer: &'a mut V,
|
2017-06-19 11:41:46 +02:00
|
|
|
static_flag: bool,
|
2016-03-19 18:37:55 +01:00
|
|
|
) -> Self {
|
2016-01-15 14:22:46 +01:00
|
|
|
Externalities {
|
|
|
|
state: state,
|
2016-01-16 20:11:12 +01:00
|
|
|
env_info: env_info,
|
2017-09-26 14:19:08 +02:00
|
|
|
machine: machine,
|
2016-01-15 14:22:46 +01:00
|
|
|
depth: depth,
|
2016-01-16 20:11:12 +01:00
|
|
|
origin_info: origin_info,
|
2016-01-15 14:22:46 +01:00
|
|
|
substate: substate,
|
2017-09-26 14:19:08 +02:00
|
|
|
schedule: machine.schedule(env_info.number),
|
2016-04-08 01:50:55 +02:00
|
|
|
output: output,
|
|
|
|
tracer: tracer,
|
2016-06-02 12:40:31 +02:00
|
|
|
vm_tracer: vm_tracer,
|
2017-06-19 11:41:46 +02:00
|
|
|
static_flag: static_flag,
|
2016-01-15 14:22:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
impl<'a, T: 'a, V: 'a, B: 'a> Ext for Externalities<'a, T, V, B>
|
|
|
|
where T: Tracer, V: VMTracer, B: StateBackend
|
2017-02-21 12:35:21 +01:00
|
|
|
{
|
2017-08-01 12:37:57 +02:00
|
|
|
fn storage_at(&self, key: &H256) -> vm::Result<H256> {
|
2017-06-19 11:41:46 +02:00
|
|
|
self.state.storage_at(&self.origin_info.address, key).map_err(Into::into)
|
2016-01-15 14:22:46 +01:00
|
|
|
}
|
|
|
|
|
2017-08-01 12:37:57 +02:00
|
|
|
fn set_storage(&mut self, key: H256, value: H256) -> vm::Result<()> {
|
2017-06-19 11:41:46 +02:00
|
|
|
if self.static_flag {
|
2017-08-01 12:37:57 +02:00
|
|
|
Err(vm::Error::MutableCallInStaticContext)
|
2017-06-19 11:41:46 +02:00
|
|
|
} else {
|
|
|
|
self.state.set_storage(&self.origin_info.address, key, value).map_err(Into::into)
|
|
|
|
}
|
2016-01-16 20:11:12 +01:00
|
|
|
}
|
|
|
|
|
2017-09-15 21:07:54 +02:00
|
|
|
fn is_static(&self) -> bool {
|
|
|
|
return self.static_flag
|
|
|
|
}
|
|
|
|
|
2017-08-01 12:37:57 +02:00
|
|
|
fn exists(&self, address: &Address) -> vm::Result<bool> {
|
2017-06-19 11:41:46 +02:00
|
|
|
self.state.exists(address).map_err(Into::into)
|
2016-01-15 14:22:46 +01:00
|
|
|
}
|
|
|
|
|
2017-08-01 12:37:57 +02:00
|
|
|
fn exists_and_not_null(&self, address: &Address) -> vm::Result<bool> {
|
2017-06-19 11:41:46 +02:00
|
|
|
self.state.exists_and_not_null(address).map_err(Into::into)
|
2016-11-03 22:22:25 +01:00
|
|
|
}
|
|
|
|
|
2017-08-01 12:37:57 +02:00
|
|
|
fn origin_balance(&self) -> vm::Result<U256> {
|
2017-06-19 11:41:46 +02:00
|
|
|
self.balance(&self.origin_info.address).map_err(Into::into)
|
|
|
|
}
|
2016-11-03 22:22:25 +01:00
|
|
|
|
2017-08-01 12:37:57 +02:00
|
|
|
fn balance(&self, address: &Address) -> vm::Result<U256> {
|
2017-06-19 11:41:46 +02:00
|
|
|
self.state.balance(address).map_err(Into::into)
|
2016-01-15 14:22:46 +01:00
|
|
|
}
|
|
|
|
|
2017-05-30 11:52:33 +02:00
|
|
|
fn blockhash(&mut self, number: &U256) -> H256 {
|
2017-09-26 14:19:08 +02:00
|
|
|
if self.env_info.number + 256 >= self.machine.params().eip210_transition {
|
|
|
|
let blockhash_contract_address = self.machine.params().eip210_contract_address;
|
2017-05-30 11:52:33 +02:00
|
|
|
let code_res = self.state.code(&blockhash_contract_address)
|
|
|
|
.and_then(|code| self.state.code_hash(&blockhash_contract_address).map(|hash| (code, hash)));
|
|
|
|
|
|
|
|
let (code, code_hash) = match code_res {
|
|
|
|
Ok((code, hash)) => (code, hash),
|
|
|
|
Err(_) => return H256::zero(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let params = ActionParams {
|
|
|
|
sender: self.origin_info.address.clone(),
|
|
|
|
address: blockhash_contract_address.clone(),
|
|
|
|
value: ActionValue::Apparent(self.origin_info.value),
|
|
|
|
code_address: blockhash_contract_address.clone(),
|
|
|
|
origin: self.origin_info.origin.clone(),
|
2017-09-26 14:19:08 +02:00
|
|
|
gas: self.machine.params().eip210_contract_gas,
|
2017-05-30 11:52:33 +02:00
|
|
|
gas_price: 0.into(),
|
|
|
|
code: code,
|
2017-06-30 11:30:32 +02:00
|
|
|
code_hash: Some(code_hash),
|
2017-05-30 11:52:33 +02:00
|
|
|
data: Some(H256::from(number).to_vec()),
|
|
|
|
call_type: CallType::Call,
|
2017-11-02 12:49:57 +01:00
|
|
|
params_type: vm::ParamsType::Separate,
|
2017-05-30 11:52:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut output = H256::new();
|
2017-09-26 14:19:08 +02:00
|
|
|
let mut ex = Executive::new(self.state, self.env_info, self.machine);
|
2017-05-30 11:52:33 +02:00
|
|
|
let r = ex.call(params, self.substate, BytesRef::Fixed(&mut output), self.tracer, self.vm_tracer);
|
|
|
|
trace!("ext: blockhash contract({}) -> {:?}({}) self.env_info.number={}\n", number, r, output, self.env_info.number);
|
|
|
|
output
|
|
|
|
} else {
|
|
|
|
// TODO: comment out what this function expects from env_info, since it will produce panics if the latter is inconsistent
|
|
|
|
match *number < U256::from(self.env_info.number) && number.low_u64() >= cmp::max(256, self.env_info.number) - 256 {
|
|
|
|
true => {
|
|
|
|
let index = self.env_info.number - number.low_u64() - 1;
|
|
|
|
assert!(index < self.env_info.last_hashes.len() as u64, format!("Inconsistent env_info, should contain at least {:?} last hashes", index+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);
|
|
|
|
r
|
|
|
|
},
|
|
|
|
false => {
|
|
|
|
trace!("ext: blockhash({}) -> null self.env_info.number={}\n", number, self.env_info.number);
|
|
|
|
H256::zero()
|
|
|
|
},
|
|
|
|
}
|
2016-01-15 14:22:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 14:30:00 +02:00
|
|
|
fn create(&mut self, gas: &U256, value: &U256, code: &[u8], address_scheme: CreateContractAddress) -> ContractCreateResult {
|
2016-01-15 14:22:46 +01:00
|
|
|
// create new contract address
|
2017-06-30 11:30:32 +02:00
|
|
|
let (address, code_hash) = match self.state.nonce(&self.origin_info.address) {
|
|
|
|
Ok(nonce) => contract_address(address_scheme, &self.origin_info.address, &nonce, &code),
|
2017-02-26 13:10:50 +01:00
|
|
|
Err(e) => {
|
|
|
|
debug!(target: "ext", "Database corruption encountered: {:?}", e);
|
|
|
|
return ContractCreateResult::Failed
|
|
|
|
}
|
|
|
|
};
|
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-02-14 12:54:27 +01:00
|
|
|
gas_price: self.origin_info.gas_price,
|
|
|
|
value: ActionValue::Transfer(*value),
|
2016-10-02 18:45:36 +02:00
|
|
|
code: Some(Arc::new(code.to_vec())),
|
2017-04-19 14:30:00 +02:00
|
|
|
code_hash: code_hash,
|
2016-01-15 15:14:41 +01:00
|
|
|
data: None,
|
2016-07-27 17:41:21 +02:00
|
|
|
call_type: CallType::None,
|
2017-11-02 12:49:57 +01:00
|
|
|
params_type: vm::ParamsType::Embedded,
|
2016-01-15 14:22:46 +01:00
|
|
|
};
|
|
|
|
|
2017-09-15 21:07:54 +02:00
|
|
|
if !self.static_flag {
|
|
|
|
if !self.schedule.eip86 || params.sender != UNSIGNED_SENDER {
|
|
|
|
if let Err(e) = self.state.inc_nonce(&self.origin_info.address) {
|
|
|
|
debug!(target: "ext", "Database corruption encountered: {:?}", e);
|
|
|
|
return ContractCreateResult::Failed
|
|
|
|
}
|
2017-04-19 14:30:00 +02:00
|
|
|
}
|
2017-02-26 13:10:50 +01:00
|
|
|
}
|
2017-09-26 14:19:08 +02:00
|
|
|
let mut ex = Executive::from_parent(self.state, self.env_info, self.machine, self.depth, self.static_flag);
|
2016-02-09 15:28:27 +01:00
|
|
|
|
2016-01-16 20:11:12 +01:00
|
|
|
// TODO: handle internal error separately
|
2017-09-05 13:22:19 +02:00
|
|
|
match ex.create(params, self.substate, &mut None, self.tracer, self.vm_tracer) {
|
2017-09-15 21:07:54 +02:00
|
|
|
Ok(FinalizationResult{ gas_left, apply_state: true, .. }) => {
|
2016-01-16 20:11:12 +01:00
|
|
|
self.substate.contracts_created.push(address.clone());
|
|
|
|
ContractCreateResult::Created(address, gas_left)
|
|
|
|
},
|
2017-09-15 21:07:54 +02:00
|
|
|
Ok(FinalizationResult{ gas_left, apply_state: false, return_data }) => {
|
|
|
|
ContractCreateResult::Reverted(gas_left, return_data)
|
|
|
|
},
|
|
|
|
_ => ContractCreateResult::Failed,
|
2016-01-15 14:22:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-09 15:28:27 +01:00
|
|
|
fn call(&mut self,
|
2016-03-20 16:24:19 +01:00
|
|
|
gas: &U256,
|
|
|
|
sender_address: &Address,
|
|
|
|
receive_address: &Address,
|
|
|
|
value: Option<U256>,
|
|
|
|
data: &[u8],
|
|
|
|
code_address: &Address,
|
2016-07-27 17:41:21 +02:00
|
|
|
output: &mut [u8],
|
|
|
|
call_type: CallType
|
2016-03-20 16:24:19 +01:00
|
|
|
) -> MessageCallResult {
|
|
|
|
trace!(target: "externalities", "call");
|
2016-01-20 16:52:22 +01:00
|
|
|
|
2017-02-26 13:10:50 +01:00
|
|
|
let code_res = self.state.code(code_address)
|
|
|
|
.and_then(|code| self.state.code_hash(code_address).map(|hash| (code, hash)));
|
|
|
|
|
|
|
|
let (code, code_hash) = match code_res {
|
|
|
|
Ok((code, hash)) => (code, hash),
|
|
|
|
Err(_) => return MessageCallResult::Failed,
|
|
|
|
};
|
|
|
|
|
2016-01-23 10:41:13 +01:00
|
|
|
let mut params = ActionParams {
|
|
|
|
sender: sender_address.clone(),
|
2016-02-09 15:28:27 +01:00
|
|
|
address: receive_address.clone(),
|
2016-02-14 12:54:27 +01:00
|
|
|
value: ActionValue::Apparent(self.origin_info.value),
|
2016-01-20 16:52:22 +01:00
|
|
|
code_address: code_address.clone(),
|
|
|
|
origin: self.origin_info.origin.clone(),
|
|
|
|
gas: *gas,
|
2016-02-14 12:54:27 +01:00
|
|
|
gas_price: self.origin_info.gas_price,
|
2017-02-26 13:10:50 +01:00
|
|
|
code: code,
|
2017-06-30 11:30:32 +02:00
|
|
|
code_hash: Some(code_hash),
|
2016-01-20 16:52:22 +01:00
|
|
|
data: Some(data.to_vec()),
|
2016-07-27 17:41:21 +02:00
|
|
|
call_type: call_type,
|
2017-11-02 12:49:57 +01:00
|
|
|
params_type: vm::ParamsType::Separate,
|
2016-01-20 16:52:22 +01:00
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
let mut ex = Executive::from_parent(self.state, self.env_info, self.machine, self.depth, self.static_flag);
|
2016-01-15 22:55:04 +01:00
|
|
|
|
2016-06-02 12:40:31 +02:00
|
|
|
match ex.call(params, self.substate, BytesRef::Fixed(output), self.tracer, self.vm_tracer) {
|
2017-09-15 21:07:54 +02:00
|
|
|
Ok(FinalizationResult{ gas_left, return_data, apply_state: true }) => MessageCallResult::Success(gas_left, return_data),
|
|
|
|
Ok(FinalizationResult{ gas_left, return_data, apply_state: false }) => MessageCallResult::Reverted(gas_left, return_data),
|
2016-01-16 20:11:12 +01:00
|
|
|
_ => MessageCallResult::Failed
|
2016-01-15 14:22:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-01 12:37:57 +02:00
|
|
|
fn extcode(&self, address: &Address) -> vm::Result<Arc<Bytes>> {
|
2017-02-26 13:10:50 +01:00
|
|
|
Ok(self.state.code(address)?.unwrap_or_else(|| Arc::new(vec![])))
|
2016-01-15 14:22:46 +01:00
|
|
|
}
|
|
|
|
|
2017-08-01 12:37:57 +02:00
|
|
|
fn extcodesize(&self, address: &Address) -> vm::Result<usize> {
|
2017-02-26 13:10:50 +01:00
|
|
|
Ok(self.state.code_size(address)?.unwrap_or(0))
|
2016-09-22 19:58:42 +02:00
|
|
|
}
|
|
|
|
|
2017-09-15 21:07:54 +02:00
|
|
|
fn ret(mut self, gas: &U256, data: &ReturnData, apply_state: bool) -> vm::Result<U256>
|
2016-06-02 19:04:15 +02:00
|
|
|
where Self: Sized {
|
2016-03-19 18:37:55 +01:00
|
|
|
let handle_copy = |to: &mut Option<&mut Bytes>| {
|
2017-06-06 17:47:12 +02:00
|
|
|
to.as_mut().map(|b| **b = data.to_vec());
|
2016-03-19 18:37:55 +01:00
|
|
|
};
|
|
|
|
match self.output {
|
|
|
|
OutputPolicy::Return(BytesRef::Fixed(ref mut slice), ref mut copy) => {
|
|
|
|
handle_copy(copy);
|
2016-03-19 21:02:44 +01:00
|
|
|
|
2016-01-15 14:22:46 +01:00
|
|
|
let len = cmp::min(slice.len(), data.len());
|
2016-06-02 19:04:15 +02:00
|
|
|
(&mut slice[..len]).copy_from_slice(&data[..len]);
|
2016-01-15 14:22:46 +01:00
|
|
|
Ok(*gas)
|
|
|
|
},
|
2016-03-19 18:37:55 +01:00
|
|
|
OutputPolicy::Return(BytesRef::Flexible(ref mut vec), ref mut copy) => {
|
|
|
|
handle_copy(copy);
|
2016-04-08 01:50:55 +02:00
|
|
|
|
2016-01-15 14:22:46 +01:00
|
|
|
vec.clear();
|
2017-06-06 17:47:12 +02:00
|
|
|
vec.extend_from_slice(&*data);
|
2016-01-15 14:22:46 +01:00
|
|
|
Ok(*gas)
|
|
|
|
},
|
2017-09-15 21:07:54 +02:00
|
|
|
OutputPolicy::InitContract(ref mut copy) if apply_state => {
|
2016-01-15 14:22:46 +01:00
|
|
|
let return_cost = U256::from(data.len()) * U256::from(self.schedule.create_data_gas);
|
2016-11-11 11:56:58 +01:00
|
|
|
if return_cost > *gas || data.len() > self.schedule.create_data_limit {
|
2016-01-15 14:22:46 +01:00
|
|
|
return match self.schedule.exceptional_failed_code_deposit {
|
2017-08-01 12:37:57 +02:00
|
|
|
true => Err(vm::Error::OutOfGas),
|
2016-01-15 14:22:46 +01:00
|
|
|
false => Ok(*gas)
|
|
|
|
}
|
|
|
|
}
|
2016-03-19 18:46:41 +01:00
|
|
|
handle_copy(copy);
|
2017-02-26 13:10:50 +01:00
|
|
|
self.state.init_code(&self.origin_info.address, data.to_vec())?;
|
2016-01-15 14:22:46 +01:00
|
|
|
Ok(*gas - return_cost)
|
2017-09-15 21:07:54 +02:00
|
|
|
},
|
|
|
|
OutputPolicy::InitContract(_) => {
|
|
|
|
Ok(*gas)
|
|
|
|
},
|
2016-01-15 14:22:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-01 12:37:57 +02:00
|
|
|
fn log(&mut self, topics: Vec<H256>, data: &[u8]) -> vm::Result<()> {
|
2016-10-24 18:35:25 +02:00
|
|
|
use log_entry::LogEntry;
|
|
|
|
|
2017-06-19 11:41:46 +02:00
|
|
|
if self.static_flag {
|
2017-08-01 12:37:57 +02:00
|
|
|
return Err(vm::Error::MutableCallInStaticContext);
|
2017-06-19 11:41:46 +02:00
|
|
|
}
|
|
|
|
|
2016-01-16 20:11:12 +01:00
|
|
|
let address = self.origin_info.address.clone();
|
2016-03-07 14:33:00 +01:00
|
|
|
self.substate.logs.push(LogEntry {
|
2016-02-16 18:21:45 +01:00
|
|
|
address: address,
|
2016-03-07 14:33:00 +01:00
|
|
|
topics: topics,
|
2016-02-16 18:21:45 +01:00
|
|
|
data: data.to_vec()
|
|
|
|
});
|
2017-06-19 11:41:46 +02:00
|
|
|
|
|
|
|
Ok(())
|
2016-01-15 14:22:46 +01:00
|
|
|
}
|
|
|
|
|
2017-08-01 12:37:57 +02:00
|
|
|
fn suicide(&mut self, refund_address: &Address) -> vm::Result<()> {
|
2017-06-19 11:41:46 +02:00
|
|
|
if self.static_flag {
|
2017-08-01 12:37:57 +02:00
|
|
|
return Err(vm::Error::MutableCallInStaticContext);
|
2017-06-19 11:41:46 +02:00
|
|
|
}
|
|
|
|
|
2016-01-16 20:11:12 +01:00
|
|
|
let address = self.origin_info.address.clone();
|
2017-02-26 13:10:50 +01:00
|
|
|
let balance = self.balance(&address)?;
|
2016-01-27 17:25:19 +01:00
|
|
|
if &address == refund_address {
|
2016-11-03 22:22:25 +01:00
|
|
|
// TODO [todr] To be consistent with CPP client we set balance to 0 in that case.
|
2017-06-28 09:10:57 +02:00
|
|
|
self.state.sub_balance(&address, &balance, &mut CleanupMode::NoEmpty)?;
|
2016-01-27 17:25:19 +01:00
|
|
|
} else {
|
2016-11-03 22:22:25 +01:00
|
|
|
trace!(target: "ext", "Suiciding {} -> {} (xfer: {})", address, refund_address, balance);
|
2017-02-26 13:10:50 +01:00
|
|
|
self.state.transfer_balance(
|
|
|
|
&address,
|
|
|
|
refund_address,
|
|
|
|
&balance,
|
|
|
|
self.substate.to_cleanup_mode(&self.schedule)
|
|
|
|
)?;
|
2016-01-27 17:25:19 +01:00
|
|
|
}
|
2016-07-21 16:26:49 +02:00
|
|
|
|
2016-07-28 20:31:29 +02:00
|
|
|
self.tracer.trace_suicide(address, balance, refund_address.clone());
|
2016-07-21 16:50:24 +02:00
|
|
|
self.substate.suicides.insert(address);
|
2017-02-26 13:10:50 +01:00
|
|
|
|
|
|
|
Ok(())
|
2016-01-15 14:22:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn schedule(&self) -> &Schedule {
|
|
|
|
&self.schedule
|
|
|
|
}
|
|
|
|
|
|
|
|
fn env_info(&self) -> &EnvInfo {
|
2016-07-26 20:31:25 +02:00
|
|
|
self.env_info
|
2016-01-16 20:11:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2016-06-02 12:40:31 +02:00
|
|
|
|
2017-07-10 13:23:40 +02:00
|
|
|
fn trace_next_instruction(&mut self, pc: usize, instruction: u8) -> bool {
|
|
|
|
self.vm_tracer.trace_next_instruction(pc, instruction)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trace_prepare_execute(&mut self, pc: usize, instruction: u8, gas_cost: U256) {
|
2017-06-09 12:31:03 +02:00
|
|
|
self.vm_tracer.trace_prepare_execute(pc, instruction, gas_cost)
|
2016-06-02 12:40:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn trace_executed(&mut self, gas_used: U256, stack_push: &[U256], mem_diff: Option<(usize, &[u8])>, store_diff: Option<(U256, U256)>) {
|
|
|
|
self.vm_tracer.trace_executed(gas_used, stack_push, mem_diff, store_diff)
|
|
|
|
}
|
2016-01-15 14:22:46 +01:00
|
|
|
}
|
2016-02-09 15:28:27 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2016-10-24 18:35:25 +02:00
|
|
|
use util::*;
|
2017-08-01 12:37:57 +02:00
|
|
|
use evm::{EnvInfo, Ext, CallType};
|
2016-08-18 15:24:27 +02:00
|
|
|
use state::{State, Substate};
|
2016-02-09 15:28:27 +01:00
|
|
|
use tests::helpers::*;
|
|
|
|
use super::*;
|
2016-06-02 12:40:31 +02:00
|
|
|
use trace::{NoopTracer, NoopVMTracer};
|
2016-02-09 15:28:27 +01:00
|
|
|
|
|
|
|
fn get_test_origin() -> OriginInfo {
|
|
|
|
OriginInfo {
|
|
|
|
address: Address::zero(),
|
|
|
|
origin: Address::zero(),
|
|
|
|
gas_price: U256::zero(),
|
|
|
|
value: U256::zero()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_test_env_info() -> EnvInfo {
|
|
|
|
EnvInfo {
|
|
|
|
number: 100,
|
2016-05-31 16:59:01 +02:00
|
|
|
author: 0.into(),
|
2016-02-09 15:28:27 +01:00
|
|
|
timestamp: 0,
|
2016-05-31 16:59:01 +02:00
|
|
|
difficulty: 0.into(),
|
2016-08-03 22:03:40 +02:00
|
|
|
last_hashes: Arc::new(vec![]),
|
2016-05-31 16:59:01 +02:00
|
|
|
gas_used: 0.into(),
|
2016-06-18 20:26:44 +02:00
|
|
|
gas_limit: 0.into(),
|
2016-02-09 15:28:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-09 17:29:52 +01:00
|
|
|
struct TestSetup {
|
2017-04-06 19:26:17 +02:00
|
|
|
state: State<::state_db::StateDB>,
|
2017-09-26 14:19:08 +02:00
|
|
|
machine: ::machine::EthereumMachine,
|
2016-02-09 17:29:52 +01:00
|
|
|
sub_state: Substate,
|
|
|
|
env_info: EnvInfo
|
|
|
|
}
|
|
|
|
|
2016-03-12 10:07:55 +01:00
|
|
|
impl Default for TestSetup {
|
|
|
|
fn default() -> Self {
|
|
|
|
TestSetup::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-09 17:29:52 +01:00
|
|
|
impl TestSetup {
|
2016-03-12 10:07:55 +01:00
|
|
|
fn new() -> Self {
|
2016-02-09 17:29:52 +01:00
|
|
|
TestSetup {
|
|
|
|
state: get_temp_state(),
|
2017-09-26 14:19:08 +02:00
|
|
|
machine: ::spec::Spec::new_test_machine(),
|
2016-04-08 01:50:55 +02:00
|
|
|
sub_state: Substate::new(),
|
2016-02-09 17:29:52 +01:00
|
|
|
env_info: get_test_env_info()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-09 15:28:27 +01:00
|
|
|
#[test]
|
|
|
|
fn can_be_created() {
|
2016-02-09 17:29:52 +01:00
|
|
|
let mut setup = TestSetup::new();
|
2017-04-06 19:26:17 +02:00
|
|
|
let state = &mut setup.state;
|
2016-04-08 01:50:55 +02:00
|
|
|
let mut tracer = NoopTracer;
|
2016-06-02 12:40:31 +02:00
|
|
|
let mut vm_tracer = NoopVMTracer;
|
2016-02-09 15:28:27 +01:00
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
let ext = Externalities::new(state, &setup.env_info, &setup.machine, 0, get_test_origin(), &mut setup.sub_state, OutputPolicy::InitContract(None), &mut tracer, &mut vm_tracer, false);
|
2016-02-09 16:31:57 +01:00
|
|
|
|
|
|
|
assert_eq!(ext.env_info().number, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-02-09 16:54:58 +01:00
|
|
|
fn can_return_block_hash_no_env() {
|
2016-02-09 17:29:52 +01:00
|
|
|
let mut setup = TestSetup::new();
|
2017-04-06 19:26:17 +02:00
|
|
|
let state = &mut setup.state;
|
2016-04-08 01:50:55 +02:00
|
|
|
let mut tracer = NoopTracer;
|
2016-06-02 12:40:31 +02:00
|
|
|
let mut vm_tracer = NoopVMTracer;
|
2016-04-08 01:50:55 +02:00
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
let mut ext = Externalities::new(state, &setup.env_info, &setup.machine, 0, get_test_origin(), &mut setup.sub_state, OutputPolicy::InitContract(None), &mut tracer, &mut vm_tracer, false);
|
2016-02-09 16:31:57 +01:00
|
|
|
|
2017-07-29 17:12:07 +02:00
|
|
|
let hash = ext.blockhash(&"0000000000000000000000000000000000000000000000000000000000120000".parse::<U256>().unwrap());
|
2016-02-09 17:29:52 +01:00
|
|
|
|
2016-02-09 16:31:57 +01:00
|
|
|
assert_eq!(hash, H256::zero());
|
2016-02-09 15:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-02-09 16:54:58 +01:00
|
|
|
#[test]
|
|
|
|
fn can_return_block_hash() {
|
|
|
|
let test_hash = H256::from("afafafafafafafafafafafbcbcbcbcbcbcbcbcbcbeeeeeeeeeeeeedddddddddd");
|
2016-02-09 17:29:52 +01:00
|
|
|
let test_env_number = 0x120001;
|
2016-02-09 16:54:58 +01:00
|
|
|
|
2016-02-09 17:29:52 +01:00
|
|
|
let mut setup = TestSetup::new();
|
|
|
|
{
|
|
|
|
let env_info = &mut setup.env_info;
|
|
|
|
env_info.number = test_env_number;
|
2016-08-03 22:03:40 +02:00
|
|
|
let mut last_hashes = (*env_info.last_hashes).clone();
|
|
|
|
last_hashes.push(test_hash.clone());
|
|
|
|
env_info.last_hashes = Arc::new(last_hashes);
|
2016-02-09 17:29:52 +01:00
|
|
|
}
|
2017-04-06 19:26:17 +02:00
|
|
|
let state = &mut setup.state;
|
2016-04-08 01:50:55 +02:00
|
|
|
let mut tracer = NoopTracer;
|
2016-06-02 12:40:31 +02:00
|
|
|
let mut vm_tracer = NoopVMTracer;
|
2016-04-08 01:50:55 +02:00
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
let mut ext = Externalities::new(state, &setup.env_info, &setup.machine, 0, get_test_origin(), &mut setup.sub_state, OutputPolicy::InitContract(None), &mut tracer, &mut vm_tracer, false);
|
2016-02-09 16:54:58 +01:00
|
|
|
|
2017-07-29 17:12:07 +02:00
|
|
|
let hash = ext.blockhash(&"0000000000000000000000000000000000000000000000000000000000120000".parse::<U256>().unwrap());
|
2016-02-09 17:29:52 +01:00
|
|
|
|
2016-02-09 16:54:58 +01:00
|
|
|
assert_eq!(test_hash, hash);
|
|
|
|
}
|
|
|
|
|
2016-02-09 17:29:52 +01:00
|
|
|
#[test]
|
2016-02-09 22:32:47 +01:00
|
|
|
#[should_panic]
|
2016-02-09 20:30:35 +01:00
|
|
|
fn can_call_fail_empty() {
|
|
|
|
let mut setup = TestSetup::new();
|
2017-04-06 19:26:17 +02:00
|
|
|
let state = &mut setup.state;
|
2016-04-08 01:50:55 +02:00
|
|
|
let mut tracer = NoopTracer;
|
2016-06-02 12:40:31 +02:00
|
|
|
let mut vm_tracer = NoopVMTracer;
|
2016-04-08 01:50:55 +02:00
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
let mut ext = Externalities::new(state, &setup.env_info, &setup.machine, 0, get_test_origin(), &mut setup.sub_state, OutputPolicy::InitContract(None), &mut tracer, &mut vm_tracer, false);
|
2016-02-09 20:30:35 +01:00
|
|
|
|
|
|
|
let mut output = vec![];
|
2016-02-09 17:29:52 +01:00
|
|
|
|
2016-02-09 23:02:31 +01:00
|
|
|
// this should panic because we have no balance on any account
|
|
|
|
ext.call(
|
2017-07-29 17:12:07 +02:00
|
|
|
&"0000000000000000000000000000000000000000000000000000000000120000".parse::<U256>().unwrap(),
|
2016-02-09 20:30:35 +01:00
|
|
|
&Address::new(),
|
|
|
|
&Address::new(),
|
2017-07-29 17:12:07 +02:00
|
|
|
Some("0000000000000000000000000000000000000000000000000000000000150000".parse::<U256>().unwrap()),
|
2016-02-15 00:51:50 +01:00
|
|
|
&[],
|
2016-02-09 20:30:35 +01:00
|
|
|
&Address::new(),
|
2016-07-27 17:41:21 +02:00
|
|
|
&mut output,
|
|
|
|
CallType::Call
|
|
|
|
);
|
2016-02-09 20:30:35 +01:00
|
|
|
}
|
2016-02-09 22:41:45 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_log() {
|
|
|
|
let log_data = vec![120u8, 110u8];
|
2016-02-09 23:02:31 +01:00
|
|
|
let log_topics = vec![H256::from("af0fa234a6af46afa23faf23bcbc1c1cb4bcb7bcbe7e7e7ee3ee2edddddddddd")];
|
2016-02-09 22:41:45 +01:00
|
|
|
|
|
|
|
let mut setup = TestSetup::new();
|
2017-04-06 19:26:17 +02:00
|
|
|
let state = &mut setup.state;
|
2016-04-08 01:50:55 +02:00
|
|
|
let mut tracer = NoopTracer;
|
2016-06-02 12:40:31 +02:00
|
|
|
let mut vm_tracer = NoopVMTracer;
|
2016-02-09 22:41:45 +01:00
|
|
|
|
|
|
|
{
|
2017-09-26 14:19:08 +02:00
|
|
|
let mut ext = Externalities::new(state, &setup.env_info, &setup.machine, 0, get_test_origin(), &mut setup.sub_state, OutputPolicy::InitContract(None), &mut tracer, &mut vm_tracer, false);
|
2017-06-19 11:41:46 +02:00
|
|
|
ext.log(log_topics, &log_data).unwrap();
|
2016-02-09 22:41:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(setup.sub_state.logs.len(), 1);
|
|
|
|
}
|
2016-02-09 23:02:31 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_suicide() {
|
|
|
|
let refund_account = &Address::new();
|
|
|
|
|
|
|
|
let mut setup = TestSetup::new();
|
2017-04-06 19:26:17 +02:00
|
|
|
let state = &mut setup.state;
|
2016-04-08 01:50:55 +02:00
|
|
|
let mut tracer = NoopTracer;
|
2016-06-02 12:40:31 +02:00
|
|
|
let mut vm_tracer = NoopVMTracer;
|
2016-02-09 23:02:31 +01:00
|
|
|
|
|
|
|
{
|
2017-09-26 14:19:08 +02:00
|
|
|
let mut ext = Externalities::new(state, &setup.env_info, &setup.machine, 0, get_test_origin(), &mut setup.sub_state, OutputPolicy::InitContract(None), &mut tracer, &mut vm_tracer, false);
|
2017-02-26 13:10:50 +01:00
|
|
|
ext.suicide(refund_account).unwrap();
|
2016-02-09 23:02:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(setup.sub_state.suicides.len(), 1);
|
|
|
|
}
|
2016-02-09 15:28:27 +01:00
|
|
|
}
|