forked from ava-labs/coreth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx_heap.go
163 lines (138 loc) · 3.4 KB
/
tx_heap.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
151
152
153
154
155
156
157
158
159
160
161
162
163
// (c) 2020-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package evm
import (
"container/heap"
"github.com/haowang0402/avalanchego/ids"
)
// txEntry is used to track the [gasPrice] transactions pay to be included in
// the mempool.
type txEntry struct {
id ids.ID
gasPrice uint64
tx *Tx
index int
}
// internalTxHeap is used to track pending atomic transactions by [gasPrice]
type internalTxHeap struct {
isMinHeap bool
items []*txEntry
lookup map[ids.ID]*txEntry
}
func newInternalTxHeap(items int, isMinHeap bool) *internalTxHeap {
return &internalTxHeap{
isMinHeap: isMinHeap,
items: make([]*txEntry, 0, items),
lookup: map[ids.ID]*txEntry{},
}
}
func (th internalTxHeap) Len() int { return len(th.items) }
func (th internalTxHeap) Less(i, j int) bool {
if th.isMinHeap {
return th.items[i].gasPrice < th.items[j].gasPrice
}
return th.items[i].gasPrice > th.items[j].gasPrice
}
func (th internalTxHeap) Swap(i, j int) {
th.items[i], th.items[j] = th.items[j], th.items[i]
th.items[i].index = i
th.items[j].index = j
}
func (th *internalTxHeap) Push(x interface{}) {
entry := x.(*txEntry)
if th.Has(entry.id) {
return
}
th.items = append(th.items, entry)
th.lookup[entry.id] = entry
}
func (th *internalTxHeap) Pop() interface{} {
n := len(th.items)
item := th.items[n-1]
th.items[n-1] = nil // avoid memory leak
th.items = th.items[0 : n-1]
delete(th.lookup, item.id)
return item
}
func (th *internalTxHeap) Get(id ids.ID) (*txEntry, bool) {
entry, ok := th.lookup[id]
if !ok {
return nil, false
}
return entry, true
}
func (th *internalTxHeap) Has(id ids.ID) bool {
_, has := th.Get(id)
return has
}
type txHeap struct {
maxHeap *internalTxHeap
minHeap *internalTxHeap
}
func newTxHeap(maxSize int) *txHeap {
return &txHeap{
maxHeap: newInternalTxHeap(maxSize, false),
minHeap: newInternalTxHeap(maxSize, true),
}
}
func (th *txHeap) Push(tx *Tx, gasPrice uint64) {
txID := tx.ID()
oldLen := th.Len()
heap.Push(th.maxHeap, &txEntry{
id: txID,
gasPrice: gasPrice,
tx: tx,
index: oldLen,
})
heap.Push(th.minHeap, &txEntry{
id: txID,
gasPrice: gasPrice,
tx: tx,
index: oldLen,
})
}
// Assumes there is non-zero items in [txHeap]
func (th *txHeap) PeekMax() (*Tx, uint64) {
txEntry := th.maxHeap.items[0]
return txEntry.tx, txEntry.gasPrice
}
// Assumes there is non-zero items in [txHeap]
func (th *txHeap) PeekMin() (*Tx, uint64) {
txEntry := th.minHeap.items[0]
return txEntry.tx, txEntry.gasPrice
}
// Assumes there is non-zero items in [txHeap]
func (th *txHeap) PopMax() *Tx {
return th.Remove(th.maxHeap.items[0].id)
}
// Assumes there is non-zero items in [txHeap]
func (th *txHeap) PopMin() *Tx {
return th.Remove(th.minHeap.items[0].id)
}
func (th *txHeap) Remove(id ids.ID) *Tx {
maxEntry, ok := th.maxHeap.Get(id)
if !ok {
return nil
}
heap.Remove(th.maxHeap, maxEntry.index)
minEntry, ok := th.minHeap.Get(id)
if !ok {
// This should never happen, as that would mean the heaps are out of
// sync.
return nil
}
return heap.Remove(th.minHeap, minEntry.index).(*txEntry).tx
}
func (th *txHeap) Len() int {
return th.maxHeap.Len()
}
func (th *txHeap) Get(id ids.ID) (*Tx, bool) {
txEntry, ok := th.maxHeap.Get(id)
if !ok {
return nil, false
}
return txEntry.tx, true
}
func (th *txHeap) Has(id ids.ID) bool {
return th.maxHeap.Has(id)
}