Skip to content

Commit

Permalink
chore(kad): expose a kad query facility allowing dynamic num_results
Browse files Browse the repository at this point in the history
  • Loading branch information
maqi committed Aug 22, 2024
1 parent d7beb55 commit 8c2bfec
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ libp2p-floodsub = { version = "0.45.0", path = "protocols/floodsub" }
libp2p-gossipsub = { version = "0.47.0", path = "protocols/gossipsub" }
libp2p-identify = { version = "0.45.0", path = "protocols/identify" }
libp2p-identity = { version = "0.2.9" }
libp2p-kad = { version = "0.46.1", path = "protocols/kad" }
libp2p-kad = { version = "0.47.0", path = "protocols/kad" }
libp2p-mdns = { version = "0.46.0", path = "protocols/mdns" }
libp2p-memory-connection-limits = { version = "0.3.0", path = "misc/memory-connection-limits" }
libp2p-metrics = { version = "0.15.0", path = "misc/metrics" }
Expand Down
5 changes: 5 additions & 0 deletions protocols/kad/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.47.0

- Expose a kad query facility allowing specify num_results dynamicly.
See [PR 5555](https://github.com/libp2p/rust-libp2p/pull/5555).

## 0.46.1

- Use new provider record update strategy to prevent Sybil attack.
Expand Down
2 changes: 1 addition & 1 deletion protocols/kad/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "libp2p-kad"
edition = "2021"
rust-version = { workspace = true }
description = "Kademlia protocol for libp2p"
version = "0.46.1"
version = "0.47.0"
authors = ["Parity Technologies <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
32 changes: 30 additions & 2 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,31 @@ where
/// The result of the query is delivered in a
/// [`Event::OutboundQueryProgressed{QueryResult::GetClosestPeers}`].
pub fn get_closest_peers<K>(&mut self, key: K) -> QueryId
where
K: Into<kbucket::Key<K>> + Into<Vec<u8>> + Clone,
{
self.get_closest_peers_inner(key, None)
}

/// Initiates an iterative query for the closest peers to the given key.
///
/// The result of the query is delivered in a
/// [`Event::OutboundQueryProgressed{QueryResult::GetClosestPeers}`].
///
/// The expected responding peers is specified by `num_results`
pub fn get_closest_peers_num_results<K>(&mut self, key: K, num_results: NonZeroUsize) -> QueryId
where
K: Into<kbucket::Key<K>> + Into<Vec<u8>> + Clone,
{
// The inner code never expect higher than K_VALUE results to be returned.
// And removing such cap will be tricky,
// since it would involve forging a new key and additional requests.
// Hence bound to K_VALUE here to set clear expectation and prevent unexpected behaviour.
let capped_num_results = std::cmp::min(num_results, K_VALUE);
self.get_closest_peers_inner(key, Some(capped_num_results))
}

fn get_closest_peers_inner<K>(&mut self, key: K, num_results: Option<NonZeroUsize>) -> QueryId
where
K: Into<kbucket::Key<K>> + Into<Vec<u8>> + Clone,
{
Expand All @@ -740,6 +765,7 @@ where
let info = QueryInfo::GetClosestPeers {
key,
step: ProgressStep::first(),
num_results,
};
let peer_keys: Vec<kbucket::Key<PeerId>> = self.kbuckets.closest_keys(&target).collect();
self.queries.add_iter_closest(target, peer_keys, info)
Expand Down Expand Up @@ -1485,7 +1511,7 @@ where
})
}

QueryInfo::GetClosestPeers { key, mut step } => {
QueryInfo::GetClosestPeers { key, mut step, .. } => {
step.last = true;

Some(Event::OutboundQueryProgressed {
Expand Down Expand Up @@ -1702,7 +1728,7 @@ where
},
}),

QueryInfo::GetClosestPeers { key, mut step } => {
QueryInfo::GetClosestPeers { key, mut step, .. } => {
step.last = true;
Some(Event::OutboundQueryProgressed {
id: query_id,
Expand Down Expand Up @@ -3175,6 +3201,8 @@ pub enum QueryInfo {
key: Vec<u8>,
/// Current index of events.
step: ProgressStep,
/// If required, `num_results` specifies expected responding peers
num_results: Option<NonZeroUsize>,
},

/// A (repeated) query initiated by [`Behaviour::get_providers`].
Expand Down
2 changes: 1 addition & 1 deletion protocols/kad/src/behaviour/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ fn query_iter() {

match swarms[0].behaviour_mut().query(&qid) {
Some(q) => match q.info() {
QueryInfo::GetClosestPeers { key, step } => {
QueryInfo::GetClosestPeers { key, step, .. } => {
assert_eq!(&key[..], search_target.to_bytes().as_slice());
assert_eq!(usize::from(step.count), 1);
}
Expand Down
10 changes: 9 additions & 1 deletion protocols/kad/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,16 @@ impl QueryPool {
T: Into<KeyBytes> + Clone,
I: IntoIterator<Item = Key<PeerId>>,
{
let num_results = match info {
QueryInfo::GetClosestPeers {
num_results: Some(val),
..
} => val,
_ => self.config.replication_factor,
};

let cfg = ClosestPeersIterConfig {
num_results: self.config.replication_factor,
num_results,
parallelism: self.config.parallelism,
..ClosestPeersIterConfig::default()
};
Expand Down

0 comments on commit 8c2bfec

Please sign in to comment.