diff --git a/Cargo.lock b/Cargo.lock index af3ceb9eb0f1..c8820f1afba3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2928,7 +2928,7 @@ dependencies = [ [[package]] name = "libp2p-kad" -version = "0.46.0" +version = "0.46.1" dependencies = [ "arrayvec", "async-std", diff --git a/Cargo.toml b/Cargo.toml index 3f00734ae3c7..cc87d6f02b96 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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.0", path = "protocols/kad" } +libp2p-kad = { version = "0.46.1", path = "protocols/kad" } libp2p-mdns = { version = "0.46.0", path = "protocols/mdns" } libp2p-memory-connection-limits = { version = "0.2.0", path = "misc/memory-connection-limits" } libp2p-metrics = { version = "0.14.2", path = "misc/metrics" } diff --git a/protocols/kad/CHANGELOG.md b/protocols/kad/CHANGELOG.md index cbb5a4decf27..d82c3bd9bb8e 100644 --- a/protocols/kad/CHANGELOG.md +++ b/protocols/kad/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.46.1 + +- Expose a kad query facility allowing specify num_results dynamicly. + See [PR 5555](https://github.com/libp2p/rust-libp2p/pull/5555). + ## 0.46.0 - Included multiaddresses of found peers alongside peer IDs in `GetClosestPeers` query results. diff --git a/protocols/kad/Cargo.toml b/protocols/kad/Cargo.toml index 72b29d00ef7b..a00959fced61 100644 --- a/protocols/kad/Cargo.toml +++ b/protocols/kad/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-kad" edition = "2021" rust-version = { workspace = true } description = "Kademlia protocol for libp2p" -version = "0.46.0" +version = "0.46.1" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index fc3d8a1adaa7..a46dcb10cf72 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -732,6 +732,26 @@ where /// The result of the query is delivered in a /// [`Event::OutboundQueryProgressed{QueryResult::GetClosestPeers}`]. pub fn get_closest_peers(&mut self, key: K) -> QueryId + where + K: Into> + Into> + 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(&mut self, key: K, num_results: NonZeroUsize) -> QueryId + where + K: Into> + Into> + Clone, + { + self.get_closest_peers_inner(key, Some(num_results)) + } + + fn get_closest_peers_inner(&mut self, key: K, num_results: Option) -> QueryId where K: Into> + Into> + Clone, { @@ -742,7 +762,13 @@ where step: ProgressStep::first(), }; let peer_keys: Vec> = self.kbuckets.closest_keys(&target).collect(); - self.queries.add_iter_closest(target, peer_keys, info) + + if let Some(num_results) = num_results { + self.queries + .add_iter_closest_num_results(target, peer_keys, info, num_results) + } else { + self.queries.add_iter_closest(target, peer_keys, info) + } } /// Returns closest peers to the given key; takes peers from local routing table only. diff --git a/protocols/kad/src/query.rs b/protocols/kad/src/query.rs index c598bac012ee..2a4fbe28814a 100644 --- a/protocols/kad/src/query.rs +++ b/protocols/kad/src/query.rs @@ -127,6 +127,24 @@ impl QueryPool { id } + /// Adds a query to the pool that iterates towards the closest peers to the target. + /// With `num_results` dynamic specified. + pub(crate) fn add_iter_closest_num_results( + &mut self, + target: T, + peers: I, + info: QueryInfo, + num_results: NonZeroUsize, + ) -> QueryId + where + T: Into + Clone, + I: IntoIterator>, + { + let id = self.next_query_id(); + self.continue_iter_closest_inner(id, target, peers, info, num_results); + id + } + /// Adds a query to the pool that iterates towards the closest peers to the target. pub(crate) fn continue_iter_closest( &mut self, @@ -137,9 +155,23 @@ impl QueryPool { ) where T: Into + Clone, I: IntoIterator>, + { + self.continue_iter_closest_inner(id, target, peers, info, self.config.replication_factor); + } + + fn continue_iter_closest_inner( + &mut self, + id: QueryId, + target: T, + peers: I, + info: QueryInfo, + num_results: NonZeroUsize, + ) where + T: Into + Clone, + I: IntoIterator>, { let cfg = ClosestPeersIterConfig { - num_results: self.config.replication_factor, + num_results, parallelism: self.config.parallelism, ..ClosestPeersIterConfig::default() };