package wire_test import ( "encoding/hex" "encoding/json" "os" "path/filepath" "runtime" "testing" "git.smesh.lol/nostrino/pkg/btcwire" "git.smesh.lol/nostrino/pkg/crypto/chainhash" "git.smesh.lol/btcd-lite/internal/wire" ) // v2Fixture is nostrino's harvested run of fork headers: 164-byte headers from // the XBT chain at 964265 onward, with the anchor they link to. type v2Fixture struct { Source string `json:"source"` AnchorHeight uint32 `json:"anchor_height"` AnchorHash string `json:"anchor_hash"` FirstHeight uint32 `json:"first_height"` Headers []string `json:"headers"` } func loadV2(t *testing.T) v2Fixture { t.Helper() _, file, _, _ := runtime.Caller(0) raw, err := os.ReadFile(filepath.Join(filepath.Dir(file), "..", "..", "testdata", "xbt-v2-headers.json")) if err != nil { t.Fatalf("read fixture: %v", err) } var f v2Fixture if err := json.Unmarshal(raw, &f); err != nil { t.Fatalf("parse fixture: %v", err) } if len(f.Headers) == 0 { t.Fatal("no headers") } return f } // TestV2HeaderHashesChain: every harvested fork header parses as a version 2 // header, its hash is the fork construction, and each header links to the one // before it. This is the fork's hash rule as the network actually produced it, // not as the code says it should be. func TestV2HeaderHashesChain(t *testing.T) { f := loadV2(t) prev, err := chainhash.NewHashFromStr(f.AnchorHash) if err != nil { t.Fatalf("anchor hash: %v", err) } for i, enc := range f.Headers { raw, err := hex.DecodeString(enc) if err != nil || len(raw) != btcwire.BlockHeaderV2Size { t.Fatalf("header %d: %v (%d bytes)", i, err, len(raw)) } var h btcwire.BlockHeader if err := h.DeserializeV2(raw); err != nil { t.Fatalf("header %d: %v", i, err) } if h.V2 == nil { t.Fatalf("header %d has no v2 fields", i) } if h.PrevBlock != *prev { t.Fatalf("header %d does not link to its predecessor", i) } if got, want := int(h.V2.Height), int(f.FirstHeight)+i; got != want { t.Fatalf("header %d states height %d, fixture says %d", i, got, want) } if !h.CheckProofOfWork() { t.Fatalf("header %d fails its own proof of work", i) } bh := h.BlockHash() prev = &bh } } // TestParseBlockForkHeader: a block whose header is a fork header is read as // one, which means the 84 mining bytes are consumed before the transaction // count, and the block's hash is the fork hash rather than double SHA-256 of // its first 80 bytes. func TestParseBlockForkHeader(t *testing.T) { f := loadV2(t) hdr, err := hex.DecodeString(f.Headers[0]) if err != nil { t.Fatal(err) } var proto btcwire.BlockHeader if err := proto.DeserializeV2(hdr); err != nil { t.Fatal(err) } // One coinbase transaction, empty script, no outputs: enough for the block // to reach its merkle check, which is where a header read at the wrong // offset would show. tx := make([]byte, 0, 64) tx = append(tx, 1, 0, 0, 0) // version tx = append(tx, 1) // one input tx = append(tx, make([]byte, 32)...) // null prevout tx = append(tx, 0xff, 0xff, 0xff, 0xff) tx = append(tx, 0) // empty script tx = append(tx, 0xff, 0xff, 0xff, 0xff) tx = append(tx, 0) // no outputs tx = append(tx, 0, 0, 0, 0) // locktime blockRaw := append(append([]byte(nil), hdr...), 1) blockRaw = append(blockRaw, tx...) block, err := wire.ParseBlock(blockRaw) if err == nil { t.Fatal("a block whose merkle root cannot match parsed") } if err == wire.ErrShort { t.Fatalf("the fork header was not read: %v", err) } _ = block // The same bytes read as a Bitcoin block give a different hash and a // different position for the transaction count, which is exactly the bug // this test pins. if chainhash.DoubleHashH(hdr[:80]) == proto.BlockHash() { t.Fatal("the fork hash equals double SHA-256: the fixture is not a v2 header") } }