Added test for restart on malformed block

This commit is contained in:
arkpar 2016-02-15 13:34:58 +01:00
parent b503fb5de8
commit f141e69671
2 changed files with 22 additions and 0 deletions

View File

@ -160,3 +160,14 @@ fn propagade_blocks() {
// NEW_BLOCK_PACKET // NEW_BLOCK_PACKET
assert_eq!(0x07, net.peer(0).queue[0].packet_id); 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);
}

View File

@ -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 { pub fn block_hash_delta_minus(&mut self, delta: usize) -> H256 {
let blocks_read = self.numbers.read().unwrap(); let blocks_read = self.numbers.read().unwrap();
let index = blocks_read.len() - delta; let index = blocks_read.len() - delta;