diff --git a/Cargo.lock b/Cargo.lock index 67d0c3fb59b..c71837e0e94 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2697,7 +2697,7 @@ dependencies = [ [[package]] name = "libp2p-core" -version = "0.41.3" +version = "0.41.4" dependencies = [ "async-std", "either", @@ -2890,7 +2890,7 @@ dependencies = [ [[package]] name = "libp2p-kad" -version = "0.46.0" +version = "0.47.0" dependencies = [ "arrayvec", "async-std", diff --git a/Cargo.toml b/Cargo.toml index 76bb4f18b3d..88f09be43b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -78,14 +78,14 @@ libp2p = { version = "0.54.0", path = "libp2p" } libp2p-allow-block-list = { version = "0.3.0", path = "misc/allow-block-list" } libp2p-autonat = { version = "0.12.1", path = "protocols/autonat" } libp2p-connection-limits = { version = "0.3.1", path = "misc/connection-limits" } -libp2p-core = { version = "0.41.3", path = "core" } +libp2p-core = { version = "0.41.4", path = "core" } libp2p-dcutr = { version = "0.11.1", path = "protocols/dcutr" } libp2p-dns = { version = "0.41.1", path = "transports/dns" } libp2p-floodsub = { version = "0.44.0", path = "protocols/floodsub" } libp2p-gossipsub = { version = "0.46.2", 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.47.0", path = "protocols/kad" } libp2p-mdns = { version = "0.45.1", 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/core/CHANGELOG.md b/core/CHANGELOG.md index 633b627d41d..0c3b81cd3e5 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.41.4 +- Add `PeerInfo` struct. + See [PR 5475](https://github.com/libp2p/rust-libp2p/pull/5475) + ## 0.41.3 - Use `web-time` instead of `instant`. See [PR 5347](https://github.com/libp2p/rust-libp2p/pull/5347). diff --git a/core/Cargo.toml b/core/Cargo.toml index 6831eb54c94..048988c75b0 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-core" edition = "2021" rust-version = { workspace = true } description = "Core traits and structs of libp2p" -version = "0.41.3" +version = "0.41.4" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/core/src/lib.rs b/core/src/lib.rs index abb83481d6c..73a4382c315 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -56,6 +56,7 @@ pub mod transport; pub mod upgrade; pub use connection::{ConnectedPoint, Endpoint}; +pub use libp2p_identity::PeerId; pub use multiaddr::Multiaddr; pub use multihash; pub use muxing::StreamMuxer; @@ -68,3 +69,10 @@ pub use upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}; #[derive(Debug, thiserror::Error)] #[error(transparent)] pub struct DecodeError(quick_protobuf::Error); + +/// Peer Info combines a Peer ID with a set of multiaddrs that the peer is listening on. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct PeerInfo { + pub peer_id: PeerId, + pub addrs: Vec, +} diff --git a/protocols/kad/CHANGELOG.md b/protocols/kad/CHANGELOG.md index 5cd537cae9e..d26a58715b3 100644 --- a/protocols/kad/CHANGELOG.md +++ b/protocols/kad/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.47.0 +- Included multiaddresses of found peers alongside peer IDs in `GetClosestPeers` query results. + See [PR 5475](https://github.com/libp2p/rust-libp2p/pull/5475) + ## 0.46.0 - Changed `FIND_NODE` response: now includes a list of closest peers when querying the recipient peer ID. Previously, this request yielded an empty response. diff --git a/protocols/kad/Cargo.toml b/protocols/kad/Cargo.toml index 494d812c6ec..04d78a9947d 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.47.0" 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 c9690d2c873..ce81c2e8dca 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -36,7 +36,7 @@ use crate::record::{ use crate::K_VALUE; use crate::{jobs::*, protocol}; use fnv::{FnvHashMap, FnvHashSet}; -use libp2p_core::{ConnectedPoint, Endpoint, Multiaddr}; +use libp2p_core::{ConnectedPoint, Endpoint, Multiaddr, PeerInfo}; use libp2p_identity::PeerId; use libp2p_swarm::behaviour::{ AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm, @@ -1390,7 +1390,7 @@ where fn query_finished(&mut self, q: Query) -> Option { let query_id = q.id(); tracing::trace!(query=?query_id, "Query finished"); - let result = q.into_result(); + let mut result = q.into_result(); match result.inner.info { QueryInfo::Bootstrap { peer, @@ -1466,14 +1466,23 @@ where QueryInfo::GetClosestPeers { key, mut step } => { step.last = true; + let peers = result + .peers + .map(|peer_id| { + let addrs = result + .inner + .addresses + .remove(&peer_id) + .unwrap_or_default() + .to_vec(); + PeerInfo { peer_id, addrs } + }) + .collect(); Some(Event::OutboundQueryProgressed { id: query_id, stats: result.stats, - result: QueryResult::GetClosestPeers(Ok(GetClosestPeersOk { - key, - peers: result.peers.collect(), - })), + result: QueryResult::GetClosestPeers(Ok(GetClosestPeersOk { key, peers })), step, }) } @@ -1629,7 +1638,7 @@ where fn query_timeout(&mut self, query: Query) -> Option { let query_id = query.id(); tracing::trace!(query=?query_id, "Query timed out"); - let result = query.into_result(); + let mut result = query.into_result(); match result.inner.info { QueryInfo::Bootstrap { peer, @@ -1684,13 +1693,25 @@ where QueryInfo::GetClosestPeers { key, mut step } => { step.last = true; + let peers = result + .peers + .map(|peer_id| { + let addrs = result + .inner + .addresses + .remove(&peer_id) + .unwrap_or_default() + .to_vec(); + PeerInfo { peer_id, addrs } + }) + .collect(); Some(Event::OutboundQueryProgressed { id: query_id, stats: result.stats, result: QueryResult::GetClosestPeers(Err(GetClosestPeersError::Timeout { key, - peers: result.peers.collect(), + peers, })), step, }) @@ -2978,14 +2999,14 @@ pub type GetClosestPeersResult = Result #[derive(Debug, Clone)] pub struct GetClosestPeersOk { pub key: Vec, - pub peers: Vec, + pub peers: Vec, } /// The error result of [`Behaviour::get_closest_peers`]. #[derive(Debug, Clone, Error)] pub enum GetClosestPeersError { #[error("the request timed out")] - Timeout { key: Vec, peers: Vec }, + Timeout { key: Vec, peers: Vec }, } impl GetClosestPeersError { diff --git a/protocols/kad/src/behaviour/test.rs b/protocols/kad/src/behaviour/test.rs index 7005f39e5e6..a3ff43ded6e 100644 --- a/protocols/kad/src/behaviour/test.rs +++ b/protocols/kad/src/behaviour/test.rs @@ -294,9 +294,11 @@ fn query_iter() { assert_eq!(&ok.key[..], search_target.to_bytes().as_slice()); assert_eq!(swarm_ids[i], expected_swarm_id); assert_eq!(swarm.behaviour_mut().queries.size(), 0); - assert!(expected_peer_ids.iter().all(|p| ok.peers.contains(p))); + let peer_ids = + ok.peers.into_iter().map(|p| p.peer_id).collect::>(); + assert!(expected_peer_ids.iter().all(|p| peer_ids.contains(p))); let key = kbucket::Key::new(ok.key); - assert_eq!(expected_distances, distances(&key, ok.peers)); + assert_eq!(expected_distances, distances(&key, peer_ids)); return Poll::Ready(()); } // Ignore any other event. @@ -408,7 +410,7 @@ fn unresponsive_not_returned_indirect() { }))) => { assert_eq!(&ok.key[..], search_target.to_bytes().as_slice()); assert_eq!(ok.peers.len(), 1); - assert_eq!(ok.peers[0], first_peer_id); + assert_eq!(ok.peers[0].peer_id, first_peer_id); return Poll::Ready(()); } // Ignore any other event.