fork_test.go raw
1 package btcwire
2
3 import (
4 "bytes"
5 "encoding/hex"
6 "testing"
7
8 "git.smesh.lol/nostrino/pkg/crypto/chainhash"
9 "golang.org/x/crypto/blake2b"
10 )
11
12 // TestBlake2bKnownAnswer pins the library's digest to the published BLAKE2b-256 vector for the
13 // empty input, so a change in dependency cannot quietly change every fork hash.
14 func TestBlake2bKnownAnswer(t *testing.T) {
15 want, err := hex.DecodeString("0e5751c026e543b2e8ab2eb06099daa1d1e5df47778f7787faab45cdf12fe3a8")
16
17 if err != nil {
18 t.Fatal(err)
19 }
20
21 sum := blake2b.Sum256(nil)
22
23 if !bytes.Equal(sum[:], want) {
24 t.Fatalf("blake2b-256 of nothing is %x, want %x", sum, want)
25 }
26 }
27
28 // TestTaggedHashPrefix: the writer starts with the tag hashed twice, which is what makes it a
29 // tagged hash. Core asserts the same shape as 0x40 bytes of prefix.
30 func TestTaggedHashPrefix(t *testing.T) {
31 prefix := taggedHash("BIP0340/challenge")
32
33 if len(prefix) != 64 {
34 t.Fatalf("tag prefix is %d bytes, want 64", len(prefix))
35 }
36
37 if !bytes.Equal(prefix[:32], prefix[32:]) {
38 t.Fatal("tag prefix halves differ")
39 }
40 }
41
42 func v2Header() BlockHeader {
43 var h BlockHeader
44
45 version := uint32(HeaderV2Flag) | 0x20000000
46 h.Version = int32(version)
47 h.PrevBlock[0] = 0x11
48 h.MerkleRoot[1] = 0x22
49 h.Timestamp = 1_700_000_000
50 h.Bits = 0x1d00ffff
51 h.Nonce = 7
52 h.V2 = &HeaderV2{
53 Nonce2: 3,
54 Nonce3: 4,
55 TimeOffset: 90,
56 TxCount: 1200,
57 Flags: 4,
58 XorKeyClear: 9,
59 Height: 961_640,
60 }
61
62 h.V2.Extranonce[0] = 0xaa
63 h.V2.XorKey[15] = 0xbb
64 h.V2.MMRHS[3] = 0xcc
65
66 return h
67 }
68
69 // TestSerializeV2RoundTrip: the mining fields survive the wire, and the timestamp is reconstructed
70 // from the offset when the header uses one.
71 func TestSerializeV2RoundTrip(t *testing.T) {
72 h := v2Header()
73
74 raw := h.SerializeV2()
75
76 if len(raw) != BlockHeaderV2Size {
77 t.Fatalf("serialized %d bytes, want %d", len(raw), BlockHeaderV2Size)
78 }
79
80 var back BlockHeader
81
82 if err := back.DeserializeV2(raw); err != nil {
83 t.Fatal(err)
84 }
85
86 if back.V2 == nil {
87 t.Fatal("no version 2 fields after a round trip")
88 }
89
90 if back.V2.Height != h.V2.Height || back.V2.TxCount != h.V2.TxCount ||
91 back.V2.Flags != h.V2.Flags || back.V2.XorKeyClear != h.V2.XorKeyClear ||
92 back.V2.Nonce2 != h.V2.Nonce2 || back.V2.Nonce3 != h.V2.Nonce3 ||
93 back.V2.TimeOffset != h.V2.TimeOffset {
94 t.Fatalf("mining fields changed: %+v", back.V2)
95 }
96
97 if back.V2.Extranonce != h.V2.Extranonce || back.V2.XorKey != h.V2.XorKey || back.V2.MMRHS != h.V2.MMRHS {
98 t.Fatal("byte fields changed")
99 }
100
101 if back.Timestamp != h.Timestamp {
102 t.Fatalf("timestamp %d, want %d", back.Timestamp, h.Timestamp)
103 }
104
105 if back.TimeOnWire() != h.TimeOnWire() {
106 t.Fatalf("wire time %d, want %d", back.TimeOnWire(), h.TimeOnWire())
107 }
108
109 if h.TimeOnWire() != h.Timestamp-90 {
110 t.Fatalf("wire time %d does not apply the offset", h.TimeOnWire())
111 }
112 }
113
114 // TestSerializeAllLength: a header's wire form depends only on whether it carries the fork fields,
115 // because the headers message mixes both kinds.
116 func TestSerializeAllLength(t *testing.T) {
117 v1 := BlockHeader{Version: 0x20000000}
118 if got := len(v1.SerializeAll()); got != BlockHeaderSize {
119 t.Fatalf("v1 header is %d bytes, want %d", got, BlockHeaderSize)
120 }
121
122 v2 := v2Header()
123 if got := len(v2.SerializeAll()); got != BlockHeaderV2Size {
124 t.Fatalf("v2 header is %d bytes, want %d", got, BlockHeaderV2Size)
125 }
126 }
127
128 // TestHeadersDecodeMixed: a headers message that carries pre-fork and fork entries decodes both,
129 // and the transaction count after each entry is still enforced.
130 func TestHeadersDecodeMixed(t *testing.T) {
131 v1 := BlockHeader{Version: 0x20000000, Timestamp: 100, Bits: 0x1d00ffff}
132 v2 := v2Header()
133
134 msg := Headers{Headers: []BlockHeader{v1, v2}}
135
136 payload, err := msg.EncodePayload()
137
138 if err != nil {
139 t.Fatal(err)
140 }
141
142 var back Headers
143
144 if err := back.DecodePayload(payload); err != nil {
145 t.Fatal(err)
146 }
147
148 if len(back.Headers) != 2 {
149 t.Fatalf("decoded %d headers, want 2", len(back.Headers))
150 }
151
152 if back.Headers[0].V2 != nil {
153 t.Fatal("first header gained version 2 fields")
154 }
155
156 if back.Headers[1].V2 == nil {
157 t.Fatal("second header lost its version 2 fields")
158 }
159
160 if back.Headers[1].V2.Height != v2.V2.Height {
161 t.Fatalf("second header height %d, want %d", back.Headers[1].V2.Height, v2.V2.Height)
162 }
163 }
164
165 // TestForkHashDependsOnEveryField: the hash commits to the fields Core commits to. A field that
166 // does not move the hash is a field the implementation forgot.
167 func TestForkHashDependsOnEveryField(t *testing.T) {
168 base, err := ForkHash(ptr(v2Header()))
169
170 if err != nil {
171 t.Fatal(err)
172 }
173
174 changes := []struct {
175 name string
176 mut func(*BlockHeader)
177 }{
178 {"version", func(h *BlockHeader) { h.Version ^= 1 }},
179 {"prev", func(h *BlockHeader) { h.PrevBlock[0] ^= 1 }},
180 {"merkle", func(h *BlockHeader) { h.MerkleRoot[0] ^= 1 }},
181 {"time", func(h *BlockHeader) { h.Timestamp++ }},
182 {"bits", func(h *BlockHeader) { h.Bits = 0x1d00fffe }},
183 {"nonce", func(h *BlockHeader) { h.Nonce++ }},
184 {"nonce2", func(h *BlockHeader) { h.V2.Nonce2++ }},
185 {"nonce3", func(h *BlockHeader) { h.V2.Nonce3++ }},
186 {"extranonce", func(h *BlockHeader) { h.V2.Extranonce[1] ^= 1 }},
187 {"time offset", func(h *BlockHeader) { h.V2.TimeOffset++ }},
188 {"txcount", func(h *BlockHeader) { h.V2.TxCount++ }},
189 {"flags", func(h *BlockHeader) { h.V2.Flags ^= 1 }},
190 {"clear bits", func(h *BlockHeader) { h.V2.XorKeyClear++ }},
191 {"xor key", func(h *BlockHeader) { h.V2.XorKey[0] ^= 1 }},
192 {"height", func(h *BlockHeader) { h.V2.Height++ }},
193 {"mm rhs", func(h *BlockHeader) { h.V2.MMRHS[0] ^= 1 }},
194 }
195
196 for _, change := range changes {
197 h := ptr(v2Header())
198 change.mut(h)
199
200 got, err := ForkHash(h)
201
202 if err != nil {
203 t.Fatalf("%s: %v", change.name, err)
204 }
205
206 if got == base {
207 t.Fatalf("changing %s did not change the hash", change.name)
208 }
209 }
210 }
211
212 // TestForkHashFlagVariants: flags' low two bits select different byte streams, so headers that
213 // differ only there must hash differently.
214 func TestForkHashFlagVariants(t *testing.T) {
215 seen := map[chainhash.Hash]uint8{}
216
217 for _, flags := range []uint8{0, 1, 2, 3, 4, 5, 6, 7} {
218 h := ptr(v2Header())
219 h.V2.Flags = flags
220
221 hash, err := ForkHash(h)
222
223 if err != nil {
224 t.Fatal(err)
225 }
226
227 if previous, ok := seen[hash]; ok {
228 t.Fatalf("flags %d and %d hash the same", previous, flags)
229 }
230
231 seen[hash] = flags
232 }
233 }
234
235 func ptr(h BlockHeader) *BlockHeader { return &h }
236