From da4e971c7e6169ad438782f7675add75a91845cf Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Fri, 17 Nov 2023 12:56:50 +0200 Subject: [PATCH] fix(swarm): do not generate `NewExternalAddrCandidate` events if there are no listeners --- Cargo.lock | 2 +- Cargo.toml | 2 +- swarm/CHANGELOG.md | 5 +++++ swarm/Cargo.toml | 2 +- swarm/src/lib.rs | 6 ++---- 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 332764c36f30..3128321b4955 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3095,7 +3095,7 @@ dependencies = [ [[package]] name = "libp2p-swarm" -version = "0.44.0" +version = "0.44.1" dependencies = [ "async-std", "either", diff --git a/Cargo.toml b/Cargo.toml index 35439a1a696c..519dfae4df62 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -99,7 +99,7 @@ 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 = { version = "0.44.1", 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-test = { version = "0.3.0", path = "swarm-test" } libp2p-tcp = { version = "0.41.0", path = "transports/tcp" } diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index 48cafee6ced4..96bee29d2df6 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.44.1 + +- Do not generate `NewExternalAddrCandidate` events if there are no listeners. + See [PR 4886](https://github.com/libp2p/rust-libp2p/pull/4886). + ## 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/lib.rs b/swarm/src/lib.rs index 0354f39cfdcd..53f4d1480158 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -1122,9 +1122,7 @@ where // For TCP without port-reuse, the observed address contains an ephemeral port which needs to be replaced by the port of a listen address. let translated_addresses = { let mut addrs: Vec<_> = self - .listened_addrs - .values() - .flatten() + .listeners() .filter_map(|server| self.transport.address_translation(server, &addr)) .collect(); @@ -1142,7 +1140,7 @@ where )); self.pending_swarm_events .push_back(SwarmEvent::NewExternalAddrCandidate { address: addr }); - } else { + } else if self.listeners().next().is_some() { for addr in translated_addresses { self.behaviour .on_swarm_event(FromSwarm::NewExternalAddrCandidate(