forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
mempool.go
215 lines (175 loc) · 5.23 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
// 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/MetalBlockchain/metalgo/cache"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/snow/engine/common"
"github.com/MetalBlockchain/metalgo/utils/linkedhashmap"
"github.com/MetalBlockchain/metalgo/utils/set"
"github.com/MetalBlockchain/metalgo/utils/units"
"github.com/MetalBlockchain/metalgo/vms/avm/txs"
)
const (
// MaxTxSize is the maximum number of bytes a transaction can use to be
// allowed into the mempool.
MaxTxSize = 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)
errDuplicateTx = errors.New("duplicate tx")
errTxTooLarge = errors.New("tx too large")
errMempoolFull = errors.New("mempool is full")
errConflictsWithOtherTx = errors.New("tx conflicts with other tx")
)
// Mempool contains transactions that have not yet been put into a block.
type Mempool interface {
Add(tx *txs.Tx) error
Has(txID ids.ID) bool
Get(txID ids.ID) *txs.Tx
Remove(txs []*txs.Tx)
// Peek returns the first tx in the mempool whose size is <= [maxTxSize].
Peek(maxTxSize int) *txs.Tx
// RequestBuildBlock notifies the consensus engine that a block should be
// built if there is at least one transaction in the mempool.
RequestBuildBlock()
// Note: Dropped txs are added to droppedTxIDs but not evicted from
// unissued. This allows previously dropped txs to be possibly reissued.
MarkDropped(txID ids.ID, reason error)
GetDropReason(txID ids.ID) error
}
type mempool struct {
bytesAvailableMetric prometheus.Gauge
bytesAvailable int
unissuedTxs linkedhashmap.LinkedHashmap[ids.ID, *txs.Tx]
numTxs prometheus.Gauge
toEngine chan<- common.Message
// Key: Tx ID
// Value: Verification error
droppedTxIDs *cache.LRU[ids.ID, error]
consumedUTXOs set.Set[ids.ID]
}
func New(
namespace string,
registerer prometheus.Registerer,
toEngine chan<- common.Message,
) (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
}
numTxsMetric := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "count",
Help: "Number of transactions in the mempool",
})
if err := registerer.Register(numTxsMetric); err != nil {
return nil, err
}
bytesAvailableMetric.Set(maxMempoolSize)
return &mempool{
bytesAvailableMetric: bytesAvailableMetric,
bytesAvailable: maxMempoolSize,
unissuedTxs: linkedhashmap.New[ids.ID, *txs.Tx](),
numTxs: numTxsMetric,
toEngine: toEngine,
droppedTxIDs: &cache.LRU[ids.ID, error]{Size: droppedTxIDsCacheSize},
consumedUTXOs: set.NewSet[ids.ID](initialConsumedUTXOsSize),
}, nil
}
func (m *mempool) Add(tx *txs.Tx) error {
// Note: a previously dropped tx can be re-added
txID := tx.ID()
if m.Has(txID) {
return fmt.Errorf("%w: %s", errDuplicateTx, txID)
}
txSize := len(tx.Bytes())
if txSize > MaxTxSize {
return fmt.Errorf("%w: %s size (%d) > max size (%d)",
errTxTooLarge,
txID,
txSize,
MaxTxSize,
)
}
if txSize > m.bytesAvailable {
return fmt.Errorf("%w: %s size (%d) > available space (%d)",
errMempoolFull,
txID,
txSize,
m.bytesAvailable,
)
}
inputs := tx.Unsigned.InputIDs()
if m.consumedUTXOs.Overlaps(inputs) {
return fmt.Errorf("%w: %s", errConflictsWithOtherTx, txID)
}
m.bytesAvailable -= txSize
m.bytesAvailableMetric.Set(float64(m.bytesAvailable))
m.unissuedTxs.Put(txID, tx)
m.numTxs.Inc()
// 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)
return nil
}
func (m *mempool) Has(txID ids.ID) bool {
return m.Get(txID) != nil
}
func (m *mempool) Get(txID ids.ID) *txs.Tx {
tx, _ := m.unissuedTxs.Get(txID)
return tx
}
func (m *mempool) Remove(txsToRemove []*txs.Tx) {
for _, tx := range txsToRemove {
txID := tx.ID()
if !m.unissuedTxs.Delete(txID) {
continue
}
m.bytesAvailable += len(tx.Bytes())
m.bytesAvailableMetric.Set(float64(m.bytesAvailable))
m.numTxs.Dec()
inputs := tx.Unsigned.InputIDs()
m.consumedUTXOs.Difference(inputs)
}
}
func (m *mempool) Peek(maxTxSize int) *txs.Tx {
txIter := m.unissuedTxs.NewIterator()
for txIter.Next() {
tx := txIter.Value()
txSize := len(tx.Bytes())
if txSize <= maxTxSize {
return tx
}
}
return nil
}
func (m *mempool) RequestBuildBlock() {
if m.unissuedTxs.Len() == 0 {
return
}
select {
case m.toEngine <- common.PendingTxs:
default:
}
}
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
}