Fixed neighbours collection (#1996)

This commit is contained in:
Arkadiy Paronyan 2016-08-24 17:00:14 +02:00 committed by Gav Wood
parent 190e4db266
commit f07a1e6baf

View File

@ -59,7 +59,7 @@ pub struct BucketEntry {
pub timeout: Option<u64>, pub timeout: Option<u64>,
} }
struct NodeBucket { pub struct NodeBucket {
nodes: VecDeque<BucketEntry>, //sorted by last active nodes: VecDeque<BucketEntry>, //sorted by last active
} }
@ -281,12 +281,12 @@ impl Discovery {
if count == BUCKET_SIZE { if count == BUCKET_SIZE {
// delete the most distant element // delete the most distant element
let remove = { let remove = {
let (_, last) = found.iter_mut().next_back().unwrap(); let (key, last) = found.iter_mut().next_back().unwrap();
last.pop(); last.pop();
last.is_empty() if last.is_empty() { Some(key.clone()) } else { None }
}; };
if remove { if let Some(remove) = remove {
found.remove(&distance); found.remove(&remove);
} }
} }
else { else {
@ -605,6 +605,21 @@ mod tests {
assert!(removed > 0); assert!(removed > 0);
} }
#[test]
fn find_nearest_saturated() {
use super::*;
let mut buckets: Vec<_> = (0..256).map(|_| NodeBucket::new()).collect();
let ep = NodeEndpoint { address: SocketAddr::from_str("127.0.0.1:40447").unwrap(), udp_port: 40447 };
for _ in 0..(16 + 10) {
buckets[0].nodes.push_back(BucketEntry {
address: NodeEntry { id: NodeId::new(), endpoint: ep.clone() },
timeout: None
});
}
let nearest = Discovery::nearest_node_entries(&NodeId::new(), &buckets);
assert_eq!(nearest.len(), 16)
}
#[test] #[test]
fn packets() { fn packets() {
let key = KeyPair::create().unwrap(); let key = KeyPair::create().unwrap();