Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(kad): add Behavior::find_closest_local_peers() #5645

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading