Skip to content

Commit

Permalink
feat(kad): add Behavior::find_closest_local_peers() (#5645)
Browse files Browse the repository at this point in the history
## Description

Fixes #5626

## Notes & open questions

This is the nicest way I came up with, I decided to leave
`get_closest_local_peers` as is since it does return all peers, not just
`replication_factor` peers.

Looking at #2436 it is not
clear if @folex really needed all peers returned or it just happened
that way. I'm also happy to change proposed API to return all peers if
that is preferred by others.

It is very unfortunate that `&mut self` is needed for this, I created
#5644 that if resolved will
allow to have `&self` instead.

## Change checklist

- [x] I have performed a self-review of my own code
- [x] I have made corresponding changes to the documentation
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [x] A changelog entry has been made in the appropriate crates

Co-authored-by: João Oliveira <[email protected]>
  • Loading branch information
nazar-pc and jxs authored Oct 24, 2024
1 parent 6cb116e commit 84c617f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
5 changes: 3 additions & 2 deletions protocols/kad/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
## 0.47.0

- Expose a kad query facility allowing specify num_results dynamicly.
- Expose a kad query facility allowing specify num_results dynamicaly.
See [PR 5555](https://github.com/libp2p/rust-libp2p/pull/5555).
- Add `mode` getter on `Behaviour`.
See [PR 5573](https://github.com/libp2p/rust-libp2p/pull/5573).

- Add `Behavior::find_closest_local_peers()`.
See [PR 5645](https://github.com/libp2p/rust-libp2p/pull/5645).

## 0.46.2

Expand Down
48 changes: 28 additions & 20 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,14 +771,32 @@ where
self.queries.add_iter_closest(target, peer_keys, info)
}

/// Returns closest peers to the given key; takes peers from local routing table only.
/// Returns all peers ordered by distance to the given key; takes peers from local routing table
/// only.
pub fn get_closest_local_peers<'a, K: Clone>(
&'a mut self,
key: &'a kbucket::Key<K>,
) -> impl Iterator<Item = kbucket::Key<PeerId>> + 'a {
self.kbuckets.closest_keys(key)
}

/// Finds the closest peers to a `key` in the context of a request by the `source` peer, such
/// that the `source` peer is never included in the result.
///
/// Takes peers from local routing table only. Only returns number of peers equal to configured
/// replication factor.
pub fn find_closest_local_peers<'a, K: Clone>(
&'a mut self,
key: &'a kbucket::Key<K>,
source: &'a PeerId,
) -> impl Iterator<Item = KadPeer> + 'a {
self.kbuckets
.closest(key)
.filter(move |e| e.node.key.preimage() != source)
.take(self.queries.config().replication_factor.get())
.map(KadPeer::from)
}

/// Performs a lookup for a record in the DHT.
///
/// The result of this operation is delivered in a
Expand Down Expand Up @@ -1212,22 +1230,6 @@ where
}
}

/// Finds the closest peers to a `target` in the context of a request by
/// the `source` peer, such that the `source` peer is never included in the
/// result.
fn find_closest<T: Clone>(
&mut self,
target: &kbucket::Key<T>,
source: &PeerId,
) -> Vec<KadPeer> {
self.kbuckets
.closest(target)
.filter(|e| e.node.key.preimage() != source)
.take(self.queries.config().replication_factor.get())
.map(KadPeer::from)
.collect()
}

/// Collects all peers who are known to be providers of the value for a given `Multihash`.
fn provider_peers(&mut self, key: &record::Key, source: &PeerId) -> Vec<KadPeer> {
let kbuckets = &mut self.kbuckets;
Expand Down Expand Up @@ -2300,7 +2302,9 @@ where
}

HandlerEvent::FindNodeReq { key, request_id } => {
let closer_peers = self.find_closest(&kbucket::Key::new(key), &source);
let closer_peers = self
.find_closest_local_peers(&kbucket::Key::new(key), &source)
.collect::<Vec<_>>();

self.queued_events
.push_back(ToSwarm::GenerateEvent(Event::InboundRequest {
Expand Down Expand Up @@ -2328,7 +2332,9 @@ where

HandlerEvent::GetProvidersReq { key, request_id } => {
let provider_peers = self.provider_peers(&key, &source);
let closer_peers = self.find_closest(&kbucket::Key::new(key), &source);
let closer_peers = self
.find_closest_local_peers(&kbucket::Key::new(key), &source)
.collect::<Vec<_>>();

self.queued_events
.push_back(ToSwarm::GenerateEvent(Event::InboundRequest {
Expand Down Expand Up @@ -2422,7 +2428,9 @@ where
None => None,
};

let closer_peers = self.find_closest(&kbucket::Key::new(key), &source);
let closer_peers = self
.find_closest_local_peers(&kbucket::Key::new(key), &source)
.collect::<Vec<_>>();

self.queued_events
.push_back(ToSwarm::GenerateEvent(Event::InboundRequest {
Expand Down
2 changes: 1 addition & 1 deletion protocols/kad/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub use behaviour::{
pub use kbucket::{
Distance as KBucketDistance, EntryView, KBucketRef, Key as KBucketKey, NodeStatus,
};
pub use protocol::ConnectionType;
pub use protocol::{ConnectionType, KadPeer};
pub use query::QueryId;
pub use record::{store, Key as RecordKey, ProviderRecord, Record};

Expand Down

0 comments on commit 84c617f

Please sign in to comment.