This repository has been archived by the owner on Dec 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 535
/
Copy pathqueue_account.go
150 lines (117 loc) · 2.69 KB
/
queue_account.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package txpool
import (
"container/heap"
"sync"
"sync/atomic"
"github.com/0xPolygon/polygon-edge/types"
)
// A thread-safe wrapper of a minNonceQueue.
// All methods assume the (correct) lock is held.
type accountQueue struct {
sync.RWMutex
wLock atomic.Bool
queue minNonceQueue
}
func newAccountQueue() *accountQueue {
q := accountQueue{
queue: make(minNonceQueue, 0),
}
heap.Init(&q.queue)
return &q
}
func (q *accountQueue) lock(write bool) {
if write {
q.Lock()
} else {
q.RLock()
}
q.wLock.Store(write)
}
func (q *accountQueue) unlock() {
if q.wLock.Swap(false) {
q.Unlock()
} else {
q.RUnlock()
}
}
// prune removes all transactions from the queue
// with nonce lower than given.
func (q *accountQueue) prune(nonce uint64) (
pruned []*types.Transaction,
) {
for {
if tx := q.peek(); tx == nil || tx.Nonce >= nonce {
break
}
pruned = append(pruned, q.pop())
}
return
}
// clear removes all transactions from the queue.
func (q *accountQueue) clear() (removed []*types.Transaction) {
// store txs
removed = q.queue
// clear the underlying queue
q.queue = q.queue[:0]
return
}
// push pushes the given transactions onto the queue.
func (q *accountQueue) push(tx *types.Transaction) {
heap.Push(&q.queue, tx)
}
// peek returns the first transaction from the queue without removing it.
func (q *accountQueue) peek() *types.Transaction {
return q.queue.Peek()
}
// pop removes the first transactions from the queue and returns it.
func (q *accountQueue) pop() *types.Transaction {
if q.length() == 0 {
return nil
}
transaction, ok := heap.Pop(&q.queue).(*types.Transaction)
if !ok {
return nil
}
return transaction
}
// length returns the number of transactions in the queue.
func (q *accountQueue) length() uint64 {
return uint64(q.queue.Len())
}
// transactions sorted by nonce (ascending)
type minNonceQueue []*types.Transaction
/* Queue methods required by the heap interface */
func (q *minNonceQueue) Peek() *types.Transaction {
if q.Len() == 0 {
return nil
}
return (*q)[0]
}
func (q *minNonceQueue) Len() int {
return len(*q)
}
func (q *minNonceQueue) Swap(i, j int) {
(*q)[i], (*q)[j] = (*q)[j], (*q)[i]
}
func (q *minNonceQueue) Less(i, j int) bool {
// The higher gas price Tx comes first if the nonces are same
if (*q)[i].Nonce == (*q)[j].Nonce {
return (*q)[i].GasPrice.Cmp((*q)[j].GasPrice) > 0
}
return (*q)[i].Nonce < (*q)[j].Nonce
}
func (q *minNonceQueue) Push(x interface{}) {
transaction, ok := x.(*types.Transaction)
if !ok {
return
}
*q = append(*q, transaction)
}
func (q *minNonceQueue) Pop() interface{} {
old := *q
n := len(old)
item := old[n-1]
old[n-1] = nil // avoid memory leak
*q = old[0 : n-1]
return item
}