Skip to content

Commit de8cba9

Browse files
feat(kad): emit ToSwarm::NewExternalAddrOfPeer (#5549)
## 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 <[email protected]>
1 parent f0cbd4f commit de8cba9

File tree

6 files changed

+31
-6
lines changed

6 files changed

+31
-6
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ libp2p-floodsub = { version = "0.45.0", path = "protocols/floodsub" }
8686
libp2p-gossipsub = { version = "0.47.0", path = "protocols/gossipsub" }
8787
libp2p-identify = { version = "0.45.0", path = "protocols/identify" }
8888
libp2p-identity = { version = "0.2.9" }
89-
libp2p-kad = { version = "0.46.1", path = "protocols/kad" }
89+
libp2p-kad = { version = "0.46.2", path = "protocols/kad" }
9090
libp2p-mdns = { version = "0.46.0", path = "protocols/mdns" }
9191
libp2p-memory-connection-limits = { version = "0.3.0", path = "misc/memory-connection-limits" }
9292
libp2p-metrics = { version = "0.15.0", path = "misc/metrics" }

protocols/kad/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.46.2
2+
3+
- Emit `ToSwarm::NewExternalAddrOfPeer`.
4+
See [PR 5549](https://github.com/libp2p/rust-libp2p/pull/5549)
5+
16
## 0.46.1
27

38
- Use new provider record update strategy to prevent Sybil attack.

protocols/kad/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "libp2p-kad"
33
edition = "2021"
44
rust-version = { workspace = true }
55
description = "Kademlia protocol for libp2p"
6-
version = "0.46.1"
6+
version = "0.46.2"
77
authors = ["Parity Technologies <[email protected]>"]
88
license = "MIT"
99
repository = "https://github.com/libp2p/rust-libp2p"

protocols/kad/src/behaviour.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2562,13 +2562,19 @@ where
25622562
// Drain applied pending entries from the routing table.
25632563
if let Some(entry) = self.kbuckets.take_applied_pending() {
25642564
let kbucket::Node { key, value } = entry.inserted;
2565+
let peer_id = key.into_preimage();
2566+
self.queued_events
2567+
.push_back(ToSwarm::NewExternalAddrOfPeer {
2568+
peer_id,
2569+
address: value.first().clone(),
2570+
});
25652571
let event = Event::RoutingUpdated {
25662572
bucket_range: self
25672573
.kbuckets
25682574
.bucket(&key)
25692575
.map(|b| b.range())
25702576
.expect("Self to never be applied from pending."),
2571-
peer: key.into_preimage(),
2577+
peer: peer_id,
25722578
is_new_peer: true,
25732579
addresses: value,
25742580
old_peer: entry.evicted.map(|n| n.key.into_preimage()),

protocols/kad/tests/client_mode.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,21 @@ async fn server_gets_added_to_routing_table_by_client() {
2323
let server_peer_id = *server.local_peer_id();
2424
async_std::task::spawn(server.loop_on_next());
2525

26-
let peer = client
26+
let external_event_peer = client
27+
.wait(|e| match e {
28+
SwarmEvent::NewExternalAddrOfPeer { peer_id, .. } => Some(peer_id),
29+
_ => None,
30+
})
31+
.await;
32+
let routing_updated_peer = client
2733
.wait(|e| match e {
2834
SwarmEvent::Behaviour(Kad(RoutingUpdated { peer, .. })) => Some(peer),
2935
_ => None,
3036
})
3137
.await;
3238

33-
assert_eq!(peer, server_peer_id);
39+
assert_eq!(external_event_peer, server_peer_id);
40+
assert_eq!(routing_updated_peer, server_peer_id);
3441
}
3542

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

127134
let server_peer_id = *server.local_peer_id();
128135

136+
let peer_id = client
137+
.wait(|e| match e {
138+
SwarmEvent::NewExternalAddrOfPeer { peer_id, .. } => Some(peer_id),
139+
_ => None,
140+
})
141+
.await;
129142
let client_event = client.wait(|e| match e {
130143
SwarmEvent::Behaviour(Kad(RoutingUpdated { peer, .. })) => Some(peer),
131144
_ => None,
@@ -138,6 +151,7 @@ async fn set_client_to_server_mode() {
138151
let (peer, info) = futures::future::join(client_event, server_event).await;
139152

140153
assert_eq!(peer, server_peer_id);
154+
assert_eq!(peer_id, server_peer_id);
141155
assert!(info
142156
.protocols
143157
.iter()

0 commit comments

Comments
 (0)