From 469bb8b326b59492742abf090bc51d9739ec71bf Mon Sep 17 00:00:00 2001 From: Benno Date: Thu, 26 Oct 2023 19:07:30 +0200 Subject: [PATCH] fix(request-response): don't keep duplicate addresses Backport of #4700 on `v0.52` branch. - fix: deduplicate autonat add_server addresses - keep peers in HashSet instead of SmallVec - change (&HashSet)::into_iter to equivalent iter Pull-Request: #4724. --- Cargo.lock | 2 +- Cargo.toml | 2 +- protocols/request-response/CHANGELOG.md | 5 +++++ protocols/request-response/Cargo.toml | 2 +- protocols/request-response/src/lib.rs | 6 +++--- 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8c80f0b50a0..42a9aad0f96 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2992,7 +2992,7 @@ dependencies = [ [[package]] name = "libp2p-request-response" -version = "0.25.2" +version = "0.25.3" dependencies = [ "async-std", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index 26387938e75..5ed8de7738e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -99,7 +99,7 @@ libp2p-quic = { version = "0.9.3", path = "transports/quic" } libp2p-relay = { version = "0.16.2", path = "protocols/relay" } libp2p-rendezvous = { version = "0.13.1", path = "protocols/rendezvous" } libp2p-upnp = { version = "0.1.1", path = "protocols/upnp" } -libp2p-request-response = { version = "0.25.2", path = "protocols/request-response" } +libp2p-request-response = { version = "0.25.3", path = "protocols/request-response" } libp2p-server = { version = "0.12.3", path = "misc/server" } libp2p-swarm = { version = "0.43.7", path = "swarm" } libp2p-swarm-derive = { version = "0.33.0", path = "swarm-derive" } diff --git a/protocols/request-response/CHANGELOG.md b/protocols/request-response/CHANGELOG.md index 73f3918a6d8..42df297795b 100644 --- a/protocols/request-response/CHANGELOG.md +++ b/protocols/request-response/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.25.3 + +- Keep peer addresses in `HashSet` instead of `SmallVec` to prevent adding duplicate addresses. + See [PR 4724](https://github.com/libp2p/rust-libp2p/pull/4724). + ## 0.25.2 - Deprecate `request_response::Config::set_connection_keep_alive` in favor of `SwarmBuilder::idle_connection_timeout`. diff --git a/protocols/request-response/Cargo.toml b/protocols/request-response/Cargo.toml index ee1049a96e3..845772ec40b 100644 --- a/protocols/request-response/Cargo.toml +++ b/protocols/request-response/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-request-response" edition = "2021" rust-version = { workspace = true } description = "Generic Request/Response Protocols" -version = "0.25.2" +version = "0.25.3" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/protocols/request-response/src/lib.rs b/protocols/request-response/src/lib.rs index f04755ebb0d..fff8dace401 100644 --- a/protocols/request-response/src/lib.rs +++ b/protocols/request-response/src/lib.rs @@ -337,7 +337,7 @@ where /// reachable addresses, if any. connected: HashMap>, /// Externally managed addresses via `add_address` and `remove_address`. - addresses: HashMap>, + addresses: HashMap>, /// Requests that have not yet been sent and are waiting for a connection /// to be established. pending_outbound_requests: HashMap; 10]>>, @@ -449,7 +449,7 @@ where /// /// Addresses added in this way are only removed by `remove_address`. pub fn add_address(&mut self, peer: &PeerId, address: Multiaddr) { - self.addresses.entry(*peer).or_default().push(address); + self.addresses.entry(*peer).or_default().insert(address); } /// Removes an address of a peer previously added via `add_address`. @@ -743,7 +743,7 @@ where addresses.extend(connections.iter().filter_map(|c| c.remote_address.clone())) } if let Some(more) = self.addresses.get(&peer) { - addresses.extend(more.into_iter().cloned()); + addresses.extend(more.iter().cloned()); } Ok(addresses)