chore: use highest known nonce (#11784)

This commit is contained in:
Matthias Seitz
2024-10-16 11:44:15 +02:00
committed by GitHub
parent 0f903b1e20
commit e454b2402b

View File

@ -110,13 +110,21 @@ impl<T: TransactionOrdering> TxPool<T> {
/// Returns the transaction with the highest nonce that is executable given the on chain nonce. /// Returns the transaction with the highest nonce that is executable given the on chain nonce.
/// ///
/// If the pool already tracks a higher nonce for the given sender, then this nonce is used
/// instead.
///
/// Note: The next pending pooled transaction must have the on chain nonce. /// Note: The next pending pooled transaction must have the on chain nonce.
pub(crate) fn get_highest_consecutive_transaction_by_sender( pub(crate) fn get_highest_consecutive_transaction_by_sender(
&self, &self,
on_chain: TransactionId, mut on_chain: TransactionId,
) -> Option<Arc<ValidPoolTransaction<T::Transaction>>> { ) -> Option<Arc<ValidPoolTransaction<T::Transaction>>> {
let mut last_consecutive_tx = None; let mut last_consecutive_tx = None;
// ensure this operates on the most recent
if let Some(current) = self.sender_info.get(&on_chain.sender) {
on_chain.nonce = on_chain.nonce.max(current.state_nonce);
}
let mut next_expected_nonce = on_chain.nonce; let mut next_expected_nonce = on_chain.nonce;
for (id, tx) in self.all().descendant_txs_inclusive(&on_chain) { for (id, tx) in self.all().descendant_txs_inclusive(&on_chain) {
if next_expected_nonce != id.nonce { if next_expected_nonce != id.nonce {
@ -2784,7 +2792,7 @@ mod tests {
// Create transactions with nonces 0, 1, 2, 4, 5. // Create transactions with nonces 0, 1, 2, 4, 5.
let sender = Address::random(); let sender = Address::random();
let txs: Vec<_> = vec![0, 1, 2, 4, 5]; let txs: Vec<_> = vec![0, 1, 2, 4, 5, 8, 9];
for nonce in txs { for nonce in txs {
let mut mock_tx = MockTransaction::eip1559(); let mut mock_tx = MockTransaction::eip1559();
mock_tx.set_sender(sender); mock_tx.set_sender(sender);
@ -2804,6 +2812,13 @@ mod tests {
let next_tx = pool.get_highest_consecutive_transaction_by_sender(sender_id.into_id(5)); let next_tx = pool.get_highest_consecutive_transaction_by_sender(sender_id.into_id(5));
assert_eq!(next_tx.map(|tx| tx.nonce()), Some(5), "Expected nonce 5 for on-chain nonce 5"); assert_eq!(next_tx.map(|tx| tx.nonce()), Some(5), "Expected nonce 5 for on-chain nonce 5");
// update the tracked nonce
let mut info = SenderInfo::default();
info.update(8, U256::ZERO);
pool.sender_info.insert(sender_id, info);
let next_tx = pool.get_highest_consecutive_transaction_by_sender(sender_id.into_id(5));
assert_eq!(next_tx.map(|tx| tx.nonce()), Some(9), "Expected nonce 9 for on-chain nonce 8");
} }
#[test] #[test]