package headerevents import ( "strings" "testing" "git.smesh.lol/nostrino/pkg/btcwire" "git.smesh.lol/nostrino/pkg/crypto/chainhash" "git.smesh.lol/nostrino/pkg/nostr" ) // bitcoinHeader is one ordinary 80-byte header, built rather than copied so the test says // what every field is. func bitcoinHeader() btcwire.BlockHeader { return btcwire.BlockHeader{ Version: 1, PrevBlock: chainhash.Hash{0: 1}, MerkleRoot: chainhash.Hash{ 0: 2, 1: 3, 2: 4, 3: 5, }, Timestamp: 1_700_000_000, Bits: 0x1d00ffff, Nonce: 42, } } // versionTwoHeader is a forked-chain header: the same 80 bytes plus the mining fields. func versionTwoHeader() btcwire.BlockHeader { header := bitcoinHeader() flag := uint32(btcwire.HeaderV2Flag) | 1 header.Version = int32(flag) header.V2 = &btcwire.HeaderV2{ Nonce2: 7, Nonce3: 8, Extranonce: [16]byte{0: 9}, TimeOffset: 11, TxCount: 12, Flags: 13, XorKeyClear: 14, Height: 1_700_000, } return header } // TestHeaderRoundTrip: an event carries a header and comes back byte-identical, with the // height and the hash it names. func TestHeaderRoundTrip(t *testing.T) { header := bitcoinHeader() event, err := Encode("btc", 1234, &header) if err != nil { t.Fatal(err) } if event.Kind != Kind { t.Fatalf("kind = %d, want %d", event.Kind, Kind) } if event.CreatedAt != int64(header.Timestamp) { t.Fatalf("created_at = %d, want the header's time %d", event.CreatedAt, header.Timestamp) } decoded, err := Decode(&event) if err != nil { t.Fatal(err) } if decoded.Chain != "btc" || decoded.Height != 1234 { t.Fatalf("decoded %q at %d", decoded.Chain, decoded.Height) } if decoded.Hash != header.BlockHash() { t.Fatalf("hash = %s, want %s", decoded.Hash.String(), header.BlockHash().String()) } if got := decoded.Raw.SerializeAll(); string(got) != string(header.SerializeAll()) { t.Fatalf("header round trip changed the bytes") } } // TestVersionTwoRoundTrip: the forked chain's header is 164 bytes and must survive the same // path, because the header's size is decided by its own version flag. func TestVersionTwoRoundTrip(t *testing.T) { header := versionTwoHeader() event, err := Encode("blake", 77, &header) if err != nil { t.Fatal(err) } decoded, err := Decode(&event) if err != nil { t.Fatal(err) } if decoded.Raw.V2 == nil { t.Fatal("the version 2 fields were lost") } if decoded.Raw.V2.Nonce2 != 7 || decoded.Raw.V2.Height != 1_700_000 { t.Fatalf("version 2 fields = %+v", *decoded.Raw.V2) } if decoded.Hash != header.BlockHash() { t.Fatalf("hash = %s, want %s", decoded.Hash.String(), header.BlockHash().String()) } // The event's time is the wire time, which for an offset header is the real time. if event.CreatedAt != int64(header.TimeOnWire()) { t.Fatalf("created_at = %d, want %d", event.CreatedAt, header.TimeOnWire()) } } // TestAHashThatDoesNotMatchIsRefused: the block hash tag is the name the header is checked // against, so an event whose tag names another block is not that block. func TestAHashThatDoesNotMatchIsRefused(t *testing.T) { header := bitcoinHeader() event, err := Encode("btc", 10, &header) if err != nil { t.Fatal(err) } for i := range event.Tags { if event.Tags[i].Name() == BlockTag { event.Tags[i] = nostr.Tag{BlockTag, strings.Repeat("11", 32)} } } if _, err := Decode(&event); err == nil { t.Fatal("an event naming another block hash decoded") } } // TestAnotherKindIsRefused: a filter batch is not a header, and decoding one as a header has // to say so rather than parse whatever is in it. func TestAnotherKindIsRefused(t *testing.T) { event := nostr.Event{Kind: 3050, Content: "AAAA"} if _, err := Decode(&event); err == nil { t.Fatal("a filter batch decoded as a header") } } // TestAHeaderWithNoHeightIsRefused: every tag the decoder needs is required, because a header // without a height cannot be placed in a chain. func TestAHeaderWithNoHeightIsRefused(t *testing.T) { header := bitcoinHeader() event, err := Encode("btc", 10, &header) if err != nil { t.Fatal(err) } kept := event.Tags[:0] for _, tag := range event.Tags { if tag.Name() != HeightTag { kept = append(kept, tag) } } event.Tags = kept if _, err := Decode(&event); err == nil { t.Fatal("a header with no height decoded") } } // TestQueryNamesEveryHeight: a relay matches tag values, so the filter asks for each height in // the range, and the limit is the number of events that range can produce. func TestQueryNamesEveryHeight(t *testing.T) { query := Query("btc", 100, 104) if query.Kinds[0] != Kind { t.Fatalf("kinds = %v", query.Kinds) } heights := query.Tags[HeightTag] if len(heights) != 5 || heights[0] != "100" || heights[4] != "104" { t.Fatalf("heights = %v", heights) } if query.Limit != 5 { t.Fatalf("limit = %d, want 5", query.Limit) } if chains := query.Tags[ChainTag]; len(chains) != 1 || chains[0] != "btc" { t.Fatalf("chains = %v", chains) } }