openethereum/src/tests/chain.rs

73 lines
3.1 KiB
Rust
Raw Normal View History

2016-01-25 18:56:36 +01:00
use std::env;
use super::test_common::*;
use client::{BlockChainClient,Client};
use pod_state::*;
use block::Block;
2016-01-25 18:56:36 +01:00
use ethereum;
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");
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();
2016-01-25 18:56:36 +01:00
let mut spec = ethereum::new_frontier_like_test();
let s = PodState::from_json(test.find("pre").unwrap());
spec.set_genesis_state(s);
2016-01-25 18:56:36 +01:00
spec.overwrite_genesis(test.find("genesisBlockHeader").unwrap());
2016-01-25 23:24:51 +01:00
assert!(spec.is_state_root_valid());
2016-01-25 18:56:36 +01:00
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;
2016-01-27 13:29:52 +01:00
assert_eq!(imported_ok, is_valid);
2016-01-26 18:02:49 +01:00
}
2016-01-25 23:24:51 +01:00
fail_unless(client.chain_info().best_block_hash == H256::from_json(&test["lastblockhash"]));
2016-01-25 18:56:36 +01:00
}
fs::remove_dir_all(&dir).unwrap();
}
if !fail {
flush(format!("ok\n"));
}
}
println!("!!! {:?} tests from failed.", failed.len());
failed
}
2016-01-26 15:39:49 +01:00
declare_test!{BlockchainTests_bcBlockGasLimitTest, "BlockchainTests/bcBlockGasLimitTest"}
2016-01-25 23:24:51 +01:00
declare_test!{BlockchainTests_bcForkBlockTest, "BlockchainTests/bcForkBlockTest"}
2016-01-27 16:05:23 +01:00
declare_test!{BlockchainTests_bcForkStressTest, "BlockchainTests/bcForkStressTest"}
2016-01-27 16:02:03 +01:00
declare_test!{BlockchainTests_bcForkUncle, "BlockchainTests/bcForkUncle"}
2016-01-26 00:26:36 +01:00
declare_test!{BlockchainTests_bcGasPricerTest, "BlockchainTests/bcGasPricerTest"}
2016-01-27 14:10:52 +01:00
declare_test!{BlockchainTests_bcInvalidHeaderTest, "BlockchainTests/bcInvalidHeaderTest"}
declare_test!{BlockchainTests_bcInvalidRLPTest, "BlockchainTests/bcInvalidRLPTest"}
declare_test!{BlockchainTests_bcMultiChainTest, "BlockchainTests/bcMultiChainTest"}
2016-01-26 00:26:36 +01:00
declare_test!{BlockchainTests_bcRPC_API_Test, "BlockchainTests/bcRPC_API_Test"}
2016-01-26 15:39:49 +01:00
declare_test!{BlockchainTests_bcStateTest, "BlockchainTests/bcStateTest"}
2016-01-27 15:01:59 +01:00
declare_test!{BlockchainTests_bcTotalDifficultyTest, "BlockchainTests/bcTotalDifficultyTest"}
declare_test!{BlockchainTests_bcUncleHeaderValiditiy, "BlockchainTests/bcUncleHeaderValiditiy"}
declare_test!{BlockchainTests_bcUncleTest, "BlockchainTests/bcUncleTest"}
2016-01-27 14:55:50 +01:00
declare_test!{BlockchainTests_bcValidBlockTest, "BlockchainTests/bcValidBlockTest"}
declare_test!{BlockchainTests_bcWalletTest, "BlockchainTests/bcWalletTest"}
2016-01-27 17:24:11 +01:00
declare_test!{BlockchainTests_RandomTests_bl10251623GO, "BlockchainTests/RandomTests/bl10251623GO"}
declare_test!{BlockchainTests_RandomTests_bl201507071825GO, "BlockchainTests/RandomTests/bl201507071825GO"}