-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmempool.go
More file actions
566 lines (472 loc) · 13 KB
/
mempool.go
File metadata and controls
566 lines (472 loc) · 13 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
package main
import (
"container/heap"
"encoding/json"
"fmt"
"sync"
"time"
)
// MempoolConfig configures the mempool
type MempoolConfig struct {
// MaxSize is the maximum number of transactions
MaxSize int
// MaxSizeBytes is the maximum total size in bytes
MaxSizeBytes int
// MinFeeRate is the minimum fee per byte to accept
MinFeeRate uint64
// ExpirationTime is how long a tx stays in mempool
ExpirationTime time.Duration
}
// DefaultMempoolConfig returns sensible defaults
func DefaultMempoolConfig() MempoolConfig {
return MempoolConfig{
MaxSize: 5000,
MaxSizeBytes: 100 * 1024 * 1024, // 100 MB
MinFeeRate: 1, // 1 unit per byte
ExpirationTime: 24 * time.Hour,
}
}
// MempoolEntry represents a transaction in the mempool
type MempoolEntry struct {
Tx *Transaction
TxID [32]byte // Transaction ID hash
TxData []byte // Serialized transaction
Fee uint64 // Transaction fee
FeeRate uint64 // Fee per byte
Size int // Size in bytes
AddedAt time.Time // When added to mempool
Height uint64 // Block height when added
// For priority queue
index int
}
// Mempool stores unconfirmed transactions
type Mempool struct {
mu sync.RWMutex
config MempoolConfig
// Transaction storage
txByID map[[32]byte]*MempoolEntry // TxID -> Entry
txByImage map[[32]byte][32]byte // KeyImage -> TxID (for double-spend detection)
// Priority queue (highest fee rate first)
priorityQueue txPriorityQueue
// Key image checker for validation (provided by chain)
isKeyImageSpent KeyImageChecker
// Ring member checker for canonical output binding.
isCanonicalRingMember RingMemberChecker
// Stats
totalSize int // Total bytes in mempool
}
// NewMempool creates a new mempool
func NewMempool(cfg MempoolConfig, isSpent KeyImageChecker, isCanonicalRingMember RingMemberChecker) *Mempool {
return &Mempool{
config: cfg,
txByID: make(map[[32]byte]*MempoolEntry),
txByImage: make(map[[32]byte][32]byte),
priorityQueue: make(txPriorityQueue, 0),
isKeyImageSpent: isSpent,
isCanonicalRingMember: isCanonicalRingMember,
}
}
// AddTransaction adds a transaction to the mempool.
func (m *Mempool) AddTransaction(tx *Transaction, txData []byte) error {
parsedTx, err := DeserializeTx(txData)
if err != nil {
return fmt.Errorf("invalid transaction data: %w", err)
}
if parsedTx.IsCoinbase() {
return fmt.Errorf("coinbase transaction cannot be added to mempool")
}
txID, err := parsedTx.TxID()
if err != nil {
return fmt.Errorf("failed to get tx ID: %w", err)
}
if err := ValidateTransaction(parsedTx, m.isKeyImageSpent, m.isCanonicalRingMember); err != nil {
return fmt.Errorf("validation failed: %w", err)
}
size := len(txData)
feeRate := tx.Fee / uint64(size)
m.mu.Lock()
defer m.mu.Unlock()
if _, exists := m.txByID[txID]; exists {
return nil
}
for _, input := range parsedTx.Inputs {
if existingTxID, exists := m.txByImage[input.KeyImage]; exists {
return fmt.Errorf("double-spend: key image already in mempool (tx %x...)", existingTxID[:8])
}
}
if feeRate < m.config.MinFeeRate {
return fmt.Errorf("fee rate too low: %d < %d", feeRate, m.config.MinFeeRate)
}
if len(m.txByID) >= m.config.MaxSize {
if !m.evictLowest(feeRate) {
return fmt.Errorf("mempool full")
}
}
if m.totalSize+size > m.config.MaxSizeBytes {
for m.totalSize+size > m.config.MaxSizeBytes && len(m.priorityQueue) > 0 {
if !m.evictLowest(feeRate) {
return fmt.Errorf("mempool size limit exceeded")
}
}
}
entry := &MempoolEntry{
Tx: parsedTx,
TxID: txID,
TxData: txData,
Fee: parsedTx.Fee,
FeeRate: feeRate,
Size: size,
AddedAt: time.Now(),
}
m.txByID[txID] = entry
for _, input := range parsedTx.Inputs {
m.txByImage[input.KeyImage] = txID
}
heap.Push(&m.priorityQueue, entry)
m.totalSize += size
return nil
}
// evictLowest removes the lowest fee rate transaction if it's lower than minFeeRate
func (m *Mempool) evictLowest(minFeeRate uint64) bool {
if len(m.priorityQueue) == 0 {
return false
}
// Peek at lowest
// Note: our heap is max-heap by fee rate, so we need min-heap for eviction
// For simplicity, we'll just remove oldest if at capacity
var oldest *MempoolEntry
var oldestID [32]byte
for id, entry := range m.txByID {
if oldest == nil || entry.AddedAt.Before(oldest.AddedAt) {
oldest = entry
oldestID = id
}
}
if oldest != nil && oldest.FeeRate < minFeeRate {
m.removeTxByID(oldestID)
return true
}
return false
}
// removeTxByID removes a transaction from mempool by ID
func (m *Mempool) removeTxByID(txID [32]byte) {
entry, exists := m.txByID[txID]
if !exists {
return
}
delete(m.txByID, txID)
for _, input := range entry.Tx.Inputs {
delete(m.txByImage, input.KeyImage)
}
m.totalSize -= entry.Size
// Remove from priority queue
if entry.index >= 0 && entry.index < len(m.priorityQueue) {
heap.Remove(&m.priorityQueue, entry.index)
}
}
// RemoveTransaction removes a transaction by ID
func (m *Mempool) RemoveTransaction(txID [32]byte) {
m.mu.Lock()
defer m.mu.Unlock()
m.removeTxByID(txID)
}
// RemoveTransactions removes multiple transactions
func (m *Mempool) RemoveTransactions(txIDs [][32]byte) {
m.mu.Lock()
defer m.mu.Unlock()
for _, txID := range txIDs {
m.removeTxByID(txID)
}
}
// RemoveByKeyImage removes any transaction using a specific key image
func (m *Mempool) RemoveByKeyImage(keyImage [32]byte) {
m.mu.Lock()
defer m.mu.Unlock()
if txID, exists := m.txByImage[keyImage]; exists {
m.removeTxByID(txID)
}
}
// GetTransaction returns a transaction by ID
func (m *Mempool) GetTransaction(txID [32]byte) (*Transaction, bool) {
m.mu.RLock()
defer m.mu.RUnlock()
entry, exists := m.txByID[txID]
if !exists {
return nil, false
}
return entry.Tx, true
}
// HasTransaction checks if a transaction is in the mempool
func (m *Mempool) HasTransaction(txID [32]byte) bool {
m.mu.RLock()
defer m.mu.RUnlock()
_, exists := m.txByID[txID]
return exists
}
// HasKeyImage checks if a key image is used by any mempool tx
func (m *Mempool) HasKeyImage(keyImage [32]byte) bool {
m.mu.RLock()
defer m.mu.RUnlock()
_, exists := m.txByImage[keyImage]
return exists
}
// GetTransactionsForBlock returns transactions for mining sorted by fee rate.
func (m *Mempool) GetTransactionsForBlock(maxSize int, maxCount int) []*Transaction {
m.mu.RLock()
defer m.mu.RUnlock()
// Collect entries sorted by fee rate (priority queue gives us this)
entries := make([]*MempoolEntry, 0, len(m.txByID))
for _, entry := range m.txByID {
entries = append(entries, entry)
}
// Sort by fee rate descending
for i := 0; i < len(entries)-1; i++ {
for j := i + 1; j < len(entries); j++ {
if entries[j].FeeRate > entries[i].FeeRate {
entries[i], entries[j] = entries[j], entries[i]
}
}
}
// Select transactions
result := make([]*Transaction, 0, maxCount)
totalSize := 0
for _, entry := range entries {
if len(result) >= maxCount {
break
}
if totalSize+entry.Size > maxSize {
continue // Skip, try next
}
result = append(result, entry.Tx)
totalSize += entry.Size
}
return result
}
// Size returns the number of transactions in mempool
func (m *Mempool) Size() int {
m.mu.RLock()
defer m.mu.RUnlock()
return len(m.txByID)
}
// SizeBytes returns the total size in bytes
func (m *Mempool) SizeBytes() int {
m.mu.RLock()
defer m.mu.RUnlock()
return m.totalSize
}
// Clear removes all transactions
func (m *Mempool) Clear() {
m.mu.Lock()
defer m.mu.Unlock()
m.txByID = make(map[[32]byte]*MempoolEntry)
m.txByImage = make(map[[32]byte][32]byte)
m.priorityQueue = make(txPriorityQueue, 0)
m.totalSize = 0
}
// RemoveExpired removes transactions that have been in mempool too long
func (m *Mempool) RemoveExpired() int {
m.mu.Lock()
defer m.mu.Unlock()
cutoff := time.Now().Add(-m.config.ExpirationTime)
removed := 0
// Collect IDs to remove (can't modify map while iterating)
var toRemove [][32]byte
for txID, entry := range m.txByID {
if entry.AddedAt.Before(cutoff) {
toRemove = append(toRemove, txID)
}
}
for _, txID := range toRemove {
m.removeTxByID(txID)
removed++
}
return removed
}
// OnBlockConnected updates mempool when a block is connected
// Removes transactions that were included in the block
func (m *Mempool) OnBlockConnected(block *Block) {
m.mu.Lock()
defer m.mu.Unlock()
for _, tx := range block.Transactions {
txID, err := tx.TxID()
if err != nil {
continue
}
if _, exists := m.txByID[txID]; exists {
m.removeTxByID(txID)
}
// Also remove by key images (in case of double-spend attempts)
for _, input := range tx.Inputs {
if existingTxID, exists := m.txByImage[input.KeyImage]; exists {
m.removeTxByID(existingTxID)
}
}
}
}
// OnBlockDisconnected updates mempool when a block is disconnected (reorg)
// Re-adds valid transactions from the disconnected block
func (m *Mempool) OnBlockDisconnected(block *Block, txDataMap map[[32]byte][]byte) {
m.mu.Lock()
defer m.mu.Unlock()
for _, tx := range block.Transactions {
// Skip coinbase - can't go back to mempool
if tx.IsCoinbase() {
continue
}
txID, err := tx.TxID()
if err != nil {
continue
}
// Already present (e.g. gossiped while block was on-chain) - keep existing
// entry/heap state unchanged.
if _, exists := m.txByID[txID]; exists {
continue
}
// Get serialized tx data if available
txData, ok := txDataMap[txID]
if !ok {
txData = tx.Serialize()
}
// Check if any inputs are now spent (by another chain)
// Skip if key images are spent
valid := true
for _, input := range tx.Inputs {
// Keep mempool free of double-spends.
if existingTxID, exists := m.txByImage[input.KeyImage]; exists && existingTxID != txID {
valid = false
break
}
if m.isKeyImageSpent(input.KeyImage) {
valid = false
break
}
}
if valid {
// Re-add to mempool (using internal method to avoid double-lock)
size := len(txData)
if size == 0 {
continue
}
feeRate := tx.Fee / uint64(size)
entry := &MempoolEntry{
Tx: tx,
TxID: txID,
TxData: txData,
Fee: tx.Fee,
FeeRate: feeRate,
Size: size,
AddedAt: time.Now(),
}
m.txByID[txID] = entry
for _, input := range tx.Inputs {
m.txByImage[input.KeyImage] = txID
}
heap.Push(&m.priorityQueue, entry)
m.totalSize += size
}
}
}
// GetAllTransactionData returns all serialized transactions
func (m *Mempool) GetAllTransactionData() [][]byte {
m.mu.RLock()
defer m.mu.RUnlock()
result := make([][]byte, 0, len(m.txByID))
for _, entry := range m.txByID {
result = append(result, entry.TxData)
}
return result
}
// Stats returns mempool statistics
type MempoolStats struct {
Count int `json:"count"`
SizeBytes int `json:"size_bytes"`
MinFee uint64 `json:"min_fee"`
MaxFee uint64 `json:"max_fee"`
AvgFee float64 `json:"avg_fee"`
}
func (m *Mempool) Stats() MempoolStats {
m.mu.RLock()
defer m.mu.RUnlock()
stats := MempoolStats{
Count: len(m.txByID),
SizeBytes: m.totalSize,
}
if stats.Count == 0 {
return stats
}
var totalFee uint64
for _, entry := range m.txByID {
if stats.MinFee == 0 || entry.Fee < stats.MinFee {
stats.MinFee = entry.Fee
}
if entry.Fee > stats.MaxFee {
stats.MaxFee = entry.Fee
}
totalFee += entry.Fee
}
stats.AvgFee = float64(totalFee) / float64(stats.Count)
return stats
}
// GetAllEntries returns all mempool entries sorted by fee rate (highest first)
func (m *Mempool) GetAllEntries() []*MempoolEntry {
m.mu.RLock()
defer m.mu.RUnlock()
entries := make([]*MempoolEntry, 0, len(m.txByID))
for _, entry := range m.txByID {
entries = append(entries, entry)
}
// Sort by fee rate descending
for i := 0; i < len(entries)-1; i++ {
for j := i + 1; j < len(entries); j++ {
if entries[j].FeeRate > entries[i].FeeRate {
entries[i], entries[j] = entries[j], entries[i]
}
}
}
return entries
}
// MarshalJSON serializes mempool for debugging
func (m *Mempool) MarshalJSON() ([]byte, error) {
m.mu.RLock()
defer m.mu.RUnlock()
data := struct {
Count int `json:"count"`
SizeBytes int `json:"size_bytes"`
TxIDs []string `json:"tx_ids"`
}{
Count: len(m.txByID),
SizeBytes: m.totalSize,
TxIDs: make([]string, 0, len(m.txByID)),
}
for txID := range m.txByID {
data.TxIDs = append(data.TxIDs, fmt.Sprintf("%x", txID))
}
return json.Marshal(data)
}
// Priority queue implementation for mempool
type txPriorityQueue []*MempoolEntry
func (pq txPriorityQueue) Len() int { return len(pq) }
func (pq txPriorityQueue) Less(i, j int) bool {
// Higher fee rate = higher priority
return pq[i].FeeRate > pq[j].FeeRate
}
func (pq txPriorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].index = i
pq[j].index = j
}
func (pq *txPriorityQueue) Push(x interface{}) {
n := len(*pq)
entry := x.(*MempoolEntry)
entry.index = n
*pq = append(*pq, entry)
}
func (pq *txPriorityQueue) Pop() interface{} {
old := *pq
n := len(old)
entry := old[n-1]
old[n-1] = nil
entry.index = -1
*pq = old[0 : n-1]
return entry
}