Remove code duplication.

This commit is contained in:
Gav Wood 2016-01-27 17:32:12 +01:00
parent 53d0b68ce8
commit dec9c3a642
2 changed files with 16 additions and 51 deletions

View File

@ -5,7 +5,12 @@ use pod_state::*;
use block::Block; use block::Block;
use ethereum; use ethereum;
fn do_json_test(json_data: &[u8]) -> Vec<String> { pub enum ChainEra {
Frontier,
Homestead,
}
pub fn json_chain_test(json_data: &[u8], era: ChainEra) -> Vec<String> {
let json = Json::from_str(::std::str::from_utf8(json_data).unwrap()).expect("Json is invalid"); let json = Json::from_str(::std::str::from_utf8(json_data).unwrap()).expect("Json is invalid");
let mut failed = Vec::new(); let mut failed = Vec::new();
@ -22,7 +27,10 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
flush(format!(" - {}...", name)); flush(format!(" - {}...", name));
let blocks: Vec<(Bytes, bool)> = test["blocks"].as_array().unwrap().iter().map(|e| (xjson!(&e["rlp"]), e.find("blockHeader").is_some())).collect(); let blocks: Vec<(Bytes, bool)> = test["blocks"].as_array().unwrap().iter().map(|e| (xjson!(&e["rlp"]), e.find("blockHeader").is_some())).collect();
let mut spec = ethereum::new_frontier_like_test(); let mut spec = match era {
ChainEra::Frontier => ethereum::new_frontier_test(),
ChainEra::Homestead => ethereum::new_homestead_test(),
};
let s = PodState::from_json(test.find("pre").unwrap()); let s = PodState::from_json(test.find("pre").unwrap());
spec.set_genesis_state(s); spec.set_genesis_state(s);
spec.overwrite_genesis(test.find("genesisBlockHeader").unwrap()); spec.overwrite_genesis(test.find("genesisBlockHeader").unwrap());
@ -52,6 +60,10 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
failed failed
} }
fn do_json_test(json_data: &[u8]) -> Vec<String> {
json_chain_test(json_data, ChainEra::Frontier)
}
declare_test!{BlockchainTests_bcBlockGasLimitTest, "BlockchainTests/bcBlockGasLimitTest"} declare_test!{BlockchainTests_bcBlockGasLimitTest, "BlockchainTests/bcBlockGasLimitTest"}
declare_test!{BlockchainTests_bcForkBlockTest, "BlockchainTests/bcForkBlockTest"} declare_test!{BlockchainTests_bcForkBlockTest, "BlockchainTests/bcForkBlockTest"}
declare_test!{BlockchainTests_bcForkStressTest, "BlockchainTests/bcForkStressTest"} declare_test!{BlockchainTests_bcForkStressTest, "BlockchainTests/bcForkStressTest"}

View File

@ -1,55 +1,8 @@
use std::env;
use super::test_common::*; use super::test_common::*;
use client::{BlockChainClient,Client}; use super::chain::{ChainEra, json_chain_test};
use pod_state::*;
use block::Block;
use ethereum;
fn do_json_test(json_data: &[u8]) -> Vec<String> { fn do_json_test(json_data: &[u8]) -> Vec<String> {
let json = Json::from_str(::std::str::from_utf8(json_data).unwrap()).expect("Json is invalid"); json_chain_test(json_data, ChainEra::Homestead)
let mut failed = Vec::new();
for (name, test) in json.as_object().unwrap() {
let mut fail = false;
{
let mut fail_unless = |cond: bool| if !cond && !fail {
failed.push(name.clone());
flush(format!("FAIL\n"));
fail = true;
true
} else {false};
flush(format!(" - {}...", name));
let blocks: Vec<(Bytes, bool)> = test["blocks"].as_array().unwrap().iter().map(|e| (xjson!(&e["rlp"]), e.find("blockHeader").is_some())).collect();
let mut spec = ethereum::new_homestead_test();
let s = PodState::from_json(test.find("pre").unwrap());
spec.set_genesis_state(s);
spec.overwrite_genesis(test.find("genesisBlockHeader").unwrap());
assert!(spec.is_state_root_valid());
let mut dir = env::temp_dir();
dir.push(H32::random().hex());
{
let client = Client::new(spec, &dir, IoChannel::disconnected()).unwrap();
for (b, is_valid) in blocks.into_iter() {
if Block::is_good(&b) {
let _ = client.import_block(b.clone());
}
client.flush_queue();
let imported_ok = client.import_verified_blocks(&IoChannel::disconnected()) > 0;
assert_eq!(imported_ok, is_valid);
}
fail_unless(client.chain_info().best_block_hash == H256::from_json(&test["lastblockhash"]));
}
fs::remove_dir_all(&dir).unwrap();
}
if !fail {
flush(format!("ok\n"));
}
}
println!("!!! {:?} tests from failed.", failed.len());
failed
} }
declare_test!{BlockchainTests_Homestead_bcBlockGasLimitTest, "BlockchainTests/Homestead/bcBlockGasLimitTest"} declare_test!{BlockchainTests_Homestead_bcBlockGasLimitTest, "BlockchainTests/Homestead/bcBlockGasLimitTest"}