From 1704ee9612e207a07cacb8cd36e7134208776475 Mon Sep 17 00:00:00 2001 From: StemCll Date: Tue, 28 Nov 2023 19:21:04 +0100 Subject: [PATCH] swarm: Add NewExternalAddrOfPeer --- Cargo.lock | 4 ++-- Cargo.toml | 4 ++-- protocols/rendezvous/src/server.rs | 1 + swarm-derive/CHANGELOG.md | 5 +++++ swarm-derive/Cargo.toml | 2 +- swarm-derive/src/lib.rs | 25 +++++++++++++++++++++++++ swarm/CHANGELOG.md | 5 +++++ swarm/Cargo.toml | 2 +- swarm/src/behaviour.rs | 18 ++++++++++++++++++ swarm/src/lib.rs | 12 ++++++++++-- 10 files changed, 70 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e91391a515ab..aa0db8e77d66 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3106,7 +3106,7 @@ dependencies = [ [[package]] name = "libp2p-swarm" -version = "0.44.0" +version = "0.44.1" dependencies = [ "async-std", "either", @@ -3139,7 +3139,7 @@ dependencies = [ [[package]] name = "libp2p-swarm-derive" -version = "0.34.0" +version = "0.35.0" dependencies = [ "heck", "proc-macro2", diff --git a/Cargo.toml b/Cargo.toml index 346b316d4dc6..de6a3db8b741 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/protocols/rendezvous/src/server.rs b/protocols/rendezvous/src/server.rs index 78aa42043cd6..525e2a73d8e9 100644 --- a/protocols/rendezvous/src/server.rs +++ b/protocols/rendezvous/src/server.rs @@ -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")); diff --git a/swarm-derive/CHANGELOG.md b/swarm-derive/CHANGELOG.md index 08adba00cdb5..fa477df1307a 100644 --- a/swarm-derive/CHANGELOG.md +++ b/swarm-derive/CHANGELOG.md @@ -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`. diff --git a/swarm-derive/Cargo.toml b/swarm-derive/Cargo.toml index 11cc4b6a12a8..31d4e6dbc610 100644 --- a/swarm-derive/Cargo.toml +++ b/swarm-derive/Cargo.toml @@ -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 "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/swarm-derive/src/lib.rs b/swarm-derive/src/lib.rs index 514975390b06..c37ad5607053 100644 --- a/swarm-derive/src/lib.rs +++ b/swarm-derive/src/lib.rs @@ -80,6 +80,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> syn::Result syn::Result 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 = { @@ -832,6 +854,9 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> syn::Result { #(#on_listener_closed_stmts)* } + #from_swarm::NewExternalAddrOfPeer( + #new_external_addr_of_peer {addr, peer_id}) + => { #(#on_new_external_addr_of_peer_stmts)* } _ => {} } } diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index 48cafee6ced4..3b8f4444d061 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -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`. diff --git a/swarm/Cargo.toml b/swarm/Cargo.toml index 3b706df6d2b9..817e4b4855e9 100644 --- a/swarm/Cargo.toml +++ b/swarm/Cargo.toml @@ -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 "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/swarm/src/behaviour.rs b/swarm/src/behaviour.rs index c25b14e75e32..77a2b85d88de 100644 --- a/swarm/src/behaviour.rs +++ b/swarm/src/behaviour.rs @@ -303,6 +303,9 @@ pub enum ToSwarm { /// 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 ToSwarm { @@ -335,6 +338,9 @@ impl ToSwarm { 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 } + } } } } @@ -366,6 +372,9 @@ impl ToSwarm { peer_id, connection, }, + ToSwarm::NewExternalAddrOfPeer { addr, peer_id } => { + ToSwarm::NewExternalAddrOfPeer { addr, peer_id } + } } } } @@ -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. @@ -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, +} diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index 0354f39cfdcd..d20e07a40d47 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -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; @@ -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}; @@ -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, + })) + } } }