Skip to content

Commit 924321a

Browse files
authored
feat(rz): emit NewExternalAddrOfPeer when discovering peers
- Resolves #5105 Pull-Request: #5138.
1 parent 0e89f38 commit 924321a

File tree

6 files changed

+47
-5
lines changed

6 files changed

+47
-5
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ libp2p-plaintext = { version = "0.43.0", path = "transports/plaintext" }
9898
libp2p-pnet = { version = "0.26.0", path = "transports/pnet" }
9999
libp2p-quic = { version = "0.12.1", path = "transports/quic" }
100100
libp2p-relay = { version = "0.20.0", path = "protocols/relay" }
101-
libp2p-rendezvous = { version = "0.16.0", path = "protocols/rendezvous" }
101+
libp2p-rendezvous = { version = "0.16.1", path = "protocols/rendezvous" }
102102
libp2p-request-response = { version = "0.28.1", path = "protocols/request-response" }
103103
libp2p-server = { version = "0.12.7", path = "misc/server" }
104104
libp2p-stream = { version = "0.3.0-alpha", path = "protocols/stream" }

protocols/rendezvous/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.16.1
2+
- Emit `ToSwarm::NewExternalAddrOfPeer` for newly discovered peers.
3+
See [PR 5138](https://github.com/libp2p/rust-libp2p/pull/5138).
4+
15
## 0.16.0
26

37
- Update to `libp2p-request-response` `v0.28.0`.

protocols/rendezvous/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "libp2p-rendezvous"
33
edition.workspace = true
44
rust-version = { workspace = true }
55
description = "Rendezvous protocol for libp2p"
6-
version = "0.16.0"
6+
version = "0.16.1"
77
authors = ["The COMIT guys <[email protected]>"]
88
license = "MIT"
99
repository = "https://github.com/libp2p/rust-libp2p"

protocols/rendezvous/src/client.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// DEALINGS IN THE SOFTWARE.
2020

2121
use std::{
22-
collections::HashMap,
22+
collections::{HashMap, VecDeque},
2323
iter,
2424
task::{Context, Poll},
2525
time::Duration,
@@ -42,6 +42,8 @@ use crate::codec::{
4242
};
4343

4444
pub struct Behaviour {
45+
events: VecDeque<ToSwarm<<Self as NetworkBehaviour>::ToSwarm, THandlerInEvent<Self>>>,
46+
4547
inner: libp2p_request_response::Behaviour<crate::codec::Codec>,
4648

4749
keypair: Keypair,
@@ -68,6 +70,7 @@ impl Behaviour {
6870
/// Create a new instance of the rendezvous [`NetworkBehaviour`].
6971
pub fn new(keypair: Keypair) -> Self {
7072
Self {
73+
events: Default::default(),
7174
inner: libp2p_request_response::Behaviour::with_codec(
7275
crate::codec::Codec::default(),
7376
iter::once((crate::PROTOCOL_IDENT, ProtocolSupport::Outbound)),
@@ -258,8 +261,11 @@ impl NetworkBehaviour for Behaviour {
258261
cx: &mut Context<'_>,
259262
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
260263
use libp2p_request_response as req_res;
261-
262264
loop {
265+
if let Some(event) = self.events.pop_front() {
266+
return Poll::Ready(event);
267+
}
268+
263269
match self.inner.poll(cx) {
264270
Poll::Ready(ToSwarm::GenerateEvent(req_res::Event::Message {
265271
message:
@@ -400,6 +406,27 @@ impl Behaviour {
400406
DiscoverResponse(Ok((registrations, cookie))) => {
401407
if let Some((rendezvous_node, _ns)) = self.waiting_for_discovery.remove(request_id)
402408
{
409+
self.events
410+
.extend(registrations.iter().flat_map(|registration| {
411+
let peer_id = registration.record.peer_id();
412+
registration
413+
.record
414+
.addresses()
415+
.iter()
416+
.filter(|addr| {
417+
!self.discovered_peers.iter().any(
418+
|((discovered_peer_id, _), addrs)| {
419+
*discovered_peer_id == peer_id && addrs.contains(addr)
420+
},
421+
)
422+
})
423+
.map(|address| ToSwarm::NewExternalAddrOfPeer {
424+
peer_id,
425+
address: address.clone(),
426+
})
427+
.collect::<Vec<_>>()
428+
}));
429+
403430
self.discovered_peers
404431
.extend(registrations.iter().map(|registration| {
405432
let peer_id = registration.record.peer_id();

protocols/rendezvous/tests/rendezvous.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ async fn registration_on_clients_expire() {
384384
new_server_with_connected_clients(rendezvous::server::Config::default().with_min_ttl(1))
385385
.await;
386386

387+
let alice_peer_id = *alice.local_peer_id();
387388
let roberts_peer_id = *robert.local_peer_id();
388389
tokio::spawn(robert.loop_on_next());
389390

@@ -406,6 +407,16 @@ async fn registration_on_clients_expire() {
406407
event => panic!("Unexpected event: {event:?}"),
407408
}
408409

410+
match bob.next_swarm_event().await {
411+
SwarmEvent::NewExternalAddrOfPeer {
412+
peer_id: discovered_peer_id,
413+
..
414+
} => {
415+
assert_eq!(discovered_peer_id, alice_peer_id);
416+
}
417+
event => panic!("Unexpected event: {event:?}"),
418+
}
419+
409420
tokio::time::sleep(Duration::from_secs(registration_ttl + 1)).await;
410421

411422
let event = bob.select_next_some().await;

0 commit comments

Comments
 (0)