From f141e696711e7409955cf5cde216bd3b0d37212f Mon Sep 17 00:00:00 2001 From: arkpar Date: Mon, 15 Feb 2016 13:34:58 +0100 Subject: [PATCH] Added test for restart on malformed block --- sync/src/tests/chain.rs | 11 +++++++++++ sync/src/tests/helpers.rs | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/sync/src/tests/chain.rs b/sync/src/tests/chain.rs index 5ee5df831..1dd9a1e78 100644 --- a/sync/src/tests/chain.rs +++ b/sync/src/tests/chain.rs @@ -160,3 +160,14 @@ fn propagade_blocks() { // NEW_BLOCK_PACKET assert_eq!(0x07, net.peer(0).queue[0].packet_id); } + +#[test] +fn restart_on_malformed_block() { + let mut net = TestNet::new(2); + net.peer_mut(1).chain.add_blocks(10, false); + net.peer_mut(1).chain.corrupt_block(6); + net.sync_steps(10); + + assert_eq!(net.peer(0).chain.chain_info().best_block_number, 4); +} + diff --git a/sync/src/tests/helpers.rs b/sync/src/tests/helpers.rs index d8cd5e54a..c561b65a3 100644 --- a/sync/src/tests/helpers.rs +++ b/sync/src/tests/helpers.rs @@ -71,6 +71,17 @@ impl TestBlockChainClient { } } + pub fn corrupt_block(&mut self, n: BlockNumber) { + let hash = self.block_hash(BlockId::Number(n)).unwrap(); + let mut header: BlockHeader = decode(&self.block_header(BlockId::Number(n)).unwrap()); + header.parent_hash = H256::new(); + let mut rlp = RlpStream::new_list(3); + rlp.append(&header); + rlp.append_raw(&rlp::NULL_RLP, 1); + rlp.append_raw(&rlp::NULL_RLP, 1); + self.blocks.write().unwrap().insert(hash, rlp.out()); + } + pub fn block_hash_delta_minus(&mut self, delta: usize) -> H256 { let blocks_read = self.numbers.read().unwrap(); let index = blocks_read.len() - delta;