This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
mempool.go
232 lines (193 loc) · 5.35 KB
/
mempool.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package mempool
import (
"container/heap"
"sync"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/spacesvm/chain"
)
var _ chain.Mempool = &Mempool{}
type Mempool struct {
mu sync.RWMutex
g *chain.Genesis
maxSize int
maxHeap *txHeap
minHeap *txHeap
// Pending is a channel of length one, which the mempool ensures has an item on
// it as long as there is an unissued transaction remaining in [txs]
Pending chan struct{}
// newTxs is an array of [Tx] that are ready to be gossiped.
newTxs []*chain.Transaction
}
// New creates a new [Mempool]. [maxSize] must be > 0 or else the
// implementation may panic.
func New(g *chain.Genesis, maxSize int) *Mempool {
return &Mempool{
g: g,
maxSize: maxSize,
maxHeap: newTxHeap(maxSize, false),
minHeap: newTxHeap(maxSize, true),
Pending: make(chan struct{}, 1),
}
}
func (th *Mempool) Add(tx *chain.Transaction) bool {
txID := tx.ID()
price := tx.GetPrice()
th.mu.Lock()
defer th.mu.Unlock()
// Don't add duplicates
if th.maxHeap.Has(txID) {
return false
}
oldLen := th.maxHeap.Len()
// Optimistically add tx to mempool
heap.Push(th.maxHeap, &txEntry{
id: txID,
price: price,
tx: tx,
index: oldLen,
})
heap.Push(th.minHeap, &txEntry{
id: txID,
price: price,
tx: tx,
index: oldLen,
})
// Remove the lowest paying tx
//
// Note: we do this after adding the new transaction in case it is the new
// lowest paying transaction
if th.maxHeap.Len() > th.maxSize {
t, _ := th.popMin()
if t.ID() == txID {
return false
}
}
// When adding [tx] to the mempool make sure that there is an item in Pending
// to signal the VM to produce a block. Note: if the VM's buildStatus has already
// been set to something other than [dontBuild], this will be ignored and won't be
// reset until the engine calls BuildBlock. This case is handled in IssueCurrentTx
// and CancelCurrentTx.
th.newTxs = append(th.newTxs, tx)
th.addPending()
return true
}
// Assumes there is non-zero items in [Mempool]
func (th *Mempool) PeekMax() (*chain.Transaction, uint64) {
th.mu.RLock()
defer th.mu.RUnlock()
txEntry := th.maxHeap.items[0]
return txEntry.tx, txEntry.price
}
// Assumes there is non-zero items in [Mempool]
func (th *Mempool) PeekMin() (*chain.Transaction, uint64) {
th.mu.RLock()
defer th.mu.RUnlock()
txEntry := th.minHeap.items[0]
return txEntry.tx, txEntry.price
}
// Assumes there is non-zero items in [Mempool]
func (th *Mempool) PopMax() (*chain.Transaction, uint64) { // O(log N)
th.mu.Lock()
defer th.mu.Unlock()
item := th.maxHeap.items[0]
return th.remove(item.id), item.price
}
// Assumes there is non-zero items in [Mempool]
func (th *Mempool) PopMin() (*chain.Transaction, uint64) { // O(log N)
th.mu.Lock()
defer th.mu.Unlock()
return th.popMin()
}
func (th *Mempool) Remove(id ids.ID) *chain.Transaction { // O(log N)
th.mu.Lock()
defer th.mu.Unlock()
return th.remove(id)
}
// Prune removes all transactions that are not found in "validHashes".
func (th *Mempool) Prune(validHashes ids.Set) {
th.mu.RLock()
toRemove := []ids.ID{}
for _, txE := range th.maxHeap.items { // O(N)
if !validHashes.Contains(txE.tx.GetBlockID()) {
toRemove = append(toRemove, txE.id)
}
}
th.mu.RUnlock()
for _, txID := range toRemove { // O(K * log N)
th.Remove(txID)
}
}
func (th *Mempool) Len() int {
th.mu.RLock()
defer th.mu.RUnlock()
return th.maxHeap.Len()
}
func (th *Mempool) Get(id ids.ID) (*chain.Transaction, bool) {
th.mu.RLock()
defer th.mu.RUnlock()
txEntry, ok := th.maxHeap.Get(id)
if !ok {
return nil, false
}
return txEntry.tx, true
}
func (th *Mempool) Has(id ids.ID) bool {
th.mu.RLock()
defer th.mu.RUnlock()
return th.maxHeap.Has(id)
}
// GetNewTxs returns the array of [newTxs] and replaces it with a new array.
func (th *Mempool) NewTxs(maxUnits uint64) []*chain.Transaction {
th.mu.Lock()
defer th.mu.Unlock()
// Note: this algorithm preserves the ordering of new transactions
var (
units uint64
selected = th.newTxs[:0]
)
for i, tx := range th.newTxs {
// It is possible that a block may have been accepted that contains some
// new transactions before [NewTxs] is called.
if !th.maxHeap.Has(tx.ID()) {
continue
}
txUnits := tx.LoadUnits(th.g)
if txUnits > maxUnits-units {
th.newTxs = th.newTxs[i:]
return selected
}
units += txUnits
selected = append(selected, tx)
}
th.newTxs = th.newTxs[len(th.newTxs):]
return selected
}
// popMin assumes the write lock is held and takes O(log N) time to run.
func (th *Mempool) popMin() (*chain.Transaction, uint64) { // O(log N)
item := th.minHeap.items[0]
return th.remove(item.id), item.price
}
// remove assumes the write lock is held and takes O(log N) time to run.
func (th *Mempool) remove(id ids.ID) *chain.Transaction {
maxEntry, ok := th.maxHeap.Get(id) // O(1)
if !ok {
return nil
}
heap.Remove(th.maxHeap, maxEntry.index) // O(log N)
minEntry, ok := th.minHeap.Get(id) // O(1)
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 // O(log N)
}
// addPending makes sure that an item is in the Pending channel.
func (th *Mempool) addPending() {
select {
case th.Pending <- struct{}{}:
default:
}
}