1 package wire_test
2 3 import (
4 "encoding/hex"
5 "encoding/json"
6 "os"
7 "path/filepath"
8 "runtime"
9 "testing"
10 11 "git.smesh.lol/nostrino/pkg/btcwire"
12 "git.smesh.lol/nostrino/pkg/crypto/chainhash"
13 14 "git.smesh.lol/btcd-lite/internal/wire"
15 )
16 17 // v2Fixture is nostrino's harvested run of fork headers: 164-byte headers from
18 // the XBT chain at 964265 onward, with the anchor they link to.
19 type v2Fixture struct {
20 Source string `json:"source"`
21 AnchorHeight uint32 `json:"anchor_height"`
22 AnchorHash string `json:"anchor_hash"`
23 FirstHeight uint32 `json:"first_height"`
24 Headers []string `json:"headers"`
25 }
26 27 func loadV2(t *testing.T) v2Fixture {
28 t.Helper()
29 _, file, _, _ := runtime.Caller(0)
30 raw, err := os.ReadFile(filepath.Join(filepath.Dir(file), "..", "..", "testdata", "xbt-v2-headers.json"))
31 if err != nil {
32 t.Fatalf("read fixture: %v", err)
33 }
34 var f v2Fixture
35 if err := json.Unmarshal(raw, &f); err != nil {
36 t.Fatalf("parse fixture: %v", err)
37 }
38 if len(f.Headers) == 0 {
39 t.Fatal("no headers")
40 }
41 return f
42 }
43 44 // TestV2HeaderHashesChain: every harvested fork header parses as a version 2
45 // header, its hash is the fork construction, and each header links to the one
46 // before it. This is the fork's hash rule as the network actually produced it,
47 // not as the code says it should be.
48 func TestV2HeaderHashesChain(t *testing.T) {
49 f := loadV2(t)
50 prev, err := chainhash.NewHashFromStr(f.AnchorHash)
51 if err != nil {
52 t.Fatalf("anchor hash: %v", err)
53 }
54 for i, enc := range f.Headers {
55 raw, err := hex.DecodeString(enc)
56 if err != nil || len(raw) != btcwire.BlockHeaderV2Size {
57 t.Fatalf("header %d: %v (%d bytes)", i, err, len(raw))
58 }
59 var h btcwire.BlockHeader
60 if err := h.DeserializeV2(raw); err != nil {
61 t.Fatalf("header %d: %v", i, err)
62 }
63 if h.V2 == nil {
64 t.Fatalf("header %d has no v2 fields", i)
65 }
66 if h.PrevBlock != *prev {
67 t.Fatalf("header %d does not link to its predecessor", i)
68 }
69 if got, want := int(h.V2.Height), int(f.FirstHeight)+i; got != want {
70 t.Fatalf("header %d states height %d, fixture says %d", i, got, want)
71 }
72 if !h.CheckProofOfWork() {
73 t.Fatalf("header %d fails its own proof of work", i)
74 }
75 bh := h.BlockHash()
76 prev = &bh
77 }
78 }
79 80 // TestParseBlockForkHeader: a block whose header is a fork header is read as
81 // one, which means the 84 mining bytes are consumed before the transaction
82 // count, and the block's hash is the fork hash rather than double SHA-256 of
83 // its first 80 bytes.
84 func TestParseBlockForkHeader(t *testing.T) {
85 f := loadV2(t)
86 hdr, err := hex.DecodeString(f.Headers[0])
87 if err != nil {
88 t.Fatal(err)
89 }
90 var proto btcwire.BlockHeader
91 if err := proto.DeserializeV2(hdr); err != nil {
92 t.Fatal(err)
93 }
94 // One coinbase transaction, empty script, no outputs: enough for the block
95 // to reach its merkle check, which is where a header read at the wrong
96 // offset would show.
97 tx := make([]byte, 0, 64)
98 tx = append(tx, 1, 0, 0, 0) // version
99 tx = append(tx, 1) // one input
100 tx = append(tx, make([]byte, 32)...) // null prevout
101 tx = append(tx, 0xff, 0xff, 0xff, 0xff)
102 tx = append(tx, 0) // empty script
103 tx = append(tx, 0xff, 0xff, 0xff, 0xff)
104 tx = append(tx, 0) // no outputs
105 tx = append(tx, 0, 0, 0, 0) // locktime
106 blockRaw := append(append([]byte(nil), hdr...), 1)
107 blockRaw = append(blockRaw, tx...)
108 block, err := wire.ParseBlock(blockRaw)
109 if err == nil {
110 t.Fatal("a block whose merkle root cannot match parsed")
111 }
112 if err == wire.ErrShort {
113 t.Fatalf("the fork header was not read: %v", err)
114 }
115 _ = block
116 // The same bytes read as a Bitcoin block give a different hash and a
117 // different position for the transaction count, which is exactly the bug
118 // this test pins.
119 if chainhash.DoubleHashH(hdr[:80]) == proto.BlockHash() {
120 t.Fatal("the fork hash equals double SHA-256: the fixture is not a v2 header")
121 }
122 }
123