/
txlist.go
executable file
·217 lines (201 loc) · 5.82 KB
/
txlist.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
/*
This file is part of go-palletone.
go-palletone is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
go-palletone is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with go-palletone. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* @author PalletOne core developers <dev@pallet.one>
* @date 2018
*/
package txspool
import (
"sort"
"strconv"
"sync"
"github.com/palletone/go-palletone/common"
"github.com/palletone/go-palletone/common/log"
"github.com/palletone/go-palletone/dag/modules"
)
type priorityHeap []*modules.TxPoolTransaction
func (h priorityHeap) Len() int { return len(h) }
func (h priorityHeap) Less(i, j int) bool {
ap, _ := strconv.ParseFloat(h[i].Priority_lvl, 64)
bp, _ := strconv.ParseFloat(h[j].Priority_lvl, 64)
return ap > bp
}
func (h priorityHeap) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}
func (h *priorityHeap) Push(x interface{}) {
item := x.(*modules.TxPoolTransaction)
*h = append(*h, item)
sort.Sort(*h)
}
func (h *priorityHeap) LastPop() interface{} {
old := *h
n := len(old)
if n <= 0 {
return nil
}
x := old[n-1]
*h = old[0 : n-1]
return x
}
func (h *priorityHeap) Pop() interface{} {
old := *h
n := len(old)
if n <= 0 {
return nil
}
x := old[0]
*h = old[1:]
return x
}
// txPricedList is a price-sorted heap to allow operating on transactions pool
// contents in a price-incrementing way.
type txPrioritiedList struct {
all *sync.Map // Pointer to the map of all transactions
items *priorityHeap // Heap of priority of all the stored transactions
stales int // Number of stale priority points to (re-heap trigger)
mu sync.RWMutex
}
// newTxPricedList creates a new price-sorted transaction heap.
func newTxPrioritiedList(all *sync.Map) *txPrioritiedList {
return &txPrioritiedList{
all: all,
items: new(priorityHeap),
}
}
// Put inserts a new transaction into the heap.
func (l *txPrioritiedList) Put(tx *modules.TxPoolTransaction) {
l.mu.Lock()
defer l.mu.Unlock()
l.items.Push(tx)
}
func (l *txPrioritiedList) Get() *modules.TxPoolTransaction {
l.mu.RLock()
defer l.mu.RUnlock()
for len(*l.items) > 0 {
tx := l.items.Pop().(*modules.TxPoolTransaction)
if _, ok := (*l.all).Load(tx.Tx.Hash()); !ok {
continue
}
if tx.Pending || tx.Discarded {
continue
}
return tx
}
return nil
}
func (l *txPrioritiedList) All() map[common.Hash]*modules.TxPoolTransaction {
txs := make(map[common.Hash]*modules.TxPoolTransaction)
(*l.all).Range(func(k, v interface{}) bool {
var hash common.Hash
hash.SetBytes((k.(common.Hash)).Bytes())
tx := v.(*modules.TxPoolTransaction)
txs[hash] = tx
return true
})
return txs
}
// Removed notifies the prices transaction list that an old transaction dropped
// from the pool. The list will just keep a counter of stale objects and update
// the heap if a large enough ratio of transactions go stale.
func (l *txPrioritiedList) Removed() {
// Bump the stale counter, but exit if still too low (< 20%)
l.mu.Lock()
defer l.mu.Unlock()
l.stales++
if l.stales <= len(*l.items)/5 {
return
}
// Seems we've reached a critical number of stale transactions, reheap
reheap := make(priorityHeap, 0)
l.stales, l.items = 0, &reheap
all := l.All()
for _, tx := range all {
if !tx.Pending && !tx.Discarded {
*l.items = append(*l.items, tx)
}
}
sort.Sort(*l.items)
}
func (l *txPrioritiedList) Cap(threshold float64) []*modules.TxPoolTransaction {
save := make([]*modules.TxPoolTransaction, 0)
drop := make([]*modules.TxPoolTransaction, 0)
l.mu.Lock()
defer l.mu.Unlock()
for len(*l.items) > 0 {
tx := l.items.Pop().(*modules.TxPoolTransaction)
if _, has := (*l.all).Load(tx.Tx.Hash()); !has {
l.stales--
continue
}
priority, _ := strconv.ParseFloat(tx.Priority_lvl, 64)
if priority > threshold {
save = append(save, tx)
break
}
drop = append(drop, tx)
}
for _, tx := range save {
l.items.Push(tx)
}
return drop
}
// Underpriced checks whether a transaction is cheaper than (or as cheap as) the
// lowest priced transaction currently being tracked.
func (l *txPrioritiedList) Underpriced(tx *modules.TxPoolTransaction) bool {
all := l.All()
if _, has := all[tx.Tx.Hash()]; has {
return false
}
l.mu.RLock()
defer l.mu.RUnlock()
// Discard stale price points if found at the heap start
for len(*l.items) > 0 {
head := []*modules.TxPoolTransaction(*l.items)[0]
if _, ok := (*l.all).Load(head.Tx.Hash()); !ok {
l.stales--
l.items.Pop()
continue
}
break
}
// Check if the transaction is underpriced or not
if len(*l.items) == 0 {
log.Error("Pricing query for empty pool") // This cannot happen, print to catch programming errors
return false
}
cheapest := []*modules.TxPoolTransaction(*l.items)[0]
cp, _ := strconv.ParseFloat(cheapest.Priority_lvl, 64)
tp, _ := strconv.ParseFloat(tx.Priority_lvl, 64)
return cp >= tp
}
// Discard finds a number of most underpriced transactions, removes them from the
// priced list and returns them for further removal from the entire pool.
func (l *txPrioritiedList) Discard(count int) modules.TxPoolTxs {
drop := make(modules.TxPoolTxs, 0, count) // Remote underpriced transactions to drop
all := l.All()
l.mu.RLock()
defer l.mu.RUnlock()
for len(*l.items) > 0 && count > 0 {
// Discard stale transactions if found during cleanup
tx := l.items.Pop().(*modules.TxPoolTransaction)
if _, ok := all[tx.Tx.Hash()]; !ok {
l.stales--
continue
}
drop = append(drop, tx)
count--
}
return drop
}