github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/mining_test.go (about) 1 // Copyright (c) 2016 The btcsuite developers 2 // Copyright (c) 2016 The Dash developers 3 // Use of this source code is governed by an ISC 4 // license that can be found in the LICENSE file. 5 6 package main 7 8 import ( 9 "container/heap" 10 "math/rand" 11 "testing" 12 13 "github.com/dashpay/godashutil" 14 ) 15 16 // TestTxFeePrioHeap ensures the priority queue for transaction fees and 17 // priorities works as expected. 18 func TestTxFeePrioHeap(t *testing.T) { 19 // Create some fake priority items that exercise the expected sort 20 // edge conditions. 21 testItems := []*txPrioItem{ 22 {feePerKB: 5678, priority: 3}, 23 {feePerKB: 5678, priority: 1}, 24 {feePerKB: 5678, priority: 1}, // Duplicate fee and prio 25 {feePerKB: 5678, priority: 5}, 26 {feePerKB: 5678, priority: 2}, 27 {feePerKB: 1234, priority: 3}, 28 {feePerKB: 1234, priority: 1}, 29 {feePerKB: 1234, priority: 5}, 30 {feePerKB: 1234, priority: 5}, // Duplicate fee and prio 31 {feePerKB: 1234, priority: 2}, 32 {feePerKB: 10000, priority: 0}, // Higher fee, smaller prio 33 {feePerKB: 0, priority: 10000}, // Higher prio, lower fee 34 } 35 36 // Add random data in addition to the edge conditions already manually 37 // specified. 38 randSeed := rand.Int63() 39 defer func() { 40 if t.Failed() { 41 t.Logf("Random numbers using seed: %v", randSeed) 42 } 43 }() 44 prng := rand.New(rand.NewSource(randSeed)) 45 for i := 0; i < 1000; i++ { 46 testItems = append(testItems, &txPrioItem{ 47 feePerKB: int64(prng.Float64() * godashutil.SatoshiPerBitcoin), 48 priority: prng.Float64() * 100, 49 }) 50 } 51 52 // Test sorting by fee per KB then priority. 53 var highest *txPrioItem 54 priorityQueue := newTxPriorityQueue(len(testItems), true) 55 for i := 0; i < len(testItems); i++ { 56 prioItem := testItems[i] 57 if highest == nil { 58 highest = prioItem 59 } 60 if prioItem.feePerKB >= highest.feePerKB && 61 prioItem.priority > highest.priority { 62 63 highest = prioItem 64 } 65 heap.Push(priorityQueue, prioItem) 66 } 67 68 for i := 0; i < len(testItems); i++ { 69 prioItem := heap.Pop(priorityQueue).(*txPrioItem) 70 if prioItem.feePerKB >= highest.feePerKB && 71 prioItem.priority > highest.priority { 72 73 t.Fatalf("fee sort: item (fee per KB: %v, "+ 74 "priority: %v) higher than than prev "+ 75 "(fee per KB: %v, priority %v)", 76 prioItem.feePerKB, prioItem.priority, 77 highest.feePerKB, highest.priority) 78 } 79 highest = prioItem 80 } 81 82 // Test sorting by priority then fee per KB. 83 highest = nil 84 priorityQueue = newTxPriorityQueue(len(testItems), false) 85 for i := 0; i < len(testItems); i++ { 86 prioItem := testItems[i] 87 if highest == nil { 88 highest = prioItem 89 } 90 if prioItem.priority >= highest.priority && 91 prioItem.feePerKB > highest.feePerKB { 92 93 highest = prioItem 94 } 95 heap.Push(priorityQueue, prioItem) 96 } 97 98 for i := 0; i < len(testItems); i++ { 99 prioItem := heap.Pop(priorityQueue).(*txPrioItem) 100 if prioItem.priority >= highest.priority && 101 prioItem.feePerKB > highest.feePerKB { 102 103 t.Fatalf("priority sort: item (fee per KB: %v, "+ 104 "priority: %v) higher than than prev "+ 105 "(fee per KB: %v, priority %v)", 106 prioItem.feePerKB, prioItem.priority, 107 highest.feePerKB, highest.priority) 108 } 109 highest = prioItem 110 } 111 }