From 17d0f30f19fb9c18f6d3d4dc57b64bb8fd5ddb19 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 21 Feb 2024 19:41:36 +0100 Subject: [PATCH] chore: add exceeds fn (#6716) --- crates/transaction-pool/src/pool/blob.rs | 8 +++++++- crates/transaction-pool/src/pool/parked.rs | 10 ++++++++-- crates/transaction-pool/src/pool/pending.rs | 12 +++++++++--- crates/transaction-pool/src/pool/txpool.rs | 5 +---- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/crates/transaction-pool/src/pool/blob.rs b/crates/transaction-pool/src/pool/blob.rs index 6d5b6ce1b1..6d2fa6822a 100644 --- a/crates/transaction-pool/src/pool/blob.rs +++ b/crates/transaction-pool/src/pool/blob.rs @@ -88,6 +88,12 @@ impl BlobTransactions { Vec::new() } + /// Returns true if the pool exceeds the given limit + #[inline] + pub(crate) fn exceeds(&self, limit: &SubPoolLimit) -> bool { + limit.is_exceeded(self.len(), self.size()) + } + /// The reported size of all transactions in this pool. pub(crate) fn size(&self) -> usize { self.size_of.into() @@ -188,7 +194,7 @@ impl BlobTransactions { ) -> Vec>> { let mut removed = Vec::new(); - while limit.is_exceeded(self.len(), self.size()) { + while self.exceeds(&limit) { let tx = self.all.last().expect("pool is not empty"); let id = *tx.transaction.id(); removed.push(self.remove_transaction(&id).expect("transaction exists")); diff --git a/crates/transaction-pool/src/pool/parked.rs b/crates/transaction-pool/src/pool/parked.rs index a764813cc4..e57548a466 100644 --- a/crates/transaction-pool/src/pool/parked.rs +++ b/crates/transaction-pool/src/pool/parked.rs @@ -156,7 +156,7 @@ impl ParkedPool { &mut self, limit: SubPoolLimit, ) -> Vec>> { - if !limit.is_exceeded(self.len(), self.size()) { + if !self.exceeds(&limit) { // if we are below the limits, we don't need to drop anything return Vec::new() } @@ -175,7 +175,7 @@ impl ParkedPool { removed.push(tx); } - if !limit.is_exceeded(self.len(), self.size()) { + if !self.exceeds(&limit) { break } } @@ -200,6 +200,12 @@ impl ParkedPool { self.by_id.len() } + /// Returns true if the pool exceeds the given limit + #[inline] + pub(crate) fn exceeds(&self, limit: &SubPoolLimit) -> bool { + limit.is_exceeded(self.len(), self.size()) + } + /// Returns whether the pool is empty #[cfg(test)] #[allow(dead_code)] diff --git a/crates/transaction-pool/src/pool/pending.rs b/crates/transaction-pool/src/pool/pending.rs index 69280508e2..7166a70a43 100644 --- a/crates/transaction-pool/src/pool/pending.rs +++ b/crates/transaction-pool/src/pool/pending.rs @@ -434,7 +434,7 @@ impl PendingPool { // return if either the pool is under limits or there are no more _eligible_ // transactions to remove - if !limit.is_exceeded(self.len(), self.size()) || non_local_senders == 0 { + if !self.exceeds(limit) || non_local_senders == 0 { return } } @@ -456,13 +456,13 @@ impl PendingPool { ) -> Vec>> { let mut removed = Vec::new(); // return early if the pool is already under the limits - if !limit.is_exceeded(self.len(), self.size()) { + if !self.exceeds(&limit) { return removed } // first truncate only non-local transactions, returning if the pool end up under the limit self.remove_to_limit(&limit, false, &mut removed); - if !limit.is_exceeded(self.len(), self.size()) { + if !self.exceeds(&limit) { return removed } @@ -473,6 +473,12 @@ impl PendingPool { removed } + /// Returns true if the pool exceeds the given limit + #[inline] + pub(crate) fn exceeds(&self, limit: &SubPoolLimit) -> bool { + limit.is_exceeded(self.len(), self.size()) + } + /// The reported size of all transactions in this pool. pub(crate) fn size(&self) -> usize { self.size_of.into() diff --git a/crates/transaction-pool/src/pool/txpool.rs b/crates/transaction-pool/src/pool/txpool.rs index a63b32c2d9..3a2755e677 100644 --- a/crates/transaction-pool/src/pool/txpool.rs +++ b/crates/transaction-pool/src/pool/txpool.rs @@ -761,10 +761,7 @@ impl TxPool { macro_rules! discard_worst { ($this:ident, $removed:ident, [$($limit:ident => $pool:ident),* $(,)*]) => { $ ( - while $this - .config - .$limit - .is_exceeded($this.$pool.len(), $this.$pool.size()) + while $this.$pool.exceeds(&$this.config.$limit) { trace!( "discarding transactions from {}, limit: {:?}, curr size: {}, curr len: {}",