From b28ab2c6bca950df1091f23dd957af4b4fcb164b Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 27 Sep 2022 11:39:10 +1000 Subject: [PATCH] build(deps): Update lru to 0.8.0 (#2908) Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Max Inden --- CHANGELOG.md | 2 ++ Cargo.toml | 2 +- misc/metrics/CHANGELOG.md | 2 ++ misc/metrics/Cargo.toml | 2 +- protocols/identify/CHANGELOG.md | 4 +++ protocols/identify/Cargo.toml | 4 +-- protocols/identify/src/identify.rs | 54 +++++++++++++++++++++++++----- 7 files changed, 58 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f77aa78..69877b0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,8 @@ - Update to [`libp2p-metrics` `v0.10.0`](misc/metrics/CHANGELOG.md#0100). - Update to [`libp2p-kad` `v0.41.0`](protocols/kad/CHANGELOG.md#0410). +- +- Update to [`libp2p-identify` `v0.39.1`](protocols/identify/CHANGELOG.md#0400). - Update to [`libp2p-noise` `v0.39.1`](transports/noise/CHANGELOG.md#0391). diff --git a/Cargo.toml b/Cargo.toml index e3e2283b..26136ae1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,7 +84,7 @@ libp2p-autonat = { version = "0.7.0", path = "protocols/autonat", optional = tru libp2p-core = { version = "0.36.1", path = "core", default-features = false } libp2p-dcutr = { version = "0.6.0", path = "protocols/dcutr", optional = true } libp2p-floodsub = { version = "0.39.1", path = "protocols/floodsub", optional = true } -libp2p-identify = { version = "0.39.0", path = "protocols/identify", optional = true } +libp2p-identify = { version = "0.39.1", path = "protocols/identify", optional = true } libp2p-kad = { version = "0.41.0", path = "protocols/kad", optional = true } libp2p-metrics = { version = "0.10.0", path = "misc/metrics", optional = true } libp2p-mplex = { version = "0.36.1", path = "muxers/mplex", optional = true } diff --git a/misc/metrics/CHANGELOG.md b/misc/metrics/CHANGELOG.md index 1b784ffa..20e2741e 100644 --- a/misc/metrics/CHANGELOG.md +++ b/misc/metrics/CHANGELOG.md @@ -1,6 +1,8 @@ # 0.10.0 [unreleased] - Update to `libp2p-kad` `v0.41.0`. +- +- Update to `libp2p-identify` `v0.39.1`. # 0.9.0 diff --git a/misc/metrics/Cargo.toml b/misc/metrics/Cargo.toml index 1c29fcee..d8f5209e 100644 --- a/misc/metrics/Cargo.toml +++ b/misc/metrics/Cargo.toml @@ -21,7 +21,7 @@ dcutr = ["libp2p-dcutr"] [dependencies] libp2p-core = { version = "0.36.0", path = "../../core", default-features = false } libp2p-dcutr = { version = "0.6.0", path = "../../protocols/dcutr", optional = true } -libp2p-identify = { version = "0.39.0", path = "../../protocols/identify", optional = true } +libp2p-identify = { version = "0.39.1", path = "../../protocols/identify", optional = true } libp2p-kad = { version = "0.41.0", path = "../../protocols/kad", optional = true } libp2p-ping = { version = "0.39.0", path = "../../protocols/ping", optional = true } libp2p-relay = { version = "0.12.0", path = "../../protocols/relay", optional = true } diff --git a/protocols/identify/CHANGELOG.md b/protocols/identify/CHANGELOG.md index 1455966b..3e9d6eb9 100644 --- a/protocols/identify/CHANGELOG.md +++ b/protocols/identify/CHANGELOG.md @@ -1,3 +1,7 @@ +# 0.39.1 [unreleased] + +- Update dependencies. + # 0.39.0 - Update to `libp2p-swarm` `v0.39.0`. diff --git a/protocols/identify/Cargo.toml b/protocols/identify/Cargo.toml index f147c5bb..34c6e3cf 100644 --- a/protocols/identify/Cargo.toml +++ b/protocols/identify/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-identify" edition = "2021" rust-version = "1.56.1" description = "Nodes identifcation protocol for libp2p" -version = "0.39.0" +version = "0.39.1" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -17,7 +17,7 @@ futures-timer = "3.0.2" libp2p-core = { version = "0.36.0", path = "../../core", default-features = false } libp2p-swarm = { version = "0.39.0", path = "../../swarm" } log = "0.4.1" -lru = "0.7.2" +lru = "0.8.0" prost-codec = { version = "0.2", path = "../../misc/prost-codec" } prost = "0.11" smallvec = "1.6.1" diff --git a/protocols/identify/src/identify.rs b/protocols/identify/src/identify.rs index d30e98e1..e3839624 100644 --- a/protocols/identify/src/identify.rs +++ b/protocols/identify/src/identify.rs @@ -31,6 +31,7 @@ use libp2p_swarm::{ NotifyHandler, PollParameters, }; use lru::LruCache; +use std::num::NonZeroUsize; use std::{ collections::{HashMap, HashSet, VecDeque}, iter::FromIterator, @@ -58,7 +59,7 @@ pub struct Identify { /// the local peer should be sent. pending_push: HashSet, /// The addresses of all peers that we have discovered. - discovered_peers: LruCache>, + discovered_peers: PeerCache, } /// A pending reply to an inbound identification request. @@ -175,7 +176,10 @@ impl IdentifyConfig { impl Identify { /// Creates a new `Identify` network behaviour. pub fn new(config: IdentifyConfig) -> Self { - let discovered_peers = LruCache::new(config.cache_size); + let discovered_peers = match NonZeroUsize::new(config.cache_size) { + None => PeerCache::disabled(), + Some(size) => PeerCache::enabled(size), + }; Identify { config, @@ -303,7 +307,7 @@ impl NetworkBehaviour for Identify { // Replace existing addresses to prevent other peer from filling up our memory. self.discovered_peers - .put(peer_id, HashSet::from_iter(info.listen_addrs.clone())); + .put(peer_id, info.listen_addrs.iter().cloned()); let observed = info.observed_addr.clone(); self.events.push_back(NetworkBehaviourAction::GenerateEvent( @@ -441,11 +445,7 @@ impl NetworkBehaviour for Identify { } fn addresses_of_peer(&mut self, peer: &PeerId) -> Vec { - self.discovered_peers - .get(peer) - .cloned() - .map(Vec::from_iter) - .unwrap_or_default() + self.discovered_peers.get(peer) } } @@ -506,6 +506,44 @@ fn multiaddr_matches_peer_id(addr: &Multiaddr, peer_id: &PeerId) -> bool { true } +struct PeerCache(Option>>); + +impl PeerCache { + fn disabled() -> Self { + Self(None) + } + + fn enabled(size: NonZeroUsize) -> Self { + Self(Some(LruCache::new(size))) + } + + fn get_mut(&mut self, peer: &PeerId) -> Option<&mut HashSet> { + self.0.as_mut()?.get_mut(peer) + } + + fn put(&mut self, peer: PeerId, addresses: impl Iterator) { + let cache = match self.0.as_mut() { + None => return, + Some(cache) => cache, + }; + + cache.put(peer, HashSet::from_iter(addresses)); + } + + fn get(&mut self, peer: &PeerId) -> Vec { + let cache = match self.0.as_mut() { + None => return Vec::new(), + Some(cache) => cache, + }; + + cache + .get(peer) + .cloned() + .map(Vec::from_iter) + .unwrap_or_default() + } +} + #[cfg(test)] mod tests { use super::*;