-
Notifications
You must be signed in to change notification settings - Fork 38
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
EVM transaction replacement #206
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,8 +12,11 @@ | |
|
||
// 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 @@ | |
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
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 | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
RemoveTxOptions
struct that has named fields?