diff --git a/src/blockchain.rs b/src/blockchain.rs index 818447df1..2610407ac 100644 --- a/src/blockchain.rs +++ b/src/blockchain.rs @@ -766,13 +766,16 @@ mod tests { #[test] fn can_contain_arbitrary_block_sequence() { - let bc = generate_dummy_blockchain(50); + let bc_result = generate_dummy_blockchain(50); + let bc = bc_result.reference(); assert_eq!(bc.best_block_number(), 49); } #[test] fn can_collect_garbage() { - let bc = generate_dummy_blockchain(3000); + let bc_result = generate_dummy_blockchain(3000); + let bc = bc_result.reference(); + assert_eq!(bc.best_block_number(), 2999); let best_hash = bc.best_block_hash(); let mut block_header = bc.block_header(&best_hash); @@ -786,4 +789,18 @@ mod tests { assert!(bc.cache_size().blocks < 1024 * 1024); } + + #[test] + fn can_contain_arbitrary_block_sequence_with_extra() { + let bc_result = generate_dummy_blockchain_with_extra(25); + let bc = bc_result.reference(); + assert_eq!(bc.best_block_number(), 24); + } + + #[test] + fn can_contain_only_genesis_block() { + let bc_result = generate_dummy_empty_blockchain(); + let bc = bc_result.reference(); + assert_eq!(bc.best_block_number(), 0); + } } diff --git a/src/tests/client.rs b/src/tests/client.rs index f6d603f43..2abeceb0b 100644 --- a/src/tests/client.rs +++ b/src/tests/client.rs @@ -30,21 +30,6 @@ fn get_bad_state_dummy_block() -> Bytes { create_test_block(&block_header) } - -fn get_test_client_with_blocks(blocks: Vec) -> Arc { - let dir = RandomTempPath::new(); - let client = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap(); - for block in &blocks { - if let Err(_) = client.import_block(block.clone()) { - panic!("panic importing block which is well-formed"); - } - } - client.flush_queue(); - client.import_verified_blocks(&IoChannel::disconnected()); - client -} - - #[test] fn created() { let dir = RandomTempPath::new(); @@ -86,7 +71,8 @@ fn query_none_block() { #[test] fn query_bad_block() { - let client = get_test_client_with_blocks(vec![get_bad_state_dummy_block()]); + let client_result = get_test_client_with_blocks(vec![get_bad_state_dummy_block()]); + let client = client_result.reference(); let bad_block:Option = client.block_header_at(1); assert!(bad_block.is_none()); @@ -95,7 +81,8 @@ fn query_bad_block() { #[test] fn returns_chain_info() { let dummy_block = get_good_dummy_block(); - let client = get_test_client_with_blocks(vec![dummy_block.clone()]); + let client_result = get_test_client_with_blocks(vec![dummy_block.clone()]); + let client = client_result.reference(); let block = BlockView::new(&dummy_block); let info = client.chain_info(); assert_eq!(info.best_block_hash, block.header().hash()); @@ -103,7 +90,8 @@ fn returns_chain_info() { #[test] fn imports_block_sequence() { - let client = generate_dummy_client(6); + let client_result = generate_dummy_client(6); + let client = client_result.reference(); let block = client.block_header_at(5).unwrap(); assert!(!block.is_empty()); @@ -111,7 +99,8 @@ fn imports_block_sequence() { #[test] fn can_collect_garbage() { - let client = generate_dummy_client(100); + let client_result = generate_dummy_client(100); + let client = client_result.reference(); client.tick(); assert!(client.cache_info().blocks < 100 * 1024); } \ No newline at end of file diff --git a/src/tests/helpers.rs b/src/tests/helpers.rs index 703c8639b..0fb8901fe 100644 --- a/src/tests/helpers.rs +++ b/src/tests/helpers.rs @@ -33,6 +33,18 @@ impl Drop for RandomTempPath { } } +#[allow(dead_code)] +pub struct GuardedTempResult { + result: T, + temp: RandomTempPath +} + +impl GuardedTempResult { + pub fn reference(&self) -> &T { + &self.result + } +} + pub fn get_test_spec() -> Spec { Spec::new_test() } @@ -45,7 +57,7 @@ pub fn create_test_block(header: &Header) -> Bytes { rlp.out() } -fn create_unverifiable_block_header(order: usize, parent_hash: H256) -> Header { +fn create_unverifiable_block_header(order: u32, parent_hash: H256) -> Header { let mut header = Header::new(); header.gas_limit = x!(0); header.difficulty = x!(order * 100); @@ -57,24 +69,24 @@ fn create_unverifiable_block_header(order: usize, parent_hash: H256) -> Header { header } -fn create_unverifiable_block(order: usize, parent_hash: H256) -> Bytes { - create_test_block_header(&create_unverifiable_block_header(order, parent_hash)) +fn create_unverifiable_block(order: u32, parent_hash: H256) -> Bytes { + create_test_block(&create_unverifiable_block_header(order, parent_hash)) } -fn create_unverifiable_block_with_extra(order: usize, parent_hash: H256, Option extra) -> Bytes { - let mut header = create_test_block_header(order, parent_hash); +fn create_unverifiable_block_with_extra(order: u32, parent_hash: H256, extra: Option) -> Bytes { + let mut header = create_unverifiable_block_header(order, parent_hash); header.extra_data = match extra { Some(extra_data) => extra_data, None => { - let base = order as u8; - vec!([1, 2, 3]) + let base = (order & 0x000000ff) as u8; + let generated: Vec = vec![base + 1, base + 2, base + 3]; + generated } - } - + }; create_test_block(&header) } -pub fn generate_dummy_client(block_number: usize) -> Arc { +pub fn generate_dummy_client(block_number: u32) -> GuardedTempResult> { let dir = RandomTempPath::new(); let client = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap(); @@ -106,10 +118,32 @@ pub fn generate_dummy_client(block_number: usize) -> Arc { } client.flush_queue(); client.import_verified_blocks(&IoChannel::disconnected()); - client + + GuardedTempResult::> { + temp: dir, + result: client + } } -pub fn generate_dummy_blockchain(block_number: usize) -> BlockChain { + +pub fn get_test_client_with_blocks(blocks: Vec) -> GuardedTempResult> { + let dir = RandomTempPath::new(); + let client = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap(); + for block in &blocks { + if let Err(_) = client.import_block(block.clone()) { + panic!("panic importing block which is well-formed"); + } + } + client.flush_queue(); + client.import_verified_blocks(&IoChannel::disconnected()); + + GuardedTempResult::> { + temp: dir, + result: client + } +} + +pub fn generate_dummy_blockchain(block_number: u32) -> GuardedTempResult { let temp = RandomTempPath::new(); let bc = BlockChain::new( &create_unverifiable_block( @@ -120,7 +154,41 @@ pub fn generate_dummy_blockchain(block_number: usize) -> BlockChain { for block_order in 1..block_number { bc.insert_block(&create_unverifiable_block(block_order, bc.best_block_hash())); } - bc + + GuardedTempResult:: { + temp: temp, + result: bc + } } -pub fn generate_dummy_blockchain_with_extra(block_number: uszie \ No newline at end of file +pub fn generate_dummy_blockchain_with_extra(block_number: u32) -> GuardedTempResult { + let temp = RandomTempPath::new(); + let bc = BlockChain::new( + &create_unverifiable_block( + 0, + H256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap()), + temp.as_path()); + + for block_order in 1..block_number { + bc.insert_block(&create_unverifiable_block_with_extra(block_order, bc.best_block_hash(), None)); + } + + GuardedTempResult:: { + temp: temp, + result: bc + } +} + +pub fn generate_dummy_empty_blockchain() -> GuardedTempResult { + let temp = RandomTempPath::new(); + let bc = BlockChain::new( + &create_unverifiable_block( + 0, + H256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap()), + temp.as_path()); + + GuardedTempResult:: { + temp: temp, + result: bc + } +} \ No newline at end of file