From e5435fc80638902b448dcbc5d9795091ed86064a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20RIBEAU?=
 <francois.ribeau.external@stormshield.eu>
Date: Wed, 28 Aug 2024 16:23:19 +0200
Subject: [PATCH 01/13] feat(swarm): improve `PeerAddresses` configurability

---
 Cargo.lock                            |  4 +--
 Cargo.toml                            |  4 +--
 protocols/identify/CHANGELOG.md       |  5 ++++
 protocols/identify/Cargo.toml         |  2 +-
 protocols/identify/src/behaviour.rs   | 35 ++++++++--------------
 protocols/identify/tests/smoke.rs     |  8 +++--
 swarm/CHANGELOG.md                    |  5 ++++
 swarm/Cargo.toml                      |  2 +-
 swarm/src/behaviour.rs                |  2 +-
 swarm/src/behaviour/peer_addresses.rs | 43 ++++++++++++++++++++-------
 swarm/src/lib.rs                      |  3 +-
 11 files changed, 71 insertions(+), 42 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 7f37b652c30..0741177a6bd 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2875,7 +2875,7 @@ dependencies = [
 
 [[package]]
 name = "libp2p-identify"
-version = "0.45.0"
+version = "0.46.0"
 dependencies = [
  "async-std",
  "asynchronous-codec",
@@ -3330,7 +3330,7 @@ dependencies = [
 
 [[package]]
 name = "libp2p-swarm"
-version = "0.45.1"
+version = "0.45.2"
 dependencies = [
  "async-std",
  "criterion",
diff --git a/Cargo.toml b/Cargo.toml
index 8d63ac3ee1e..553f8658836 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -84,7 +84,7 @@ libp2p-dcutr = { version = "0.12.0", path = "protocols/dcutr" }
 libp2p-dns = { version = "0.42.0", path = "transports/dns" }
 libp2p-floodsub = { version = "0.45.0", path = "protocols/floodsub" }
 libp2p-gossipsub = { version = "0.47.0", path = "protocols/gossipsub" }
-libp2p-identify = { version = "0.45.0", path = "protocols/identify" }
+libp2p-identify = { version = "0.46.0", path = "protocols/identify" }
 libp2p-identity = { version = "0.2.9" }
 libp2p-kad = { version = "0.47.0", path = "protocols/kad" }
 libp2p-mdns = { version = "0.46.0", path = "protocols/mdns" }
@@ -102,7 +102,7 @@ libp2p-rendezvous = { version = "0.15.0", path = "protocols/rendezvous" }
 libp2p-request-response = { version = "0.27.0", path = "protocols/request-response" }
 libp2p-server = { version = "0.12.7", path = "misc/server" }
 libp2p-stream = { version = "0.2.0-alpha", path = "protocols/stream" }
-libp2p-swarm = { version = "0.45.1", path = "swarm" }
+libp2p-swarm = { version = "0.45.2", 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.4.0", path = "swarm-test" }
 libp2p-tcp = { version = "0.42.0", path = "transports/tcp" }
diff --git a/protocols/identify/CHANGELOG.md b/protocols/identify/CHANGELOG.md
index 275f7114b28..481c05c8a05 100644
--- a/protocols/identify/CHANGELOG.md
+++ b/protocols/identify/CHANGELOG.md
@@ -1,3 +1,8 @@
+## 0.46.0
+
+- Update to the new `PeerAddresses` API with `PeerAddressesConfig`, changing `with_cache_size` to `with_cache_config`.
+  See [PR 5574](https://github.com/libp2p/rust-libp2p/pull/5574).
+
 ## 0.45.0
 
 - Address translation is moved here from `libp2p-core`.
diff --git a/protocols/identify/Cargo.toml b/protocols/identify/Cargo.toml
index cdc5ce587de..13c43b6a71f 100644
--- a/protocols/identify/Cargo.toml
+++ b/protocols/identify/Cargo.toml
@@ -3,7 +3,7 @@ name = "libp2p-identify"
 edition = "2021"
 rust-version = { workspace = true }
 description = "Nodes identification protocol for libp2p"
-version = "0.45.0"
+version = "0.46.0"
 authors = ["Parity Technologies <admin@parity.io>"]
 license = "MIT"
 repository = "https://github.com/libp2p/rust-libp2p"
diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs
index 6590ccd6588..79ed2b9c07d 100644
--- a/protocols/identify/src/behaviour.rs
+++ b/protocols/identify/src/behaviour.rs
@@ -28,13 +28,12 @@ use libp2p_identity::PublicKey;
 use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm};
 use libp2p_swarm::{
     ConnectionDenied, DialError, ExternalAddresses, ListenAddresses, NetworkBehaviour,
-    NotifyHandler, PeerAddresses, StreamUpgradeError, THandlerInEvent, ToSwarm,
-    _address_translation,
+    NotifyHandler, PeerAddresses, PeerAddressesConfig, StreamUpgradeError, THandlerInEvent,
+    ToSwarm, _address_translation,
 };
 use libp2p_swarm::{ConnectionId, THandler, THandlerOutEvent};
 
 use std::collections::hash_map::Entry;
-use std::num::NonZeroUsize;
 use std::{
     collections::{HashMap, HashSet, VecDeque},
     task::Context,
@@ -142,11 +141,8 @@ pub struct Config {
     /// Disabled by default.
     pub push_listen_addr_updates: bool,
 
-    /// How many entries of discovered peers to keep before we discard
-    /// the least-recently used one.
-    ///
-    /// Disabled by default.
-    pub cache_size: usize,
+    /// Configuration for the LRU cache of discovered peers.
+    pub cache_config: Option<PeerAddressesConfig>,
 }
 
 impl Config {
@@ -159,7 +155,7 @@ impl Config {
             local_public_key,
             interval: Duration::from_secs(5 * 60),
             push_listen_addr_updates: false,
-            cache_size: 100,
+            cache_config: Some(Default::default()),
         }
     }
 
@@ -184,9 +180,11 @@ impl Config {
         self
     }
 
-    /// Configures the size of the LRU cache, caching addresses of discovered peers.
-    pub fn with_cache_size(mut self, cache_size: usize) -> Self {
-        self.cache_size = cache_size;
+    /// Configuration for the LRU cache responsible for caching addresses of discovered peers.
+    ///
+    /// If set to [`None`], caching is disabled.
+    pub fn with_cache_config(mut self, cache_config: Option<PeerAddressesConfig>) -> Self {
+        self.cache_config = cache_config;
         self
     }
 }
@@ -194,10 +192,7 @@ impl Config {
 impl Behaviour {
     /// Creates a new identify [`Behaviour`].
     pub fn new(config: Config) -> Self {
-        let discovered_peers = match NonZeroUsize::new(config.cache_size) {
-            None => PeerCache::disabled(),
-            Some(size) => PeerCache::enabled(size),
-        };
+        let discovered_peers = PeerCache::new(config.cache_config.clone());
 
         Self {
             config,
@@ -602,12 +597,8 @@ fn multiaddr_matches_peer_id(addr: &Multiaddr, peer_id: &PeerId) -> bool {
 struct PeerCache(Option<PeerAddresses>);
 
 impl PeerCache {
-    fn disabled() -> Self {
-        Self(None)
-    }
-
-    fn enabled(size: NonZeroUsize) -> Self {
-        Self(Some(PeerAddresses::new(size)))
+    fn new(cache_config: Option<PeerAddressesConfig>) -> Self {
+        Self(cache_config.map(PeerAddresses::new))
     }
 
     fn get(&mut self, peer: &PeerId) -> Vec<Multiaddr> {
diff --git a/protocols/identify/tests/smoke.rs b/protocols/identify/tests/smoke.rs
index 49ae9f0726f..3a791d69f94 100644
--- a/protocols/identify/tests/smoke.rs
+++ b/protocols/identify/tests/smoke.rs
@@ -1,9 +1,10 @@
 use futures::StreamExt;
 use libp2p_identify as identify;
-use libp2p_swarm::{Swarm, SwarmEvent};
+use libp2p_swarm::{PeerAddressesConfig, Swarm, SwarmEvent};
 use libp2p_swarm_test::SwarmExt;
 use std::collections::HashSet;
 use std::iter;
+use std::num::NonZeroUsize;
 use std::time::{Duration, Instant};
 use tracing_subscriber::EnvFilter;
 
@@ -161,7 +162,10 @@ async fn emits_unique_listen_addresses() {
             identify::Config::new("a".to_string(), identity.public())
                 .with_agent_version("b".to_string())
                 .with_interval(Duration::from_secs(1))
-                .with_cache_size(10),
+                .with_cache_config(Some(PeerAddressesConfig {
+                    number_of_peers: NonZeroUsize::new(10).expect("10 != 0"),
+                    ..Default::default()
+                })),
         )
     });
     let mut swarm2 = Swarm::new_ephemeral(|identity| {
diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md
index e7931a60de2..1bf4c1f4cbb 100644
--- a/swarm/CHANGELOG.md
+++ b/swarm/CHANGELOG.md
@@ -1,3 +1,8 @@
+## 0.45.2
+
+- Add `PeerAddressesConfig` and the possibility to configure the number of addresses cached per peer.
+  See [PR 5574](https://github.com/libp2p/rust-libp2p/pull/5574).
+
 ## 0.45.1
 
 - Update `libp2p-swarm-derive` to version `0.35.0`, see [PR 5545]
diff --git a/swarm/Cargo.toml b/swarm/Cargo.toml
index 3d0b1a84eee..cdee67f3fb3 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.45.1"
+version = "0.45.2"
 authors = ["Parity Technologies <admin@parity.io>"]
 license = "MIT"
 repository = "https://github.com/libp2p/rust-libp2p"
diff --git a/swarm/src/behaviour.rs b/swarm/src/behaviour.rs
index 35aed12fba5..1e0f2354c9b 100644
--- a/swarm/src/behaviour.rs
+++ b/swarm/src/behaviour.rs
@@ -26,7 +26,7 @@ pub mod toggle;
 
 pub use external_addresses::ExternalAddresses;
 pub use listen_addresses::ListenAddresses;
-pub use peer_addresses::PeerAddresses;
+pub use peer_addresses::{PeerAddresses, PeerAddressesConfig};
 
 use crate::connection::ConnectionId;
 use crate::dial_opts::DialOpts;
diff --git a/swarm/src/behaviour/peer_addresses.rs b/swarm/src/behaviour/peer_addresses.rs
index 1eeead56ca1..226e0914c1c 100644
--- a/swarm/src/behaviour/peer_addresses.rs
+++ b/swarm/src/behaviour/peer_addresses.rs
@@ -8,16 +8,39 @@ use lru::LruCache;
 
 use std::num::NonZeroUsize;
 
+#[derive(Debug, Clone)]
+/// Configuration of a [`PeerAddresses`] instance.
+pub struct PeerAddressesConfig {
+    /// Capacity of the [`PeerAddresses`] cache.
+    pub number_of_peers: NonZeroUsize,
+
+    /// Maximum number of cached addresses per peer.
+    pub number_of_addresses_by_peer: NonZeroUsize,
+}
+
+impl Default for PeerAddressesConfig {
+    fn default() -> Self {
+        Self {
+            number_of_peers: NonZeroUsize::new(100).expect("100 != 0"),
+            number_of_addresses_by_peer: NonZeroUsize::new(10).expect("10 != 0"),
+        }
+    }
+}
+
 /// Struct for tracking peers' external addresses of the [`Swarm`](crate::Swarm).
 #[derive(Debug)]
-pub struct PeerAddresses(LruCache<PeerId, LruCache<Multiaddr, ()>>);
+pub struct PeerAddresses {
+    config: PeerAddressesConfig,
+    inner: LruCache<PeerId, LruCache<Multiaddr, ()>>,
+}
 
 impl PeerAddresses {
     /// Creates a [`PeerAddresses`] cache with capacity for the given number of peers.
     ///
-    /// For each peer, we will at most store 10 addresses.
-    pub fn new(number_of_peers: NonZeroUsize) -> Self {
-        Self(LruCache::new(number_of_peers))
+    /// For each peer, we will at most store `config.number_of_addresses_by_peer` addresses.
+    pub fn new(config: PeerAddressesConfig) -> Self {
+        let inner = LruCache::new(config.number_of_peers);
+        Self { config, inner }
     }
 
     /// Feed a [`FromSwarm`] event to this struct.
@@ -50,12 +73,12 @@ impl PeerAddresses {
     pub fn add(&mut self, peer: PeerId, address: Multiaddr) -> bool {
         match prepare_addr(&peer, &address) {
             Ok(address) => {
-                if let Some(cached) = self.0.get_mut(&peer) {
+                if let Some(cached) = self.inner.get_mut(&peer) {
                     cached.put(address, ()).is_none()
                 } else {
-                    let mut set = LruCache::new(NonZeroUsize::new(10).expect("10 > 0"));
+                    let mut set = LruCache::new(self.config.number_of_addresses_by_peer);
                     set.put(address, ());
-                    self.0.put(peer, set);
+                    self.inner.put(peer, set);
 
                     true
                 }
@@ -66,7 +89,7 @@ impl PeerAddresses {
 
     /// Returns peer's external addresses.
     pub fn get(&mut self, peer: &PeerId) -> impl Iterator<Item = Multiaddr> + '_ {
-        self.0
+        self.inner
             .get(peer)
             .into_iter()
             .flat_map(|c| c.iter().map(|(m, ())| m))
@@ -76,7 +99,7 @@ impl PeerAddresses {
     /// Removes address from peer addresses cache.
     /// Returns true if the address was removed.
     pub fn remove(&mut self, peer: &PeerId, address: &Multiaddr) -> bool {
-        match self.0.get_mut(peer) {
+        match self.inner.get_mut(peer) {
             Some(addrs) => match prepare_addr(peer, address) {
                 Ok(address) => addrs.pop(&address).is_some(),
                 Err(_) => false,
@@ -92,7 +115,7 @@ fn prepare_addr(peer: &PeerId, addr: &Multiaddr) -> Result<Multiaddr, Multiaddr>
 
 impl Default for PeerAddresses {
     fn default() -> Self {
-        Self(LruCache::new(NonZeroUsize::new(100).unwrap()))
+        Self::new(Default::default())
     }
 }
 
diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs
index 81b1ca1a68d..360e71ebf71 100644
--- a/swarm/src/lib.rs
+++ b/swarm/src/lib.rs
@@ -111,7 +111,8 @@ pub use behaviour::{
     AddressChange, CloseConnection, ConnectionClosed, DialFailure, ExpiredListenAddr,
     ExternalAddrExpired, ExternalAddresses, FromSwarm, ListenAddresses, ListenFailure,
     ListenerClosed, ListenerError, NetworkBehaviour, NewExternalAddrCandidate,
-    NewExternalAddrOfPeer, NewListenAddr, NotifyHandler, PeerAddresses, ToSwarm,
+    NewExternalAddrOfPeer, NewListenAddr, NotifyHandler, PeerAddresses, PeerAddressesConfig,
+    ToSwarm,
 };
 pub use connection::pool::ConnectionCounters;
 pub use connection::{ConnectionError, ConnectionId, SupportedProtocols};

From 3baf1ecf3b0e32af8a9d69613c9652b922716263 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20RIBEAU?=
 <francois.ribeau.external@stormshield.eu>
Date: Thu, 29 Aug 2024 09:27:04 +0200
Subject: [PATCH 02/13] trigger GitHub actions


From 7ddd8548c78fb1b5a9e7a143bf6696832e9fe43a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20RIBEAU?=
 <francois.ribeau.external@stormshield.eu>
Date: Fri, 6 Sep 2024 09:55:19 +0200
Subject: [PATCH 03/13] deprecate with_cache_size instead of removing it

---
 Cargo.lock                            |  2 +-
 Cargo.toml                            |  2 +-
 protocols/identify/CHANGELOG.md       |  8 +++-----
 protocols/identify/Cargo.toml         |  2 +-
 protocols/identify/src/behaviour.rs   | 18 ++++++++++++++++++
 protocols/identify/tests/smoke.rs     | 13 ++++++++-----
 swarm/src/behaviour/peer_addresses.rs | 23 ++++++++++++++++++++---
 7 files changed, 52 insertions(+), 16 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 1df8789216b..bec8543559a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2874,7 +2874,7 @@ dependencies = [
 
 [[package]]
 name = "libp2p-identify"
-version = "0.46.0"
+version = "0.45.1"
 dependencies = [
  "async-std",
  "asynchronous-codec",
diff --git a/Cargo.toml b/Cargo.toml
index 2c56773854a..43d7984f024 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -84,7 +84,7 @@ libp2p-dcutr = { version = "0.12.0", path = "protocols/dcutr" }
 libp2p-dns = { version = "0.42.0", path = "transports/dns" }
 libp2p-floodsub = { version = "0.45.0", path = "protocols/floodsub" }
 libp2p-gossipsub = { version = "0.47.1", path = "protocols/gossipsub" }
-libp2p-identify = { version = "0.46.0", path = "protocols/identify" }
+libp2p-identify = { version = "0.45.1", path = "protocols/identify" }
 libp2p-identity = { version = "0.2.9" }
 libp2p-kad = { version = "0.47.0", path = "protocols/kad" }
 libp2p-mdns = { version = "0.46.0", path = "protocols/mdns" }
diff --git a/protocols/identify/CHANGELOG.md b/protocols/identify/CHANGELOG.md
index ee866620dc9..5bca560d169 100644
--- a/protocols/identify/CHANGELOG.md
+++ b/protocols/identify/CHANGELOG.md
@@ -1,12 +1,10 @@
-## 0.46.0
-
-- Update to the new `PeerAddresses` API with `PeerAddressesConfig`, changing `with_cache_size` to `with_cache_config`.
-  See [PR 5574](https://github.com/libp2p/rust-libp2p/pull/5574).
-
 ## 0.45.1
 
 - Add `hide_listen_addrs` option to prevent leaking (local) listen addresses.
   See [PR 5507](https://github.com/libp2p/rust-libp2p/pull/5507).
+- Add `with_cache_config` to fully configure the identify cache using the new `PeerAddressesConfig`.
+  Deprecating `with_cache_size` in favor of the new `with_cache_config`.
+  See [PR 5574](https://github.com/libp2p/rust-libp2p/pull/5574).
 
 ## 0.45.0
 
diff --git a/protocols/identify/Cargo.toml b/protocols/identify/Cargo.toml
index 13c43b6a71f..c3fb585c99c 100644
--- a/protocols/identify/Cargo.toml
+++ b/protocols/identify/Cargo.toml
@@ -3,7 +3,7 @@ name = "libp2p-identify"
 edition = "2021"
 rust-version = { workspace = true }
 description = "Nodes identification protocol for libp2p"
-version = "0.46.0"
+version = "0.45.1"
 authors = ["Parity Technologies <admin@parity.io>"]
 license = "MIT"
 repository = "https://github.com/libp2p/rust-libp2p"
diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs
index 83ea4ac21d0..458b1f393d5 100644
--- a/protocols/identify/src/behaviour.rs
+++ b/protocols/identify/src/behaviour.rs
@@ -34,6 +34,7 @@ use libp2p_swarm::{
 use libp2p_swarm::{ConnectionId, THandler, THandlerOutEvent};
 
 use std::collections::hash_map::Entry;
+use std::num::NonZeroUsize;
 use std::{
     collections::{HashMap, HashSet, VecDeque},
     task::Context,
@@ -195,6 +196,23 @@ impl Config {
         self
     }
 
+    /// Configures the size of the LRU cache, caching addresses of discovered peers.
+    #[deprecated(since = "0.45.1", note = "Use `Config::with_cache_config` instead.")]
+    pub fn with_cache_size(mut self, cache_size: usize) -> Self {
+        match NonZeroUsize::new(cache_size) {
+            Some(cache_size) => {
+                if let Some(cache_config) = &mut self.cache_config {
+                    cache_config.number_of_peers = cache_size;
+                } else {
+                    self.cache_config =
+                        Some(PeerAddressesConfig::default().with_number_of_peers(cache_size))
+                }
+            }
+            None => self.cache_config = None,
+        }
+        self
+    }
+
     /// Configures whether we prevent sending out our listen addresses.
     pub fn with_hide_listen_addrs(mut self, b: bool) -> Self {
         self.hide_listen_addrs = b;
diff --git a/protocols/identify/tests/smoke.rs b/protocols/identify/tests/smoke.rs
index 8ab0cc6095b..f2c4f45fb31 100644
--- a/protocols/identify/tests/smoke.rs
+++ b/protocols/identify/tests/smoke.rs
@@ -162,10 +162,10 @@ async fn emits_unique_listen_addresses() {
             identify::Config::new("a".to_string(), identity.public())
                 .with_agent_version("b".to_string())
                 .with_interval(Duration::from_secs(1))
-                .with_cache_config(Some(PeerAddressesConfig {
-                    number_of_peers: NonZeroUsize::new(10).expect("10 != 0"),
-                    ..Default::default()
-                })),
+                .with_cache_config(Some(
+                    PeerAddressesConfig::default()
+                        .with_number_of_peers(NonZeroUsize::new(10).expect("10 != 0")),
+                )),
         )
     });
     let mut swarm2 = Swarm::new_ephemeral(|identity| {
@@ -237,7 +237,10 @@ async fn hides_listen_addresses() {
             identify::Config::new("a".to_string(), identity.public())
                 .with_agent_version("b".to_string())
                 .with_interval(Duration::from_secs(1))
-                .with_cache_size(10),
+                .with_cache_config(Some(
+                    PeerAddressesConfig::default()
+                        .with_number_of_peers(NonZeroUsize::new(10).expect("10 != 0")),
+                )),
         )
     });
     let mut swarm2 = Swarm::new_ephemeral(|identity| {
diff --git a/swarm/src/behaviour/peer_addresses.rs b/swarm/src/behaviour/peer_addresses.rs
index 226e0914c1c..f689c1f5ab2 100644
--- a/swarm/src/behaviour/peer_addresses.rs
+++ b/swarm/src/behaviour/peer_addresses.rs
@@ -15,14 +15,31 @@ pub struct PeerAddressesConfig {
     pub number_of_peers: NonZeroUsize,
 
     /// Maximum number of cached addresses per peer.
-    pub number_of_addresses_by_peer: NonZeroUsize,
+    pub number_of_addresses_per_peer: NonZeroUsize,
+}
+
+impl PeerAddressesConfig {
+    /// Configure the capacity of the [`PeerAddresses`] cache.
+    pub fn with_number_of_peers(mut self, number_of_peers: NonZeroUsize) -> Self {
+        self.number_of_peers = number_of_peers;
+        self
+    }
+
+    /// Configure the maximum number of cached addresses per peer.
+    pub fn with_number_of_addresses_by_peer(
+        mut self,
+        number_of_addresses_per_peer: NonZeroUsize,
+    ) -> Self {
+        self.number_of_addresses_per_peer = number_of_addresses_per_peer;
+        self
+    }
 }
 
 impl Default for PeerAddressesConfig {
     fn default() -> Self {
         Self {
             number_of_peers: NonZeroUsize::new(100).expect("100 != 0"),
-            number_of_addresses_by_peer: NonZeroUsize::new(10).expect("10 != 0"),
+            number_of_addresses_per_peer: NonZeroUsize::new(10).expect("10 != 0"),
         }
     }
 }
@@ -76,7 +93,7 @@ impl PeerAddresses {
                 if let Some(cached) = self.inner.get_mut(&peer) {
                     cached.put(address, ()).is_none()
                 } else {
-                    let mut set = LruCache::new(self.config.number_of_addresses_by_peer);
+                    let mut set = LruCache::new(self.config.number_of_addresses_per_peer);
                     set.put(address, ());
                     self.inner.put(peer, set);
 

From 4d17c210a35bd50cd4f6a7f3b547c1733a6d186b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20RIBEAU?=
 <francois.ribeau.external@stormshield.eu>
Date: Fri, 6 Sep 2024 09:56:37 +0200
Subject: [PATCH 04/13] slight changes to changelog messages

---
 protocols/identify/CHANGELOG.md | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/protocols/identify/CHANGELOG.md b/protocols/identify/CHANGELOG.md
index 5bca560d169..3eac7da9f25 100644
--- a/protocols/identify/CHANGELOG.md
+++ b/protocols/identify/CHANGELOG.md
@@ -2,8 +2,8 @@
 
 - Add `hide_listen_addrs` option to prevent leaking (local) listen addresses.
   See [PR 5507](https://github.com/libp2p/rust-libp2p/pull/5507).
-- Add `with_cache_config` to fully configure the identify cache using the new `PeerAddressesConfig`.
-  Deprecating `with_cache_size` in favor of the new `with_cache_config`.
+- Add `with_cache_config` to fully configure the identify cache using `PeerAddressesConfig`.
+  Deprecating `with_cache_size` in favor of `with_cache_config`.
   See [PR 5574](https://github.com/libp2p/rust-libp2p/pull/5574).
 
 ## 0.45.0

From 457c06375a9f1b0d00da5cc4e7009e2af2c266f1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20RIBEAU?=
 <francois.ribeau.external@stormshield.eu>
Date: Fri, 6 Sep 2024 10:03:07 +0200
Subject: [PATCH 05/13] don't make PeerAddressesConfig attributes public

---
 protocols/identify/src/behaviour.rs   | 16 +++++-----------
 swarm/src/behaviour/peer_addresses.rs |  4 ++--
 2 files changed, 7 insertions(+), 13 deletions(-)

diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs
index 458b1f393d5..b3be900341b 100644
--- a/protocols/identify/src/behaviour.rs
+++ b/protocols/identify/src/behaviour.rs
@@ -199,17 +199,11 @@ impl Config {
     /// Configures the size of the LRU cache, caching addresses of discovered peers.
     #[deprecated(since = "0.45.1", note = "Use `Config::with_cache_config` instead.")]
     pub fn with_cache_size(mut self, cache_size: usize) -> Self {
-        match NonZeroUsize::new(cache_size) {
-            Some(cache_size) => {
-                if let Some(cache_config) = &mut self.cache_config {
-                    cache_config.number_of_peers = cache_size;
-                } else {
-                    self.cache_config =
-                        Some(PeerAddressesConfig::default().with_number_of_peers(cache_size))
-                }
-            }
-            None => self.cache_config = None,
-        }
+        self.cache_config = NonZeroUsize::new(cache_size).map(|cache_size| {
+            self.cache_config
+                .unwrap_or_default()
+                .with_number_of_peers(cache_size)
+        });
         self
     }
 
diff --git a/swarm/src/behaviour/peer_addresses.rs b/swarm/src/behaviour/peer_addresses.rs
index f689c1f5ab2..dd489124d11 100644
--- a/swarm/src/behaviour/peer_addresses.rs
+++ b/swarm/src/behaviour/peer_addresses.rs
@@ -12,10 +12,10 @@ use std::num::NonZeroUsize;
 /// Configuration of a [`PeerAddresses`] instance.
 pub struct PeerAddressesConfig {
     /// Capacity of the [`PeerAddresses`] cache.
-    pub number_of_peers: NonZeroUsize,
+    number_of_peers: NonZeroUsize,
 
     /// Maximum number of cached addresses per peer.
-    pub number_of_addresses_per_peer: NonZeroUsize,
+    number_of_addresses_per_peer: NonZeroUsize,
 }
 
 impl PeerAddressesConfig {

From 6521a7fc0d98ab6b58b1cdafb15d435612e4b977 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20RIBEAU?=
 <francois.ribeau.external@stormshield.eu>
Date: Fri, 6 Sep 2024 10:36:13 +0200
Subject: [PATCH 06/13] fix semver checks

---
 protocols/identify/src/behaviour.rs | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)

diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs
index b3be900341b..2829a6a4946 100644
--- a/protocols/identify/src/behaviour.rs
+++ b/protocols/identify/src/behaviour.rs
@@ -142,6 +142,11 @@ pub struct Config {
     /// Disabled by default.
     pub push_listen_addr_updates: bool,
 
+    /// How many entries of discovered peers to keep before we discard
+    /// the least-recently used one.
+    #[deprecated(since = "0.45.1", note = "Use `Config::cache_config` instead.")]
+    pub cache_size: usize,
+
     /// Configuration for the LRU cache of discovered peers.
     pub cache_config: Option<PeerAddressesConfig>,
 
@@ -156,13 +161,15 @@ impl Config {
     /// Creates a new configuration for the identify [`Behaviour`] that
     /// advertises the given protocol version and public key.
     pub fn new(protocol_version: String, local_public_key: PublicKey) -> Self {
+        #[allow(deprecated)]
         Self {
             protocol_version,
             agent_version: format!("rust-libp2p/{}", env!("CARGO_PKG_VERSION")),
             local_public_key,
             interval: Duration::from_secs(5 * 60),
             push_listen_addr_updates: false,
-            cache_config: Some(Default::default()),
+            cache_size: 100,
+            cache_config: None, // TODO: when removing `cache_size`, replace `None` with `Some(Default::default())`
             hide_listen_addrs: false,
         }
     }
@@ -197,13 +204,12 @@ impl Config {
     }
 
     /// Configures the size of the LRU cache, caching addresses of discovered peers.
+    ///
+    /// If `cache_config` is set, then `cache_size` is ignored.
     #[deprecated(since = "0.45.1", note = "Use `Config::with_cache_config` instead.")]
+    #[allow(deprecated)]
     pub fn with_cache_size(mut self, cache_size: usize) -> Self {
-        self.cache_config = NonZeroUsize::new(cache_size).map(|cache_size| {
-            self.cache_config
-                .unwrap_or_default()
-                .with_number_of_peers(cache_size)
-        });
+        self.cache_size = cache_size;
         self
     }
 
@@ -216,7 +222,14 @@ impl Config {
 
 impl Behaviour {
     /// Creates a new identify [`Behaviour`].
-    pub fn new(config: Config) -> Self {
+    pub fn new(mut config: Config) -> Self {
+        #[allow(deprecated)]
+        // If `cache_config` value is not set but `cache_size` is, we build `cache_config` from `cache_size`.
+        if config.cache_config.is_none() {
+            config.cache_config = NonZeroUsize::new(config.cache_size)
+                .map(|cache_size| PeerAddressesConfig::default().with_number_of_peers(cache_size));
+        }
+
         let discovered_peers = PeerCache::new(config.cache_config.clone());
 
         Self {

From bad6c2970b109e7f56aec78d99a7cf1b8fa6d55b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20RIBEAU?=
 <francois.ribeau.external@stormshield.eu>
Date: Fri, 6 Sep 2024 10:37:39 +0200
Subject: [PATCH 07/13] Uniformed rust doc

---
 protocols/identify/src/behaviour.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs
index 2829a6a4946..aa818f4e61f 100644
--- a/protocols/identify/src/behaviour.rs
+++ b/protocols/identify/src/behaviour.rs
@@ -195,7 +195,7 @@ impl Config {
         self
     }
 
-    /// Configuration for the LRU cache responsible for caching addresses of discovered peers.
+    /// Configures the LRU cache responsible for caching addresses of discovered peers.
     ///
     /// If set to [`None`], caching is disabled.
     pub fn with_cache_config(mut self, cache_config: Option<PeerAddressesConfig>) -> Self {

From 187f0106dd2a24401bbd821f9a8067af245135c7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20RIBEAU?=
 <francois.ribeau.external@stormshield.eu>
Date: Fri, 6 Sep 2024 10:41:36 +0200
Subject: [PATCH 08/13] ensure caching can be disabled

---
 protocols/identify/src/behaviour.rs | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs
index aa818f4e61f..f51c4de5295 100644
--- a/protocols/identify/src/behaviour.rs
+++ b/protocols/identify/src/behaviour.rs
@@ -199,6 +199,11 @@ impl Config {
     ///
     /// If set to [`None`], caching is disabled.
     pub fn with_cache_config(mut self, cache_config: Option<PeerAddressesConfig>) -> Self {
+        #[allow(deprecated)]
+        {
+            // set cache_size to 0 to ensure if user call `with_cache_config(None)`, caching do get disabled.
+            self.cache_size = 0;
+        }
         self.cache_config = cache_config;
         self
     }

From 10356495354cb1e89eb27e29d3d862c63c58080a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20RIBEAU?=
 <francois.ribeau.external@stormshield.eu>
Date: Fri, 15 Nov 2024 17:21:17 +0100
Subject: [PATCH 09/13] removing deprecations

---
 protocols/identify/src/behaviour.rs | 39 ++++-------------------------
 1 file changed, 5 insertions(+), 34 deletions(-)

diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs
index 4f6cad4d3cb..299e58b837b 100644
--- a/protocols/identify/src/behaviour.rs
+++ b/protocols/identify/src/behaviour.rs
@@ -34,7 +34,6 @@ use libp2p_swarm::{
 use libp2p_swarm::{ConnectionId, THandler, THandlerOutEvent};
 
 use std::collections::hash_map::Entry;
-use std::num::NonZeroUsize;
 use std::{
     collections::{HashMap, HashSet, VecDeque},
     task::Context,
@@ -142,11 +141,6 @@ pub struct Config {
     /// Disabled by default.
     push_listen_addr_updates: bool,
 
-    /// How many entries of discovered peers to keep before we discard
-    /// the least-recently used one.
-    #[deprecated(since = "0.45.1", note = "Use `Config::cache_config` instead.")]
-    cache_size: usize,
-
     /// Configuration for the LRU cache of discovered peers.
     cache_config: Option<PeerAddressesConfig>,
 
@@ -168,8 +162,7 @@ impl Config {
             local_public_key,
             interval: Duration::from_secs(5 * 60),
             push_listen_addr_updates: false,
-            cache_size: 100,
-            cache_config: None, // TODO: when removing `cache_size`, replace `None` with `Some(Default::default())`
+            cache_config: Some(Default::default()),
             hide_listen_addrs: false,
         }
     }
@@ -199,25 +192,10 @@ impl Config {
     ///
     /// If set to [`None`], caching is disabled.
     pub fn with_cache_config(mut self, cache_config: Option<PeerAddressesConfig>) -> Self {
-        #[allow(deprecated)]
-        {
-            // set cache_size to 0 to ensure if user call `with_cache_config(None)`, caching do get disabled.
-            self.cache_size = 0;
-        }
         self.cache_config = cache_config;
         self
     }
 
-    /// Configures the size of the LRU cache, caching addresses of discovered peers.
-    ///
-    /// If `cache_config` is set, then `cache_size` is ignored.
-    #[deprecated(since = "0.45.1", note = "Use `Config::with_cache_config` instead.")]
-    #[allow(deprecated)]
-    pub fn with_cache_size(mut self, cache_size: usize) -> Self {
-        self.cache_size = cache_size;
-        self
-    }
-
     /// Configures whether we prevent sending out our listen addresses.
     pub fn with_hide_listen_addrs(mut self, b: bool) -> Self {
         self.hide_listen_addrs = b;
@@ -249,9 +227,9 @@ impl Config {
         self.push_listen_addr_updates
     }
 
-    /// Get the cache size of the Config.
-    pub fn cache_size(&self) -> usize {
-        self.cache_size
+    /// Get the config of the LRU cache responsible for caching addresses of discovered peers.
+    pub fn cache_config(&self) -> &Option<PeerAddressesConfig> {
+        &self.cache_config
     }
 
     /// Get the hide listen address boolean value of the Config.
@@ -262,14 +240,7 @@ impl Config {
 
 impl Behaviour {
     /// Creates a new identify [`Behaviour`].
-    pub fn new(mut config: Config) -> Self {
-        #[allow(deprecated)]
-        // If `cache_config` value is not set but `cache_size` is, we build `cache_config` from `cache_size`.
-        if config.cache_config.is_none() {
-            config.cache_config = NonZeroUsize::new(config.cache_size)
-                .map(|cache_size| PeerAddressesConfig::default().with_number_of_peers(cache_size));
-        }
-
+    pub fn new(config: Config) -> Self {
         let discovered_peers = PeerCache::new(config.cache_config.clone());
 
         Self {

From 3e66c4f5f51554e10faa45a527901a9bba48a5af Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20RIBEAU?=
 <francois.ribeau.external@stormshield.eu>
Date: Fri, 15 Nov 2024 17:24:08 +0100
Subject: [PATCH 10/13] remove unneeded allow deprecated

---
 protocols/identify/src/behaviour.rs | 1 -
 1 file changed, 1 deletion(-)

diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs
index 299e58b837b..91e35b6fb72 100644
--- a/protocols/identify/src/behaviour.rs
+++ b/protocols/identify/src/behaviour.rs
@@ -155,7 +155,6 @@ impl Config {
     /// Creates a new configuration for the identify [`Behaviour`] that
     /// advertises the given protocol version and public key.
     pub fn new(protocol_version: String, local_public_key: PublicKey) -> Self {
-        #[allow(deprecated)]
         Self {
             protocol_version,
             agent_version: format!("rust-libp2p/{}", env!("CARGO_PKG_VERSION")),

From 82219041b941869f5bbd69c5da35606adf4038b6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20RIBEAU?=
 <francois.ribeau.external@stormshield.eu>
Date: Fri, 15 Nov 2024 17:24:18 +0100
Subject: [PATCH 11/13] update changelog

---
 protocols/identify/CHANGELOG.md | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/protocols/identify/CHANGELOG.md b/protocols/identify/CHANGELOG.md
index 4f9c6b7e427..c6f582011f5 100644
--- a/protocols/identify/CHANGELOG.md
+++ b/protocols/identify/CHANGELOG.md
@@ -2,14 +2,13 @@
 
 - Make `identify::Config` fields private and add getter functions.
   See [PR 5663](https://github.com/libp2p/rust-libp2p/pull/5663).
+- Replace `with_cache_size` by `with_cache_config` to fully configure the identify cache using `PeerAddressesConfig`.
+  See [PR 5574](https://github.com/libp2p/rust-libp2p/pull/5574).
 
 ## 0.45.1
 
 - Add `hide_listen_addrs` option to prevent leaking (local) listen addresses.
   See [PR 5507](https://github.com/libp2p/rust-libp2p/pull/5507).
-- Add `with_cache_config` to fully configure the identify cache using `PeerAddressesConfig`.
-  Deprecating `with_cache_size` in favor of `with_cache_config`.
-  See [PR 5574](https://github.com/libp2p/rust-libp2p/pull/5574).
 
 ## 0.45.0
 

From 914662d246f9153b4b9daf678a12ff6571628d5a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20RIBEAU?=
 <francois.ribeau.external@stormshield.eu>
Date: Fri, 15 Nov 2024 17:27:19 +0100
Subject: [PATCH 12/13] rename with_number_of_addresses_by(per)_peer

---
 swarm/src/behaviour/peer_addresses.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/swarm/src/behaviour/peer_addresses.rs b/swarm/src/behaviour/peer_addresses.rs
index dd489124d11..7baea839950 100644
--- a/swarm/src/behaviour/peer_addresses.rs
+++ b/swarm/src/behaviour/peer_addresses.rs
@@ -26,7 +26,7 @@ impl PeerAddressesConfig {
     }
 
     /// Configure the maximum number of cached addresses per peer.
-    pub fn with_number_of_addresses_by_peer(
+    pub fn with_number_of_addresses_per_peer(
         mut self,
         number_of_addresses_per_peer: NonZeroUsize,
     ) -> Self {

From 31ab1fbae309cdd762bcfec0269d28ea6e6bd5a5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20RIBEAU?=
 <francois.ribeau.external@stormshield.eu>
Date: Fri, 15 Nov 2024 17:29:59 +0100
Subject: [PATCH 13/13] fix typo

---
 swarm/src/behaviour/peer_addresses.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/swarm/src/behaviour/peer_addresses.rs b/swarm/src/behaviour/peer_addresses.rs
index 7baea839950..6f44ddff986 100644
--- a/swarm/src/behaviour/peer_addresses.rs
+++ b/swarm/src/behaviour/peer_addresses.rs
@@ -54,7 +54,7 @@ pub struct PeerAddresses {
 impl PeerAddresses {
     /// Creates a [`PeerAddresses`] cache with capacity for the given number of peers.
     ///
-    /// For each peer, we will at most store `config.number_of_addresses_by_peer` addresses.
+    /// For each peer, we will at most store `config.number_of_addresses_per_peer` addresses.
     pub fn new(config: PeerAddressesConfig) -> Self {
         let inner = LruCache::new(config.number_of_peers);
         Self { config, inner }