openethereum/ethcore/src/externalities.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

912 lines
28 KiB
Rust
Raw Normal View History

2020-09-22 14:53:52 +02:00
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of OpenEthereum.
2016-02-05 13:40:41 +01:00
2020-09-22 14:53:52 +02:00
// OpenEthereum is free software: you can redistribute it and/or modify
2016-02-05 13:40:41 +01:00
// 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.
2020-09-22 14:53:52 +02:00
// OpenEthereum is distributed in the hope that it will be useful,
2016-02-05 13:40:41 +01:00
// 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
2020-09-22 14:53:52 +02:00
// along with OpenEthereum. If not, see <http://www.gnu.org/licenses/>.
2016-02-05 13:40:41 +01:00
2016-01-15 14:22:46 +01:00
//! Transaction Execution environment.
use bytes::Bytes;
use ethereum_types::{Address, H256, U256};
2016-01-15 14:22:46 +01:00
use executive::*;
use machine::EthereumMachine as Machine;
use state::{Backend as StateBackend, CleanupMode, State, Substate};
2017-07-29 21:56:42 +02:00
use std::{cmp, sync::Arc};
use trace::{Tracer, VMTracer};
use types::transaction::UNSIGNED_SENDER;
use vm::{
2020-11-04 19:11:05 +01:00
self, AccessList, ActionParams, ActionValue, CallType, ContractCreateResult,
CreateContractAddress, EnvInfo, Ext, MessageCallResult, ReturnData, Schedule, TrapKind,
};
2016-01-15 14:22:46 +01:00
/// Policy for handling output data on `RETURN` opcode.
pub enum OutputPolicy {
2016-01-15 14:22:46 +01:00
/// Return reference to fixed sized output.
/// Used for message calls.
Return,
2016-01-15 14:22:46 +01:00
/// Init new contract as soon as `RETURN` is called.
InitContract,
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.
pub struct Externalities<'a, T: 'a, V: 'a, B: 'a> {
state: &'a mut State<B>,
2016-01-16 20:11:12 +01:00
env_info: &'a EnvInfo,
2016-01-15 14:22:46 +01:00
depth: usize,
2018-10-02 16:33:19 +02:00
stack_depth: usize,
origin_info: &'a OriginInfo,
2016-01-15 14:22:46 +01:00
substate: &'a mut Substate,
machine: &'a Machine,
schedule: &'a Schedule,
output: OutputPolicy,
tracer: &'a mut T,
vm_tracer: &'a mut V,
2017-06-19 11:41:46 +02:00
static_flag: bool,
2016-01-15 14:22:46 +01:00
}
impl<'a, T: 'a, V: 'a, B: 'a> Externalities<'a, T, V, B>
where
T: Tracer,
V: VMTracer,
B: StateBackend,
{
2016-01-15 14:22:46 +01:00
/// Basic `Externalities` constructor.
pub fn new(
state: &'a mut State<B>,
2016-03-19 18:37:55 +01:00
env_info: &'a EnvInfo,
machine: &'a Machine,
schedule: &'a Schedule,
2016-03-19 18:37:55 +01:00
depth: usize,
2018-10-02 16:33:19 +02:00
stack_depth: usize,
origin_info: &'a OriginInfo,
2016-03-19 18:37:55 +01:00
substate: &'a mut Substate,
output: OutputPolicy,
tracer: &'a mut T,
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,
2016-01-15 14:22:46 +01:00
depth: depth,
2018-10-02 16:33:19 +02:00
stack_depth: stack_depth,
2016-01-16 20:11:12 +01:00
origin_info: origin_info,
2016-01-15 14:22:46 +01:00
substate: substate,
machine: machine,
schedule: schedule,
output: output,
tracer: tracer,
vm_tracer: vm_tracer,
2017-06-19 11:41:46 +02:00
static_flag: static_flag,
2016-01-15 14:22:46 +01:00
}
}
}
impl<'a, T: 'a, V: 'a, B: 'a> Ext for Externalities<'a, T, V, B>
where
T: Tracer,
V: VMTracer,
B: StateBackend,
{
fn initial_storage_at(&self, key: &H256) -> vm::Result<H256> {
if self
.state
.is_base_storage_root_unchanged(&self.origin_info.address)?
{
self.state
.checkpoint_storage_at(0, &self.origin_info.address, key)
.map(|v| v.unwrap_or_default())
.map_err(Into::into)
} else {
warn!(target: "externalities", "Detected existing account {:#x} where a forced contract creation happened.", self.origin_info.address);
Ok(H256::zero())
}
2020-08-05 06:08:03 +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
}
2020-08-05 06:08:03 +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 {
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
}
2020-08-05 06:08:03 +02:00
}
fn is_static(&self) -> bool {
return self.static_flag;
}
2020-08-05 06:08:03 +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
}
2020-08-05 06:08:03 +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)
}
2020-08-05 06:08:03 +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)
}
2020-08-05 06:08:03 +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
}
2020-08-05 06:08:03 +02:00
fn blockhash(&mut self, number: &U256) -> H256 {
if self.env_info.number + 256 >= self.machine.params().eip210_transition {
let blockhash_contract_address = self.machine.params().eip210_contract_address;
let code_res = self
.state
.code(&blockhash_contract_address)
.and_then(|code| {
self.state
.code_hash(&blockhash_contract_address)
.map(|hash| (code, hash))
});
2020-08-05 06:08:03 +02:00
let (code, code_hash) = match code_res {
Ok((code, hash)) => (code, hash),
Err(_) => return H256::zero(),
};
2020-08-05 06:08:03 +02:00
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(),
gas: self.machine.params().eip210_contract_gas,
gas_price: 0.into(),
code: code,
code_hash: code_hash,
data: Some(H256::from(number).to_vec()),
call_type: CallType::Call,
params_type: vm::ParamsType::Separate,
2020-11-04 19:11:05 +01:00
access_list: AccessList::default(),
};
2020-08-05 06:08:03 +02:00
let mut ex = Executive::new(self.state, self.env_info, self.machine, self.schedule);
2020-11-04 19:11:05 +01:00
let r = ex.call_with_stack_depth(
2018-10-02 16:33:19 +02:00
params,
self.substate,
self.stack_depth + 1,
self.tracer,
self.vm_tracer,
);
let output = match &r {
Ok(ref r) => H256::from(&r.return_data[..32]),
_ => H256::new(),
};
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
2020-08-05 06:08:03 +02:00
)
);
let r = self.env_info.last_hashes[index as usize].clone();
trace!(
"ext: blockhash({}) -> {} self.env_info.number={}\n",
number,
2020-08-05 06:08:03 +02:00
r,
self.env_info.number
2018-10-02 16:33:19 +02:00
);
2020-08-05 06:08:03 +02:00
r
}
2018-10-02 16:33:19 +02:00
false => {
trace!(
2017-06-30 11:30:32 +02:00
"ext: blockhash({}) -> null self.env_info.number={}\n",
number,
self.env_info.number
2020-08-05 06:08:03 +02:00
);
2017-06-30 11:30:32 +02:00
H256::zero()
2020-08-05 06:08:03 +02:00
}
}
}
}
fn create(
2017-06-30 11:30:32 +02:00
&mut self,
gas: &U256,
value: &U256,
2017-06-30 11:30:32 +02:00
code: &[u8],
address_scheme: CreateContractAddress,
trap: bool,
2018-10-02 16:33:19 +02:00
) -> ::std::result::Result<ContractCreateResult, TrapKind> {
// create new contract address
2016-01-16 20:11:12 +01:00
let (address, code_hash) = match self.state.nonce(&self.origin_info.address) {
2017-06-30 11:30:32 +02:00
Ok(nonce) => contract_address(address_scheme, &self.origin_info.address, &nonce, &code),
Err(e) => {
2016-01-16 20:11:12 +01:00
debug!(target: "ext", "Database corruption encountered: {:?}", e);
2018-10-02 16:33:19 +02:00
return Ok(ContractCreateResult::Failed);
2020-08-05 06:08:03 +02:00
}
};
2016-01-15 14:22:46 +01:00
// prepare the params
2016-01-23 10:41:13 +01:00
let params = ActionParams {
2016-01-16 20:11:12 +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(),
2016-02-14 12:54:27 +01:00
origin: self.origin_info.origin.clone(),
gas: *gas,
gas_price: self.origin_info.gas_price,
value: ActionValue::Transfer(*value),
code: Some(Arc::new(code.to_vec())),
code_hash: code_hash,
data: None,
call_type: CallType::None,
params_type: vm::ParamsType::Embedded,
2020-11-04 19:11:05 +01:00
access_list: self.substate.access_list.clone(),
2016-01-15 14:22:46 +01:00
};
2020-08-05 06:08:03 +02:00
if !self.static_flag {
if !self.schedule.keep_unsigned_nonce || params.sender != UNSIGNED_SENDER {
if let Err(e) = self.state.inc_nonce(&self.origin_info.address) {
debug!(target: "ext", "Database corruption encountered: {:?}", e);
2018-10-02 16:33:19 +02:00
return Ok(ContractCreateResult::Failed);
}
2020-08-05 06:08:03 +02:00
}
}
2018-10-02 16:33:19 +02:00
if trap {
return Err(TrapKind::Create(params, address));
2016-01-15 14:22:46 +01:00
}
2020-08-05 06:08:03 +02:00
2018-10-02 16:33:19 +02:00
// TODO: handle internal error separately
let mut ex = Executive::from_parent(
self.state,
self.env_info,
self.machine,
self.schedule,
self.depth,
self.static_flag,
2020-08-05 06:08:03 +02:00
);
2020-11-04 19:11:05 +01:00
let out = ex.create_with_stack_depth(
2020-08-05 06:08:03 +02:00
params,
self.substate,
2016-03-20 16:24:19 +01:00
self.stack_depth + 1,
2018-10-02 16:33:19 +02:00
self.tracer,
2016-03-20 16:24:19 +01:00
self.vm_tracer,
);
Ok(into_contract_create_result(out, &address, self.substate))
2020-08-05 06:08:03 +02:00
}
2020-11-04 19:11:05 +01:00
fn calc_address(&self, code: &[u8], address_scheme: CreateContractAddress) -> Option<Address> {
match self.state.nonce(&self.origin_info.address) {
Ok(nonce) => {
Some(contract_address(address_scheme, &self.origin_info.address, &nonce, &code).0)
}
Err(_) => None,
}
}
fn call(
&mut self,
gas: &U256,
2016-01-23 10:41:13 +01:00
sender_address: &Address,
2016-02-09 15:28:27 +01:00
receive_address: &Address,
2016-02-14 12:54:27 +01:00
value: Option<U256>,
2016-03-20 16:24:19 +01:00
data: &[u8],
code_address: &Address,
2016-02-14 12:54:27 +01:00
call_type: CallType,
2018-10-02 16:33:19 +02:00
trap: bool,
2016-02-14 12:54:27 +01:00
) -> ::std::result::Result<MessageCallResult, TrapKind> {
trace!(target: "externalities", "call");
2020-08-05 06:08:03 +02:00
2017-06-30 11:30:32 +02:00
let code_res = self
2020-08-05 06:08:03 +02:00
.state
.code(code_address)
2016-02-14 12:54:27 +01:00
.and_then(|code| self.state.code_hash(code_address).map(|hash| (code, hash)));
2020-08-05 06:08:03 +02:00
2016-01-20 16:52:22 +01:00
let (code, code_hash) = match code_res {
Ok((code, hash)) => (code, hash),
2018-10-02 16:33:19 +02:00
Err(_) => return Ok(MessageCallResult::Failed),
2020-08-05 06:08:03 +02:00
};
2016-01-23 10:41:13 +01:00
let mut params = ActionParams {
2016-01-20 16:52:22 +01:00
sender: sender_address.clone(),
address: receive_address.clone(),
value: ActionValue::Apparent(self.origin_info.value),
code_address: code_address.clone(),
2016-02-14 12:54:27 +01:00
origin: self.origin_info.origin.clone(),
gas: *gas,
gas_price: self.origin_info.gas_price,
code: code,
code_hash: code_hash,
2016-01-20 16:52:22 +01:00
data: Some(data.to_vec()),
call_type: call_type,
params_type: vm::ParamsType::Separate,
2020-11-04 19:11:05 +01:00
access_list: self.substate.access_list.clone(),
2016-01-20 16:52:22 +01:00
};
2020-08-05 06:08:03 +02: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
}
2020-08-05 06:08:03 +02:00
2018-10-02 16:33:19 +02:00
if trap {
return Err(TrapKind::Call(params));
2016-01-15 14:22:46 +01:00
}
2020-08-05 06:08:03 +02:00
2018-10-02 16:33:19 +02:00
let mut ex = Executive::from_parent(
self.state,
self.env_info,
self.machine,
self.schedule,
self.depth,
self.static_flag,
2020-08-05 06:08:03 +02:00
);
2020-11-04 19:11:05 +01:00
let out = ex.call_with_stack_depth(
2020-08-05 06:08:03 +02:00
params,
Beta 2.5.3 (#10776) * ethcore/res: activate atlantis classic hf on block 8772000 (#10766) * fix docker tags for publishing (#10741) * fix: aura don't add `SystemTime::now()` (#10720) This commit does the following: - Prevent overflow in `verify_timestamp()` by not adding `now` to found faulty timestamp - Use explicit `CheckedSystemTime::checked_add` to prevent potential consensus issues because SystemTime is platform depedent - remove `#[cfg(not(time_checked_add))]` conditional compilation * Update version * Treat empty account the same as non-exist accounts in EIP-1052 (#10775) * DevP2p: Get node IP address and udp port from Socket, if not included in PING packet (#10705) * get node IP address and udp port from Socket, if not included in PING packet * prevent bootnodes from being added to host nodes * code corrections * code corrections * code corrections * code corrections * docs * code corrections * code corrections * Apply suggestions from code review Co-Authored-By: David <dvdplm@gmail.com> * Add a way to signal shutdown to snapshotting threads (#10744) * Add a way to signal shutdown to snapshotting threads * Pass Progress to fat_rlps() so we can abort from there too. * Checking for abort in a single spot * Remove nightly-only weak/strong counts * fix warning * Fix tests * Add dummy impl to abort snapshots * Add another dummy impl for TestSnapshotService * Remove debugging code * Return error instead of the odd Ok(()) Switch to AtomicU64 * revert .as_bytes() change * fix build * fix build maybe
2019-06-25 15:38:29 +02:00
self.substate,
self.stack_depth + 1,
2018-10-02 16:33:19 +02:00
self.tracer,
self.vm_tracer,
2020-08-05 06:08:03 +02:00
);
Beta 2.5.3 (#10776) * ethcore/res: activate atlantis classic hf on block 8772000 (#10766) * fix docker tags for publishing (#10741) * fix: aura don't add `SystemTime::now()` (#10720) This commit does the following: - Prevent overflow in `verify_timestamp()` by not adding `now` to found faulty timestamp - Use explicit `CheckedSystemTime::checked_add` to prevent potential consensus issues because SystemTime is platform depedent - remove `#[cfg(not(time_checked_add))]` conditional compilation * Update version * Treat empty account the same as non-exist accounts in EIP-1052 (#10775) * DevP2p: Get node IP address and udp port from Socket, if not included in PING packet (#10705) * get node IP address and udp port from Socket, if not included in PING packet * prevent bootnodes from being added to host nodes * code corrections * code corrections * code corrections * code corrections * docs * code corrections * code corrections * Apply suggestions from code review Co-Authored-By: David <dvdplm@gmail.com> * Add a way to signal shutdown to snapshotting threads (#10744) * Add a way to signal shutdown to snapshotting threads * Pass Progress to fat_rlps() so we can abort from there too. * Checking for abort in a single spot * Remove nightly-only weak/strong counts * fix warning * Fix tests * Add dummy impl to abort snapshots * Add another dummy impl for TestSnapshotService * Remove debugging code * Return error instead of the odd Ok(()) Switch to AtomicU64 * revert .as_bytes() change * fix build * fix build maybe
2019-06-25 15:38:29 +02:00
Ok(into_message_call_result(out))
2016-01-15 14:22:46 +01:00
}
2020-08-05 06:08:03 +02:00
fn extcode(&self, address: &Address) -> vm::Result<Option<Arc<Bytes>>> {
Ok(self.state.code(address)?)
}
2020-08-05 06:08:03 +02:00
fn extcodehash(&self, address: &Address) -> vm::Result<Option<H256>> {
if self.state.exists_and_not_null(address)? {
Ok(self.state.code_hash(address)?)
} else {
2020-08-05 06:08:03 +02:00
Ok(None)
}
}
fn extcodesize(&self, address: &Address) -> vm::Result<Option<usize>> {
2016-01-15 14:22:46 +01:00
Ok(self.state.code_size(address)?)
2020-08-05 06:08:03 +02:00
}
2016-01-15 14:22:46 +01:00
fn ret(self, gas: &U256, data: &ReturnData, apply_state: bool) -> vm::Result<U256>
where
Self: Sized,
{
2016-01-15 14:22:46 +01:00
match self.output {
OutputPolicy::Return => Ok(*gas),
OutputPolicy::InitContract if apply_state => {
let return_cost =
U256::from(data.len()) * U256::from(self.schedule.create_data_gas);
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 {
true => Err(vm::Error::OutOfGas),
2016-01-15 14:22:46 +01:00
false => Ok(*gas),
2020-08-05 06:08:03 +02:00
};
}
self.state
.init_code(&self.origin_info.address, data.to_vec())?;
Ok(*gas - return_cost)
2016-01-15 14:22:46 +01:00
}
OutputPolicy::InitContract => Ok(*gas),
2020-08-05 06:08:03 +02:00
}
}
fn log(&mut self, topics: Vec<H256>, data: &[u8]) -> vm::Result<()> {
use types::log_entry::LogEntry;
2020-08-05 06:08:03 +02:00
2017-06-19 11:41:46 +02:00
if self.static_flag {
return Err(vm::Error::MutableCallInStaticContext);
2017-06-19 11:41:46 +02:00
}
2020-08-05 06:08:03 +02:00
2016-01-16 20:11:12 +01:00
let address = self.origin_info.address.clone();
self.substate.logs.push(LogEntry {
address: address,
topics: topics,
data: data.to_vec(),
});
2020-08-05 06:08:03 +02:00
2017-06-19 11:41:46 +02:00
Ok(())
2016-01-15 14:22:46 +01:00
}
2020-08-05 06:08:03 +02:00
fn suicide(&mut self, refund_address: &Address) -> vm::Result<()> {
2017-06-19 11:41:46 +02:00
if self.static_flag {
return Err(vm::Error::MutableCallInStaticContext);
2017-06-19 11:41:46 +02:00
}
2020-08-05 06:08:03 +02:00
2016-01-16 20:11:12 +01:00
let address = self.origin_info.address.clone();
let balance = self.balance(&address)?;
if &address == refund_address {
// TODO [todr] To be consistent with CPP client we set balance to 0 in that case.
self.state
.sub_balance(&address, &balance, &mut CleanupMode::NoEmpty)?;
} else {
trace!(target: "ext", "Suiciding {} -> {} (xfer: {})", address, refund_address, balance);
self.state.transfer_balance(
&address,
refund_address,
&balance,
self.substate.to_cleanup_mode(&self.schedule),
)?;
}
2020-08-05 06:08:03 +02:00
self.tracer
.trace_suicide(address, balance, refund_address.clone());
2016-07-21 16:50:24 +02:00
self.substate.suicides.insert(address);
2020-08-05 06:08:03 +02:00
Ok(())
2016-01-15 14:22:46 +01:00
}
2020-08-05 06:08:03 +02:00
2016-01-15 14:22:46 +01:00
fn schedule(&self) -> &Schedule {
&self.schedule
}
2020-08-05 06:08:03 +02:00
2016-01-15 14:22:46 +01:00
fn env_info(&self) -> &EnvInfo {
self.env_info
2016-01-16 20:11:12 +01:00
}
2020-08-05 06:08:03 +02:00
fn chain_id(&self) -> u64 {
self.machine.params().chain_id
}
2020-08-05 06:08:03 +02:00
2016-01-16 20:11:12 +01:00
fn depth(&self) -> usize {
self.depth
}
2020-08-05 06:08:03 +02:00
fn add_sstore_refund(&mut self, value: usize) {
self.substate.sstore_clears_refund += value as i128;
}
2020-08-05 06:08:03 +02:00
fn sub_sstore_refund(&mut self, value: usize) {
self.substate.sstore_clears_refund -= value as i128;
2016-01-15 14:22:46 +01:00
}
2020-08-05 06:08:03 +02:00
fn trace_next_instruction(&mut self, pc: usize, instruction: u8, current_gas: U256) -> bool {
self.vm_tracer
.trace_next_instruction(pc, instruction, current_gas)
}
2020-08-05 06:08:03 +02:00
2018-10-02 16:33:19 +02:00
fn trace_prepare_execute(
&mut self,
pc: usize,
instruction: u8,
gas_cost: U256,
mem_written: Option<(usize, usize)>,
store_written: Option<(U256, U256)>,
) {
self.vm_tracer
.trace_prepare_execute(pc, instruction, gas_cost, mem_written, store_written)
}
2020-08-05 06:08:03 +02:00
fn trace_failed(&mut self) {
self.vm_tracer.trace_failed();
}
2020-08-05 06:08:03 +02:00
2018-10-02 16:33:19 +02:00
fn trace_executed(&mut self, gas_used: U256, stack_push: &[U256], mem: &[u8]) {
self.vm_tracer.trace_executed(gas_used, stack_push, mem)
}
2020-11-04 19:11:05 +01:00
fn al_is_enabled(&self) -> bool {
self.substate.access_list.is_enabled()
}
fn al_contains_storage_key(&self, address: &Address, key: &H256) -> bool {
self.substate.access_list.contains_storage_key(address, key)
}
fn al_insert_storage_key(&mut self, address: Address, key: H256) {
self.substate.access_list.insert_storage_key(address, key)
}
fn al_contains_address(&self, address: &Address) -> bool {
self.substate.access_list.contains_address(address)
}
fn al_insert_address(&mut self, address: Address) {
self.substate.access_list.insert_address(address)
}
2016-01-15 14:22:46 +01:00
}
2016-02-09 15:28:27 +01:00
#[cfg(test)]
mod tests {
use super::*;
use ethereum_types::{Address, U256};
use evm::{CallType, EnvInfo, Ext};
use state::{State, Substate};
Private transactions integration pr (#6422) * Private transaction message added * Empty line removed * Private transactions logic removed from client into the separate module * Fixed compilation after merge with head * Signed private transaction message added as well * Comments after the review fixed * Private tx execution * Test update * Renamed some methods * Fixed some tests * Reverted submodules * Fixed build * Private transaction message added * Empty line removed * Private transactions logic removed from client into the separate module * Fixed compilation after merge with head * Signed private transaction message added as well * Comments after the review fixed * Encrypted private transaction message and signed reply added * Private tx execution * Test update * Main scenario completed * Merged with the latest head * Private transactions API * Comments after review fixed * Parameters for private transactions added to parity arguments * New files added * New API methods added * Do not process packets from unconfirmed peers * Merge with ptm_ss branch * Encryption and permissioning with key server added * Fixed compilation after merge * Version of Parity protocol incremented in order to support private transactions * Doc strings for constants added * Proper format for doc string added * fixed some encryptor.rs grumbles * Private transactions functionality moved to the separate crate * Refactoring in order to remove late initialisation * Tests fixed after moving to the separate crate * Fetch method removed * Sync test helpers refactored * Interaction with encryptor refactored * Contract address retrieving via substate removed * Sensible gas limit for private transactions implemented * New private contract with nonces added * Parsing of the response from key server fixed * Build fixed after the merge, native contracts removed * Crate renamed * Tests moved to the separate directory * Handling of errors reworked in order to use error chain * Encodable macro added, new constructor replaced with default * Native ethabi usage removed * Couple conversions optimized * Interactions with client reworked * Errors omitting removed * Fix after merge * Fix after the merge * private transactions improvements in progress * private_transactions -> ethcore/private-tx * making private transactions more idiomatic * private-tx encryptor uses shared FetchClient and is more idiomatic * removed redundant tests, moved integration tests to tests/ dir * fixed failing service test * reenable add_notify on private tx provider * removed private_tx tests from sync module * removed commented out code * Use plain password instead of unlocking account manager * remove dead code * Link to the contract changed * Transaction signature chain replay protection module created * Redundant type conversion removed * Contract address returned by private provider * Test fixed * Addressing grumbles in PrivateTransactions (#8249) * Tiny fixes part 1. * A bunch of additional comments and todos. * Fix ethsync tests. * resolved merge conflicts * final private tx pr (#8318) * added cli option that enables private transactions * fixed failing test * fixed failing test * fixed failing test * fixed failing test
2018-04-09 16:14:33 +02:00
use test_helpers::get_temp_state;
use trace::{NoopTracer, NoopVMTracer};
2020-08-05 06:08:03 +02:00
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(),
}
}
2020-08-05 06:08:03 +02:00
2016-02-09 15:28:27 +01:00
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(),
last_hashes: Arc::new(vec![]),
2016-05-31 16:59:01 +02:00
gas_used: 0.into(),
gas_limit: 0.into(),
2016-02-09 15:28:27 +01:00
}
2020-08-05 06:08:03 +02:00
}
2016-02-09 17:29:52 +01:00
struct TestSetup {
2017-04-06 19:26:17 +02:00
state: State<::state_db::StateDB>,
machine: ::machine::EthereumMachine,
schedule: Schedule,
2016-02-09 17:29:52 +01:00
sub_state: Substate,
env_info: EnvInfo,
2020-08-05 06:08:03 +02:00
}
2016-02-09 17:29:52 +01:00
impl Default for TestSetup {
2017-04-06 19:26:17 +02:00
fn default() -> Self {
TestSetup::new()
2020-08-05 06:08:03 +02:00
}
}
2017-04-06 19:26:17 +02:00
impl TestSetup {
fn new() -> Self {
let machine = ::spec::Spec::new_test_machine();
2018-10-02 16:33:19 +02:00
let env_info = get_test_env_info();
let schedule = machine.schedule(env_info.number);
2016-02-09 17:29:52 +01:00
TestSetup {
state: get_temp_state(),
schedule: schedule,
2016-02-09 17:29:52 +01:00
machine: machine,
sub_state: Substate::new(),
env_info: env_info,
2020-08-05 06:08:03 +02:00
}
}
2016-02-09 17:29:52 +01:00
}
2020-08-05 06:08:03 +02:00
2016-03-12 10:07:55 +01:00
#[test]
fn can_be_created() {
let mut setup = TestSetup::new();
2016-02-09 17:29:52 +01:00
let state = &mut setup.state;
let mut tracer = NoopTracer;
let mut vm_tracer = NoopVMTracer;
let origin_info = get_test_origin();
2020-08-05 06:08:03 +02:00
let ext = Externalities::new(
2020-08-05 06:08:03 +02:00
state,
&setup.env_info,
&setup.machine,
&setup.schedule,
2020-08-05 06:08:03 +02:00
0,
0,
2018-10-02 16:33:19 +02:00
&origin_info,
&mut setup.sub_state,
2018-10-02 16:33:19 +02:00
OutputPolicy::InitContract,
&mut tracer,
&mut vm_tracer,
2020-08-05 06:08:03 +02:00
false,
);
assert_eq!(ext.env_info().number, 100);
2016-02-09 17:29:52 +01:00
}
2020-08-05 06:08:03 +02:00
2016-02-09 15:28:27 +01:00
#[test]
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;
let mut tracer = NoopTracer;
let mut vm_tracer = NoopVMTracer;
2018-10-02 16:33:19 +02:00
let origin_info = get_test_origin();
2020-08-05 06:08:03 +02:00
2018-10-02 16:33:19 +02:00
let mut ext = Externalities::new(
state,
&setup.env_info,
&setup.machine,
&setup.schedule,
0,
0,
&origin_info,
&mut setup.sub_state,
OutputPolicy::InitContract,
&mut tracer,
&mut vm_tracer,
false,
);
2020-08-05 06:08:03 +02:00
2016-02-09 16:31:57 +01:00
let hash = ext.blockhash(
&"0000000000000000000000000000000000000000000000000000000000120000"
2017-07-29 17:12:07 +02:00
.parse::<U256>()
2020-08-05 06:08:03 +02:00
.unwrap(),
);
2016-02-09 16:31:57 +01:00
assert_eq!(hash, H256::zero());
}
2020-08-05 06:08:03 +02:00
2016-02-09 16:31:57 +01:00
#[test]
2016-02-09 16:54:58 +01:00
fn can_return_block_hash() {
let test_hash =
H256::from("afafafafafafafafafafafbcbcbcbcbcbcbcbcbcbeeeeeeeeeeeeedddddddddd");
let test_env_number = 0x120001;
2020-08-05 06:08:03 +02:00
2016-02-09 17:29:52 +01:00
let mut setup = TestSetup::new();
2020-08-05 06:08:03 +02:00
{
2016-02-09 17:29:52 +01:00
let env_info = &mut setup.env_info;
env_info.number = test_env_number;
let mut last_hashes = (*env_info.last_hashes).clone();
last_hashes.push(test_hash.clone());
env_info.last_hashes = Arc::new(last_hashes);
2020-08-05 06:08:03 +02:00
}
2017-04-06 19:26:17 +02:00
let state = &mut setup.state;
let mut tracer = NoopTracer;
let mut vm_tracer = NoopVMTracer;
2018-10-02 16:33:19 +02:00
let origin_info = get_test_origin();
2020-08-05 06:08:03 +02:00
2018-10-02 16:33:19 +02:00
let mut ext = Externalities::new(
state,
&setup.env_info,
&setup.machine,
&setup.schedule,
0,
0,
&origin_info,
&mut setup.sub_state,
OutputPolicy::InitContract,
&mut tracer,
&mut vm_tracer,
false,
2017-07-29 17:12:07 +02:00
);
2020-08-05 06:08:03 +02:00
2017-07-29 17:12:07 +02:00
let hash = ext.blockhash(
&"0000000000000000000000000000000000000000000000000000000000120000"
2016-02-09 16:31:57 +01:00
.parse::<U256>()
.unwrap(),
);
2020-08-05 06:08:03 +02:00
2016-02-09 16:54:58 +01:00
assert_eq!(test_hash, hash);
2020-08-05 06:08:03 +02:00
}
2016-02-09 16:54:58 +01:00
#[test]
#[should_panic]
fn can_call_fail_empty() {
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;
let mut tracer = NoopTracer;
let mut vm_tracer = NoopVMTracer;
2018-10-02 16:33:19 +02:00
let origin_info = get_test_origin();
2020-08-05 06:08:03 +02:00
2018-10-02 16:33:19 +02:00
let mut ext = Externalities::new(
state,
&setup.env_info,
&setup.machine,
&setup.schedule,
0,
0,
&origin_info,
&mut setup.sub_state,
OutputPolicy::InitContract,
&mut tracer,
&mut vm_tracer,
false,
2017-07-29 17:12:07 +02:00
);
2020-08-05 06:08:03 +02:00
2017-07-29 17:12:07 +02:00
// this should panic because we have no balance on any account
ext.call(
&"0000000000000000000000000000000000000000000000000000000000120000"
.parse::<U256>()
.unwrap(),
2016-02-09 20:30:35 +01:00
&Address::new(),
2018-10-02 16:33:19 +02:00
&Address::new(),
Some(
2017-07-29 17:12:07 +02:00
"0000000000000000000000000000000000000000000000000000000000150000"
.parse::<U256>()
.unwrap(),
2020-08-05 06:08:03 +02:00
),
2017-07-29 17:12:07 +02:00
&[],
2016-02-09 20:30:35 +01:00
&Address::new(),
2017-07-29 17:12:07 +02:00
CallType::Call,
false,
2020-08-05 06:08:03 +02:00
)
2017-07-29 17:12:07 +02:00
.ok()
.unwrap();
2020-08-05 06:08:03 +02:00
}
#[test]
2016-02-09 22:41:45 +01:00
fn can_log() {
let log_data = vec![120u8, 110u8];
2017-07-29 17:12:07 +02:00
let log_topics = vec![H256::from(
2016-02-09 20:30:35 +01:00
"af0fa234a6af46afa23faf23bcbc1c1cb4bcb7bcbe7e7e7ee3ee2edddddddddd",
2020-08-05 06:08:03 +02:00
)];
2016-02-09 20:30:35 +01:00
let mut setup = TestSetup::new();
2016-02-09 22:41:45 +01:00
let state = &mut setup.state;
let mut tracer = NoopTracer;
2016-02-09 23:02:31 +01:00
let mut vm_tracer = NoopVMTracer;
2016-02-09 22:41:45 +01:00
let origin_info = get_test_origin();
2020-08-05 06:08:03 +02:00
{
2016-02-09 22:41:45 +01:00
let mut ext = Externalities::new(
2017-04-06 19:26:17 +02:00
state,
2018-10-02 16:33:19 +02:00
&setup.env_info,
&setup.machine,
2017-04-06 19:26:17 +02:00
&setup.schedule,
2020-08-05 06:08:03 +02:00
0,
0,
2018-10-02 16:33:19 +02:00
&origin_info,
2017-04-06 19:26:17 +02:00
&mut setup.sub_state,
2018-10-02 16:33:19 +02:00
OutputPolicy::InitContract,
&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
}
2020-08-05 06:08:03 +02:00
2016-02-09 22:41:45 +01:00
assert_eq!(setup.sub_state.logs.len(), 1);
}
2020-08-05 06:08:03 +02:00
#[test]
2016-02-09 23:02:31 +01:00
fn can_suicide() {
let refund_account = &Address::new();
2020-08-05 06:08:03 +02:00
2016-02-09 23:02:31 +01:00
let mut setup = TestSetup::new();
let state = &mut setup.state;
let mut tracer = NoopTracer;
let mut vm_tracer = NoopVMTracer;
let origin_info = get_test_origin();
2020-08-05 06:08:03 +02:00
{
2017-04-06 19:26:17 +02:00
let mut ext = Externalities::new(
state,
2018-10-02 16:33:19 +02:00
&setup.env_info,
&setup.machine,
2017-04-06 19:26:17 +02:00
&setup.schedule,
2020-08-05 06:08:03 +02:00
0,
0,
2018-10-02 16:33:19 +02:00
&origin_info,
&mut setup.sub_state,
OutputPolicy::InitContract,
&mut tracer,
&mut vm_tracer,
false,
);
ext.suicide(refund_account).unwrap();
2016-02-09 23:02:31 +01:00
}
2020-08-05 06:08:03 +02:00
2016-02-09 23:02:31 +01:00
assert_eq!(setup.sub_state.suicides.len(), 1);
}
2020-08-05 06:08:03 +02:00
#[test]
fn can_create() {
use std::str::FromStr;
2020-08-05 06:08:03 +02:00
let mut setup = TestSetup::new();
let state = &mut setup.state;
let mut tracer = NoopTracer;
let mut vm_tracer = NoopVMTracer;
let origin_info = get_test_origin();
2020-08-05 06:08:03 +02:00
let address = {
let mut ext = Externalities::new(
state,
&setup.env_info,
2018-10-02 16:33:19 +02:00
&setup.machine,
&setup.schedule,
2020-08-05 06:08:03 +02:00
0,
0,
2018-10-02 16:33:19 +02:00
&origin_info,
&mut setup.sub_state,
OutputPolicy::InitContract,
&mut tracer,
&mut vm_tracer,
false,
);
match ext.create(
&U256::max_value(),
&U256::zero(),
&[],
CreateContractAddress::FromSenderAndNonce,
false,
) {
Ok(ContractCreateResult::Created(address, _)) => address,
_ => panic!("Test create failed; expected Created, got Failed/Reverted."),
}
};
2020-08-05 06:08:03 +02:00
assert_eq!(
address,
Address::from_str("bd770416a3345f91e4b34576cb804a576fa48eb1").unwrap()
);
}
2020-08-05 06:08:03 +02:00
#[test]
fn can_create2() {
use std::str::FromStr;
2020-08-05 06:08:03 +02:00
let mut setup = TestSetup::new();
let state = &mut setup.state;
let mut tracer = NoopTracer;
let mut vm_tracer = NoopVMTracer;
2018-10-02 16:33:19 +02:00
let origin_info = get_test_origin();
2020-08-05 06:08:03 +02:00
let address = {
2018-10-02 16:33:19 +02:00
let mut ext = Externalities::new(
state,
&setup.env_info,
&setup.machine,
&setup.schedule,
0,
0,
&origin_info,
&mut setup.sub_state,
OutputPolicy::InitContract,
&mut tracer,
&mut vm_tracer,
false,
2020-08-05 06:08:03 +02:00
);
2018-10-02 16:33:19 +02:00
match ext.create(
&U256::max_value(),
&U256::zero(),
&[],
CreateContractAddress::FromSenderSaltAndCodeHash(H256::default()),
2020-08-05 06:08:03 +02:00
false,
2018-10-02 16:33:19 +02:00
) {
Ok(ContractCreateResult::Created(address, _)) => address,
_ => panic!("Test create failed; expected Created, got Failed/Reverted."),
}
};
2020-08-05 06:08:03 +02:00
assert_eq!(
address,
Address::from_str("e33c0c7f7df4809055c3eba6c09cfe4baf1bd9e0").unwrap()
);
}
2016-02-09 15:28:27 +01:00
}