-
Notifications
You must be signed in to change notification settings - Fork 642
/
mempool.go
300 lines (245 loc) · 7.17 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package mempool
import (
"errors"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/ava-labs/avalanchego/cache"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/txheap"
)
const (
// targetTxSize is the maximum number of bytes a transaction can use to be
// allowed into the mempool.
targetTxSize = 64 * units.KiB
// droppedTxIDsCacheSize is the maximum number of dropped txIDs to cache
droppedTxIDsCacheSize = 64
initialConsumedUTXOsSize = 512
// maxMempoolSize is the maximum number of bytes allowed in the mempool
maxMempoolSize = 64 * units.MiB
)
var (
_ Mempool = (*mempool)(nil)
errMempoolFull = errors.New("mempool is full")
)
type BlockTimer interface {
// ResetBlockTimer schedules a timer to notify the consensus engine once
// there is a block ready to be built. If a block is ready to be built when
// this function is called, the engine will be notified directly.
ResetBlockTimer()
}
type Mempool interface {
// we may want to be able to stop valid transactions
// from entering the mempool, e.g. during blocks creation
EnableAdding()
DisableAdding()
Add(tx *txs.Tx) error
Has(txID ids.ID) bool
Get(txID ids.ID) *txs.Tx
Remove(txs []*txs.Tx)
// Following Banff activation, all mempool transactions,
// (both decision and staker) are included into Standard blocks.
// HasTxs allow to check for availability of any mempool transaction.
HasTxs() bool
// PeekTxs returns the next txs for Banff blocks
// up to maxTxsBytes without removing them from the mempool.
PeekTxs(maxTxsBytes int) []*txs.Tx
HasStakerTx() bool
// PeekStakerTx returns the next stakerTx without removing it from mempool.
// It returns nil if !HasStakerTx().
// It's guaranteed that the returned tx, if not nil, is a StakerTx.
PeekStakerTx() *txs.Tx
// Note: dropped txs are added to droppedTxIDs but not
// not evicted from unissued decision/staker txs.
// This allows previously dropped txs to be possibly
// reissued.
MarkDropped(txID ids.ID, reason error)
GetDropReason(txID ids.ID) error
}
// Transactions from clients that have not yet been put into blocks and added to
// consensus
type mempool struct {
// If true, drop transactions added to the mempool via Add.
dropIncoming bool
bytesAvailableMetric prometheus.Gauge
bytesAvailable int
unissuedDecisionTxs txheap.Heap
unissuedStakerTxs txheap.Heap
// Key: Tx ID
// Value: Verification error
droppedTxIDs *cache.LRU[ids.ID, error]
consumedUTXOs set.Set[ids.ID]
blkTimer BlockTimer
}
func NewMempool(
namespace string,
registerer prometheus.Registerer,
blkTimer BlockTimer,
) (Mempool, error) {
bytesAvailableMetric := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "bytes_available",
Help: "Number of bytes of space currently available in the mempool",
})
if err := registerer.Register(bytesAvailableMetric); err != nil {
return nil, err
}
unissuedDecisionTxs, err := txheap.NewWithMetrics(
txheap.NewByAge(),
fmt.Sprintf("%s_decision_txs", namespace),
registerer,
)
if err != nil {
return nil, err
}
unissuedStakerTxs, err := txheap.NewWithMetrics(
txheap.NewByStartTime(),
fmt.Sprintf("%s_staker_txs", namespace),
registerer,
)
if err != nil {
return nil, err
}
bytesAvailableMetric.Set(maxMempoolSize)
return &mempool{
bytesAvailableMetric: bytesAvailableMetric,
bytesAvailable: maxMempoolSize,
unissuedDecisionTxs: unissuedDecisionTxs,
unissuedStakerTxs: unissuedStakerTxs,
droppedTxIDs: &cache.LRU[ids.ID, error]{Size: droppedTxIDsCacheSize},
consumedUTXOs: set.NewSet[ids.ID](initialConsumedUTXOsSize),
dropIncoming: false, // enable tx adding by default
blkTimer: blkTimer,
}, nil
}
func (m *mempool) EnableAdding() {
m.dropIncoming = false
}
func (m *mempool) DisableAdding() {
m.dropIncoming = true
}
func (m *mempool) Add(tx *txs.Tx) error {
if m.dropIncoming {
return fmt.Errorf("tx %s not added because mempool is closed", tx.ID())
}
// Note: a previously dropped tx can be re-added
txID := tx.ID()
if m.Has(txID) {
return fmt.Errorf("duplicate tx %s", txID)
}
txBytes := tx.Bytes()
if len(txBytes) > targetTxSize {
return fmt.Errorf("tx %s size (%d) > target size (%d)", txID, len(txBytes), targetTxSize)
}
if len(txBytes) > m.bytesAvailable {
return fmt.Errorf("%w, tx %s size (%d) exceeds available space (%d)",
errMempoolFull,
txID,
len(txBytes),
m.bytesAvailable,
)
}
inputs := tx.Unsigned.InputIDs()
if m.consumedUTXOs.Overlaps(inputs) {
return fmt.Errorf("tx %s conflicts with a transaction in the mempool", txID)
}
if err := tx.Unsigned.Visit(&issuer{
m: m,
tx: tx,
}); err != nil {
return err
}
// Mark these UTXOs as consumed in the mempool
m.consumedUTXOs.Union(inputs)
// An explicitly added tx must not be marked as dropped.
m.droppedTxIDs.Evict(txID)
m.blkTimer.ResetBlockTimer()
return nil
}
func (m *mempool) Has(txID ids.ID) bool {
return m.Get(txID) != nil
}
func (m *mempool) Get(txID ids.ID) *txs.Tx {
if tx := m.unissuedDecisionTxs.Get(txID); tx != nil {
return tx
}
return m.unissuedStakerTxs.Get(txID)
}
func (m *mempool) Remove(txsToRemove []*txs.Tx) {
remover := &remover{
m: m,
}
for _, tx := range txsToRemove {
remover.tx = tx
_ = tx.Unsigned.Visit(remover)
}
}
func (m *mempool) HasTxs() bool {
return m.unissuedDecisionTxs.Len() > 0 || m.unissuedStakerTxs.Len() > 0
}
func (m *mempool) PeekTxs(maxTxsBytes int) []*txs.Tx {
txs := m.unissuedDecisionTxs.List()
txs = append(txs, m.unissuedStakerTxs.List()...)
size := 0
for i, tx := range txs {
size += len(tx.Bytes())
if size > maxTxsBytes {
return txs[:i]
}
}
return txs
}
func (m *mempool) addDecisionTx(tx *txs.Tx) {
m.unissuedDecisionTxs.Add(tx)
m.register(tx)
}
func (m *mempool) addStakerTx(tx *txs.Tx) {
m.unissuedStakerTxs.Add(tx)
m.register(tx)
}
func (m *mempool) HasStakerTx() bool {
return m.unissuedStakerTxs.Len() > 0
}
func (m *mempool) removeDecisionTxs(txs []*txs.Tx) {
for _, tx := range txs {
txID := tx.ID()
if m.unissuedDecisionTxs.Remove(txID) != nil {
m.deregister(tx)
}
}
}
func (m *mempool) removeStakerTx(tx *txs.Tx) {
txID := tx.ID()
if m.unissuedStakerTxs.Remove(txID) != nil {
m.deregister(tx)
}
}
func (m *mempool) PeekStakerTx() *txs.Tx {
if m.unissuedStakerTxs.Len() == 0 {
return nil
}
return m.unissuedStakerTxs.Peek()
}
func (m *mempool) MarkDropped(txID ids.ID, reason error) {
m.droppedTxIDs.Put(txID, reason)
}
func (m *mempool) GetDropReason(txID ids.ID) error {
err, _ := m.droppedTxIDs.Get(txID)
return err
}
func (m *mempool) register(tx *txs.Tx) {
txBytes := tx.Bytes()
m.bytesAvailable -= len(txBytes)
m.bytesAvailableMetric.Set(float64(m.bytesAvailable))
}
func (m *mempool) deregister(tx *txs.Tx) {
txBytes := tx.Bytes()
m.bytesAvailable += len(txBytes)
m.bytesAvailableMetric.Set(float64(m.bytesAvailable))
inputs := tx.Unsigned.InputIDs()
m.consumedUTXOs.Difference(inputs)
}