package headercache import ( "testing" "git.smesh.lol/nostrino/pkg/btcwire" "git.smesh.lol/nostrino/pkg/headerevents" "git.smesh.lol/nostrino/pkg/nostr" "git.smesh.lol/nostrino/pkg/relay" ) // headerAt builds one header for the test, distinguished by its merkle root and time. func headerAt(prev [32]byte, tag byte) btcwire.BlockHeader { header := btcwire.BlockHeader{ Version: 1, Timestamp: 1_700_000_000 + uint32(tag), Bits: 0x1d00ffff, } copy(header.PrevBlock[:], prev[:]) header.MerkleRoot[0] = tag return header } // eventFor encodes and signs one header event, which is what a relay stores. func eventFor(t *testing.T, height uint32, header *btcwire.BlockHeader) *nostr.Event { t.Helper() event, err := headerevents.Encode("btc", height, header) if err != nil { t.Fatal(err) } if err := event.Sign([32]byte{1, 2, 3}); err != nil { t.Fatal(err) } return &event } func newTestCache(t *testing.T) *Cache { t.Helper() cache, err := New(Config{ Chain: "btc", Relays: []string{"wss://relay.example"}, Dial: func(string) (relay.Transport, error) { return nil, nil }, }) if err != nil { t.Fatal(err) } return cache } // TestTheRunIsContiguousFromTheFirstHeight: neutrino appends the answer to its own chain, so a // run must start exactly at the height it asked for and have no holes. func TestTheRunIsContiguousFromTheFirstHeight(t *testing.T) { cache := newTestCache(t) prev := [32]byte{0: 9} h1 := headerAt(prev, 1) h2 := headerAt(h1.BlockHash(), 2) h3 := headerAt(h2.BlockHash(), 3) events := []*nostr.Event{ eventFor(t, 10, &h1), eventFor(t, 11, &h2), eventFor(t, 12, &h3), } run, err := cache.run(events, 10, 12) if err != nil { t.Fatal(err) } if len(run) != 3 { t.Fatalf("run of %d headers, want 3", len(run)) } if run[0].BlockHash() != h1.BlockHash() || run[2].BlockHash() != h3.BlockHash() { t.Fatalf("the run is not in height order") } } // TestAHoleStopsTheRun: a height the relay does not hold ends the run there, because the // headers above it cannot be appended by a client that chains them. func TestAHoleStopsTheRun(t *testing.T) { cache := newTestCache(t) prev := [32]byte{0: 9} h1 := headerAt(prev, 1) h3 := headerAt([32]byte{0: 0x33}, 3) events := []*nostr.Event{ eventFor(t, 10, &h1), eventFor(t, 12, &h3), } run, err := cache.run(events, 10, 12) if err != nil { t.Fatal(err) } if len(run) != 1 { t.Fatalf("run of %d headers, want the one before the hole", len(run)) } if run[0].BlockHash() != h1.BlockHash() { t.Fatalf("the run kept the wrong header") } } // TestAHeaderOutsideTheWindowIsDropped: a relay that answers loosely must not cost the client // anything, so events outside the range are ignored. func TestAHeaderOutsideTheWindowIsDropped(t *testing.T) { cache := newTestCache(t) inside := headerAt([32]byte{0: 9}, 1) outside := headerAt([32]byte{0: 8}, 2) events := []*nostr.Event{ eventFor(t, 4, &outside), eventFor(t, 10, &inside), } run, err := cache.run(events, 10, 12) if err != nil { t.Fatal(err) } if len(run) != 1 || run[0].BlockHash() != inside.BlockHash() { t.Fatalf("run = %d headers, want only the one inside the window", len(run)) } } // TestAnotherChainsHeaderIsDropped: the chain tag is the whole reason a relay can serve more // than one chain, so a header tagged with another chain is not this chain's. func TestAnotherChainsHeaderIsDropped(t *testing.T) { cache := newTestCache(t) header := headerAt([32]byte{0: 9}, 1) event, err := headerevents.Encode("blake", 10, &header) if err != nil { t.Fatal(err) } if err := event.Sign([32]byte{1, 2, 3}); err != nil { t.Fatal(err) } run, err := cache.run([]*nostr.Event{&event}, 10, 12) if err != nil { t.Fatal(err) } if len(run) != 0 { t.Fatalf("a header from another chain was accepted") } } // TestTwoHeadersAtOneHeightKeepTheFirst: a reorg publishes a competing header at a height. The // cache is not the arbiter, so it returns one and lets the chain decide whether it connects. func TestTwoHeadersAtOneHeightKeepTheFirst(t *testing.T) { cache := newTestCache(t) first := headerAt([32]byte{0: 9}, 1) second := headerAt([32]byte{0: 9}, 2) run, err := cache.run([]*nostr.Event{ eventFor(t, 10, &first), eventFor(t, 10, &second), }, 10, 10) if err != nil { t.Fatal(err) } if len(run) != 1 { t.Fatalf("run of %d headers at one height, want 1", len(run)) } if run[0].BlockHash() != first.BlockHash() { t.Fatalf("the run kept the later header") } }