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
|
|
|
|
|
2020-09-08 02:48:09 +02:00
|
|
|
|
use super::HookType;
|
|
|
|
|
use client::{
|
|
|
|
|
Balance, BlockChainClient, BlockId, ChainInfo, Client, ClientConfig, EvmTestClient,
|
|
|
|
|
ImportBlock, Nonce, StateOrBlock,
|
|
|
|
|
};
|
|
|
|
|
use ethereum_types::{H256, U256};
|
2016-03-19 18:38:02 +01:00
|
|
|
|
use ethjson;
|
2016-08-05 10:32:04 +02:00
|
|
|
|
use io::IoChannel;
|
2020-09-08 02:48:09 +02:00
|
|
|
|
use log::warn;
|
2016-05-31 22:24:32 +02:00
|
|
|
|
use miner::Miner;
|
2020-09-08 02:48:09 +02:00
|
|
|
|
use rustc_hex::ToHex;
|
2016-03-19 18:13:14 +01:00
|
|
|
|
use spec::Genesis;
|
2017-07-29 23:19:33 +02:00
|
|
|
|
use std::{path::Path, sync::Arc};
|
2018-06-20 15:13:07 +02:00
|
|
|
|
use test_helpers;
|
2018-08-02 11:20:46 +02:00
|
|
|
|
use verification::{queue::kind::blocks::Unverified, VerifierType};
|
2018-06-25 11:21:45 +02:00
|
|
|
|
|
2020-09-08 02:48:09 +02:00
|
|
|
|
fn check_poststate(
|
|
|
|
|
client: &Arc<Client>,
|
|
|
|
|
test_name: &str,
|
|
|
|
|
post_state: ethjson::blockchain::State,
|
|
|
|
|
) -> bool {
|
|
|
|
|
let mut success = true;
|
|
|
|
|
|
|
|
|
|
for (address, expected) in post_state {
|
|
|
|
|
if let Some(expected_balance) = expected.balance {
|
|
|
|
|
let expected_balance: U256 = expected_balance.into();
|
|
|
|
|
let current_balance = client
|
|
|
|
|
.balance(
|
|
|
|
|
&address.clone().into(),
|
|
|
|
|
StateOrBlock::Block(BlockId::Latest),
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
if expected_balance != current_balance {
|
|
|
|
|
warn!(target: "json-tests", "{} – Poststate {:?} balance mismatch current={} expected={}",
|
|
|
|
|
test_name, address, current_balance, expected_balance);
|
|
|
|
|
success = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-25 11:21:45 +02:00
|
|
|
|
|
2020-09-08 02:48:09 +02:00
|
|
|
|
if let Some(expected_nonce) = expected.nonce {
|
|
|
|
|
let expected_nonce: U256 = expected_nonce.into();
|
|
|
|
|
let current_nonce = client
|
|
|
|
|
.nonce(&address.clone().into(), BlockId::Latest)
|
|
|
|
|
.unwrap();
|
|
|
|
|
if expected_nonce != current_nonce {
|
|
|
|
|
warn!(target: "json-tests", "{} – Poststate {:?} nonce mismatch current={} expected={}",
|
|
|
|
|
test_name, address, current_nonce, expected_nonce);
|
|
|
|
|
success = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(expected_code) = expected.code {
|
|
|
|
|
let expected_code: String = expected_code.to_hex();
|
|
|
|
|
let current_code = match client.code(
|
|
|
|
|
&address.clone().into(),
|
|
|
|
|
StateOrBlock::Block(BlockId::Latest),
|
|
|
|
|
) {
|
|
|
|
|
Some(Some(code)) => code.to_hex(),
|
|
|
|
|
_ => "".to_string(),
|
|
|
|
|
};
|
|
|
|
|
if current_code != expected_code {
|
|
|
|
|
warn!(target: "json-tests", "{} – Poststate {:?} code mismatch current={} expected={}",
|
|
|
|
|
test_name, address, current_code, expected_code);
|
|
|
|
|
success = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(expected_storage) = expected.storage {
|
|
|
|
|
for (uint_position, uint_expected_value) in expected_storage.iter() {
|
|
|
|
|
let mut position = H256::default();
|
|
|
|
|
uint_position.0.to_big_endian(position.as_mut());
|
|
|
|
|
|
|
|
|
|
let mut expected_value = H256::default();
|
|
|
|
|
uint_expected_value.0.to_big_endian(expected_value.as_mut());
|
|
|
|
|
|
|
|
|
|
let current_value = client
|
|
|
|
|
.storage_at(
|
|
|
|
|
&address.clone().into(),
|
|
|
|
|
&position,
|
|
|
|
|
StateOrBlock::Block(BlockId::Latest),
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
if current_value != expected_value {
|
|
|
|
|
let position: &[u8] = position.as_ref();
|
|
|
|
|
let current_value: &[u8] = current_value.as_ref();
|
|
|
|
|
let expected_value: &[u8] = expected_value.as_ref();
|
|
|
|
|
warn!(target: "json-tests", "{} – Poststate {:?} state {} mismatch actual={} expected={}",
|
|
|
|
|
test_name, address, position.to_hex(), current_value.to_hex(),
|
|
|
|
|
expected_value.to_hex());
|
|
|
|
|
success = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-25 11:21:45 +02:00
|
|
|
|
|
2020-09-08 02:48:09 +02:00
|
|
|
|
if expected.builtin.is_some() {
|
|
|
|
|
warn!(target: "json-tests", "{} – Poststate {:?} builtin not supported", test_name, address);
|
|
|
|
|
success = false;
|
|
|
|
|
}
|
|
|
|
|
if expected.constructor.is_some() {
|
|
|
|
|
warn!(target: "json-tests", "{} – Poststate {:?} constructor not supported", test_name, address);
|
|
|
|
|
success = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
success
|
2018-09-25 12:24:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-25 11:21:45 +02:00
|
|
|
|
pub fn json_chain_test<H: FnMut(&str, HookType)>(
|
2020-09-08 02:48:09 +02:00
|
|
|
|
test: ðjson::test::ChainTests,
|
|
|
|
|
path: &Path,
|
2018-06-25 11:21:45 +02:00
|
|
|
|
json_data: &[u8],
|
|
|
|
|
start_stop_hook: &mut H,
|
|
|
|
|
) -> Vec<String> {
|
2019-01-08 15:07:20 +01:00
|
|
|
|
let _ = ::env_logger::try_init();
|
2020-09-08 02:48:09 +02:00
|
|
|
|
let tests = ethjson::blockchain::Test::load(json_data).expect(&format!(
|
|
|
|
|
"Could not parse JSON chain test data from {}",
|
|
|
|
|
path.display()
|
|
|
|
|
));
|
2016-01-25 18:56:36 +01:00
|
|
|
|
let mut failed = Vec::new();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
|
2016-03-24 01:25:59 +01:00
|
|
|
|
for (name, blockchain) in tests.into_iter() {
|
2020-09-10 08:04:14 +02:00
|
|
|
|
if !super::debug_include_test(&name) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 02:48:09 +02:00
|
|
|
|
let skip_test = test
|
|
|
|
|
.skip
|
|
|
|
|
.iter()
|
|
|
|
|
.any(|block_test| block_test.names.contains(&name));
|
2020-09-10 08:04:14 +02:00
|
|
|
|
|
2020-09-08 02:48:09 +02:00
|
|
|
|
if skip_test {
|
|
|
|
|
info!(" SKIPPED {:?} {:?}", name, blockchain.network);
|
2018-09-25 12:24:40 +02:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
|
2016-01-25 18:56:36 +01:00
|
|
|
|
let mut fail = false;
|
|
|
|
|
{
|
|
|
|
|
let mut fail_unless = |cond: bool| {
|
|
|
|
|
if !cond && !fail {
|
|
|
|
|
failed.push(name.clone());
|
2016-02-03 13:20:32 +01:00
|
|
|
|
flushln!("FAIL");
|
2016-01-25 18:56:36 +01:00
|
|
|
|
fail = true;
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
2020-08-05 06:08:03 +02:00
|
|
|
|
}
|
2016-01-25 18:56:36 +01:00
|
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
|
2016-08-05 23:33:55 +02:00
|
|
|
|
let spec = {
|
2017-09-15 21:07:54 +02:00
|
|
|
|
let mut spec = match EvmTestClient::spec_from_json(&blockchain.network) {
|
2018-06-25 11:21:45 +02:00
|
|
|
|
Some(spec) => spec,
|
2017-09-15 21:07:54 +02:00
|
|
|
|
None => {
|
2020-09-08 02:48:09 +02:00
|
|
|
|
info!(
|
|
|
|
|
" SKIPPED {:?} {:?} - Unimplemented chainspec ",
|
2017-09-15 21:07:54 +02:00
|
|
|
|
name, blockchain.network
|
|
|
|
|
);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
|
2016-06-24 10:49:13 +02:00
|
|
|
|
let genesis = Genesis::from(blockchain.genesis());
|
|
|
|
|
let state = From::from(blockchain.pre_state.clone());
|
2017-05-03 09:00:02 +02:00
|
|
|
|
spec.set_genesis_state(state)
|
|
|
|
|
.expect("Failed to overwrite genesis state");
|
2016-06-24 10:49:13 +02:00
|
|
|
|
spec.overwrite_genesis_params(genesis);
|
|
|
|
|
spec
|
2016-01-27 17:32:12 +01:00
|
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
|
2020-09-08 02:48:09 +02:00
|
|
|
|
start_stop_hook(&name, HookType::OnStart);
|
|
|
|
|
|
2016-01-25 18:56:36 +01:00
|
|
|
|
{
|
2018-06-20 15:13:07 +02:00
|
|
|
|
let db = test_helpers::new_db();
|
2017-10-10 16:56:03 +02:00
|
|
|
|
let mut config = ClientConfig::default();
|
2018-11-01 11:04:32 +01:00
|
|
|
|
if ethjson::blockchain::Engine::NoProof == blockchain.engine {
|
|
|
|
|
config.verifier_type = VerifierType::CanonNoSeal;
|
|
|
|
|
config.check_seal = false;
|
|
|
|
|
}
|
2017-10-10 16:56:03 +02:00
|
|
|
|
config.history = 8;
|
2020-09-08 02:48:09 +02:00
|
|
|
|
config.queue.verifier_settings.num_verifiers = 1;
|
2016-06-24 10:49:13 +02:00
|
|
|
|
let client = Client::new(
|
2017-10-10 16:56:03 +02:00
|
|
|
|
config,
|
2016-08-05 23:33:55 +02:00
|
|
|
|
&spec,
|
2017-02-20 17:21:55 +01:00
|
|
|
|
db,
|
2018-04-13 17:34:27 +02:00
|
|
|
|
Arc::new(Miner::new_for_tests(&spec, None)),
|
2016-09-07 15:27:28 +02:00
|
|
|
|
IoChannel::disconnected(),
|
2016-06-24 10:49:13 +02:00
|
|
|
|
)
|
2020-09-08 02:48:09 +02:00
|
|
|
|
.expect("Failed to instantiate a new Client");
|
|
|
|
|
|
2018-08-02 11:20:46 +02:00
|
|
|
|
for b in blockchain.blocks_rlp() {
|
2020-09-08 02:48:09 +02:00
|
|
|
|
let bytes_len = b.len();
|
|
|
|
|
let block = Unverified::from_rlp(b);
|
|
|
|
|
match block {
|
|
|
|
|
Ok(block) => {
|
|
|
|
|
let num = block.header.number();
|
2021-01-28 17:23:01 +01:00
|
|
|
|
debug!(target: "json-tests", "{} – Importing {} bytes. Block #{}", name, bytes_len, num);
|
2020-09-08 02:48:09 +02:00
|
|
|
|
let res = client.import_block(block);
|
|
|
|
|
if let Err(e) = res {
|
|
|
|
|
warn!(target: "json-tests", "{} – Error importing block #{}: {:?}", name, num, e);
|
|
|
|
|
}
|
|
|
|
|
client.flush_queue();
|
|
|
|
|
client.import_verified_blocks();
|
|
|
|
|
}
|
|
|
|
|
Err(decoder_err) => {
|
|
|
|
|
warn!(target: "json-tests", "Error decoding test block: {:?} ({} bytes)", decoder_err, bytes_len);
|
|
|
|
|
}
|
2016-01-26 18:02:49 +01:00
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
}
|
2020-09-08 02:48:09 +02:00
|
|
|
|
|
|
|
|
|
let post_state_success = if let Some(post_state) = blockchain.post_state.clone() {
|
|
|
|
|
check_poststate(&client, &name, post_state)
|
|
|
|
|
} else {
|
|
|
|
|
true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fail_unless(
|
|
|
|
|
client.chain_info().best_block_hash == blockchain.best_block.into()
|
|
|
|
|
&& post_state_success,
|
|
|
|
|
);
|
2016-01-25 18:56:36 +01:00
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 02:48:09 +02:00
|
|
|
|
if fail {
|
|
|
|
|
flushln!(" - chain: {}...FAILED", name);
|
|
|
|
|
} else {
|
|
|
|
|
flushln!(" - chain: {}...OK", name);
|
2016-01-25 18:56:36 +01:00
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
|
2018-06-25 11:21:45 +02:00
|
|
|
|
start_stop_hook(&name, HookType::OnStop);
|
2016-01-25 18:56:36 +01:00
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
|
2016-01-25 18:56:36 +01:00
|
|
|
|
failed
|
|
|
|
|
}
|