-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcoinbase_fuzz_test.go
More file actions
136 lines (119 loc) · 3.59 KB
/
coinbase_fuzz_test.go
File metadata and controls
136 lines (119 loc) · 3.59 KB
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
package main
import (
"bytes"
"sort"
"testing"
"github.com/btcsuite/btcd/chaincfg"
)
// FuzzCoinbasePayoutSerializationRoundTrip stress-tests the full payout path:
// generated btcd addresses -> scripts -> coinbase serialization -> tx decode.
func FuzzCoinbasePayoutSerializationRoundTrip(f *testing.F) {
f.Add([]byte{0x00})
f.Add([]byte{0x01, 0x20, 0x40, 0x80})
f.Add([]byte("goPool-fuzz-coinbase-payouts"))
f.Fuzz(func(t *testing.T, seed []byte) {
if len(seed) == 0 {
seed = []byte{0x00}
}
var params *chaincfg.Params
switch seed[0] % 3 {
case 0:
params = &chaincfg.MainNetParams
case 1:
params = &chaincfg.TestNet3Params
default:
params = &chaincfg.RegressionNetParams
}
height := int64(100000 + int(seed[0]))
ex1 := []byte{0x01, 0x02, 0x03, 0x04}
ex2 := []byte{0xaa, 0xbb, 0xcc, 0xdd}
templateExtra := 8
count := 1 + int(seed[len(seed)-1]%8) // 1-8 payouts
payouts := make([]coinbasePayoutOutput, 0, count)
for i := 0; i < count; i++ {
addrType := fuzzDigest(seed, byte(i), 0x31)
addr := fuzzGeneratedAddress(t, params, seed, i, int(addrType[0]%5))
script, err := scriptForAddress(addr, params)
if err != nil {
t.Fatalf("scriptForAddress(%s): %v", addr, err)
}
valSeed := fuzzDigest(seed, byte(i), 0x32)
value := int64(1 + (int64(valSeed[0]) << 2) + int64(i%3)) // keep non-zero, with ties.
payouts = append(payouts, coinbasePayoutOutput{
Script: script,
Value: value,
})
}
var commitment []byte
if seed[0]&1 == 1 {
// OP_RETURN(0x6a) + push-36(0x24) + aa21a9ed + 32-byte commitment.
commitment = append([]byte{0x6a, 0x24, 0xaa, 0x21, 0xa9, 0xed}, bytes.Repeat([]byte{0x00}, 32)...)
}
raw, txid, err := serializeCoinbaseTxPayoutsPredecoded(
height,
ex1,
ex2,
templateExtra,
payouts,
commitment,
nil,
"fuzz-payouts",
0,
)
if err != nil {
t.Fatalf("serializeCoinbaseTxPayoutsPredecoded: %v", err)
}
if len(txid) != 32 {
t.Fatalf("txid len mismatch: got %d", len(txid))
}
outs := parseCoinbaseOutputs(t, raw)
wantCount := len(payouts)
if len(commitment) > 0 {
wantCount++
}
if len(outs) != wantCount {
t.Fatalf("output count mismatch: got %d want %d", len(outs), wantCount)
}
start := 0
if len(commitment) > 0 {
if outs[0].Value != 0 || !bytes.Equal(outs[0].PkScript, commitment) {
t.Fatalf("commitment output mismatch")
}
start = 1
}
expected := append([]coinbasePayoutOutput(nil), payouts...)
sort.SliceStable(expected, func(i, j int) bool {
return expected[i].Value > expected[j].Value
})
var gotTotal int64
for i, want := range expected {
got := outs[start+i]
if got.Value != want.Value {
t.Fatalf("value mismatch idx=%d got=%d want=%d", i, got.Value, want.Value)
}
if !bytes.Equal(got.PkScript, want.Script) {
t.Fatalf("script mismatch idx=%d got=%x want=%x", i, got.PkScript, want.Script)
}
gotTotal += got.Value
// Ensure script<->address conversion still holds on serialized outputs.
addr := scriptToAddress(got.PkScript, params)
if addr == "" {
t.Fatalf("scriptToAddress returned empty for idx=%d script=%x", i, got.PkScript)
}
rtScript, err := scriptForAddress(addr, params)
if err != nil {
t.Fatalf("round-trip scriptForAddress failed for idx=%d addr=%s: %v", i, addr, err)
}
if !bytes.Equal(rtScript, got.PkScript) {
t.Fatalf("round-trip script mismatch idx=%d addr=%s", i, addr)
}
}
var wantTotal int64
for _, o := range payouts {
wantTotal += o.Value
}
if gotTotal != wantTotal {
t.Fatalf("payout total mismatch: got=%d want=%d", gotTotal, wantTotal)
}
})
}