-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix(txpool): prevent removed transactions from being included in blocks #19908
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
base: main
Are you sure you want to change the base?
Conversation
Previously, there was a race condition where: 1. Block builder creates a BestTransactions iterator (snapshot via BTreeMap::clone()) 2. Maintenance job removes a transaction from the pool 3. Block builder's snapshot was independent, still containing the removed transaction This could cause expired transactions with time-based validity to be included in blocks after removal. This commit adds removal notifications: - PendingPool broadcasts transaction removals via a new channel - BestTransactions iterator subscribes to removal notifications - Iterator removes transactions from its snapshot when notified - Test verifies that removed transactions are not found in snapshots Fixes race condition for transactions with time-based expiration.
6574689 to
91709cf
Compare
|
could you elaborate on why
would be problematic, because atm I believe this would only happen if the pending pool is at capacity, but I assume you need this to satisfy some protocol level rules?
not following the sequence of events here that's causing db/State root issues, maybe this has something to do with some protocol specific rules? |
mattsse
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lacking some context on why excluding removed txs is critical
but maybe this is similar to OP interop inclusion rules
reth/crates/optimism/txpool/src/transaction.rs
Lines 104 to 115 in 9f3949c
| impl<Cons, Pooled> MaybeInteropTransaction for OpPooledTransaction<Cons, Pooled> { | |
| fn set_interop_deadline(&self, deadline: u64) { | |
| self.interop.store(deadline, Ordering::Relaxed); | |
| } | |
| fn interop_deadline(&self) -> Option<u64> { | |
| let interop = self.interop.load(Ordering::Relaxed); | |
| if interop > NO_INTEROP_TX { | |
| return Some(interop) | |
| } | |
| None | |
| } |
which is checked during block building for example
reth/crates/optimism/payload/src/builder.rs
Line 696 in 9f3949c
| let interop = tx.interop_deadline(); |
reth/crates/optimism/payload/src/builder.rs
Lines 730 to 737 in 9f3949c
| // We skip invalid cross chain txs, they would be removed on the next block update in | |
| // the maintenance job | |
| if let Some(interop) = interop && | |
| !is_valid_interop(interop, self.config.attributes.timestamp()) | |
| { | |
| best_txs.mark_invalid(tx.signer(), tx.nonce()); | |
| continue | |
| } |
| pub(crate) new_transaction_receiver: Option<Receiver<PendingTransaction<T>>>, | ||
| /// Used to receive transaction removals from the pool after this iterator was created. | ||
| /// | ||
| /// Removed transactions are deleted from this iterator's snapshot before yielding the next | ||
| /// value, preventing inclusion of transactions that were removed by maintenance jobs. | ||
| pub(crate) removed_transaction_receiver: Option<Receiver<TransactionId>>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not opposed to this, but since this is an internal channel we can unify this by introducing
enum PendingPoolEvent<T> {Added(Tx),Removed(tx)}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mattsse Thanks for the feedback. I think my original description was misleading.
The issue is not that removed transactions get included in blocks. The actual problem is that when a transaction is removed from the pool (e.g., due to --txpool.lifetime expiration), the BestTransactions iterator still holds references to it in its snapshot. During block building, this mismatch causes the iterator to get stuck trying to access transactions that no longer exist in the pool's state, leading to long-running DB read transactions and eventually blocking block production.
I've also unified the two notification channels into a single PendingPoolEvent enum as you suggested:
0f2d0af
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
leading to long-running DB read transactions and eventually blocking block production.
can you expand on this a bit more, because I don't quite understand how this would cause issues during building, is there something that just endlessly loops then? and how exactly
Merge new_transaction_notifier and removed_transaction_notifier into a single event_notifier using PendingPoolEvent enum.
|
@wo-o we had another recent fix for updates in this one c7b6890 unsure if related, but we now have a smol conflict, sorry about this. I have one more questions, because it's unclear how still behaviour would stall block production because this iter is a full snapshot, basically independent of the pool |
Problem
A race condition exists where
BestTransactionsiterator creates an independent snapshot viaBTreeMap::clone(). When the maintenance job removes transactions from the pool, existing iterators remain unaware, causing removed transactions to be included in blocks.Production Impact
During load testing with
--txpool.lifetime 600, this caused sequencer failure:The sequencer stopped producing blocks because:
This is critical for transactions with time-based expiration.
Root Cause
crates/transaction-pool/src/pool/pending.rs:110- The iterator snapshot is created viaBTreeMap::clone(), which creates an independent copy. The pool has notification support for new transactions but lacks removal notifications.Solution
Add removal notifications following the existing pattern for new transactions:
PendingPoolbroadcasts transaction removals via newremoved_transaction_notifierchannelBestTransactionssubscribes to removal notifications on creation