fixed tests

This commit is contained in:
Svyatoslav Nikolsky 2016-10-31 14:46:11 +03:00
parent 4e3ea35835
commit b5d0644408
4 changed files with 37 additions and 38 deletions

View File

@ -282,27 +282,26 @@ impl Index<usize> for HashQueueChain {
#[cfg(test)]
mod tests {
use primitives::hash::H256;
use super::{HashQueue, HashQueueChain, HashPosition};
#[test]
fn hash_queue_empty() {
let queue = HashQueue::new();
assert_eq!(queue.len(), 1);
let mut queue = HashQueue::new();
assert_eq!(queue.len(), 0);
assert_eq!(queue.is_empty(), true);
assert_eq!(queue.front(), None);
assert_eq!(queue.back(), None);
assert_eq!(queue.pre_back(), None);
assert_eq!(queue.back_skip_n(), None);
assert_eq!(queue.contains("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f".into()), false);
assert_eq!(queue.back_skip_n(100), None);
assert_eq!(queue.contains(&"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f".into()), false);
assert_eq!(queue.pop_front(), None);
assert_eq!(queue.pop_front_n(100), vec![]);
assert_eq!(queue.remove("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f".into()), HashPosition::Missing);
assert_eq!(queue.remove(&"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f".into()), HashPosition::Missing);
}
#[test]
fn hash_queue_chain_empty() {
let chain = HashQueueChain::with_number_of_queues(3);
let mut chain = HashQueueChain::with_number_of_queues(3);
assert_eq!(chain.len(), 0);
assert_eq!(chain.len_of(0), 0);
assert_eq!(chain.is_empty_at(0), true);

View File

@ -377,9 +377,11 @@ impl<T> Synchronization<T> where T: TaskExecutor + Send + 'static {
/// Schedule new synchronization tasks, if any.
fn execute_synchronization_tasks(&mut self) {
let mut tasks: Vec<Task> = Vec::new();
let idle_peers = self.peers.idle_peers();
let idle_peers_len = idle_peers.len() as u64;
// prepare synchronization tasks
{
if idle_peers_len != 0 {
// display information if processed many blocks || enough time has passed since sync start
let mut chain = self.chain.write();
if let State::Synchronizing(timestamp, num_of_blocks) = self.state {
@ -401,16 +403,12 @@ impl<T> Synchronization<T> where T: TaskExecutor + Send + 'static {
let scheduled_hashes_len = chain.length_of_state(BlockState::Scheduled);
if scheduled_hashes_len < MAX_SCHEDULED_HASHES {
if self.state.is_synchronizing() {
if let Some(idle_peer) = self.peers.idle_peer() {
tasks.push(Task::RequestBestInventory(idle_peer));
self.peers.on_inventory_requested(idle_peer);
}
tasks.push(Task::RequestBestInventory(idle_peers[0]));
self.peers.on_inventory_requested(idle_peers[0]);
}
else {
if let Some(idle_peer) = self.peers.idle_peer() {
tasks.push(Task::RequestInventory(idle_peer));
self.peers.on_inventory_requested(idle_peer);
}
tasks.push(Task::RequestInventory(idle_peers[0]));
self.peers.on_inventory_requested(idle_peers[0]);
}
}
@ -418,20 +416,16 @@ impl<T> Synchronization<T> where T: TaskExecutor + Send + 'static {
let requested_hashes_len = chain.length_of_state(BlockState::Requested);
let verifying_hashes_len = chain.length_of_state(BlockState::Verifying);
if requested_hashes_len + verifying_hashes_len < MAX_REQUESTED_BLOCKS + MAX_VERIFYING_BLOCKS && scheduled_hashes_len != 0 {
let idle_peers = self.peers.idle_peers();
let idle_peers_len = idle_peers.len() as u64;
if idle_peers_len != 0 {
let chunk_size = min(MAX_BLOCKS_IN_REQUEST, max(scheduled_hashes_len / idle_peers_len, MIN_BLOCKS_IN_REQUEST));
for idle_peer in idle_peers {
let peer_chunk_size = min(chain.length_of_state(BlockState::Scheduled), chunk_size);
if peer_chunk_size == 0 {
break;
}
let requested_hashes = chain.request_blocks_hashes(peer_chunk_size);
self.peers.on_blocks_requested(idle_peer, &requested_hashes);
tasks.push(Task::RequestBlocks(idle_peer, requested_hashes));
let chunk_size = min(MAX_BLOCKS_IN_REQUEST, max(scheduled_hashes_len / idle_peers_len, MIN_BLOCKS_IN_REQUEST));
for idle_peer in idle_peers {
let peer_chunk_size = min(chain.length_of_state(BlockState::Scheduled), chunk_size);
if peer_chunk_size == 0 {
break;
}
let requested_hashes = chain.request_blocks_hashes(peer_chunk_size);
self.peers.on_blocks_requested(idle_peer, &requested_hashes);
tasks.push(Task::RequestBlocks(idle_peer, requested_hashes));
}
}
}
@ -545,7 +539,7 @@ mod tests {
let block1: Block = "010000006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000982051fd1e4ba744bbbe680e1fee14677ba1a3c3540bf7b1cdb606e857233e0e61bc6649ffff001d01e362990101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d0104ffffffff0100f2052a0100000043410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac00000000".into();
let block2: Block = "010000004860eb18bf1b1620e37e9490fc8a427514416fd75159ab86688e9a8300000000d5fdcc541e25de1c7a5addedf24858b8bb665c9f36ef744ee42c316022c90f9bb0bc6649ffff001d08d2bd610101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d010bffffffff0100f2052a010000004341047211a824f55b505228e4c3d5194c1fcfaa15a456abdf37f9b9d97a4040afc073dee6c89064984f03385237d92167c13e236446b417ab79a0fcae412ae3316b77ac00000000".into();
sync.on_unknown_blocks(5, vec![block1.hash()]);
sync.on_new_blocks_inventory(5, vec![block1.hash()]);
let tasks = executor.lock().take_tasks();
assert_eq!(tasks.len(), 2);
assert_eq!(tasks[0], Task::RequestBestInventory(5));
@ -575,7 +569,8 @@ mod tests {
assert_eq!(sync.information().chain.scheduled, 0);
assert_eq!(sync.information().chain.requested, 0);
assert_eq!(sync.information().chain.stored, 2);
assert_eq!(sync.information().peers.idle, 1);
// we have just requested new `inventory` from the peer => peer is forgotten
assert_eq!(sync.information().peers.idle, 0);
assert_eq!(sync.information().peers.active, 0);
}
@ -586,7 +581,7 @@ mod tests {
let block2: Block = "010000004860eb18bf1b1620e37e9490fc8a427514416fd75159ab86688e9a8300000000d5fdcc541e25de1c7a5addedf24858b8bb665c9f36ef744ee42c316022c90f9bb0bc6649ffff001d08d2bd610101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d010bffffffff0100f2052a010000004341047211a824f55b505228e4c3d5194c1fcfaa15a456abdf37f9b9d97a4040afc073dee6c89064984f03385237d92167c13e236446b417ab79a0fcae412ae3316b77ac00000000".into();
sync.on_unknown_blocks(5, vec![block2.hash()]);
sync.on_new_blocks_inventory(5, vec![block2.hash()]);
sync.on_peer_block(5, block2);
// out-of-order block was presented by the peer
@ -595,7 +590,8 @@ mod tests {
assert_eq!(sync.information().chain.scheduled, 0);
assert_eq!(sync.information().chain.requested, 0);
assert_eq!(sync.information().chain.stored, 1);
assert_eq!(sync.information().peers.idle, 1);
// we have just requested new `inventory` from the peer => peer is forgotten
assert_eq!(sync.information().peers.idle, 0);
assert_eq!(sync.information().peers.active, 0);
// TODO: check that peer is penalized
}

View File

@ -419,7 +419,7 @@ mod tests {
]);
chain.request_blocks_hashes(10);
assert_eq!(chain.best_block_locator_hashes()[0], "0000000000000000000000000000000000000000000000000000000000000016".into());
assert_eq!(chain.best_block_locator_hashes()[0], "0000000000000000000000000000000000000000000000000000000000000014".into());
assert_eq!(chain.block_locator_hashes(), vec![
"0000000000000000000000000000000000000000000000000000000000000016".into(),
"0000000000000000000000000000000000000000000000000000000000000015".into(),
@ -442,7 +442,7 @@ mod tests {
"0000000000000000000000000000000000000000000000000000000000000022".into(),
]);
assert_eq!(chain.best_block_locator_hashes()[0], "0000000000000000000000000000000000000000000000000000000000000022".into());
assert_eq!(chain.best_block_locator_hashes()[0], "0000000000000000000000000000000000000000000000000000000000000020".into());
assert_eq!(chain.block_locator_hashes(), vec![
"0000000000000000000000000000000000000000000000000000000000000022".into(),
"0000000000000000000000000000000000000000000000000000000000000021".into(),

View File

@ -41,6 +41,7 @@ impl Peers {
}
/// Get idle peer.
#[cfg(test)]
pub fn idle_peer(&self) -> Option<usize> {
self.idle_peers.iter().cloned().next()
}
@ -77,15 +78,18 @@ impl Peers {
/// Blocks have been requested from peer.
pub fn on_blocks_requested(&mut self, peer_index: usize, blocks_hashes: &Vec<H256>) {
// blocks can only be requested from idle peers
assert_eq!(self.idle_peers.remove(&peer_index), true);
// inventory can only be requested from idle peers
assert!(!self.blocks_requests.contains_key(&peer_index));
self.idle_peers.remove(&peer_index);
self.blocks_requests.entry(peer_index).or_insert(HashSet::new()).extend(blocks_hashes.iter().cloned());
}
/// Inventory has been requested from peer.
pub fn on_inventory_requested(&mut self, peer_index: usize) {
// inventory can only be requested from idle peers
assert_eq!(self.idle_peers.remove(&peer_index), true);
assert!(!self.blocks_requests.contains_key(&peer_index));
self.idle_peers.remove(&peer_index);
// peer is now out-of-synchronization process, because:
// 1) if it has new blocks, it will respond with `inventory` message && will be insrted back here