diff --git a/crates/transaction-pool/src/pool/pending.rs b/crates/transaction-pool/src/pool/pending.rs index 34bbef1d47..fde9c36df4 100644 --- a/crates/transaction-pool/src/pool/pending.rs +++ b/crates/transaction-pool/src/pool/pending.rs @@ -522,11 +522,18 @@ impl PendingPool { /// Get transactions by sender pub(crate) fn get_txs_by_sender(&self, sender: SenderId) -> Vec { + self.iter_txs_by_sender(sender).copied().collect() + } + + /// Returns an iterator over all transaction with the sender id + pub(crate) fn iter_txs_by_sender( + &self, + sender: SenderId, + ) -> impl Iterator + '_ { self.by_id .range((sender.start_bound(), Unbounded)) .take_while(move |(other, _)| sender == other.sender) - .map(|(tx_id, _)| *tx_id) - .collect() + .map(|(tx_id, _)| tx_id) } /// Retrieves a transaction with the given ID from the pool, if it exists. diff --git a/crates/transaction-pool/src/pool/txpool.rs b/crates/transaction-pool/src/pool/txpool.rs index 2b587036aa..3c84bea80b 100644 --- a/crates/transaction-pool/src/pool/txpool.rs +++ b/crates/transaction-pool/src/pool/txpool.rs @@ -748,10 +748,9 @@ impl TxPool { } } - /// Determines if the tx sender is delegated or has a - /// pending delegation, and if so, ensures they have at most one in-flight - /// **executable** transaction, e.g. disallow stacked and nonce-gapped transactions - /// from the account. + /// Determines if the tx sender is delegated or has a pending delegation, and if so, ensures + /// they have at most one in-flight **executable** transaction, e.g. disallow stacked and + /// nonce-gapped transactions from the account. fn check_delegation_limit( &self, transaction: &ValidPoolTransaction, @@ -765,8 +764,10 @@ impl TxPool { return Ok(()) } - let pending_txs = self.pending_pool.get_txs_by_sender(transaction.sender_id()); - if pending_txs.is_empty() { + let mut txs_by_sender = + self.pending_pool.iter_txs_by_sender(transaction.sender_id()).peekable(); + + if txs_by_sender.peek().is_none() { // Transaction with gapped nonce is not supported for delegated accounts if transaction.nonce() > on_chain_nonce { return Err(PoolError::new( @@ -779,8 +780,8 @@ impl TxPool { return Ok(()) } - // Transaction replacement is supported - if pending_txs.contains(&transaction.transaction_id) { + if txs_by_sender.any(|id| id == &transaction.transaction_id) { + // Transaction replacement is supported return Ok(()) }