Skip to content

Commit

Permalink
swarm: Add NewExternalAddrOfPeer
Browse files Browse the repository at this point in the history
  • Loading branch information
StemCll committed Aug 21, 2023
1 parent e974efb commit 711b03b
Show file tree
Hide file tree
Showing 23 changed files with 141 additions and 44 deletions.
27 changes: 14 additions & 13 deletions misc/allow-block-list/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,19 +233,20 @@ where

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
match event {
FromSwarm::ConnectionClosed(_) => {}
FromSwarm::ConnectionEstablished(_) => {}
FromSwarm::AddressChange(_) => {}
FromSwarm::DialFailure(_) => {}
FromSwarm::ListenFailure(_) => {}
FromSwarm::NewListener(_) => {}
FromSwarm::NewListenAddr(_) => {}
FromSwarm::ExpiredListenAddr(_) => {}
FromSwarm::ListenerError(_) => {}
FromSwarm::ListenerClosed(_) => {}
FromSwarm::NewExternalAddrCandidate(_) => {}
FromSwarm::ExternalAddrExpired(_) => {}
FromSwarm::ExternalAddrConfirmed(_) => {}
FromSwarm::ConnectionClosed(_)
| FromSwarm::ConnectionEstablished(_)
| FromSwarm::AddressChange(_)
| FromSwarm::DialFailure(_)
| FromSwarm::ListenFailure(_)
| FromSwarm::NewListener(_)
| FromSwarm::NewListenAddr(_)
| FromSwarm::ExpiredListenAddr(_)
| FromSwarm::ListenerError(_)
| FromSwarm::ListenerClosed(_)
| FromSwarm::NewExternalAddrCandidate(_)
| FromSwarm::ExternalAddrExpired(_)
| FromSwarm::ExternalAddrConfirmed(_)
| FromSwarm::NewExternalAddrOfPeer(_) => {}
}
}

Expand Down
17 changes: 9 additions & 8 deletions misc/connection-limits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,14 +344,15 @@ impl NetworkBehaviour for Behaviour {
FromSwarm::ListenFailure(ListenFailure { connection_id, .. }) => {
self.pending_inbound_connections.remove(&connection_id);
}
FromSwarm::NewListener(_) => {}
FromSwarm::NewListenAddr(_) => {}
FromSwarm::ExpiredListenAddr(_) => {}
FromSwarm::ListenerError(_) => {}
FromSwarm::ListenerClosed(_) => {}
FromSwarm::NewExternalAddrCandidate(_) => {}
FromSwarm::ExternalAddrExpired(_) => {}
FromSwarm::ExternalAddrConfirmed(_) => {}
FromSwarm::NewListener(_)
| FromSwarm::NewListenAddr(_)
| FromSwarm::ExpiredListenAddr(_)
| FromSwarm::ListenerError(_)
| FromSwarm::ListenerClosed(_)
| FromSwarm::NewExternalAddrCandidate(_)
| FromSwarm::ExternalAddrExpired(_)
| FromSwarm::ExternalAddrConfirmed(_)
| FromSwarm::NewExternalAddrOfPeer(_) => {}
}
}

Expand Down
5 changes: 4 additions & 1 deletion protocols/autonat/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use libp2p_request_response::{
use libp2p_swarm::{
behaviour::{
AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, ExpiredListenAddr,
ExternalAddrExpired, FromSwarm,
ExternalAddrExpired, FromSwarm, NewExternalAddrOfPeer,
},
ConnectionDenied, ConnectionId, ListenAddresses, NetworkBehaviour, NewExternalAddrCandidate,
PollParameters, THandler, THandlerInEvent, THandlerOutEvent, ToSwarm,
Expand Down Expand Up @@ -589,6 +589,9 @@ impl NetworkBehaviour for Behaviour {
self.inner.on_swarm_event(listener_closed)
}
confirmed @ FromSwarm::ExternalAddrConfirmed(_) => self.inner.on_swarm_event(confirmed),
FromSwarm::NewExternalAddrOfPeer(NewExternalAddrOfPeer { addr }) => {
todo!();
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion protocols/dcutr/src/behaviour_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ impl NetworkBehaviour for Behaviour {
| FromSwarm::ListenerClosed(_)
| FromSwarm::NewExternalAddrCandidate(_)
| FromSwarm::ExternalAddrExpired(_)
| FromSwarm::ExternalAddrConfirmed(_) => {}
| FromSwarm::ExternalAddrConfirmed(_)
| FromSwarm::NewExternalAddrOfPeer(_) => {}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion protocols/floodsub/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,8 @@ impl NetworkBehaviour for Floodsub {
| FromSwarm::ListenerClosed(_)
| FromSwarm::NewExternalAddrCandidate(_)
| FromSwarm::ExternalAddrExpired(_)
| FromSwarm::ExternalAddrConfirmed(_) => {}
| FromSwarm::ExternalAddrConfirmed(_)
| FromSwarm::NewExternalAddrOfPeer(_) => {}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3507,7 +3507,8 @@ where
| FromSwarm::ListenerClosed(_)
| FromSwarm::NewExternalAddrCandidate(_)
| FromSwarm::ExternalAddrExpired(_)
| FromSwarm::ExternalAddrConfirmed(_) => {}
| FromSwarm::ExternalAddrConfirmed(_)
| FromSwarm::NewExternalAddrOfPeer(_) => {}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions protocols/identify/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ impl NetworkBehaviour for Behaviour {
}
}
}
FromSwarm::NewExternalAddrOfPeer(_addr) => todo!(),
FromSwarm::NewListenAddr(_)
| FromSwarm::ExpiredListenAddr(_)
| FromSwarm::AddressChange(_)
Expand Down
1 change: 1 addition & 0 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2551,6 +2551,7 @@ where
}
FromSwarm::DialFailure(dial_failure) => self.on_dial_failure(dial_failure),
FromSwarm::AddressChange(address_change) => self.on_address_change(address_change),
FromSwarm::NewExternalAddrOfPeer(_addr) => todo!(),
FromSwarm::ExpiredListenAddr(_)
| FromSwarm::NewExternalAddrCandidate(_)
| FromSwarm::NewListenAddr(_)
Expand Down
3 changes: 2 additions & 1 deletion protocols/mdns/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ where
| FromSwarm::ListenerClosed(_)
| FromSwarm::NewExternalAddrCandidate(_)
| FromSwarm::ExternalAddrExpired(_)
| FromSwarm::ExternalAddrConfirmed(_) => {}
| FromSwarm::ExternalAddrConfirmed(_)
| FromSwarm::NewExternalAddrOfPeer(_) => {}
}
}

Expand Down
3 changes: 2 additions & 1 deletion protocols/perf/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ impl NetworkBehaviour for Behaviour {
| FromSwarm::ListenerClosed(_)
| FromSwarm::NewExternalAddrCandidate(_)
| FromSwarm::ExternalAddrConfirmed(_)
| FromSwarm::ExternalAddrExpired(_) => {}
| FromSwarm::ExternalAddrExpired(_)
| FromSwarm::NewExternalAddrOfPeer(_) => {}
};

self.request_response.on_swarm_event(event);
Expand Down
3 changes: 2 additions & 1 deletion protocols/ping/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ impl NetworkBehaviour for Behaviour {
| FromSwarm::ListenerClosed(_)
| FromSwarm::NewExternalAddrCandidate(_)
| FromSwarm::ExternalAddrExpired(_)
| FromSwarm::ExternalAddrConfirmed(_) => {}
| FromSwarm::ExternalAddrConfirmed(_)
| FromSwarm::NewExternalAddrOfPeer(_) => {}
}
}
}
3 changes: 2 additions & 1 deletion protocols/relay/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ impl NetworkBehaviour for Behaviour {
| FromSwarm::ListenerClosed(_)
| FromSwarm::NewExternalAddrCandidate(_)
| FromSwarm::ExternalAddrExpired(_)
| FromSwarm::ExternalAddrConfirmed(_) => {}
| FromSwarm::ExternalAddrConfirmed(_)
| FromSwarm::NewExternalAddrOfPeer(_) => {}
}
}

Expand Down
3 changes: 2 additions & 1 deletion protocols/relay/src/priv_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ impl NetworkBehaviour for Behaviour {
| FromSwarm::ListenerClosed(_)
| FromSwarm::NewExternalAddrCandidate(_)
| FromSwarm::ExternalAddrExpired(_)
| FromSwarm::ExternalAddrConfirmed(_) => {}
| FromSwarm::ExternalAddrConfirmed(_)
| FromSwarm::NewExternalAddrOfPeer(_) => {}
}
}

Expand Down
3 changes: 2 additions & 1 deletion protocols/rendezvous/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ impl NetworkBehaviour for Behaviour {
| ToSwarm::Dial { .. }
| ToSwarm::CloseConnection { .. }
| ToSwarm::ListenOn { .. }
| ToSwarm::RemoveListener { .. }),
| ToSwarm::RemoveListener { .. }
| ToSwarm::NewExternalAddrOfPeer { .. }),
) => {
let new_to_swarm =
other.map_out(|_| unreachable!("we manually map `GenerateEvent` variants"));
Expand Down
1 change: 1 addition & 0 deletions protocols/rendezvous/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ impl NetworkBehaviour for Behaviour {
| ToSwarm::NewExternalAddrCandidate(_)
| ToSwarm::ExternalAddrConfirmed(_)
| ToSwarm::ExternalAddrExpired(_)
| ToSwarm::NewExternalAddrOfPeer { .. }
| ToSwarm::CloseConnection { .. } => {
let new_to_swarm = to_swarm
.map_out(|_| unreachable!("we manually map `GenerateEvent` variants"));
Expand Down
19 changes: 10 additions & 9 deletions protocols/request-response/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,15 +775,16 @@ where
}
FromSwarm::AddressChange(address_change) => self.on_address_change(address_change),
FromSwarm::DialFailure(dial_failure) => self.on_dial_failure(dial_failure),
FromSwarm::ListenFailure(_) => {}
FromSwarm::NewListener(_) => {}
FromSwarm::NewListenAddr(_) => {}
FromSwarm::ExpiredListenAddr(_) => {}
FromSwarm::ListenerError(_) => {}
FromSwarm::ListenerClosed(_) => {}
FromSwarm::NewExternalAddrCandidate(_) => {}
FromSwarm::ExternalAddrExpired(_) => {}
FromSwarm::ExternalAddrConfirmed(_) => {}
FromSwarm::ListenFailure(_)
| FromSwarm::NewListener(_)
| FromSwarm::NewListenAddr(_)
| FromSwarm::ExpiredListenAddr(_)
| FromSwarm::ListenerError(_)
| FromSwarm::ListenerClosed(_)
| FromSwarm::NewExternalAddrCandidate(_)
| FromSwarm::ExternalAddrExpired(_)
| FromSwarm::ExternalAddrConfirmed(_)
| FromSwarm::NewExternalAddrOfPeer(_) => {}
}
}

Expand Down
28 changes: 28 additions & 0 deletions swarm-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> syn::Result<Toke
let new_listen_addr = quote! { #prelude_path::NewListenAddr };
let expired_listen_addr = quote! { #prelude_path::ExpiredListenAddr };
let new_external_addr_candidate = quote! { #prelude_path::NewExternalAddrCandidate };
let new_external_addr_of_peer = quote! { #prelude_path::NewExternalAddrOfPeer };
let external_addr_expired = quote! { #prelude_path::ExternalAddrExpired };
let external_addr_confirmed = quote! { #prelude_path::ExternalAddrConfirmed };
let listener_error = quote! { #prelude_path::ListenerError };
Expand Down Expand Up @@ -463,6 +464,27 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> syn::Result<Toke
})
};

// Build the list of statements to put in the body of `on_swarm_event()`
// for the `FromSwarm::NewExternalAddrOfPeer` variant.
let on_new_external_addr_of_peer_stmts = {
data_struct
.fields
.iter()
.enumerate()
.map(|(field_n, field)| match field.ident {
Some(ref i) => quote! {
self.#i.on_swarm_event(#from_swarm::NewExternalAddrOfPeer(#new_external_addr_of_peer {
addr,
}));
},
None => quote! {
self.#field_n.on_swarm_event(#from_swarm::NewExternalAddrOfPeer(#new_external_addr_of_peer {
addr,
}));
},
})
};

// Build the list of statements to put in the body of `on_swarm_event()`
// for the `FromSwarm::ExternalAddrExpired` variant.
let on_external_addr_expired_stmts = {
Expand Down Expand Up @@ -757,6 +779,9 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> syn::Result<Toke
std::task::Poll::Ready(#network_behaviour_action::CloseConnection { peer_id, connection }) => {
return std::task::Poll::Ready(#network_behaviour_action::CloseConnection { peer_id, connection });
}
std::task::Poll::Ready(#network_behaviour_action::NewExternalAddrOfPeer(addr)) => {
return std::task::Poll::Ready(#network_behaviour_action::NewExternalAddrOfPeer(addr));
}
std::task::Poll::Pending => {},
}
}
Expand Down Expand Up @@ -883,6 +908,9 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> syn::Result<Toke
#from_swarm::ListenerClosed(
#listener_closed { listener_id, reason })
=> { #(#on_listener_closed_stmts)* }
#from_swarm::NewExternalAddrOfPeer(
#new_external_addr_of_peer {addr})
=> { #(#on_new_external_addr_of_peer_stmts)* }
_ => {}
}
}
Expand Down
15 changes: 15 additions & 0 deletions swarm/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ pub enum ToSwarm<TOutEvent, TInEvent> {
/// Whether to close a specific or all connections to the given peer.
connection: CloseConnection,
},

/// TODO: more documentation needed
/// Reports external address of a remote peer to other `NetworkBehaviours`
NewExternalAddrOfPeer(Multiaddr),
}

impl<TOutEvent, TInEventOld> ToSwarm<TOutEvent, TInEventOld> {
Expand Down Expand Up @@ -352,6 +356,7 @@ impl<TOutEvent, TInEventOld> ToSwarm<TOutEvent, TInEventOld> {
ToSwarm::NewExternalAddrCandidate(addr) => ToSwarm::NewExternalAddrCandidate(addr),
ToSwarm::ExternalAddrConfirmed(addr) => ToSwarm::ExternalAddrConfirmed(addr),
ToSwarm::ExternalAddrExpired(addr) => ToSwarm::ExternalAddrExpired(addr),
ToSwarm::NewExternalAddrOfPeer(addr) => ToSwarm::NewExternalAddrOfPeer(addr),
}
}
}
Expand Down Expand Up @@ -383,6 +388,7 @@ impl<TOutEvent, THandlerIn> ToSwarm<TOutEvent, THandlerIn> {
peer_id,
connection,
},
ToSwarm::NewExternalAddrOfPeer(addr) => ToSwarm::NewExternalAddrOfPeer(addr),
}
}
}
Expand Down Expand Up @@ -447,6 +453,8 @@ pub enum FromSwarm<'a, Handler> {
ExternalAddrConfirmed(ExternalAddrConfirmed<'a>),
/// Informs the behaviour that an external address of the local node expired, i.e. is no-longer confirmed.
ExternalAddrExpired(ExternalAddrExpired<'a>),
/// TODO: document it
NewExternalAddrOfPeer(NewExternalAddrOfPeer<'a>),
}

/// [`FromSwarm`] variant that informs the behaviour about a newly established connection to a peer.
Expand Down Expand Up @@ -559,6 +567,12 @@ pub struct ExternalAddrExpired<'a> {
pub addr: &'a Multiaddr,
}

/// [`FromSwarm`] variant that informs the behaviour that a new external address was detected.
#[derive(Clone, Copy)]
pub struct NewExternalAddrOfPeer<'a> {
pub addr: &'a Multiaddr,
}

impl<'a, Handler> FromSwarm<'a, Handler> {
fn map_handler<NewHandler>(
self,
Expand Down Expand Up @@ -658,6 +672,7 @@ impl<'a, Handler> FromSwarm<'a, Handler> {
FromSwarm::NewExternalAddrCandidate(e) => Some(FromSwarm::NewExternalAddrCandidate(e)),
FromSwarm::ExternalAddrExpired(e) => Some(FromSwarm::ExternalAddrExpired(e)),
FromSwarm::ExternalAddrConfirmed(e) => Some(FromSwarm::ExternalAddrConfirmed(e)),
FromSwarm::NewExternalAddrOfPeer(e) => Some(FromSwarm::NewExternalAddrOfPeer(e)),
}
}
}
3 changes: 2 additions & 1 deletion swarm/src/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ impl NetworkBehaviour for Behaviour {
| FromSwarm::ListenerClosed(_)
| FromSwarm::NewExternalAddrCandidate(_)
| FromSwarm::ExternalAddrExpired(_)
| FromSwarm::ExternalAddrConfirmed(_) => {}
| FromSwarm::ExternalAddrConfirmed(_)
| FromSwarm::NewExternalAddrOfPeer(_) => {}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion swarm/src/keep_alive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ impl NetworkBehaviour for Behaviour {
| FromSwarm::ListenerClosed(_)
| FromSwarm::NewExternalAddrCandidate(_)
| FromSwarm::ExternalAddrExpired(_)
| FromSwarm::ExternalAddrConfirmed(_) => {}
| FromSwarm::ExternalAddrConfirmed(_)
| FromSwarm::NewExternalAddrOfPeer(_) => {}
}
}
}
Expand Down
32 changes: 32 additions & 0 deletions swarm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pub mod derive_prelude {
pub use crate::behaviour::ListenerClosed;
pub use crate::behaviour::ListenerError;
pub use crate::behaviour::NewExternalAddrCandidate;
pub use crate::behaviour::NewExternalAddrOfPeer;
pub use crate::behaviour::NewListenAddr;
pub use crate::behaviour::NewListener;
pub use crate::connection::ConnectionId;
Expand Down Expand Up @@ -1112,6 +1113,37 @@ where
self.pool.disconnect(peer_id);
}
},
ToSwarm::NewExternalAddrOfPeer(addr) => {
log::debug!("ToSwarm::NewExternalAddrOfPeer");

let translated_addresses = {
let mut addrs: Vec<_> = self
.listened_addrs
.values()
.flatten()
.filter_map(|server| self.transport.address_translation(server, &addr))
.collect();

// remove duplicates
addrs.sort_unstable();
addrs.dedup();
addrs
};

if translated_addresses.is_empty() {
self.behaviour
.on_swarm_event(FromSwarm::NewExternalAddrCandidate(
NewExternalAddrCandidate { addr: &addr },
));
} else {
for addr in translated_addresses {
self.behaviour
.on_swarm_event(FromSwarm::NewExternalAddrCandidate(
NewExternalAddrCandidate { addr: &addr },
));
}
}
}
}

None
Expand Down
Loading

0 comments on commit 711b03b

Please sign in to comment.