Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pending transaction ordering #12382

Merged
merged 17 commits into from
Nov 8, 2024
Merged

fix: pending transaction ordering #12382

merged 17 commits into from
Nov 8, 2024

Conversation

klkvr
Copy link
Member

@klkvr klkvr commented Nov 7, 2024

Closes #12340
Closes #12286

Updates the way transaction adding is handled in Pending pool.

  • add_transaction now correctly handles inserted transactions and does not make assumptions on their order. This is done by stroing a mapping sender -> higher/lower nonce instead of a simple set of highest/lowest transactions
  • whenever we update BestTransactions we now pre-sort the transactions vec by nonce to make sure that there will be no nonce gaps. I didn't apply same approach as with pending pool here because we insert pretty rary and need ability to get the independet tx with highes priority which is hard to do in a nice way with a hashmap

@klkvr klkvr requested a review from mattsse as a code owner November 7, 2024 15:39
@github-actions github-actions bot added the C-bug An unexpected or incorrect behavior label Nov 7, 2024
Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style nits

@klkvr klkvr force-pushed the klkvr/fix-ordering-pending branch from bd62618 to 8407c4a Compare November 7, 2024 17:28
Comment on lines 134 to 138
let mut new_txs = Vec::new();
while let Some(pending_tx) = self.try_recv() {
new_txs.push(pending_tx);
}
new_txs.sort_by_key(|tx| tx.transaction.nonce());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks a bit weird, why can we no longer process txs one by one?

how many txs are buffered in new_txs is arbitrary, so doesn't make sense that we have to sort this arbitrary chunk

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated BestTransactions, it now holds the following:

/// Transactions that can be executed right away: these have the expected nonce.
///
/// Once an `independent` transaction with the nonce `N` is returned, it unlocks `N+1`, which
/// then can be moved from the `all` set to the `independent` set.
pub(crate) independent: BTreeSet<PendingTransaction<T>>,
/// Mapping from sender to lowest nonce of its transactions in the pool
pub(crate) lowest_nonces: BTreeMap<SenderId, u64>,

when inserting, we make sure to replace independent tx if the nonce of tx being added is lower than the lowest we know for the given sender

that way we still don't make assumptions on order of transactions and don't have to sort everything

Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mattsse mattsse added this pull request to the merge queue Nov 8, 2024
Merged via the queue into main with commit 462540f Nov 8, 2024
41 checks passed
@mattsse mattsse deleted the klkvr/fix-ordering-pending branch November 8, 2024 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug An unexpected or incorrect behavior
Projects
None yet
2 participants