This repository was archived by the owner on Jan 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
EVM transaction replacement #206
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,8 +12,11 @@ var _ heap.Interface = (*TxPriorityQueue)(nil) | |
|
|
||
| // TxPriorityQueue defines a thread-safe priority queue for valid transactions. | ||
| type TxPriorityQueue struct { | ||
| mtx sync.RWMutex | ||
| txs []*WrappedTx // priority heap | ||
| mtx sync.RWMutex | ||
| txs []*WrappedTx // priority heap | ||
| // invariant 1: no duplicate nonce in the same queue | ||
| // invariant 2: no nonce gap in the same queue | ||
| // invariant 3: head of the queue must be in heap | ||
| evmQueue map[string][]*WrappedTx // sorted by nonce | ||
| } | ||
|
|
||
|
|
@@ -50,6 +53,51 @@ func NewTxPriorityQueue() *TxPriorityQueue { | |
| return pq | ||
| } | ||
|
|
||
| func (pq *TxPriorityQueue) GetTxWithSameNonce(tx *WrappedTx) (*WrappedTx, int) { | ||
| pq.mtx.RLock() | ||
| defer pq.mtx.RUnlock() | ||
| return pq.getTxWithSameNonceUnsafe(tx) | ||
| } | ||
|
|
||
| func (pq *TxPriorityQueue) getTxWithSameNonceUnsafe(tx *WrappedTx) (*WrappedTx, int) { | ||
| queue, ok := pq.evmQueue[tx.evmAddress] | ||
| if !ok { | ||
| return nil, -1 | ||
| } | ||
| idx := binarySearch(queue, tx) | ||
| if idx < len(queue) && queue[idx].evmNonce == tx.evmNonce { | ||
| return queue[idx], idx | ||
| } | ||
| return nil, -1 | ||
| } | ||
|
|
||
| func (pq *TxPriorityQueue) TryReplacement(tx *WrappedTx) (replaced *WrappedTx, shouldDrop bool) { | ||
| if !tx.isEVM { | ||
| return nil, false | ||
| } | ||
| pq.mtx.Lock() | ||
| defer pq.mtx.Unlock() | ||
| queue, ok := pq.evmQueue[tx.evmAddress] | ||
| if ok && len(queue) > 0 { | ||
| existing, idx := pq.getTxWithSameNonceUnsafe(tx) | ||
| if existing != nil { | ||
| if tx.priority > existing.priority { | ||
| // should replace | ||
| // replace heap if applicable | ||
|
Comment on lines
+85
to
+86
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit comment fix |
||
| if hi, ok := pq.findTxIndexUnsafe(existing); ok { | ||
| heap.Remove(pq, hi) | ||
| heap.Push(pq, tx) // need to be in the heap since it has the same nonce | ||
| } | ||
| pq.evmQueue[tx.evmAddress][idx] = tx // replace queue item in-place | ||
| return existing, false | ||
| } | ||
| // tx should be dropped since it's dominated by an existing tx | ||
| return nil, true | ||
| } | ||
| } | ||
| return nil, false | ||
| } | ||
|
|
||
| // GetEvictableTxs attempts to find and return a list of *WrappedTx than can be | ||
| // evicted to make room for another *WrappedTx with higher priority. If no such | ||
| // list of *WrappedTx exists, nil will be returned. The returned list of *WrappedTx | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
(once we have so many flags, it might make sense to split these out as named methods)
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.
yeah makes sense. Or group them into a
RemoveTxOptionsstruct that has named fields?