forked from gcash/bchd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolicy_test.go
168 lines (157 loc) · 5.24 KB
/
policy_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
// Copyright (c) 2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package mining
import (
"encoding/hex"
"testing"
"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
)
// newHashFromStr converts the passed big-endian hex string into a
// chainhash.Hash. It only differs from the one available in chainhash in that
// it panics on an error since it will only (and must only) be called with
// hard-coded, and therefore known good, hashes.
func newHashFromStr(hexStr string) *chainhash.Hash {
hash, err := chainhash.NewHashFromStr(hexStr)
if err != nil {
panic("invalid hash in source file: " + hexStr)
}
return hash
}
// hexToBytes converts the passed hex string into bytes and will panic if there
// is an error. This is only provided for the hard-coded constants so errors in
// the source code can be detected. It will only (and must only) be called with
// hard-coded values.
func hexToBytes(s string) []byte {
b, err := hex.DecodeString(s)
if err != nil {
panic("invalid hex in source file: " + s)
}
return b
}
// newUtxoViewpoint returns a new utxo view populated with outputs of the
// provided source transactions as if there were available at the respective
// block height specified in the heights slice. The length of the source txns
// and source tx heights must match or it will panic.
func newUtxoViewpoint(sourceTxns []*wire.MsgTx, sourceTxHeights []int32) *blockchain.UtxoViewpoint {
if len(sourceTxns) != len(sourceTxHeights) {
panic("each transaction must have its block height specified")
}
view := blockchain.NewUtxoViewpoint()
for i, tx := range sourceTxns {
view.AddTxOuts(btcutil.NewTx(tx), sourceTxHeights[i])
}
return view
}
// TestCalcPriority ensures the priority calculations work as intended.
func TestCalcPriority(t *testing.T) {
// commonSourceTx1 is a valid transaction used in the tests below as an
// input to transactions that are having their priority calculated.
//
// From block 7 in main blockchain.
// tx 0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9
commonSourceTx1 := &wire.MsgTx{
Version: 1,
TxIn: []*wire.TxIn{{
PreviousOutPoint: wire.OutPoint{
Hash: chainhash.Hash{},
Index: wire.MaxPrevOutIndex,
},
SignatureScript: hexToBytes("04ffff001d0134"),
Sequence: 0xffffffff,
}},
TxOut: []*wire.TxOut{{
Value: 5000000000,
PkScript: hexToBytes("410411db93e1dcdb8a016b49840f8c5" +
"3bc1eb68a382e97b1482ecad7b148a6909a5cb2e0ead" +
"dfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8" +
"643f656b412a3ac"),
}},
LockTime: 0,
}
// commonRedeemTx1 is a valid transaction used in the tests below as the
// transaction to calculate the priority for.
//
// It originally came from block 170 in main blockchain.
commonRedeemTx1 := &wire.MsgTx{
Version: 1,
TxIn: []*wire.TxIn{{
PreviousOutPoint: wire.OutPoint{
Hash: *newHashFromStr("0437cd7f8525ceed232435" +
"9c2d0ba26006d92d856a9c20fa0241106ee5" +
"a597c9"),
Index: 0,
},
SignatureScript: hexToBytes("47304402204e45e16932b8af" +
"514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5f" +
"b8cd410220181522ec8eca07de4860a4acdd12909d83" +
"1cc56cbbac4622082221a8768d1d0901"),
Sequence: 0xffffffff,
}},
TxOut: []*wire.TxOut{{
Value: 1000000000,
PkScript: hexToBytes("4104ae1a62fe09c5f51b13905f07f06" +
"b99a2f7159b2225f374cd378d71302fa28414e7aab37" +
"397f554a7df5f142c21c1b7303b8a0626f1baded5c72" +
"a704f7e6cd84cac"),
}, {
Value: 4000000000,
PkScript: hexToBytes("410411db93e1dcdb8a016b49840f8c5" +
"3bc1eb68a382e97b1482ecad7b148a6909a5cb2e0ead" +
"dfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8" +
"643f656b412a3ac"),
}},
LockTime: 0,
}
tests := []struct {
name string // test description
tx *wire.MsgTx // tx to calc priority for
utxoView *blockchain.UtxoViewpoint // inputs to tx
nextHeight int32 // height for priority calc
want float64 // expected priority
}{
{
name: "one height 7 input, prio tx height 169",
tx: commonRedeemTx1,
utxoView: newUtxoViewpoint([]*wire.MsgTx{commonSourceTx1},
[]int32{7}),
nextHeight: 169,
want: 5e9,
},
{
name: "one height 100 input, prio tx height 169",
tx: commonRedeemTx1,
utxoView: newUtxoViewpoint([]*wire.MsgTx{commonSourceTx1},
[]int32{100}),
nextHeight: 169,
want: 2129629629.6296296,
},
{
name: "one height 7 input, prio tx height 100000",
tx: commonRedeemTx1,
utxoView: newUtxoViewpoint([]*wire.MsgTx{commonSourceTx1},
[]int32{7}),
nextHeight: 100000,
want: 3086203703703.7036,
},
{
name: "one height 100 input, prio tx height 100000",
tx: commonRedeemTx1,
utxoView: newUtxoViewpoint([]*wire.MsgTx{commonSourceTx1},
[]int32{100}),
nextHeight: 100000,
want: 3083333333333.3335,
},
}
for i, test := range tests {
got := CalcPriority(test.tx, test.utxoView, test.nextHeight)
if got != test.want {
t.Errorf("CalcPriority #%d (%q): unexpected priority "+
"got %v want %v", i, test.name, got, test.want)
continue
}
}
}