package serve_test import ( "context" "testing" "git.smesh.lol/nostrino/pkg/crypto/chainhash" ) // TestSourcesServeWhatTheStoresHold: the header source returns a contiguous // window and stops at its coverage, and the block source resolves a hash // through the header chain to the block. func TestSourcesServeWhatTheStoresHold(t *testing.T) { v, raw := build(t) blocks := split(raw, 0) ctx := context.Background() hdrs, err := v.HeaderSource("btc")(ctx, 10, 20) if err != nil { t.Fatal(err) } if len(hdrs) != 11 { t.Fatalf("%d headers for 10..20", len(hdrs)) } if _, err := v.HeaderSource("btc")(ctx, 1000, 1010); err != nil { t.Fatal(err) } // The block at height 7, resolved by the hash its header gives. hdr, ok := v.Headers["btc"].Header(7) if !ok { t.Fatal("no header at 7") } got, err := v.BlockSource()(ctx, hdr.BlockHash()) if err != nil { t.Fatalf("block source: %v", err) } if string(got) != string(blocks[7]) { t.Fatal("the block source answered another block") } // A hash no header has is not answered. if _, err := v.BlockSource()(ctx, chainhash.DoubleHashH([]byte("nowhere"))); err == nil { t.Fatal("an unknown hash was answered") } }