Skip to content

Commit

Permalink
feat(kad): Return multiaddrs alognside peer IDs
Browse files Browse the repository at this point in the history
Pull-Request: #5475.
  • Loading branch information
Wiezzel authored Jun 21, 2024
1 parent 605f208 commit e28de77
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
4 changes: 4 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
8 changes: 8 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Multiaddr>,
}
4 changes: 4 additions & 0 deletions protocols/kad/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
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.0"
version = "0.47.0"
authors = ["Parity Technologies <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
41 changes: 31 additions & 10 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1390,7 +1390,7 @@ where
fn query_finished(&mut self, q: Query<QueryInner>) -> Option<Event> {
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,
Expand Down Expand Up @@ -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,
})
}
Expand Down Expand Up @@ -1629,7 +1638,7 @@ where
fn query_timeout(&mut self, query: Query<QueryInner>) -> Option<Event> {
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,
Expand Down Expand Up @@ -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,
})
Expand Down Expand Up @@ -2978,14 +2999,14 @@ pub type GetClosestPeersResult = Result<GetClosestPeersOk, GetClosestPeersError>
#[derive(Debug, Clone)]
pub struct GetClosestPeersOk {
pub key: Vec<u8>,
pub peers: Vec<PeerId>,
pub peers: Vec<PeerInfo>,
}

/// The error result of [`Behaviour::get_closest_peers`].
#[derive(Debug, Clone, Error)]
pub enum GetClosestPeersError {
#[error("the request timed out")]
Timeout { key: Vec<u8>, peers: Vec<PeerId> },
Timeout { key: Vec<u8>, peers: Vec<PeerInfo> },
}

impl GetClosestPeersError {
Expand Down
8 changes: 5 additions & 3 deletions protocols/kad/src/behaviour/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();
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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit e28de77

Please sign in to comment.