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 28
/
tx_heap.go
82 lines (68 loc) · 1.66 KB
/
tx_heap.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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package mempool
import (
"fmt"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/spacesvm/chain"
)
// txEntry is used to track the work transactions pay to be included in the
// mempool.
type txEntry struct {
id ids.ID
tx *chain.Transaction
price uint64
index int
}
// txHeap is used to track pending transactions by [price]
type txHeap struct {
isMinHeap bool
items []*txEntry
lookup map[ids.ID]*txEntry
}
func newTxHeap(items int, isMinHeap bool) *txHeap {
return &txHeap{
isMinHeap: isMinHeap,
items: make([]*txEntry, 0, items),
lookup: make(map[ids.ID]*txEntry, items),
}
}
func (th txHeap) Len() int { return len(th.items) }
func (th txHeap) Less(i, j int) bool {
if th.isMinHeap {
return th.items[i].price < th.items[j].price
}
return th.items[i].price > th.items[j].price
}
func (th txHeap) Swap(i, j int) {
th.items[i], th.items[j] = th.items[j], th.items[i]
th.items[i].index = i
th.items[j].index = j
}
func (th *txHeap) Push(x interface{}) {
entry, ok := x.(*txEntry)
if !ok {
panic(fmt.Errorf("unexpected %T, expected *txEntry", x))
}
if th.Has(entry.id) {
return
}
th.items = append(th.items, entry)
th.lookup[entry.id] = entry
}
func (th *txHeap) Pop() interface{} {
n := len(th.items)
item := th.items[n-1]
th.items[n-1] = nil // avoid memory leak
th.items = th.items[0 : n-1]
delete(th.lookup, item.id)
return item
}
func (th *txHeap) Get(id ids.ID) (*txEntry, bool) {
entry, ok := th.lookup[id]
return entry, ok
}
func (th *txHeap) Has(id ids.ID) bool {
_, has := th.Get(id)
return has
}