Skip to content

Commit

Permalink
swarm: Add NewExternalAddrOfPeer
Browse files Browse the repository at this point in the history
  • Loading branch information
StemCll committed Nov 28, 2023
1 parent 4d7a535 commit 1704ee9
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ libp2p-relay = { version = "0.17.1", path = "protocols/relay" }
libp2p-rendezvous = { version = "0.14.0", path = "protocols/rendezvous" }
libp2p-request-response = { version = "0.26.0", path = "protocols/request-response" }
libp2p-server = { version = "0.12.4", path = "misc/server" }
libp2p-swarm = { version = "0.44.0", path = "swarm" }
libp2p-swarm-derive = { version = "=0.34.0", path = "swarm-derive" } # `libp2p-swarm-derive` may not be compatible with different `libp2p-swarm` non-breaking releases. E.g. `libp2p-swarm` might introduce a new enum variant `FromSwarm` (which is `#[non-exhaustive]`) in a non-breaking release. Older versions of `libp2p-swarm-derive` would not forward this enum variant within the `NetworkBehaviour` hierarchy. Thus the version pinning is required.
libp2p-swarm = { version = "0.44.1", path = "swarm" }
libp2p-swarm-derive = { version = "=0.35.0", path = "swarm-derive" } # `libp2p-swarm-derive` may not be compatible with different `libp2p-swarm` non-breaking releases. E.g. `libp2p-swarm` might introduce a new enum variant `FromSwarm` (which is `#[non-exhaustive]`) in a non-breaking release. Older versions of `libp2p-swarm-derive` would not forward this enum variant within the `NetworkBehaviour` hierarchy. Thus the version pinning is required.
libp2p-swarm-test = { version = "0.3.0", path = "swarm-test" }
libp2p-tcp = { version = "0.41.0", path = "transports/tcp" }
libp2p-tls = { version = "0.3.0", path = "transports/tls" }
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 @@ -222,6 +222,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
5 changes: 5 additions & 0 deletions swarm-derive/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.35.0 - unreleased

- Generate code for `libp2p-swarm`'s `from_swarm::NewExternalAddrOfPeer` enum variant.
See [PR 4371](https://github.com/libp2p/rust-libp2p/pull/4371)

## 0.34.0

- Adapt to interface changes in `libp2p-swarm`.
Expand Down
2 changes: 1 addition & 1 deletion swarm-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "libp2p-swarm-derive"
edition = "2021"
rust-version = { workspace = true }
description = "Procedural macros of libp2p-swarm"
version = "0.34.0"
version = "0.35.0"
authors = ["Parity Technologies <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
25 changes: 25 additions & 0 deletions swarm-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,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 @@ -447,6 +448,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, peer_id
}));
},
None => quote! {
self.#field_n.on_swarm_event(#from_swarm::NewExternalAddrOfPeer(#new_external_addr_of_peer {
addr, peer_id
}));
},
})
};

// 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 @@ -832,6 +854,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, peer_id})
=> { #(#on_new_external_addr_of_peer_stmts)* }
_ => {}
}
}
Expand Down
5 changes: 5 additions & 0 deletions swarm/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.44.1 - unreleased

- Add `FromSwarm::NewExternalAddrOfPeer` event.
See [PR 4371](https://github.com/libp2p/rust-libp2p/pull/4371)

## 0.44.0

- Add `#[non_exhaustive]` to `FromSwarm`, `ToSwarm`, `SwarmEvent`, `ConnectionHandlerEvent`, `ConnectionEvent`.
Expand Down
2 changes: 1 addition & 1 deletion swarm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "libp2p-swarm"
edition = "2021"
rust-version = { workspace = true }
description = "The libp2p swarm"
version = "0.44.0"
version = "0.44.1"
authors = ["Parity Technologies <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
18 changes: 18 additions & 0 deletions swarm/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ pub enum ToSwarm<TOutEvent, TInEvent> {
/// Whether to close a specific or all connections to the given peer.
connection: CloseConnection,
},

/// Reports external address of a remote peer to the [`Swarm`](crate::Swarm) and through that to other [`NetworkBehaviour`]s.
NewExternalAddrOfPeer { peer_id: PeerId, addr: Multiaddr },
}

impl<TOutEvent, TInEventOld> ToSwarm<TOutEvent, TInEventOld> {
Expand Down Expand Up @@ -335,6 +338,9 @@ 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, peer_id } => {
ToSwarm::NewExternalAddrOfPeer { addr, peer_id }
}
}
}
}
Expand Down Expand Up @@ -366,6 +372,9 @@ impl<TOutEvent, THandlerIn> ToSwarm<TOutEvent, THandlerIn> {
peer_id,
connection,
},
ToSwarm::NewExternalAddrOfPeer { addr, peer_id } => {
ToSwarm::NewExternalAddrOfPeer { addr, peer_id }
}
}
}
}
Expand Down Expand Up @@ -432,6 +441,8 @@ pub enum FromSwarm<'a> {
ExternalAddrConfirmed(ExternalAddrConfirmed<'a>),
/// Informs the behaviour that an external address of the local node expired, i.e. is no-longer confirmed.
ExternalAddrExpired(ExternalAddrExpired<'a>),
/// Informs the behaviour that we have discovered a new external address for a remote peer.
NewExternalAddrOfPeer(NewExternalAddrOfPeer<'a>),
}

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

/// [`FromSwarm`] variant that informs the behaviour that a new external address for a remote peer was detected.
#[derive(Clone, Copy, Debug)]
pub struct NewExternalAddrOfPeer<'a> {
pub peer_id: PeerId,
pub addr: &'a Multiaddr,
}
12 changes: 10 additions & 2 deletions swarm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,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 All @@ -108,8 +109,8 @@ pub mod derive_prelude {
pub use behaviour::{
AddressChange, CloseConnection, ConnectionClosed, DialFailure, ExpiredListenAddr,
ExternalAddrExpired, ExternalAddresses, FromSwarm, ListenAddresses, ListenFailure,
ListenerClosed, ListenerError, NetworkBehaviour, NewExternalAddrCandidate, NewListenAddr,
NotifyHandler, ToSwarm,
ListenerClosed, ListenerError, NetworkBehaviour, NewExternalAddrCandidate,
NewExternalAddrOfPeer, NewListenAddr, NotifyHandler, ToSwarm,
};
pub use connection::pool::ConnectionCounters;
pub use connection::{ConnectionError, ConnectionId, SupportedProtocols};
Expand Down Expand Up @@ -1176,6 +1177,13 @@ where
self.pool.disconnect(peer_id);
}
},
ToSwarm::NewExternalAddrOfPeer { addr, peer_id } => {
self.behaviour
.on_swarm_event(FromSwarm::NewExternalAddrOfPeer(NewExternalAddrOfPeer {
peer_id,
addr: &addr,
}))
}
}
}

Expand Down

0 comments on commit 1704ee9

Please sign in to comment.