sources_test.go raw

   1  package serve_test
   2  
   3  import (
   4  	"context"
   5  	"testing"
   6  
   7  	"git.smesh.lol/nostrino/pkg/crypto/chainhash"
   8  )
   9  
  10  // TestSourcesServeWhatTheStoresHold: the header source returns a contiguous
  11  // window and stops at its coverage, and the block source resolves a hash
  12  // through the header chain to the block.
  13  func TestSourcesServeWhatTheStoresHold(t *testing.T) {
  14  	v, raw := build(t)
  15  	blocks := split(raw, 0)
  16  	ctx := context.Background()
  17  	hdrs, err := v.HeaderSource("btc")(ctx, 10, 20)
  18  	if err != nil {
  19  		t.Fatal(err)
  20  	}
  21  	if len(hdrs) != 11 {
  22  		t.Fatalf("%d headers for 10..20", len(hdrs))
  23  	}
  24  	if _, err := v.HeaderSource("btc")(ctx, 1000, 1010); err != nil {
  25  		t.Fatal(err)
  26  	}
  27  	// The block at height 7, resolved by the hash its header gives.
  28  	hdr, ok := v.Headers["btc"].Header(7)
  29  	if !ok {
  30  		t.Fatal("no header at 7")
  31  	}
  32  	got, err := v.BlockSource()(ctx, hdr.BlockHash())
  33  	if err != nil {
  34  		t.Fatalf("block source: %v", err)
  35  	}
  36  	if string(got) != string(blocks[7]) {
  37  		t.Fatal("the block source answered another block")
  38  	}
  39  	// A hash no header has is not answered.
  40  	if _, err := v.BlockSource()(ctx, chainhash.DoubleHashH([]byte("nowhere"))); err == nil {
  41  		t.Fatal("an unknown hash was answered")
  42  	}
  43  }
  44