package btcwire import ( "bytes" "encoding/hex" "testing" "git.smesh.lol/nostrino/pkg/crypto/chainhash" "golang.org/x/crypto/blake2b" ) // TestBlake2bKnownAnswer pins the library's digest to the published BLAKE2b-256 vector for the // empty input, so a change in dependency cannot quietly change every fork hash. func TestBlake2bKnownAnswer(t *testing.T) { want, err := hex.DecodeString("0e5751c026e543b2e8ab2eb06099daa1d1e5df47778f7787faab45cdf12fe3a8") if err != nil { t.Fatal(err) } sum := blake2b.Sum256(nil) if !bytes.Equal(sum[:], want) { t.Fatalf("blake2b-256 of nothing is %x, want %x", sum, want) } } // TestTaggedHashPrefix: the writer starts with the tag hashed twice, which is what makes it a // tagged hash. Core asserts the same shape as 0x40 bytes of prefix. func TestTaggedHashPrefix(t *testing.T) { prefix := taggedHash("BIP0340/challenge") if len(prefix) != 64 { t.Fatalf("tag prefix is %d bytes, want 64", len(prefix)) } if !bytes.Equal(prefix[:32], prefix[32:]) { t.Fatal("tag prefix halves differ") } } func v2Header() BlockHeader { var h BlockHeader version := uint32(HeaderV2Flag) | 0x20000000 h.Version = int32(version) h.PrevBlock[0] = 0x11 h.MerkleRoot[1] = 0x22 h.Timestamp = 1_700_000_000 h.Bits = 0x1d00ffff h.Nonce = 7 h.V2 = &HeaderV2{ Nonce2: 3, Nonce3: 4, TimeOffset: 90, TxCount: 1200, Flags: 4, XorKeyClear: 9, Height: 961_640, } h.V2.Extranonce[0] = 0xaa h.V2.XorKey[15] = 0xbb h.V2.MMRHS[3] = 0xcc return h } // TestSerializeV2RoundTrip: the mining fields survive the wire, and the timestamp is reconstructed // from the offset when the header uses one. func TestSerializeV2RoundTrip(t *testing.T) { h := v2Header() raw := h.SerializeV2() if len(raw) != BlockHeaderV2Size { t.Fatalf("serialized %d bytes, want %d", len(raw), BlockHeaderV2Size) } var back BlockHeader if err := back.DeserializeV2(raw); err != nil { t.Fatal(err) } if back.V2 == nil { t.Fatal("no version 2 fields after a round trip") } if back.V2.Height != h.V2.Height || back.V2.TxCount != h.V2.TxCount || back.V2.Flags != h.V2.Flags || back.V2.XorKeyClear != h.V2.XorKeyClear || back.V2.Nonce2 != h.V2.Nonce2 || back.V2.Nonce3 != h.V2.Nonce3 || back.V2.TimeOffset != h.V2.TimeOffset { t.Fatalf("mining fields changed: %+v", back.V2) } if back.V2.Extranonce != h.V2.Extranonce || back.V2.XorKey != h.V2.XorKey || back.V2.MMRHS != h.V2.MMRHS { t.Fatal("byte fields changed") } if back.Timestamp != h.Timestamp { t.Fatalf("timestamp %d, want %d", back.Timestamp, h.Timestamp) } if back.TimeOnWire() != h.TimeOnWire() { t.Fatalf("wire time %d, want %d", back.TimeOnWire(), h.TimeOnWire()) } if h.TimeOnWire() != h.Timestamp-90 { t.Fatalf("wire time %d does not apply the offset", h.TimeOnWire()) } } // TestSerializeAllLength: a header's wire form depends only on whether it carries the fork fields, // because the headers message mixes both kinds. func TestSerializeAllLength(t *testing.T) { v1 := BlockHeader{Version: 0x20000000} if got := len(v1.SerializeAll()); got != BlockHeaderSize { t.Fatalf("v1 header is %d bytes, want %d", got, BlockHeaderSize) } v2 := v2Header() if got := len(v2.SerializeAll()); got != BlockHeaderV2Size { t.Fatalf("v2 header is %d bytes, want %d", got, BlockHeaderV2Size) } } // TestHeadersDecodeMixed: a headers message that carries pre-fork and fork entries decodes both, // and the transaction count after each entry is still enforced. func TestHeadersDecodeMixed(t *testing.T) { v1 := BlockHeader{Version: 0x20000000, Timestamp: 100, Bits: 0x1d00ffff} v2 := v2Header() msg := Headers{Headers: []BlockHeader{v1, v2}} payload, err := msg.EncodePayload() if err != nil { t.Fatal(err) } var back Headers if err := back.DecodePayload(payload); err != nil { t.Fatal(err) } if len(back.Headers) != 2 { t.Fatalf("decoded %d headers, want 2", len(back.Headers)) } if back.Headers[0].V2 != nil { t.Fatal("first header gained version 2 fields") } if back.Headers[1].V2 == nil { t.Fatal("second header lost its version 2 fields") } if back.Headers[1].V2.Height != v2.V2.Height { t.Fatalf("second header height %d, want %d", back.Headers[1].V2.Height, v2.V2.Height) } } // TestForkHashDependsOnEveryField: the hash commits to the fields Core commits to. A field that // does not move the hash is a field the implementation forgot. func TestForkHashDependsOnEveryField(t *testing.T) { base, err := ForkHash(ptr(v2Header())) if err != nil { t.Fatal(err) } changes := []struct { name string mut func(*BlockHeader) }{ {"version", func(h *BlockHeader) { h.Version ^= 1 }}, {"prev", func(h *BlockHeader) { h.PrevBlock[0] ^= 1 }}, {"merkle", func(h *BlockHeader) { h.MerkleRoot[0] ^= 1 }}, {"time", func(h *BlockHeader) { h.Timestamp++ }}, {"bits", func(h *BlockHeader) { h.Bits = 0x1d00fffe }}, {"nonce", func(h *BlockHeader) { h.Nonce++ }}, {"nonce2", func(h *BlockHeader) { h.V2.Nonce2++ }}, {"nonce3", func(h *BlockHeader) { h.V2.Nonce3++ }}, {"extranonce", func(h *BlockHeader) { h.V2.Extranonce[1] ^= 1 }}, {"time offset", func(h *BlockHeader) { h.V2.TimeOffset++ }}, {"txcount", func(h *BlockHeader) { h.V2.TxCount++ }}, {"flags", func(h *BlockHeader) { h.V2.Flags ^= 1 }}, {"clear bits", func(h *BlockHeader) { h.V2.XorKeyClear++ }}, {"xor key", func(h *BlockHeader) { h.V2.XorKey[0] ^= 1 }}, {"height", func(h *BlockHeader) { h.V2.Height++ }}, {"mm rhs", func(h *BlockHeader) { h.V2.MMRHS[0] ^= 1 }}, } for _, change := range changes { h := ptr(v2Header()) change.mut(h) got, err := ForkHash(h) if err != nil { t.Fatalf("%s: %v", change.name, err) } if got == base { t.Fatalf("changing %s did not change the hash", change.name) } } } // TestForkHashFlagVariants: flags' low two bits select different byte streams, so headers that // differ only there must hash differently. func TestForkHashFlagVariants(t *testing.T) { seen := map[chainhash.Hash]uint8{} for _, flags := range []uint8{0, 1, 2, 3, 4, 5, 6, 7} { h := ptr(v2Header()) h.V2.Flags = flags hash, err := ForkHash(h) if err != nil { t.Fatal(err) } if previous, ok := seen[hash]; ok { t.Fatalf("flags %d and %d hash the same", previous, flags) } seen[hash] = flags } } func ptr(h BlockHeader) *BlockHeader { return &h }