2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2018-03-12 18:05:52 +01:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2018-03-12 18:05:52 +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.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2018-03-12 18:05:52 +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
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2018-03-12 18:05:52 +01:00
|
|
|
|
2017-07-12 13:09:17 +02:00
|
|
|
//! Tests of EVM integration with transaction execution.
|
|
|
|
|
2017-07-29 21:56:42 +02:00
|
|
|
use std::sync::Arc;
|
2017-08-30 19:18:28 +02:00
|
|
|
use hash::keccak;
|
2017-11-02 12:49:57 +01:00
|
|
|
use vm::{EnvInfo, ActionParams, ActionValue, CallType, ParamsType};
|
2017-07-12 13:09:17 +02:00
|
|
|
use evm::{Factory, VMType};
|
|
|
|
use executive::Executive;
|
|
|
|
use state::Substate;
|
2018-04-09 16:14:33 +02:00
|
|
|
use test_helpers::get_temp_state_with_factory;
|
2017-07-12 13:09:17 +02:00
|
|
|
use trace::{NoopVMTracer, NoopTracer};
|
2019-01-04 14:05:46 +01:00
|
|
|
use types::transaction::SYSTEM_ADDRESS;
|
2017-07-12 13:09:17 +02:00
|
|
|
|
|
|
|
use rustc_hex::FromHex;
|
|
|
|
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::{H256, Address};
|
2017-07-12 13:09:17 +02:00
|
|
|
|
2018-04-04 11:07:49 +02:00
|
|
|
evm_test!{test_blockhash_eip210: test_blockhash_eip210_int}
|
2017-07-12 13:09:17 +02:00
|
|
|
fn test_blockhash_eip210(factory: Factory) {
|
|
|
|
let get_prev_hash_code = Arc::new("600143034060205260206020f3".from_hex().unwrap()); // this returns previous block hash
|
2017-08-30 19:18:28 +02:00
|
|
|
let get_prev_hash_code_hash = keccak(get_prev_hash_code.as_ref());
|
2017-07-12 13:09:17 +02:00
|
|
|
// This is same as DEFAULT_BLOCKHASH_CONTRACT except for metropolis transition block check removed.
|
|
|
|
let test_blockhash_contract = "73fffffffffffffffffffffffffffffffffffffffe33141561007a57600143036020526000356101006020510755600061010060205107141561005057600035610100610100602051050761010001555b6000620100006020510714156100755760003561010062010000602051050761020001555b61014a565b4360003512151561009057600060405260206040f35b610100600035430312156100b357610100600035075460605260206060f3610149565b62010000600035430312156100d157600061010060003507146100d4565b60005b156100f6576101006101006000350507610100015460805260206080f3610148565b630100000060003543031215610116576000620100006000350714610119565b60005b1561013c57610100620100006000350507610200015460a052602060a0f3610147565b600060c052602060c0f35b5b5b5b5b";
|
|
|
|
let blockhash_contract_code = Arc::new(test_blockhash_contract.from_hex().unwrap());
|
2017-08-30 19:18:28 +02:00
|
|
|
let blockhash_contract_code_hash = keccak(blockhash_contract_code.as_ref());
|
2018-09-11 20:08:23 +02:00
|
|
|
let machine = ::ethereum::new_eip210_test_machine();
|
2017-07-12 13:09:17 +02:00
|
|
|
let mut env_info = EnvInfo::default();
|
|
|
|
|
|
|
|
// populate state with 256 last hashes
|
|
|
|
let mut state = get_temp_state_with_factory(factory);
|
|
|
|
let contract_address: Address = 0xf0.into();
|
|
|
|
state.init_code(&contract_address, (*blockhash_contract_code).clone()).unwrap();
|
|
|
|
for i in 1 .. 257 {
|
|
|
|
env_info.number = i.into();
|
|
|
|
let params = ActionParams {
|
|
|
|
code_address: contract_address.clone(),
|
|
|
|
address: contract_address,
|
|
|
|
sender: SYSTEM_ADDRESS.clone(),
|
|
|
|
origin: SYSTEM_ADDRESS.clone(),
|
|
|
|
gas: 100000.into(),
|
|
|
|
gas_price: 0.into(),
|
|
|
|
value: ActionValue::Transfer(0.into()),
|
|
|
|
code: Some(blockhash_contract_code.clone()),
|
|
|
|
code_hash: Some(blockhash_contract_code_hash),
|
|
|
|
data: Some(H256::from(i - 1).to_vec()),
|
|
|
|
call_type: CallType::Call,
|
2017-11-02 12:49:57 +01:00
|
|
|
params_type: ParamsType::Separate,
|
2017-07-12 13:09:17 +02:00
|
|
|
};
|
2018-07-23 15:48:01 +02:00
|
|
|
let schedule = machine.schedule(env_info.number);
|
|
|
|
let mut ex = Executive::new(&mut state, &env_info, &machine, &schedule);
|
2017-07-12 13:09:17 +02:00
|
|
|
let mut substate = Substate::new();
|
2018-08-13 23:27:13 +02:00
|
|
|
if let Err(e) = ex.call(params, &mut substate, &mut NoopTracer, &mut NoopVMTracer) {
|
2017-07-12 13:09:17 +02:00
|
|
|
panic!("Encountered error on updating last hashes: {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
env_info.number = 256;
|
|
|
|
let params = ActionParams {
|
|
|
|
code_address: Address::new(),
|
|
|
|
address: Address::new(),
|
|
|
|
sender: Address::new(),
|
|
|
|
origin: Address::new(),
|
|
|
|
gas: 100000.into(),
|
|
|
|
gas_price: 0.into(),
|
|
|
|
value: ActionValue::Transfer(0.into()),
|
|
|
|
code: Some(get_prev_hash_code),
|
|
|
|
code_hash: Some(get_prev_hash_code_hash),
|
|
|
|
data: None,
|
|
|
|
call_type: CallType::Call,
|
2017-11-02 12:49:57 +01:00
|
|
|
params_type: ParamsType::Separate,
|
2017-07-12 13:09:17 +02:00
|
|
|
};
|
2018-07-23 15:48:01 +02:00
|
|
|
let schedule = machine.schedule(env_info.number);
|
|
|
|
let mut ex = Executive::new(&mut state, &env_info, &machine, &schedule);
|
2017-07-12 13:09:17 +02:00
|
|
|
let mut substate = Substate::new();
|
2018-08-13 23:27:13 +02:00
|
|
|
let res = ex.call(params, &mut substate, &mut NoopTracer, &mut NoopVMTracer);
|
|
|
|
let output = match res {
|
|
|
|
Ok(res) => H256::from(&res.return_data[..32]),
|
|
|
|
Err(e) => {
|
|
|
|
panic!("Encountered error on getting last hash: {}", e);
|
|
|
|
},
|
|
|
|
};
|
2017-07-12 13:09:17 +02:00
|
|
|
assert_eq!(output, 255.into());
|
|
|
|
}
|