mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-28 01:01:34 +00:00
Kademlia: Optimise iteration over closest keys / entries. (#1117)
* Kademlia: Optimise iteration over closest entries. The current implementation for finding the entries whose keys are closest to some target key in the Kademlia routing table involves copying the keys of all buckets into a new `Vec` which is then sorted based on the distances to the target and turned into an iterator from which only a small number of elements (by default 20) are drawn. This commit introduces an iterator over buckets for finding the closest keys to a target that visits the buckets in the optimal order, based on the information contained in the distance bit-string representing the distance between the local key and the target. Correctness is tested against full-table scans. Also included: * Updated documentation. * The `Entry` API was moved to the `kbucket::entry` sub-module for ease of maintenance. * The pending node handling has been slightly refactored in order to bring code and documentation in agreement and clarify the semantics a little. * Rewrite pending node handling and add tests.
This commit is contained in:
@ -97,7 +97,7 @@ fn query_iter() {
|
||||
|
||||
// Connect each swarm in the list to its predecessor in the list.
|
||||
for (i, (swarm, peer)) in &mut swarms.iter_mut().skip(1).zip(swarm_ids.clone()).enumerate() {
|
||||
swarm.add_not_connected_address(&peer, Protocol::Memory(port_base + i as u64).into())
|
||||
swarm.add_address(&peer, Protocol::Memory(port_base + i as u64).into())
|
||||
}
|
||||
|
||||
// Ask the last peer in the list to search a random peer. The search should
|
||||
@ -150,7 +150,7 @@ fn unresponsive_not_returned_direct() {
|
||||
|
||||
// Add fake addresses.
|
||||
for _ in 0 .. 10 {
|
||||
swarms[0].add_not_connected_address(&PeerId::random(), Protocol::Udp(10u16).into());
|
||||
swarms[0].add_address(&PeerId::random(), Protocol::Udp(10u16).into());
|
||||
}
|
||||
|
||||
// Ask first to search a random value.
|
||||
@ -189,14 +189,14 @@ fn unresponsive_not_returned_indirect() {
|
||||
// Add fake addresses to first.
|
||||
let first_peer_id = Swarm::local_peer_id(&swarms[0]).clone();
|
||||
for _ in 0 .. 10 {
|
||||
swarms[0].add_not_connected_address(
|
||||
swarms[0].add_address(
|
||||
&PeerId::random(),
|
||||
multiaddr![Udp(10u16)]
|
||||
);
|
||||
}
|
||||
|
||||
// Connect second to first.
|
||||
swarms[1].add_not_connected_address(&first_peer_id, Protocol::Memory(port_base).into());
|
||||
swarms[1].add_address(&first_peer_id, Protocol::Memory(port_base).into());
|
||||
|
||||
// Ask second to search a random value.
|
||||
let search_target = PeerId::random();
|
||||
|
Reference in New Issue
Block a user