basic client tests working

This commit is contained in:
Nikolay Volf 2016-01-26 17:33:22 +04:00
parent 46e0a81c52
commit 81bffd48e8
3 changed files with 80 additions and 16 deletions

View File

@ -85,7 +85,7 @@ impl IsBlock for Block {
///
/// It's a bit like a Vec<Transaction>, eccept that whenever a transaction is pushed, we execute it and
/// maintain the system `state()`. We also archive execution receipts in preparation for later block creation.
pub struct OpenBlock<'x, 'y> {
pub struct OpenBlock<'x, 'y> {
block: Block,
engine: &'x Engine,
last_hashes: &'y LastHashes,

View File

@ -191,7 +191,7 @@ impl BlockQueue {
/// Wait for queue to be empty
pub fn flush(&mut self) {
let mut verification = self.verification.lock().unwrap();
while !verification.unverified.is_empty() && !verification.verifying.is_empty() {
while !verification.unverified.is_empty() || !verification.verifying.is_empty() {
verification = self.empty.wait(verification).unwrap();
}
}

View File

@ -1,21 +1,85 @@
use ethereum;
use client::{BlockChainClient,Client};
use client::{BlockChainClient,Client};
use std::env;
use pod_state::*;
use super::test_common::*;
use std::path::PathBuf;
use spec::*;
#[cfg(test)]
fn get_random_temp_dir() -> PathBuf {
let mut dir = env::temp_dir();
dir.push(H32::random().hex());
dir
}
#[cfg(test)]
fn get_test_spec() -> Spec {
Spec::new_test()
}
#[cfg(test)]
fn get_good_dummy_block() -> Bytes {
let mut block_header = Header::new();
let test_spec = get_test_spec();
let test_engine = test_spec.to_engine().unwrap();
block_header.gas_limit = decode(test_engine.spec().engine_params.get("minGasLimit").unwrap());
block_header.difficulty = decode(test_engine.spec().engine_params.get("minimumDifficulty").unwrap());
block_header.timestamp = 40;
block_header.number = 1;
block_header.parent_hash = test_engine.spec().genesis_header().hash();
block_header.state_root = test_engine.spec().genesis_header().state_root;
create_test_block(&block_header)
}
#[cfg(test)]
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()
}
#[test]
fn test_client_is_created() {
let mut spec = ethereum::new_frontier_like_test();
spec.set_genesis_state(PodState::from_json(test.find("pre").unwrap()));
spec.overwrite_genesis(test.find("genesisBlockHeader").unwrap());
let mut dir = env::temp_dir();
dir.push(H32::random().hex());
let client_result = Client::new(spec, &dir, IOChannel::disconnected());
fn created() {
let client_result = Client::new(get_test_spec(), &get_random_temp_dir(), IoChannel::disconnected());
assert!(client_result.is_ok());
}
#[test]
fn imports_from_empty() {
let client = Client::new(get_test_spec(), &get_random_temp_dir(), IoChannel::disconnected()).unwrap();
client.import_verified_blocks(&IoChannel::disconnected());
client.flush_queue();
}
#[test]
fn imports_good_block() {
::env_logger::init().ok();
let client = Client::new(get_test_spec(), &get_random_temp_dir(), IoChannel::disconnected()).unwrap();
let good_block = get_good_dummy_block();
if let Err(_) = client.import_block(good_block) {
panic!("error importing block being good by definition");
}
client.flush_queue();
client.import_verified_blocks(&IoChannel::disconnected());
let block = client.block_header_at(1).unwrap();
assert!(!block.is_empty());
}
#[test]
fn query_none_block() {
let client = Client::new(get_test_spec(), &get_random_temp_dir(), IoChannel::disconnected()).unwrap();
let non_existant = client.block_header_at(188);
assert!(non_existant == Option::None);
}