2020-09-22 14:53:52 +02:00
|
|
|
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of OpenEthereum.
|
2017-10-10 14:04:29 +02:00
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is free software: you can redistribute it and/or modify
|
2017-10-10 14:04:29 +02: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,
|
2017-10-10 14:04:29 +02: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/>.
|
2017-10-10 14:04:29 +02:00
|
|
|
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::U256;
|
2017-10-10 14:04:29 +02:00
|
|
|
use ethjson;
|
|
|
|
use spec::Spec;
|
2020-09-08 02:48:09 +02:00
|
|
|
use std::path::Path;
|
2019-01-04 14:05:46 +01:00
|
|
|
use types::header::Header;
|
2017-10-10 14:04:29 +02:00
|
|
|
|
2018-06-25 11:21:45 +02:00
|
|
|
use super::HookType;
|
|
|
|
|
|
|
|
pub fn json_difficulty_test<H: FnMut(&str, HookType)>(
|
2020-09-08 02:48:09 +02:00
|
|
|
path: &Path,
|
2018-06-25 11:21:45 +02:00
|
|
|
json_data: &[u8],
|
|
|
|
spec: Spec,
|
|
|
|
start_stop_hook: &mut H,
|
|
|
|
) -> Vec<String> {
|
2020-09-08 02:48:09 +02:00
|
|
|
let mut ret = Vec::new();
|
|
|
|
let _ = env_logger::try_init();
|
|
|
|
let tests = ethjson::test::DifficultyTest::load(json_data).expect(&format!(
|
|
|
|
"Could not parse JSON difficulty test data from {}",
|
|
|
|
path.display()
|
|
|
|
));
|
2017-10-10 14:04:29 +02:00
|
|
|
let engine = &spec.engine;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-10-10 14:04:29 +02:00
|
|
|
for (name, test) in tests.into_iter() {
|
2020-09-10 08:04:14 +02:00
|
|
|
if !super::debug_include_test(&name) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-06-25 11:21:45 +02:00
|
|
|
start_stop_hook(&name, HookType::OnStart);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-10-10 14:04:29 +02:00
|
|
|
let mut parent_header = Header::new();
|
|
|
|
let block_number: u64 = test.current_block_number.into();
|
|
|
|
parent_header.set_number(block_number - 1);
|
|
|
|
parent_header.set_gas_limit(0x20000.into());
|
|
|
|
parent_header.set_timestamp(test.parent_timestamp.into());
|
|
|
|
parent_header.set_difficulty(test.parent_difficulty.into());
|
|
|
|
parent_header.set_uncles_hash(test.parent_uncles.into());
|
|
|
|
let mut header = Header::new();
|
|
|
|
header.set_number(block_number);
|
|
|
|
header.set_timestamp(test.current_timestamp.into());
|
|
|
|
engine.populate_from_parent(&mut header, &parent_header);
|
|
|
|
let expected_difficulty: U256 = test.current_difficulty.into();
|
2020-09-08 02:48:09 +02:00
|
|
|
if header.difficulty() == &expected_difficulty {
|
|
|
|
flushln!(" - difficulty: {}...OK", name);
|
|
|
|
} else {
|
|
|
|
flushln!(" - difficulty: {}...FAILED", name);
|
|
|
|
ret.push(format!("{}:{}", path.to_string_lossy(), name));
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-06-25 11:21:45 +02:00
|
|
|
start_stop_hook(&name, HookType::OnStop);
|
2017-10-10 14:04:29 +02:00
|
|
|
}
|
2020-09-08 02:48:09 +02:00
|
|
|
ret
|
2018-09-10 22:38:30 +02:00
|
|
|
}
|