compute_filters_test.go raw

   1  package gate
   2  
   3  import (
   4  	"encoding/binary"
   5  	"encoding/hex"
   6  	"encoding/json"
   7  	"path/filepath"
   8  	"testing"
   9  )
  10  
  11  // TestFiltersBuiltWithoutTheSet is the small-host path end to end: a fill of
  12  // bodies, then the serve command with -filters-only, which computes the filters
  13  // from those bodies with the spent scripts on disk and never builds a UTXO set.
  14  func TestFiltersBuiltWithoutTheSet(t *testing.T) {
  15  	r := nodeOn(t)
  16  	r.createWallet(t)
  17  	r.mine(t, 20)
  18  	tip := r.height(t)
  19  	bin := t.TempDir()
  20  	buildBinary(t, bin, "btcd-lited")
  21  	buildBinary(t, bin, "btcd-lite-serve")
  22  	data := t.TempDir()
  23  	instPort := freePort(t)
  24  	inst := startProcess(t, filepath.Join(bin, "btcd-lited"),
  25  		"-chain", "btc", "-peer", "127.0.0.1:"+itoa(r.p2p), "-regtest",
  26  		"-data", data, "-listen", "127.0.0.1:"+itoa(instPort))
  27  	waitForBlocks(t, inst, "", "http://127.0.0.1:"+itoa(instPort), tip+1)
  28  	_ = inst.Process.Kill()
  29  
  30  	port := freePort(t)
  31  	serveBin := filepath.Join(bin, "btcd-lite-serve")
  32  	serve := startProcess(t, serveBin,
  33  		"-chain", "btc", "-regtest", "-data", data,
  34  		"-listen", "127.0.0.1:"+itoa(port), "-filters-only")
  35  	base := "http://127.0.0.1:" + itoa(port)
  36  	waitForHTTP(t, base+"/v1/status")
  37  	defer func() { _ = serve.Process.Kill() }()
  38  
  39  	// No UTXO dataset was asked for, and none is served.
  40  	var status struct {
  41  		Chains  map[string]struct{ Tip uint32 } `json:"chains"`
  42  		Blocks  int                             `json:"blocks"`
  43  		Filters int                             `json:"filters"`
  44  	}
  45  	get(t, base+"/v1/status", &status)
  46  	if status.Filters != tip+1 {
  47  		t.Fatalf("the instance serves %d filters for %d heights", status.Filters, tip+1)
  48  	}
  49  	// Every filter a range serves is the node's for that block.
  50  	names := rangeNamesOf(t, base, "btc", "flt", 0, uint32(tip)+1)
  51  	if len(names) == 0 {
  52  		t.Fatal("no filter chunks")
  53  	}
  54  	at := 0
  55  	var got [][]byte
  56  	for _, name := range names {
  57  		raw := chunkAt(t, base, name)
  58  		for at < len(raw) {
  59  			n := int(binary.LittleEndian.Uint32(raw[at:]))
  60  			got = append(got, raw[at+4:at+4+n])
  61  			at += 4 + n
  62  		}
  63  	}
  64  	if len(got) != tip+1 {
  65  		t.Fatalf("%d filters served", len(got))
  66  	}
  67  	for h := 0; h <= tip; h++ {
  68  		hash := r.must(t, "getblockhash", itoa(h))
  69  		var node struct {
  70  			Filter string `json:"filter"`
  71  		}
  72  		raw, err := r.cli("getblockfilter", hash)
  73  		if err != nil {
  74  			t.Fatalf("getblockfilter %d: %v", h, err)
  75  		}
  76  		if err := json.Unmarshal([]byte(raw), &node); err != nil {
  77  			t.Fatalf("height %d: %v", h, err)
  78  		}
  79  		want, err := hex.DecodeString(node.Filter)
  80  		if err != nil {
  81  			t.Fatal(err)
  82  		}
  83  		if string(got[h]) != string(want) {
  84  			t.Fatalf("height %d: the served filter is not the node's", h)
  85  		}
  86  	}
  87  	t.Logf("%d filters computed from bodies with no UTXO set and served to a client", len(got))
  88  }
  89