Skip to content

Commit

Permalink
feat(kad): emit ToSwarm::NewExternalAddrOfPeer (#5549)
Browse files Browse the repository at this point in the history
## Description

<!--
Please write a summary of your changes and why you made them.
This section will appear as the commit message after merging.
Please craft it accordingly.
For a quick primer on good commit messages, check out this blog post:
https://cbea.ms/git-commit/

Please include any relevant issues in here, for example:

Related https://github.com/libp2p/rust-libp2p/issues/ABCD.
Fixes https://github.com/libp2p/rust-libp2p/issues/XYZ.
-->
Updates `libp2p-kad` to emit new event `ToSwarm::NewExternalAddrOfPeer`
whenever it discovers a new address through the DHT.
Related: #5103 

## Notes & open questions

<!--
Any notes, remarks or open questions you have to make about the PR which
don't need to go into the final commit message.
-->

## Change checklist

<!-- Please add a Changelog entry in the appropriate crates and bump the
crate versions if needed. See
<https://github.com/libp2p/rust-libp2p/blob/master/docs/release.md#development-between-releases>-->

- [X] I have performed a self-review of my own code
- [ ] I have made corresponding changes to the documentation
- [X] I have added tests that prove my fix is effective or that my
feature works
- [ ] A changelog entry has been made in the appropriate crates

---------

Co-authored-by: Guillaume Michel <guillaumemichel@users.noreply.github.com>
  • Loading branch information
PanGan21 and guillaumemichel committed Aug 27, 2024
1 parent f0cbd4f commit de8cba9
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 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 @@ -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.1", path = "protocols/kad" }
libp2p-kad = { version = "0.46.2", path = "protocols/kad" }
libp2p-mdns = { version = "0.46.0", path = "protocols/mdns" }
libp2p-memory-connection-limits = { version = "0.3.0", path = "misc/memory-connection-limits" }
libp2p-metrics = { version = "0.15.0", path = "misc/metrics" }
Expand Down
5 changes: 5 additions & 0 deletions protocols/kad/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.46.2

- Emit `ToSwarm::NewExternalAddrOfPeer`.
See [PR 5549](https://github.com/libp2p/rust-libp2p/pull/5549)

## 0.46.1

- Use new provider record update strategy to prevent Sybil attack.
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.1"
version = "0.46.2"
authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
8 changes: 7 additions & 1 deletion protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2562,13 +2562,19 @@ where
// Drain applied pending entries from the routing table.
if let Some(entry) = self.kbuckets.take_applied_pending() {
let kbucket::Node { key, value } = entry.inserted;
let peer_id = key.into_preimage();
self.queued_events
.push_back(ToSwarm::NewExternalAddrOfPeer {
peer_id,
address: value.first().clone(),
});
let event = Event::RoutingUpdated {
bucket_range: self
.kbuckets
.bucket(&key)
.map(|b| b.range())
.expect("Self to never be applied from pending."),
peer: key.into_preimage(),
peer: peer_id,
is_new_peer: true,
addresses: value,
old_peer: entry.evicted.map(|n| n.key.into_preimage()),
Expand Down
18 changes: 16 additions & 2 deletions protocols/kad/tests/client_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,21 @@ async fn server_gets_added_to_routing_table_by_client() {
let server_peer_id = *server.local_peer_id();
async_std::task::spawn(server.loop_on_next());

let peer = client
let external_event_peer = client
.wait(|e| match e {
SwarmEvent::NewExternalAddrOfPeer { peer_id, .. } => Some(peer_id),
_ => None,
})
.await;
let routing_updated_peer = client
.wait(|e| match e {
SwarmEvent::Behaviour(Kad(RoutingUpdated { peer, .. })) => Some(peer),
_ => None,
})
.await;

assert_eq!(peer, server_peer_id);
assert_eq!(external_event_peer, server_peer_id);
assert_eq!(routing_updated_peer, server_peer_id);
}

#[async_std::test]
Expand Down Expand Up @@ -126,6 +133,12 @@ async fn set_client_to_server_mode() {

let server_peer_id = *server.local_peer_id();

let peer_id = client
.wait(|e| match e {
SwarmEvent::NewExternalAddrOfPeer { peer_id, .. } => Some(peer_id),
_ => None,
})
.await;
let client_event = client.wait(|e| match e {
SwarmEvent::Behaviour(Kad(RoutingUpdated { peer, .. })) => Some(peer),
_ => None,
Expand All @@ -138,6 +151,7 @@ async fn set_client_to_server_mode() {
let (peer, info) = futures::future::join(client_event, server_event).await;

assert_eq!(peer, server_peer_id);
assert_eq!(peer_id, server_peer_id);
assert!(info
.protocols
.iter()
Expand Down

0 comments on commit de8cba9

Please sign in to comment.