Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Difficulty tests #6687

Merged
merged 4 commits into from
Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ethcore/res/ethereum/tests
Submodule tests updated 13925 files
6 changes: 4 additions & 2 deletions ethcore/src/json_tests/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ pub fn json_chain_test(json_data: &[u8]) -> Vec<String> {

{
let db = Arc::new(::kvdb::in_memory(::db::NUM_COLUMNS.unwrap_or(0)));
let mut config = ClientConfig::default();
config.history = 8;
let client = Client::new(
ClientConfig::default(),
config,
&spec,
db,
Arc::new(Miner::with_spec(&spec)),
Expand Down Expand Up @@ -101,7 +103,7 @@ mod block_tests {
declare_test!{BlockchainTests_bcMultiChainTest, "BlockchainTests/bcMultiChainTest"}
declare_test!{BlockchainTests_bcRandomBlockhashTest, "BlockchainTests/bcRandomBlockhashTest"}
declare_test!{BlockchainTests_bcTotalDifficultyTest, "BlockchainTests/bcTotalDifficultyTest"}
declare_test!{BlockchainTests_bcUncleHeaderValiditiy, "BlockchainTests/bcUncleHeaderValiditiy"}
declare_test!{BlockchainTests_bcUncleHeaderValidity, "BlockchainTests/bcUncleHeaderValidity"}
declare_test!{BlockchainTests_bcUncleTest, "BlockchainTests/bcUncleTest"}
declare_test!{BlockchainTests_bcValidBlockTest, "BlockchainTests/bcValidBlockTest"}
declare_test!{BlockchainTests_bcWalletTest, "BlockchainTests/bcWalletTest"}
Expand Down
71 changes: 71 additions & 0 deletions ethcore/src/json_tests/difficulty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// 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.

// Parity is distributed in the hope that it will be useful,
// 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
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

use ethjson;
use header::Header;
use bigint::prelude::U256;
use spec::Spec;

pub fn json_difficulty_test(json_data: &[u8], spec: Spec) -> Vec<String> {
::ethcore_logger::init_log();
let tests = ethjson::test::DifficultyTest::load(json_data).unwrap();
let engine = &spec.engine;

for (name, test) in tests.into_iter() {
flush!(" - {}...", name);
println!(" - {}...", name);

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();
assert_eq!(header.difficulty(), &expected_difficulty);
flushln!("ok");
}
vec![]
}

mod difficulty_test_byzantium {
use super::json_difficulty_test;

fn do_json_test(json_data: &[u8]) -> Vec<String> {
json_difficulty_test(json_data, ::ethereum::new_byzantium_test())
}

declare_test!{DifficultyTests_difficultyByzantium, "BasicTests/difficultyByzantium.json"}
}


mod difficulty_test_foundation {
use super::json_difficulty_test;

fn do_json_test(json_data: &[u8]) -> Vec<String> {
json_difficulty_test(json_data, ::ethereum::new_foundation(&::std::env::temp_dir()))
}

declare_test!{DifficultyTests_difficultyMainNetwork, "BasicTests/difficultyMainNetwork.json"}
}



1 change: 1 addition & 0 deletions ethcore/src/json_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ mod executive;
mod state;
mod chain;
mod trie;
mod difficulty;
1 change: 1 addition & 0 deletions json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ pub mod maybe;
pub mod state;
pub mod transaction;
pub mod misc;
pub mod test;
67 changes: 67 additions & 0 deletions json/src/test/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// 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.

// Parity is distributed in the hope that it will be useful,
// 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
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

//! Additional test structures deserialization.

use std::collections::BTreeMap;
use std::io::Read;
use serde_json;
use serde_json::Error;
use hash::H256;
use uint::Uint;

/// Blockchain test header deserializer.
#[derive(Debug, PartialEq, Deserialize)]
pub struct DifficultyTestCase {
/// Parent timestamp.
#[serde(rename="parentTimestamp")]
pub parent_timestamp: Uint,
/// Parent difficulty.
#[serde(rename="parentDifficulty")]
pub parent_difficulty: Uint,
/// Parent uncle hash.
#[serde(rename="parentUncles")]
pub parent_uncles: H256,
/// Current timestamp.
#[serde(rename="currentTimestamp")]
pub current_timestamp: Uint,
/// Current difficulty.
#[serde(rename="currentDifficulty")]
pub current_difficulty: Uint,
/// Current block number.
#[serde(rename="currentBlockNumber")]
pub current_block_number: Uint,
}
/// Blockchain test deserializer.
#[derive(Debug, PartialEq, Deserialize)]
pub struct DifficultyTest(BTreeMap<String, DifficultyTestCase>);

impl IntoIterator for DifficultyTest {
type Item = <BTreeMap<String, DifficultyTestCase> as IntoIterator>::Item;
type IntoIter = <BTreeMap<String, DifficultyTestCase> as IntoIterator>::IntoIter;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}

impl DifficultyTest {
/// Loads test from json.
pub fn load<R>(reader: R) -> Result<Self, Error> where R: Read {
serde_json::from_reader(reader)
}
}