-
Notifications
You must be signed in to change notification settings - Fork 661
/
mempool.go
220 lines (177 loc) · 4.84 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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package mempool
import (
"errors"
"fmt"
"sync"
"github.com/ava-labs/avalanchego/cache"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/linked"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/setmap"
"github.com/ava-labs/avalanchego/utils/units"
)
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
// maxMempoolSize is the maximum number of bytes allowed in the mempool
maxMempoolSize = 64 * units.MiB
)
var (
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")
)
type Tx interface {
InputIDs() set.Set[ids.ID]
ID() ids.ID
Size() int
}
type Metrics interface {
Update(numTxs, bytesAvailable int)
}
type Mempool[T Tx] interface {
Add(tx T) error
Get(txID ids.ID) (T, bool)
// Remove [txs] and any conflicts of [txs] from the mempool.
Remove(txs ...T)
// Peek returns the oldest tx in the mempool.
Peek() (tx T, exists bool)
// Iterate iterates over the txs until f returns false
Iterate(f func(tx T) bool)
// Note: dropped txs are added to droppedTxIDs but are 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
// Len returns the number of txs in the mempool.
Len() int
}
type mempool[T Tx] struct {
lock sync.RWMutex
unissuedTxs *linked.Hashmap[ids.ID, T]
consumedUTXOs *setmap.SetMap[ids.ID, ids.ID] // TxID -> Consumed UTXOs
bytesAvailable int
droppedTxIDs *cache.LRU[ids.ID, error] // TxID -> Verification error
metrics Metrics
}
func New[T Tx](
metrics Metrics,
) *mempool[T] {
m := &mempool[T]{
unissuedTxs: linked.NewHashmap[ids.ID, T](),
consumedUTXOs: setmap.New[ids.ID, ids.ID](),
bytesAvailable: maxMempoolSize,
droppedTxIDs: &cache.LRU[ids.ID, error]{Size: droppedTxIDsCacheSize},
metrics: metrics,
}
m.updateMetrics()
return m
}
func (m *mempool[T]) updateMetrics() {
m.metrics.Update(m.unissuedTxs.Len(), m.bytesAvailable)
}
func (m *mempool[T]) Add(tx T) error {
txID := tx.ID()
m.lock.Lock()
defer m.lock.Unlock()
if _, ok := m.unissuedTxs.Get(txID); ok {
return fmt.Errorf("%w: %s", ErrDuplicateTx, txID)
}
txSize := tx.Size()
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.InputIDs()
if m.consumedUTXOs.HasOverlap(inputs) {
return fmt.Errorf("%w: %s", ErrConflictsWithOtherTx, txID)
}
m.bytesAvailable -= txSize
m.unissuedTxs.Put(txID, tx)
m.updateMetrics()
// Mark these UTXOs as consumed in the mempool
m.consumedUTXOs.Put(txID, inputs)
// An added tx must not be marked as dropped.
m.droppedTxIDs.Evict(txID)
return nil
}
func (m *mempool[T]) Get(txID ids.ID) (T, bool) {
m.lock.RLock()
defer m.lock.RUnlock()
return m.unissuedTxs.Get(txID)
}
func (m *mempool[T]) Remove(txs ...T) {
m.lock.Lock()
defer m.lock.Unlock()
for _, tx := range txs {
txID := tx.ID()
// If the transaction is in the mempool, remove it.
if _, ok := m.consumedUTXOs.DeleteKey(txID); ok {
m.unissuedTxs.Delete(txID)
m.bytesAvailable += tx.Size()
continue
}
// If the transaction isn't in the mempool, remove any conflicts it has.
inputs := tx.InputIDs()
for _, removed := range m.consumedUTXOs.DeleteOverlapping(inputs) {
tx, _ := m.unissuedTxs.Get(removed.Key)
m.unissuedTxs.Delete(removed.Key)
m.bytesAvailable += tx.Size()
}
}
m.updateMetrics()
}
func (m *mempool[T]) Peek() (T, bool) {
m.lock.RLock()
defer m.lock.RUnlock()
_, tx, exists := m.unissuedTxs.Oldest()
return tx, exists
}
func (m *mempool[T]) Iterate(f func(T) bool) {
m.lock.RLock()
defer m.lock.RUnlock()
it := m.unissuedTxs.NewIterator()
for it.Next() {
if !f(it.Value()) {
return
}
}
}
func (m *mempool[_]) MarkDropped(txID ids.ID, reason error) {
if errors.Is(reason, ErrMempoolFull) {
return
}
m.lock.RLock()
defer m.lock.RUnlock()
if _, ok := m.unissuedTxs.Get(txID); ok {
return
}
m.droppedTxIDs.Put(txID, reason)
}
func (m *mempool[_]) GetDropReason(txID ids.ID) error {
err, _ := m.droppedTxIDs.Get(txID)
return err
}
func (m *mempool[_]) Len() int {
m.lock.RLock()
defer m.lock.RUnlock()
return m.unissuedTxs.Len()
}