Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rz): emit NewExternalAddrOfPeer when discovering peers #5138

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
31 changes: 29 additions & 2 deletions protocols/rendezvous/src/client.rs
Expand Up @@ -31,12 +31,14 @@ use libp2p_swarm::{
ConnectionDenied, ConnectionId, ExternalAddresses, FromSwarm, NetworkBehaviour, THandler,
THandlerInEvent, THandlerOutEvent, ToSwarm,
};
use std::collections::HashMap;
use std::collections::{HashMap, VecDeque};
use std::iter;
use std::task::{Context, Poll};
use std::time::Duration;

pub struct Behaviour {
events: VecDeque<ToSwarm<<Self as NetworkBehaviour>::ToSwarm, THandlerInEvent<Self>>>,

inner: libp2p_request_response::Behaviour<crate::codec::Codec>,

keypair: Keypair,
Expand All @@ -61,6 +63,7 @@ impl Behaviour {
/// Create a new instance of the rendezvous [`NetworkBehaviour`].
pub fn new(keypair: Keypair) -> Self {
Self {
events: Default::default(),
inner: libp2p_request_response::Behaviour::with_codec(
crate::codec::Codec::default(),
iter::once((crate::PROTOCOL_IDENT, ProtocolSupport::Outbound)),
Expand Down Expand Up @@ -244,8 +247,11 @@ impl NetworkBehaviour for Behaviour {
cx: &mut Context<'_>,
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
use libp2p_request_response as req_res;

loop {
if let Some(event) = self.events.pop_front() {
return Poll::Ready(event);
}

match self.inner.poll(cx) {
Poll::Ready(ToSwarm::GenerateEvent(req_res::Event::Message {
message:
Expand Down Expand Up @@ -386,6 +392,27 @@ impl Behaviour {
DiscoverResponse(Ok((registrations, cookie))) => {
if let Some((rendezvous_node, _ns)) = self.waiting_for_discovery.remove(request_id)
{
self.events
.extend(registrations.iter().flat_map(|registration| {
let peer_id = registration.record.peer_id();
registration
.record
.addresses()
.iter()
.filter(|addr| {
!self.discovered_peers.iter().any(
|((discovered_peer_id, _), addrs)| {
*discovered_peer_id == peer_id && addrs.contains(addr)
},
)
})
.map(|address| ToSwarm::NewExternalAddrOfPeer {
peer_id,
address: address.clone(),
})
.collect::<Vec<_>>()
}));

self.discovered_peers
.extend(registrations.iter().map(|registration| {
let peer_id = registration.record.peer_id();
Expand Down