From a64cc4a8600dd7cf7839c170b5f795dc8a21a8ec Mon Sep 17 00:00:00 2001
From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com>
Date: Tue, 4 Oct 2022 11:28:21 +0000
Subject: [PATCH] Bump lru from 0.7.8 to 0.8.0 (#6060)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* Bump lru from 0.7.8 to 0.8.0

Bumps [lru](https://github.com/jeromefroe/lru-rs) from 0.7.8 to 0.8.0.
- [Release notes](https://github.com/jeromefroe/lru-rs/releases)
- [Changelog](https://github.com/jeromefroe/lru-rs/blob/master/CHANGELOG.md)
- [Commits](https://github.com/jeromefroe/lru-rs/compare/0.7.8...0.8.0)

---
updated-dependencies:
- dependency-name: lru
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* Change `LruCache` paramerter to `NonZeroUsize`

* Change type of `session_cache_lru_size` to `NonZeroUsize`

* Add expects instead of unwrap

Co-authored-by: Bastian Köcher <info@kchr.de>

* Use match to get rid of expects

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Sebastian Kunert <skunert49@gmail.com>
Co-authored-by: Bastian Köcher <info@kchr.de>
---
 polkadot/Cargo.lock                            | 18 +++++++++---------
 polkadot/node/core/approval-voting/Cargo.toml  |  2 +-
 polkadot/node/core/approval-voting/src/lib.rs  |  7 ++++++-
 .../node/core/dispute-coordinator/Cargo.toml   |  2 +-
 .../dispute-coordinator/src/scraping/mod.rs    | 10 ++++++++--
 .../availability-distribution/Cargo.toml       |  2 +-
 .../src/requester/session_cache.rs             |  4 ++--
 .../network/availability-recovery/Cargo.toml   |  2 +-
 .../network/availability-recovery/src/lib.rs   |  6 +++++-
 .../network/dispute-distribution/Cargo.toml    |  2 +-
 .../network/dispute-distribution/src/lib.rs    |  5 ++++-
 .../dispute-distribution/src/receiver/mod.rs   | 13 ++++++++++---
 polkadot/node/overseer/Cargo.toml              |  2 +-
 polkadot/node/overseer/src/lib.rs              |  6 +++++-
 polkadot/node/service/Cargo.toml               |  2 +-
 polkadot/node/subsystem-util/Cargo.toml        |  2 +-
 .../node/subsystem-util/src/runtime/mod.rs     | 11 +++++++----
 17 files changed, 64 insertions(+), 32 deletions(-)

diff --git a/polkadot/Cargo.lock b/polkadot/Cargo.lock
index 044fb12ba4e..99033d5da5d 100644
--- a/polkadot/Cargo.lock
+++ b/polkadot/Cargo.lock
@@ -6167,7 +6167,7 @@ dependencies = [
  "fatality",
  "futures",
  "futures-timer",
- "lru 0.7.8",
+ "lru 0.8.0",
  "parity-scale-codec",
  "polkadot-erasure-coding",
  "polkadot-node-network-protocol",
@@ -6197,7 +6197,7 @@ dependencies = [
  "futures",
  "futures-timer",
  "log",
- "lru 0.7.8",
+ "lru 0.8.0",
  "parity-scale-codec",
  "polkadot-erasure-coding",
  "polkadot-node-network-protocol",
@@ -6335,7 +6335,7 @@ dependencies = [
  "futures",
  "futures-timer",
  "lazy_static",
- "lru 0.7.8",
+ "lru 0.8.0",
  "parity-scale-codec",
  "polkadot-erasure-coding",
  "polkadot-node-network-protocol",
@@ -6455,7 +6455,7 @@ dependencies = [
  "futures-timer",
  "kvdb",
  "kvdb-memorydb",
- "lru 0.7.8",
+ "lru 0.8.0",
  "merlin",
  "parity-scale-codec",
  "parking_lot 0.12.1",
@@ -6624,7 +6624,7 @@ dependencies = [
  "futures-timer",
  "kvdb",
  "kvdb-memorydb",
- "lru 0.7.8",
+ "lru 0.8.0",
  "parity-scale-codec",
  "polkadot-node-primitives",
  "polkadot-node-subsystem",
@@ -6911,7 +6911,7 @@ dependencies = [
  "kvdb-shared-tests",
  "lazy_static",
  "log",
- "lru 0.7.8",
+ "lru 0.8.0",
  "parity-db",
  "parity-scale-codec",
  "parity-util-mem",
@@ -6945,7 +6945,7 @@ dependencies = [
  "femme",
  "futures",
  "futures-timer",
- "lru 0.7.8",
+ "lru 0.8.0",
  "orchestra",
  "parity-util-mem",
  "parking_lot 0.12.1",
@@ -7302,7 +7302,7 @@ dependencies = [
  "kvdb",
  "kvdb-rocksdb",
  "log",
- "lru 0.7.8",
+ "lru 0.8.0",
  "pallet-babe",
  "pallet-im-online",
  "pallet-staking",
@@ -11921,7 +11921,7 @@ version = "1.6.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675"
 dependencies = [
- "cfg-if 1.0.0",
+ "cfg-if 0.1.10",
  "digest 0.10.3",
  "rand 0.8.5",
  "static_assertions",
diff --git a/polkadot/node/core/approval-voting/Cargo.toml b/polkadot/node/core/approval-voting/Cargo.toml
index f2572cac823..e39a589b567 100644
--- a/polkadot/node/core/approval-voting/Cargo.toml
+++ b/polkadot/node/core/approval-voting/Cargo.toml
@@ -10,7 +10,7 @@ futures-timer = "3.0.2"
 parity-scale-codec = { version = "3.1.5", default-features = false, features = ["bit-vec", "derive"] }
 gum = { package = "tracing-gum", path = "../../gum" }
 bitvec = { version = "1.0.0", default-features = false, features = ["alloc"] }
-lru = "0.7"
+lru = "0.8"
 merlin = "2.0"
 schnorrkel = "0.9.1"
 kvdb = "0.11.0"
diff --git a/polkadot/node/core/approval-voting/src/lib.rs b/polkadot/node/core/approval-voting/src/lib.rs
index ac025f366ab..467d8be612e 100644
--- a/polkadot/node/core/approval-voting/src/lib.rs
+++ b/polkadot/node/core/approval-voting/src/lib.rs
@@ -70,6 +70,7 @@ use std::{
 	collections::{
 		btree_map::Entry as BTMEntry, hash_map::Entry as HMEntry, BTreeMap, HashMap, HashSet,
 	},
+	num::NonZeroUsize,
 	sync::Arc,
 	time::Duration,
 };
@@ -104,7 +105,11 @@ const APPROVAL_CHECKING_TIMEOUT: Duration = Duration::from_secs(120);
 /// Value rather arbitrarily: Should not be hit in practice, it exists to more easily diagnose dead
 /// lock issues for example.
 const WAIT_FOR_SIGS_TIMEOUT: Duration = Duration::from_millis(500);
-const APPROVAL_CACHE_SIZE: usize = 1024;
+const APPROVAL_CACHE_SIZE: NonZeroUsize = match NonZeroUsize::new(1024) {
+	Some(cap) => cap,
+	None => panic!("Approval cache size must be non-zero."),
+};
+
 const TICK_TOO_FAR_IN_FUTURE: Tick = 20; // 10 seconds.
 const APPROVAL_DELAY: Tick = 2;
 const LOG_TARGET: &str = "parachain::approval-voting";
diff --git a/polkadot/node/core/dispute-coordinator/Cargo.toml b/polkadot/node/core/dispute-coordinator/Cargo.toml
index bc22b40c852..7d7bc25e91d 100644
--- a/polkadot/node/core/dispute-coordinator/Cargo.toml
+++ b/polkadot/node/core/dispute-coordinator/Cargo.toml
@@ -10,7 +10,7 @@ gum = { package = "tracing-gum", path = "../../gum" }
 parity-scale-codec = "3.1.5"
 kvdb = "0.11.0"
 thiserror = "1.0.31"
-lru = "0.7.7"
+lru = "0.8.0"
 fatality = "0.0.6"
 
 polkadot-primitives = { path = "../../../primitives" }
diff --git a/polkadot/node/core/dispute-coordinator/src/scraping/mod.rs b/polkadot/node/core/dispute-coordinator/src/scraping/mod.rs
index b45dbfa9519..7d5d33e1ff4 100644
--- a/polkadot/node/core/dispute-coordinator/src/scraping/mod.rs
+++ b/polkadot/node/core/dispute-coordinator/src/scraping/mod.rs
@@ -14,7 +14,10 @@
 // You should have received a copy of the GNU General Public License
 // along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
 
-use std::collections::{BTreeMap, HashSet};
+use std::{
+	collections::{BTreeMap, HashSet},
+	num::NonZeroUsize,
+};
 
 use futures::channel::oneshot;
 use lru::LruCache;
@@ -44,7 +47,10 @@ mod tests;
 /// `last_observed_blocks` LRU. This means, this value should the very least be as large as the
 /// number of expected forks for keeping chain scraping efficient. Making the LRU much larger than
 /// that has very limited use.
-const LRU_OBSERVED_BLOCKS_CAPACITY: usize = 20;
+const LRU_OBSERVED_BLOCKS_CAPACITY: NonZeroUsize = match NonZeroUsize::new(20) {
+	Some(cap) => cap,
+	None => panic!("Observed blocks cache size must be non-zero"),
+};
 
 /// Chain scraper
 ///
diff --git a/polkadot/node/network/availability-distribution/Cargo.toml b/polkadot/node/network/availability-distribution/Cargo.toml
index 43d56a1ace2..3e8626c1889 100644
--- a/polkadot/node/network/availability-distribution/Cargo.toml
+++ b/polkadot/node/network/availability-distribution/Cargo.toml
@@ -19,7 +19,7 @@ sp-keystore = { git = "https://github.com/paritytech/substrate", branch = "maste
 thiserror = "1.0.31"
 rand = "0.8.5"
 derive_more = "0.99.17"
-lru = "0.7.7"
+lru = "0.8.0"
 fatality = "0.0.6"
 
 [dev-dependencies]
diff --git a/polkadot/node/network/availability-distribution/src/requester/session_cache.rs b/polkadot/node/network/availability-distribution/src/requester/session_cache.rs
index 6d41d930123..cf01e448b70 100644
--- a/polkadot/node/network/availability-distribution/src/requester/session_cache.rs
+++ b/polkadot/node/network/availability-distribution/src/requester/session_cache.rs
@@ -14,7 +14,7 @@
 // You should have received a copy of the GNU General Public License
 // along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
 
-use std::collections::HashSet;
+use std::{collections::HashSet, num::NonZeroUsize};
 
 use lru::LruCache;
 use rand::{seq::SliceRandom, thread_rng};
@@ -85,7 +85,7 @@ impl SessionCache {
 	pub fn new() -> Self {
 		SessionCache {
 			// We need to cache the current and the last session the most:
-			session_info_cache: LruCache::new(2),
+			session_info_cache: LruCache::new(NonZeroUsize::new(2).unwrap()),
 		}
 	}
 
diff --git a/polkadot/node/network/availability-recovery/Cargo.toml b/polkadot/node/network/availability-recovery/Cargo.toml
index fce9755a05a..86f6237740f 100644
--- a/polkadot/node/network/availability-recovery/Cargo.toml
+++ b/polkadot/node/network/availability-recovery/Cargo.toml
@@ -6,7 +6,7 @@ edition = "2021"
 
 [dependencies]
 futures = "0.3.21"
-lru = "0.7.7"
+lru = "0.8.0"
 rand = "0.8.5"
 fatality = "0.0.6"
 thiserror = "1.0.31"
diff --git a/polkadot/node/network/availability-recovery/src/lib.rs b/polkadot/node/network/availability-recovery/src/lib.rs
index 62401e3ad61..f2f92cc5449 100644
--- a/polkadot/node/network/availability-recovery/src/lib.rs
+++ b/polkadot/node/network/availability-recovery/src/lib.rs
@@ -20,6 +20,7 @@
 
 use std::{
 	collections::{HashMap, VecDeque},
+	num::NonZeroUsize,
 	pin::Pin,
 	time::Duration,
 };
@@ -77,7 +78,10 @@ const LOG_TARGET: &str = "parachain::availability-recovery";
 const N_PARALLEL: usize = 50;
 
 // Size of the LRU cache where we keep recovered data.
-const LRU_SIZE: usize = 16;
+const LRU_SIZE: NonZeroUsize = match NonZeroUsize::new(16) {
+	Some(cap) => cap,
+	None => panic!("Availability-recovery cache size must be non-zero."),
+};
 
 const COST_INVALID_REQUEST: Rep = Rep::CostMajor("Peer sent unparsable request");
 
diff --git a/polkadot/node/network/dispute-distribution/Cargo.toml b/polkadot/node/network/dispute-distribution/Cargo.toml
index f50f24bf42c..c37f26f7bec 100644
--- a/polkadot/node/network/dispute-distribution/Cargo.toml
+++ b/polkadot/node/network/dispute-distribution/Cargo.toml
@@ -20,7 +20,7 @@ sp-application-crypto = { git = "https://github.com/paritytech/substrate", branc
 sp-keystore = { git = "https://github.com/paritytech/substrate", branch = "master" }
 thiserror = "1.0.31"
 fatality = "0.0.6"
-lru = "0.7.7"
+lru = "0.8.0"
 
 [dev-dependencies]
 async-trait = "0.1.57"
diff --git a/polkadot/node/network/dispute-distribution/src/lib.rs b/polkadot/node/network/dispute-distribution/src/lib.rs
index aefd66e0ae7..440b70b786d 100644
--- a/polkadot/node/network/dispute-distribution/src/lib.rs
+++ b/polkadot/node/network/dispute-distribution/src/lib.rs
@@ -24,6 +24,8 @@
 //! The sender is responsible for getting our vote out, see [`sender`]. The receiver handles
 //! incoming [`DisputeRequest`]s and offers spam protection, see [`receiver`].
 
+use std::num::NonZeroUsize;
+
 use futures::{channel::mpsc, FutureExt, StreamExt, TryFutureExt};
 
 use polkadot_node_network_protocol::authority_discovery::AuthorityDiscovery;
@@ -145,7 +147,8 @@ where
 	) -> Self {
 		let runtime = RuntimeInfo::new_with_config(runtime::Config {
 			keystore: Some(keystore),
-			session_cache_lru_size: DISPUTE_WINDOW.get() as usize,
+			session_cache_lru_size: NonZeroUsize::new(DISPUTE_WINDOW.get() as usize)
+				.expect("Dispute window can not be 0; qed"),
 		});
 		let (tx, sender_rx) = mpsc::channel(1);
 		let disputes_sender = DisputeSender::new(tx, metrics.clone());
diff --git a/polkadot/node/network/dispute-distribution/src/receiver/mod.rs b/polkadot/node/network/dispute-distribution/src/receiver/mod.rs
index 9193947e78d..c38ca2133f5 100644
--- a/polkadot/node/network/dispute-distribution/src/receiver/mod.rs
+++ b/polkadot/node/network/dispute-distribution/src/receiver/mod.rs
@@ -16,6 +16,7 @@
 
 use std::{
 	collections::HashSet,
+	num::NonZeroUsize,
 	pin::Pin,
 	task::{Context, Poll},
 };
@@ -61,6 +62,11 @@ const COST_NOT_A_VALIDATOR: Rep = Rep::CostMajor("Reporting peer was not a valid
 /// How many statement imports we want to issue in parallel:
 pub const MAX_PARALLEL_IMPORTS: usize = 10;
 
+const BANNED_PEERS_CACHE_SIZE: NonZeroUsize = match NonZeroUsize::new(MAX_PARALLEL_IMPORTS) {
+	Some(cap) => cap,
+	None => panic!("Banned peers cache size should not be 0."),
+};
+
 /// State for handling incoming `DisputeRequest` messages.
 ///
 /// This is supposed to run as its own task in order to easily impose back pressure on the incoming
@@ -146,7 +152,8 @@ where
 	) -> Self {
 		let runtime = RuntimeInfo::new_with_config(runtime::Config {
 			keystore: None,
-			session_cache_lru_size: DISPUTE_WINDOW.get() as usize,
+			session_cache_lru_size: NonZeroUsize::new(DISPUTE_WINDOW.get() as usize)
+				.expect("Dispute window can not be 0; qed"),
 		});
 		Self {
 			runtime,
@@ -156,7 +163,7 @@ where
 			pending_imports: PendingImports::new(),
 			// Size of MAX_PARALLEL_IMPORTS ensures we are going to immediately get rid of any
 			// malicious requests still pending in the incoming queue.
-			banned_peers: LruCache::new(MAX_PARALLEL_IMPORTS),
+			banned_peers: LruCache::new(BANNED_PEERS_CACHE_SIZE),
 			metrics,
 		}
 	}
@@ -222,7 +229,7 @@ where
 		}
 
 		// Wait for a free slot:
-		if self.pending_imports.len() >= MAX_PARALLEL_IMPORTS as usize {
+		if self.pending_imports.len() >= MAX_PARALLEL_IMPORTS {
 			// Wait for one to finish:
 			let r = self.pending_imports.next().await;
 			self.ban_bad_peer(r.expect("pending_imports.len() is greater 0. qed."))?;
diff --git a/polkadot/node/overseer/Cargo.toml b/polkadot/node/overseer/Cargo.toml
index 0db382e4e78..4bc360df47e 100644
--- a/polkadot/node/overseer/Cargo.toml
+++ b/polkadot/node/overseer/Cargo.toml
@@ -17,7 +17,7 @@ polkadot-node-metrics = { path = "../metrics" }
 polkadot-primitives = { path = "../../primitives" }
 orchestra = "0.0.2"
 gum = { package = "tracing-gum", path = "../gum" }
-lru = "0.7"
+lru = "0.8"
 parity-util-mem = { version = "0.11.0", default-features = false }
 sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
 async-trait = "0.1.57"
diff --git a/polkadot/node/overseer/src/lib.rs b/polkadot/node/overseer/src/lib.rs
index 1ce6a6fdb65..70dbe92b243 100644
--- a/polkadot/node/overseer/src/lib.rs
+++ b/polkadot/node/overseer/src/lib.rs
@@ -62,6 +62,7 @@
 use std::{
 	collections::{hash_map, HashMap},
 	fmt::{self, Debug},
+	num::NonZeroUsize,
 	pin::Pin,
 	sync::Arc,
 	time::Duration,
@@ -112,7 +113,10 @@ pub use orchestra::{
 
 /// Store 2 days worth of blocks, not accounting for forks,
 /// in the LRU cache. Assumes a 6-second block time.
-pub const KNOWN_LEAVES_CACHE_SIZE: usize = 2 * 24 * 3600 / 6;
+pub const KNOWN_LEAVES_CACHE_SIZE: NonZeroUsize = match NonZeroUsize::new(2 * 24 * 3600 / 6) {
+	Some(cap) => cap,
+	None => panic!("Known leaves cache size must be non-zero"),
+};
 
 #[cfg(test)]
 mod tests;
diff --git a/polkadot/node/service/Cargo.toml b/polkadot/node/service/Cargo.toml
index a9c9484b6eb..1d261353781 100644
--- a/polkadot/node/service/Cargo.toml
+++ b/polkadot/node/service/Cargo.toml
@@ -73,7 +73,7 @@ kvdb = "0.11.0"
 kvdb-rocksdb = { version = "0.15.2", optional = true }
 parity-db = { version = "0.3.16", optional = true }
 async-trait = "0.1.57"
-lru = "0.7"
+lru = "0.8"
 
 # Polkadot
 polkadot-node-core-parachains-inherent = { path = "../core/parachains-inherent" }
diff --git a/polkadot/node/subsystem-util/Cargo.toml b/polkadot/node/subsystem-util/Cargo.toml
index a3985a89884..6f120beec7c 100644
--- a/polkadot/node/subsystem-util/Cargo.toml
+++ b/polkadot/node/subsystem-util/Cargo.toml
@@ -17,7 +17,7 @@ thiserror = "1.0.31"
 fatality = "0.0.6"
 gum = { package = "tracing-gum", path = "../gum" }
 derive_more = "0.99.17"
-lru = "0.7.7"
+lru = "0.8.0"
 
 polkadot-node-subsystem = {path = "../subsystem" }
 polkadot-node-jaeger = { path = "../jaeger" }
diff --git a/polkadot/node/subsystem-util/src/runtime/mod.rs b/polkadot/node/subsystem-util/src/runtime/mod.rs
index fc660a9dc6d..7fcae2c57b0 100644
--- a/polkadot/node/subsystem-util/src/runtime/mod.rs
+++ b/polkadot/node/subsystem-util/src/runtime/mod.rs
@@ -16,7 +16,7 @@
 
 //! Convenient interface to runtime information.
 
-use std::cmp::max;
+use std::num::NonZeroUsize;
 
 use lru::LruCache;
 
@@ -52,7 +52,7 @@ pub struct Config {
 	pub keystore: Option<SyncCryptoStorePtr>,
 
 	/// How many sessions should we keep in the cache?
-	pub session_cache_lru_size: usize,
+	pub session_cache_lru_size: NonZeroUsize,
 }
 
 /// Caching of session info.
@@ -95,7 +95,7 @@ impl Default for Config {
 		Self {
 			keystore: None,
 			// Usually we need to cache the current and the last session.
-			session_cache_lru_size: 2,
+			session_cache_lru_size: NonZeroUsize::new(2).expect("2 is larger than 0; qed"),
 		}
 	}
 }
@@ -109,7 +109,10 @@ impl RuntimeInfo {
 	/// Create with more elaborate configuration options.
 	pub fn new_with_config(cfg: Config) -> Self {
 		Self {
-			session_index_cache: LruCache::new(max(10, cfg.session_cache_lru_size)),
+			session_index_cache: LruCache::new(
+				cfg.session_cache_lru_size
+					.max(NonZeroUsize::new(10).expect("10 is larger than 0; qed")),
+			),
 			session_info_cache: LruCache::new(cfg.session_cache_lru_size),
 			keystore: cfg.keystore,
 		}
-- 
GitLab