openethereum/ethcore/src/json_tests/chain.rs

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

249 lines
9.0 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
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;
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;
use spec::Genesis;
2017-07-29 23:19:33 +02:00
use std::{path::Path, sync::Arc};
use test_helpers;
use verification::{queue::kind::blocks::Unverified, VerifierType};
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;
}
}
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;
}
}
}
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
}
pub fn json_chain_test<H: FnMut(&str, HookType)>(
2020-09-08 02:48:09 +02:00
test: &ethjson::test::ChainTests,
path: &Path,
json_data: &[u8],
start_stop_hook: &mut H,
) -> Vec<String> {
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
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);
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
let spec = {
let mut spec = match EvmTestClient::spec_from_json(&blockchain.network) {
Some(spec) => spec,
None => {
2020-09-08 02:48:09 +02:00
info!(
" SKIPPED {:?} {:?} - Unimplemented chainspec ",
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());
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
{
let db = test_helpers::new_db();
2017-10-10 16:56:03 +02:00
let mut config = ClientConfig::default();
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,
&spec,
db,
New Transaction Queue implementation (#8074) * Implementation of Verifier, Scoring and Ready. * Queue in progress. * TransactionPool. * Prepare for txpool release. * Miner refactor [WiP] * WiP reworking miner. * Make it compile. * Add some docs. * Split blockchain access to a separate file. * Work on miner API. * Fix ethcore tests. * Refactor miner interface for sealing/work packages. * Implement next nonce. * RPC compiles. * Implement couple of missing methdods for RPC. * Add transaction queue listeners. * Compiles! * Clean-up and parallelize. * Get rid of RefCell in header. * Revert "Get rid of RefCell in header." This reverts commit 0f2424c9b7319a786e1565ea2a8a6d801a21b4fb. * Override Sync requirement. * Fix status display. * Unify logging. * Extract some cheap checks. * Measurements and optimizations. * Fix scoring bug, heap size of bug and add cache * Disable tx queueing and parallel verification. * Make ethcore and ethcore-miner compile again. * Make RPC compile again. * Bunch of txpool tests. * Migrate transaction queue tests. * Nonce Cap * Nonce cap cache and tests. * Remove stale future transactions from the queue. * Optimize scoring and write some tests. * Simple penalization. * Clean up and support for different scoring algorithms. * Add CLI parameters for the new queue. * Remove banning queue. * Disable debug build. * Change per_sender limit to be 1% instead of 5% * Avoid cloning when propagating transactions. * Remove old todo. * Post-review fixes. * Fix miner options default. * Implement back ready transactions for light client. * Get rid of from_pending_block * Pass rejection reason. * Add more details to drop. * Rollback heap size of. * Avoid cloning hashes when propagating and include more details on rejection. * Fix tests. * Introduce nonces cache. * Remove uneccessary hashes allocation. * Lower the mem limit. * Re-enable parallel verification. * Add miner log. Don't check the type if not below min_gas_price. * Add more traces, fix disabling miner. * Fix creating pending blocks twice on AuRa authorities. * Fix tests. * re-use pending blocks in AuRa * Use reseal_min_period to prevent too frequent update_sealing. * Fix log to contain hash not sender. * Optimize local transactions. * Fix aura tests. * Update locks comments. * Get rid of unsafe Sync impl. * Review fixes. * Remove excessive matches. * Fix compilation errors. * Use new pool in private transactions. * Fix private-tx test. * Fix secret store tests. * Actually use gas_floor_target * Fix config tests. * Fix pool tests. * Address grumbles.
2018-04-13 17:34:27 +02:00
Arc::new(Miner::new_for_tests(&spec, None)),
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");
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();
trace!(target: "json-tests", "{} Importing {} bytes. Block #{}", name, bytes_len, num);
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
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
}