Skip to content

Commit c0a8cc0

Browse files
committed
chore(kad): expose a kad query facility allowing dynamic num_results
1 parent d9ee266 commit c0a8cc0

7 files changed

Lines changed: 45 additions & 7 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ libp2p-floodsub = { version = "0.45.0", path = "protocols/floodsub" }
8686
libp2p-gossipsub = { version = "0.47.0", path = "protocols/gossipsub" }
8787
libp2p-identify = { version = "0.45.0", path = "protocols/identify" }
8888
libp2p-identity = { version = "0.2.9" }
89-
libp2p-kad = { version = "0.46.1", path = "protocols/kad" }
89+
libp2p-kad = { version = "0.46.2", path = "protocols/kad" }
9090
libp2p-mdns = { version = "0.46.0", path = "protocols/mdns" }
9191
libp2p-memory-connection-limits = { version = "0.3.0", path = "misc/memory-connection-limits" }
9292
libp2p-metrics = { version = "0.14.2", path = "misc/metrics" }

protocols/kad/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.46.2
2+
3+
- Expose a kad query facility allowing specify num_results dynamicly.
4+
See [PR 5555](https://github.com/libp2p/rust-libp2p/pull/5555).
5+
16
## 0.46.1
27

38
- Use new provider record update strategy to prevent Sybil attack.

protocols/kad/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "libp2p-kad"
33
edition = "2021"
44
rust-version = { workspace = true }
55
description = "Kademlia protocol for libp2p"
6-
version = "0.46.1"
6+
version = "0.46.2"
77
authors = ["Parity Technologies <[email protected]>"]
88
license = "MIT"
99
repository = "https://github.com/libp2p/rust-libp2p"

protocols/kad/src/behaviour.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,26 @@ where
732732
/// The result of the query is delivered in a
733733
/// [`Event::OutboundQueryProgressed{QueryResult::GetClosestPeers}`].
734734
pub fn get_closest_peers<K>(&mut self, key: K) -> QueryId
735+
where
736+
K: Into<kbucket::Key<K>> + Into<Vec<u8>> + Clone,
737+
{
738+
self.get_closest_peers_inner(key, None)
739+
}
740+
741+
/// Initiates an iterative query for the closest peers to the given key.
742+
///
743+
/// The result of the query is delivered in a
744+
/// [`Event::OutboundQueryProgressed{QueryResult::GetClosestPeers}`].
745+
///
746+
/// The expected responding peers is specified by `num_results`
747+
pub fn get_closest_peers_num_results<K>(&mut self, key: K, num_results: NonZeroUsize) -> QueryId
748+
where
749+
K: Into<kbucket::Key<K>> + Into<Vec<u8>> + Clone,
750+
{
751+
self.get_closest_peers_inner(key, Some(num_results))
752+
}
753+
754+
fn get_closest_peers_inner<K>(&mut self, key: K, num_results: Option<NonZeroUsize>) -> QueryId
735755
where
736756
K: Into<kbucket::Key<K>> + Into<Vec<u8>> + Clone,
737757
{
@@ -740,8 +760,10 @@ where
740760
let info = QueryInfo::GetClosestPeers {
741761
key,
742762
step: ProgressStep::first(),
763+
num_results,
743764
};
744765
let peer_keys: Vec<kbucket::Key<PeerId>> = self.kbuckets.closest_keys(&target).collect();
766+
745767
self.queries.add_iter_closest(target, peer_keys, info)
746768
}
747769

@@ -1485,7 +1507,7 @@ where
14851507
})
14861508
}
14871509

1488-
QueryInfo::GetClosestPeers { key, mut step } => {
1510+
QueryInfo::GetClosestPeers { key, mut step, .. } => {
14891511
step.last = true;
14901512

14911513
Some(Event::OutboundQueryProgressed {
@@ -1702,7 +1724,7 @@ where
17021724
},
17031725
}),
17041726

1705-
QueryInfo::GetClosestPeers { key, mut step } => {
1727+
QueryInfo::GetClosestPeers { key, mut step, .. } => {
17061728
step.last = true;
17071729
Some(Event::OutboundQueryProgressed {
17081730
id: query_id,
@@ -3175,6 +3197,8 @@ pub enum QueryInfo {
31753197
key: Vec<u8>,
31763198
/// Current index of events.
31773199
step: ProgressStep,
3200+
/// If required, `num_results` specifies expected responding peers
3201+
num_results: Option<NonZeroUsize>,
31783202
},
31793203

31803204
/// A (repeated) query initiated by [`Behaviour::get_providers`].

protocols/kad/src/behaviour/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ fn query_iter() {
263263

264264
match swarms[0].behaviour_mut().query(&qid) {
265265
Some(q) => match q.info() {
266-
QueryInfo::GetClosestPeers { key, step } => {
266+
QueryInfo::GetClosestPeers { key, step, .. } => {
267267
assert_eq!(&key[..], search_target.to_bytes().as_slice());
268268
assert_eq!(usize::from(step.count), 1);
269269
}

protocols/kad/src/query.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,17 @@ impl QueryPool {
138138
T: Into<KeyBytes> + Clone,
139139
I: IntoIterator<Item = Key<PeerId>>,
140140
{
141+
let mut num_results_val = self.config.replication_factor;
142+
if let QueryInfo::GetClosestPeers {
143+
num_results: Some(val),
144+
..
145+
} = info
146+
{
147+
num_results_val = val;
148+
}
149+
141150
let cfg = ClosestPeersIterConfig {
142-
num_results: self.config.replication_factor,
151+
num_results: num_results_val,
143152
parallelism: self.config.parallelism,
144153
..ClosestPeersIterConfig::default()
145154
};

0 commit comments

Comments
 (0)