final tests

This commit is contained in:
Nikolay Volf
2016-02-06 01:45:25 +03:00
parent 90e4722284
commit b01f954b05
3 changed files with 119 additions and 17 deletions

View File

@@ -122,14 +122,18 @@ fn status_packet() {
#[test]
fn propagade() {
let mut net = TestNet::new(2);
net.peer_mut(0).chain.add_blocks(100, false);
net.peer_mut(1).chain.add_blocks(100, false);
let mut net = TestNet::new(3);
net.peer_mut(1).chain.add_blocks(1000, false);
net.peer_mut(2).chain.add_blocks(1000, false);
net.sync();
let status = net.peer(0).sync.status();
assert_eq!(status.state, SyncState::Idle);
net.peer_mut(0).chain.add_blocks(10, false);
net.sync_step_peer(0);
assert_eq!(1, net.peer(0).queue.len());
// 2 peers to sync
assert_eq!(2, net.peer(0).queue.len());
// NEW_BLOCK_HASHES_PACKET
assert_eq!(0x01, net.peer(0).queue[0].packet_id);
}

View File

@@ -69,6 +69,12 @@ impl TestBlockChainClient {
self.import_block(rlp.as_raw().to_vec()).unwrap();
}
}
pub fn block_hash_delta_minus(&mut self, delta: usize) -> H256 {
let blocks_read = self.numbers.read().unwrap();
let index = blocks_read.len() - delta;
blocks_read[&index].clone()
}
}
impl BlockChainClient for TestBlockChainClient {
@@ -125,11 +131,33 @@ impl BlockChainClient for TestBlockChainClient {
}
}
fn tree_route(&self, _from: &H256, _to: &H256) -> Option<TreeRoute> {
// works only if blocks are one after another 1 -> 2 -> 3
fn tree_route(&self, from: &H256, to: &H256) -> Option<TreeRoute> {
Some(TreeRoute {
blocks: Vec::new(),
ancestor: H256::new(),
index: 0
index: 0,
blocks: {
let numbers_read = self.numbers.read().unwrap();
let mut adding = false;
let mut blocks = Vec::new();
for (_, hash) in numbers_read.iter().sort_by(|tuple1, tuple2| tuple1.0.cmp(tuple2.0)) {
if hash == to {
if adding {
blocks.push(hash.clone());
}
adding = false;
break;
}
if hash == from {
adding = true;
}
if adding {
blocks.push(hash.clone());
}
}
if adding { Vec::new() } else { blocks }
}
})
}