dcrd/internal/mining/mining_test.go
Dave Collins 6ea800f516
mining: Move to internal.
This makes the mining package an internal package such that it will no
longer be an exported module.  The only place its functionality is
really needed is for the internal implementation of dcrd itself and thus
having an unnecessary exported module significantly increases the
maintenance burden.

This is part of the overall effort to reduce the total number of modules
and eventually get to the point it will be possible to follow semver for
the root module.

It is worth noting that there are a few constants, which will be listed
below, that were exported from this module that callers might have
previously relied upon.  However, due to previous discussions about the
goal of removing the module, a code search has revealed that there are
no known callers that still rely on them.

The aforementioned constants are:

- UnminedHeight
- MinHighPriority

Overview of the major changes:

- Move the following files from mining -> internal/mining:
  - README.md
  - cpuminer.go
  - doc.go
  - miningerror.go -> error.go
  - log.go
  - mining.go
  - mining_test.go
  - policy.go
  - policy_test.go
- Remove mining/go.mod and mining/go.sum
- Make the README.md and doc.go files match the new reality
- Update all import paths in the repository accordingly
- Remove the dependency from the root go.mod
- Run go mod tidy on all modules
2020-07-20 18:07:59 -05:00

129 lines
4.1 KiB
Go

// Copyright (c) 2016 The btcsuite developers
// Copyright (c) 2015-2020 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package mining
import (
"container/heap"
"math/rand"
"testing"
"github.com/decred/dcrd/blockchain/stake/v3"
)
// 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
numEdgeConditionElements := 12
// Create some fake priority items that exercise the expected sort
// edge conditions.
testItems := []*txPrioItem{
{feePerKB: 5678, txType: stake.TxTypeRegular, priority: 3},
{feePerKB: 5678, txType: stake.TxTypeRegular, priority: 1},
{feePerKB: 5678, txType: stake.TxTypeRegular, priority: 1}, // Duplicate fee and prio
{feePerKB: 5678, txType: stake.TxTypeRegular, priority: 5},
{feePerKB: 5678, txType: stake.TxTypeRegular, priority: 2},
{feePerKB: 1234, txType: stake.TxTypeRegular, priority: 3},
{feePerKB: 1234, txType: stake.TxTypeRegular, priority: 1},
{feePerKB: 1234, txType: stake.TxTypeRegular, priority: 5},
{feePerKB: 1234, txType: stake.TxTypeRegular, priority: 5}, // Duplicate fee and prio
{feePerKB: 1234, txType: stake.TxTypeRegular, priority: 2},
{feePerKB: 10000, txType: stake.TxTypeRegular, priority: 0}, // Higher fee, smaller prio
{feePerKB: 0, txType: stake.TxTypeRegular, priority: 10000}, // Higher prio, lower fee
}
ph := newTxPriorityQueue((numElements + numEdgeConditionElements), txPQByStakeAndFee)
// Add random data in addition to the edge conditions already manually
// specified.
for i := 0; i < (numElements + numEdgeConditionElements); i++ {
if i >= numEdgeConditionElements {
randType := stake.TxType(rand.Intn(4))
randPrio := rand.Float64() * 100
randFeePerKB := rand.Float64() * 10
testItems = append(testItems, &txPrioItem{
tx: nil,
txType: randType,
feePerKB: randFeePerKB,
priority: randPrio,
})
}
heap.Push(ph, testItems[i])
}
// 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(len(testItems), 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,
feePerKB: randFeePerKB,
priority: randPrio,
}
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
}
}
}