package wire_test import ( "bytes" "encoding/hex" "encoding/json" "os" "path/filepath" "runtime" "testing" "git.smesh.lol/nostrino/pkg/crypto/chainhash" "git.smesh.lol/btcd-lite/internal/wire" ) type vector struct { Height uint32 `json:"height"` BlockHash string `json:"blockHash"` Block string `json:"block"` PrevOutputScripts []string `json:"prevOutputScripts"` Filter string `json:"filter"` Note string `json:"note"` } func loadVectors(t *testing.T) []vector { t.Helper() _, file, _, _ := runtime.Caller(0) raw, err := os.ReadFile(filepath.Join(filepath.Dir(file), "..", "..", "testdata", "bip158_testnet19.json")) if err != nil { t.Fatalf("read vectors: %v", err) } var vectors []vector if err := json.Unmarshal(raw, &vectors); err != nil { t.Fatalf("parse vectors: %v", err) } if len(vectors) == 0 { t.Fatal("no vectors") } return vectors } // TestParseVectors decodes every official test vector block, requires its hash // to be the vector's, and requires the re-serialized non-witness transactions // to hash to the leaves of the block's own merkle root. func TestParseVectors(t *testing.T) { for _, v := range loadVectors(t) { v := v t.Run(v.BlockHash, func(t *testing.T) { raw, err := hex.DecodeString(v.Block) if err != nil { t.Fatalf("decode: %v", err) } block, err := wire.ParseBlock(raw) if err != nil { t.Fatalf("parse: %v", err) } if got := block.BlockHash().String(); got != v.BlockHash { t.Fatalf("block hash %s, want %s", got, v.BlockHash) } if block.Size != len(raw) { t.Fatalf("size %d, want %d", block.Size, len(raw)) } if got := block.MerkleRoot(); got != block.HeaderMerkleRoot() { t.Fatalf("merkle root %s, header %s", got, block.HeaderMerkleRoot()) } // Every non-witness serialization must be stable: decoding and // re-encoding is the identity, which is what makes a txid from a // parsed transaction the txid the block committed to. for i := range block.Txs { tx := &block.Txs[i] again, err := wire.ParseTx(tx.SerializeNoWitness()) if err != nil { t.Fatalf("tx %d: re-parse: %v", i, err) } if again.TxID() != tx.TxID() { t.Fatalf("tx %d: txid drift on re-encode", i) } if tx.HasWitness && bytes.Equal(tx.SerializeNoWitness(), nil) { t.Fatalf("tx %d: empty no-witness form", i) } } }) } } // TestReject checks the invariants a body has to meet before it is a block. func TestReject(t *testing.T) { vectors := loadVectors(t) raw, err := hex.DecodeString(vectors[1].Block) if err != nil { t.Fatal(err) } if _, err := wire.ParseBlock(raw[:len(raw)-1]); err == nil { t.Fatal("a truncated block parsed") } trailing := append(append([]byte(nil), raw...), 0x00) if _, err := wire.ParseBlock(trailing); err == nil { t.Fatal("a block with trailing bytes parsed") } // Flip one byte of the coinbase script; the header no longer matches. block, err := wire.ParseBlock(raw) if err != nil { t.Fatal(err) } cb := &block.Txs[0] if !cb.IsCoinBase() { t.Fatal("first transaction is not a coinbase") } if _, err := wire.ParseTx(raw); err == nil { t.Fatal("a whole block parsed as a transaction") } } // TestMerkleRoot checks the duplicate-last rule at odd widths against a // two-leaf and three-leaf tree computed by hand. func TestMerkleRoot(t *testing.T) { leaf := func(b byte) chainhash.Hash { var h chainhash.Hash h[0] = b return h } d := func(a, b chainhash.Hash) chainhash.Hash { buf := append(a[:], b[:]...) return chainhash.DoubleHashH(buf) } a, b, c := leaf(1), leaf(2), leaf(3) if got, want := wire.MerkleRoot([]chainhash.Hash{a}), a; got != want { t.Fatalf("single: %s want %s", got, want) } if got, want := wire.MerkleRoot([]chainhash.Hash{a, b}), d(a, b); got != want { t.Fatalf("pair: %s want %s", got, want) } want := d(d(a, b), d(c, c)) if got := wire.MerkleRoot([]chainhash.Hash{a, b, c}); got != want { t.Fatalf("three: %s want %s", got, want) } }