From eaca2a4a7fbbdc2f5cd15eab9a8a18ede1891bda Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 21 Aug 2023 14:41:31 +0200 Subject: [PATCH] feat: provide a way to opt out of pool updates (#4270) --- crates/transaction-pool/src/pool/best.rs | 12 ++++++++++-- crates/transaction-pool/src/pool/pending.rs | 2 +- crates/transaction-pool/src/traits.rs | 9 +++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/crates/transaction-pool/src/pool/best.rs b/crates/transaction-pool/src/pool/best.rs index 7beb11e8ec..5fc5ebc931 100644 --- a/crates/transaction-pool/src/pool/best.rs +++ b/crates/transaction-pool/src/pool/best.rs @@ -25,6 +25,10 @@ impl crate::traits::BestTransactions for BestTransaction fn mark_invalid(&mut self, tx: &Self::Item) { BestTransactions::mark_invalid(&mut self.best, tx) } + + fn no_updates(&mut self) { + self.best.no_updates() + } } impl Iterator for BestTransactionsWithBasefee { @@ -67,7 +71,7 @@ pub(crate) struct BestTransactions { /// /// These new pending transactions are inserted into this iterator's pool before yielding the /// next value - pub(crate) new_transaction_reciever: Receiver>, + pub(crate) new_transaction_receiver: Option>>, } impl BestTransactions { @@ -87,7 +91,7 @@ impl BestTransactions { /// Non-blocking read on the new pending transactions subscription channel fn try_recv(&mut self) -> Option> { loop { - match self.new_transaction_reciever.try_recv() { + match self.new_transaction_receiver.as_mut()?.try_recv() { Ok(tx) => return Some(tx), // note TryRecvError::Lagged can be returned here, which is an error that attempts // to correct itself on consecutive try_recv() attempts @@ -126,6 +130,10 @@ impl crate::traits::BestTransactions for BestTransaction fn mark_invalid(&mut self, tx: &Self::Item) { BestTransactions::mark_invalid(self, tx) } + + fn no_updates(&mut self) { + self.new_transaction_receiver.take(); + } } impl Iterator for BestTransactions { diff --git a/crates/transaction-pool/src/pool/pending.rs b/crates/transaction-pool/src/pool/pending.rs index eaff315459..65be2a7c1e 100644 --- a/crates/transaction-pool/src/pool/pending.rs +++ b/crates/transaction-pool/src/pool/pending.rs @@ -88,7 +88,7 @@ impl PendingPool { all: self.by_id.clone(), independent: self.independent_transactions.clone(), invalid: Default::default(), - new_transaction_reciever: self.new_transaction_notifier.subscribe(), + new_transaction_receiver: Some(self.new_transaction_notifier.subscribe()), } } diff --git a/crates/transaction-pool/src/traits.rs b/crates/transaction-pool/src/traits.rs index a9411dd212..9d3c5026dc 100644 --- a/crates/transaction-pool/src/traits.rs +++ b/crates/transaction-pool/src/traits.rs @@ -509,11 +509,20 @@ pub trait BestTransactions: Iterator + Send { /// In other words, this must remove the given transaction _and_ drain all transaction that /// depend on it. fn mark_invalid(&mut self, transaction: &Self::Item); + + /// An iterator may be able to receive additional pending transactions that weren't present it + /// the pool when it was created. + /// + /// This ensures that iterator will return the best transaction that it currently knows and not + /// listen to pool updates. + fn no_updates(&mut self); } /// A no-op implementation that yields no transactions. impl BestTransactions for std::iter::Empty { fn mark_invalid(&mut self, _tx: &T) {} + + fn no_updates(&mut self) {} } /// Trait for transaction types used inside the pool