forked from decred/dcrd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmining_test.go
280 lines (260 loc) · 7.75 KB
/
mining_test.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
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
// Copyright (c) 2016 The btcsuite developers
// Copyright (c) 2015-2016 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"container/heap"
"math/rand"
"testing"
"github.com/decred/dcrd/blockchain/stake"
"github.com/decred/dcrutil"
)
// TestTxFeePrioHeap ensures the priority queue for transaction fees and
// priorities works as expected. It doesn't set anything for the stake
// priority, so this test only tests sorting by fee and then priority.
func TestTxFeePrioHeap(t *testing.T) {
// Create some fake priority items that exercise the expected sort
// edge conditions.
testItems := []*txPrioItem{
{feePerKB: 5678, priority: 1},
{feePerKB: 5678, priority: 1}, // Duplicate fee and prio
{feePerKB: 5678, priority: 5},
{feePerKB: 5678, priority: 2},
{feePerKB: 1234, priority: 3},
{feePerKB: 1234, priority: 1},
{feePerKB: 1234, priority: 5},
{feePerKB: 1234, priority: 5}, // Duplicate fee and prio
{feePerKB: 1234, priority: 2},
{feePerKB: 10000, priority: 0}, // Higher fee, smaller prio
{feePerKB: 0, priority: 10000}, // Higher prio, lower fee
}
numItems := len(testItems)
// Add random data in addition to the edge conditions already manually
// specified.
randSeed := rand.Int63()
defer func() {
if t.Failed() {
t.Logf("Random numbers using seed: %v", randSeed)
}
}()
prng := rand.New(rand.NewSource(randSeed))
for i := 0; i < 1000; i++ {
testItems = append(testItems, &txPrioItem{
feePerKB: prng.Float64() * dcrutil.AtomsPerCoin,
priority: prng.Float64() * 100,
})
}
// Test sorting by fee per KB then priority.
var highest *txPrioItem
priorityQueue := newTxPriorityQueue(len(testItems), txPQByFee)
for i := 0; i < len(testItems); i++ {
prioItem := testItems[i]
if highest == nil {
highest = prioItem
}
if prioItem.feePerKB >= highest.feePerKB {
highest = prioItem
if prioItem.feePerKB == highest.feePerKB {
if prioItem.priority >= highest.priority {
highest = prioItem
}
}
}
heap.Push(priorityQueue, prioItem)
}
for i := 0; i < len(testItems); i++ {
prioItem := heap.Pop(priorityQueue).(*txPrioItem)
feesEqual := false
switch {
case prioItem.feePerKB > highest.feePerKB:
t.Fatalf("priority sort: item (fee per KB: %v, "+
"priority: %v) higher than than prev "+
"(fee per KB: %v, priority %v)",
prioItem.feePerKB, prioItem.priority,
highest.feePerKB, highest.priority)
case prioItem.feePerKB == highest.feePerKB:
feesEqual = true
default:
}
if feesEqual {
switch {
case prioItem.priority > highest.priority:
t.Fatalf("priority sort: item (fee per KB: %v, "+
"priority: %v) higher than than prev "+
"(fee per KB: %v, priority %v)",
prioItem.feePerKB, prioItem.priority,
highest.feePerKB, highest.priority)
case prioItem.priority == highest.priority:
default:
}
}
highest = prioItem
}
// Test sorting by priority then fee per KB.
highest = nil
priorityQueue = newTxPriorityQueue(numItems, txPQByPriority)
for i := 0; i < len(testItems); i++ {
prioItem := testItems[i]
if highest == nil {
highest = prioItem
}
if prioItem.priority >= highest.priority {
highest = prioItem
if prioItem.priority == highest.priority {
if prioItem.feePerKB >= highest.feePerKB {
highest = prioItem
}
}
}
heap.Push(priorityQueue, prioItem)
}
for i := 0; i < len(testItems); i++ {
prioItem := heap.Pop(priorityQueue).(*txPrioItem)
prioEqual := false
switch {
case prioItem.priority > highest.priority:
t.Fatalf("priority sort: item (fee per KB: %v, "+
"priority: %v) higher than than prev "+
"(fee per KB: %v, priority %v)",
prioItem.feePerKB, prioItem.priority,
highest.feePerKB, highest.priority)
case prioItem.priority == highest.priority:
prioEqual = true
default:
}
if prioEqual {
switch {
case prioItem.feePerKB > highest.feePerKB:
t.Fatalf("priority sort: item (fee per KB: %v, "+
"priority: %v) higher than than prev "+
"(fee per KB: %v, priority %v)",
prioItem.feePerKB, prioItem.priority,
highest.feePerKB, highest.priority)
case prioItem.priority == highest.priority:
default:
}
}
highest = prioItem
}
}
// TestStakeTxFeePrioHeap tests the priority heaps including the stake types for
// both transaction fees per KB and transaction priority. It ensures that the
// primary sorting is first by stake type, and then by the latter chosen priority
// type.
func TestStakeTxFeePrioHeap(t *testing.T) {
numElements := 1000
ph := newTxPriorityQueue(numElements, txPQByStakeAndFee)
for i := 0; i < numElements; i++ {
randType := stake.TxType(rand.Intn(4))
randPrio := rand.Float64() * 100
randFeePerKB := rand.Float64() * 10
prioItem := &txPrioItem{
tx: nil,
txType: randType,
priority: randPrio,
feePerKB: randFeePerKB,
}
heap.Push(ph, prioItem)
}
// Test sorting by stake and fee per KB.
last := &txPrioItem{
tx: nil,
txType: stake.TxTypeSSGen,
priority: 10000.0,
feePerKB: 10000.0,
}
for i := 0; i < numElements; i++ {
prioItem := heap.Pop(ph)
txpi, ok := prioItem.(*txPrioItem)
if ok {
if txpi.feePerKB > last.feePerKB &&
compareStakePriority(txpi, last) >= 0 {
t.Errorf("bad pop: %v fee per KB was more than last of %v "+
"while the txtype was %v but last was %v",
txpi.feePerKB, last.feePerKB, txpi.txType, last.txType)
}
last = txpi
}
}
ph = newTxPriorityQueue(numElements, txPQByStakeAndPriority)
for i := 0; i < numElements; i++ {
randType := stake.TxType(rand.Intn(4))
randPrio := rand.Float64() * 100
randFeePerKB := rand.Float64() * 10
prioItem := &txPrioItem{
tx: nil,
txType: randType,
priority: randPrio,
feePerKB: randFeePerKB,
}
heap.Push(ph, prioItem)
}
// Test sorting by stake and priority.
last = &txPrioItem{
tx: nil,
txType: stake.TxTypeSSGen,
priority: 10000.0,
feePerKB: 10000.0,
}
for i := 0; i < numElements; i++ {
prioItem := heap.Pop(ph)
txpi, ok := prioItem.(*txPrioItem)
if ok {
if txpi.priority > last.priority &&
compareStakePriority(txpi, last) >= 0 {
t.Errorf("bad pop: %v fee per KB was more than last of %v "+
"while the txtype was %v but last was %v",
txpi.feePerKB, last.feePerKB, txpi.txType, last.txType)
}
last = txpi
}
}
ph = newTxPriorityQueue(numElements, txPQByStakeAndFeeAndThenPriority)
for i := 0; i < numElements; i++ {
randType := stake.TxType(rand.Intn(4))
randPrio := rand.Float64() * 100
randFeePerKB := rand.Float64() * 10
prioItem := &txPrioItem{
tx: nil,
txType: randType,
priority: randPrio,
feePerKB: randFeePerKB,
}
heap.Push(ph, prioItem)
}
// Test sorting with fees per KB for high stake priority, then
// priority for low stake priority.
last = &txPrioItem{
tx: nil,
txType: stake.TxTypeSSGen,
priority: 10000.0,
feePerKB: 10000.0,
}
for i := 0; i < numElements; i++ {
prioItem := heap.Pop(ph)
txpi, ok := prioItem.(*txPrioItem)
if ok {
bothAreLowStakePriority :=
txStakePriority(txpi.txType) == regOrRevocPriority &&
txStakePriority(last.txType) == regOrRevocPriority
if !bothAreLowStakePriority {
if txpi.feePerKB > last.feePerKB &&
compareStakePriority(txpi, last) >= 0 {
t.Errorf("bad pop: %v fee per KB was more than last of %v "+
"while the txtype was %v but last was %v",
txpi.feePerKB, last.feePerKB, txpi.txType, last.txType)
}
}
if bothAreLowStakePriority {
if txpi.priority > last.priority &&
compareStakePriority(txpi, last) >= 0 {
t.Errorf("bad pop: %v priority was more than last of %v "+
"while the txtype was %v but last was %v",
txpi.feePerKB, last.feePerKB, txpi.txType, last.txType)
}
}
last = txpi
}
}
}