Skip to content

Commit

Permalink
feat(kad): Return multiaddrs alognside peer IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
Wiezzel committed Jun 19, 2024
1 parent 32e917f commit 92b5b92
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 20 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 @@ -85,7 +85,7 @@ libp2p-floodsub = { version = "0.44.0", path = "protocols/floodsub" }
libp2p-gossipsub = { version = "0.46.2", path = "protocols/gossipsub" }
libp2p-identify = { version = "0.44.2", 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 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 <admin@parity.io>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
46 changes: 37 additions & 9 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
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();
FoundPeer { 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)
.expect("peer not present in address map")
.to_vec();
FoundPeer { 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,21 @@ pub type GetClosestPeersResult = Result<GetClosestPeersOk, GetClosestPeersError>
#[derive(Debug, Clone)]
pub struct GetClosestPeersOk {
pub key: Vec<u8>,
pub peers: Vec<PeerId>,
pub peers: Vec<FoundPeer>,
}

/// Found peer with addresses
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FoundPeer {
pub peer_id: PeerId,
pub addrs: Vec<Multiaddr>,
}

/// 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<FoundPeer> },
}

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
10 changes: 5 additions & 5 deletions protocols/kad/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ mod proto {
pub use addresses::Addresses;
pub use behaviour::{
AddProviderContext, AddProviderError, AddProviderOk, AddProviderPhase, AddProviderResult,
BootstrapError, BootstrapOk, BootstrapResult, GetClosestPeersError, GetClosestPeersOk,
GetClosestPeersResult, GetProvidersError, GetProvidersOk, GetProvidersResult, GetRecordError,
GetRecordOk, GetRecordResult, InboundRequest, Mode, NoKnownPeers, PeerRecord, PutRecordContext,
PutRecordError, PutRecordOk, PutRecordPhase, PutRecordResult, QueryInfo, QueryMut, QueryRef,
QueryResult, QueryStats, RoutingUpdate,
BootstrapError, BootstrapOk, BootstrapResult, FoundPeer, GetClosestPeersError,
GetClosestPeersOk, GetClosestPeersResult, GetProvidersError, GetProvidersOk,
GetProvidersResult, GetRecordError, GetRecordOk, GetRecordResult, InboundRequest, Mode,
NoKnownPeers, PeerRecord, PutRecordContext, PutRecordError, PutRecordOk, PutRecordPhase,
PutRecordResult, QueryInfo, QueryMut, QueryRef, QueryResult, QueryStats, RoutingUpdate,
};
pub use behaviour::{
Behaviour, BucketInserts, Caching, Config, Event, ProgressStep, Quorum, StoreInserts,
Expand Down

0 comments on commit 92b5b92

Please sign in to comment.