openethereum/src/tests/helpers.rs

87 lines
2.3 KiB
Rust
Raw Normal View History

2016-01-27 11:50:48 +01:00
use client::{BlockChainClient,Client};
use std::env;
use super::test_common::*;
use std::path::PathBuf;
use spec::*;
2016-01-27 14:21:54 +01:00
use std::fs::{remove_dir_all};
2016-01-27 17:32:53 +01:00
use blockchain:{BlockChain};
2016-01-27 11:50:48 +01:00
2016-01-27 13:41:41 +01:00
2016-01-27 14:21:54 +01:00
pub struct RandomTempPath {
path: PathBuf
}
2016-01-27 11:50:48 +01:00
2016-01-27 14:21:54 +01:00
impl RandomTempPath {
pub fn new() -> RandomTempPath {
let mut dir = env::temp_dir();
dir.push(H32::random().hex());
RandomTempPath {
path: dir.clone()
}
}
2016-01-27 11:50:48 +01:00
2016-01-27 14:21:54 +01:00
pub fn as_path(&self) -> &PathBuf {
&self.path
2016-01-27 11:50:48 +01:00
}
}
2016-01-27 14:21:54 +01:00
impl Drop for RandomTempPath {
fn drop(&mut self) {
if let Err(e) = remove_dir_all(self.as_path()) {
panic!("failed to remove temp directory, probably something failed to destroyed ({})", e);
}
}
2016-01-27 11:50:48 +01:00
}
pub fn get_test_spec() -> Spec {
Spec::new_test()
}
2016-01-27 13:23:24 +01:00
pub fn create_test_block(header: &Header) -> Bytes {
let mut rlp = RlpStream::new_list(3);
rlp.append(header);
rlp.append_raw(&rlp::EMPTY_LIST_RLP, 1);
rlp.append_raw(&rlp::EMPTY_LIST_RLP, 1);
rlp.out()
}
pub fn generate_dummy_client(block_number: usize) -> Arc<Client> {
2016-01-27 14:21:54 +01:00
let dir = RandomTempPath::new();
let client = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap();
2016-01-27 13:23:24 +01:00
let test_spec = get_test_spec();
let test_engine = test_spec.to_engine().unwrap();
let state_root = test_engine.spec().genesis_header().state_root;
2016-01-27 11:50:48 +01:00
let mut rolling_hash = test_engine.spec().genesis_header().hash();
let mut rolling_block_number = 1;
2016-01-27 13:23:24 +01:00
let mut rolling_timestamp = 40;
2016-01-27 11:50:48 +01:00
for _ in 0..block_number {
let mut header = Header::new();
header.gas_limit = decode(test_engine.spec().engine_params.get("minGasLimit").unwrap());
header.difficulty = decode(test_engine.spec().engine_params.get("minimumDifficulty").unwrap());
2016-01-27 13:23:24 +01:00
header.timestamp = rolling_timestamp;
2016-01-27 11:50:48 +01:00
header.number = rolling_block_number;
2016-01-27 13:23:24 +01:00
header.parent_hash = rolling_hash;
header.state_root = state_root.clone();
rolling_hash = header.hash();
rolling_block_number = rolling_block_number + 1;
rolling_timestamp = rolling_timestamp + 10;
if let Err(_) = client.import_block(create_test_block(&header)) {
panic!("error importing block which is valid by definition");
}
2016-01-27 11:50:48 +01:00
}
2016-01-27 13:23:24 +01:00
client.flush_queue();
client.import_verified_blocks(&IoChannel::disconnected());
client
2016-01-27 17:32:53 +01:00
}
pub fn generate_dummy_blockchain(block_number: usize) -> BlockChain {
let temp = RandomTempPath::new();
let bc = BlockChain::new(&("fffffff".from_hex().unwrap()), temp.as_path());
bc
2016-01-27 11:50:48 +01:00
}