package gate import ( "encoding/binary" "encoding/hex" "encoding/json" "path/filepath" "testing" ) // TestFiltersBuiltWithoutTheSet is the small-host path end to end: a fill of // bodies, then the serve command with -filters-only, which computes the filters // from those bodies with the spent scripts on disk and never builds a UTXO set. func TestFiltersBuiltWithoutTheSet(t *testing.T) { r := nodeOn(t) r.createWallet(t) r.mine(t, 20) tip := r.height(t) bin := t.TempDir() buildBinary(t, bin, "btcd-lited") buildBinary(t, bin, "btcd-lite-serve") data := t.TempDir() instPort := freePort(t) inst := startProcess(t, filepath.Join(bin, "btcd-lited"), "-chain", "btc", "-peer", "127.0.0.1:"+itoa(r.p2p), "-regtest", "-data", data, "-listen", "127.0.0.1:"+itoa(instPort)) waitForBlocks(t, inst, "", "http://127.0.0.1:"+itoa(instPort), tip+1) _ = inst.Process.Kill() port := freePort(t) serveBin := filepath.Join(bin, "btcd-lite-serve") serve := startProcess(t, serveBin, "-chain", "btc", "-regtest", "-data", data, "-listen", "127.0.0.1:"+itoa(port), "-filters-only") base := "http://127.0.0.1:" + itoa(port) waitForHTTP(t, base+"/v1/status") defer func() { _ = serve.Process.Kill() }() // No UTXO dataset was asked for, and none is served. var status struct { Chains map[string]struct{ Tip uint32 } `json:"chains"` Blocks int `json:"blocks"` Filters int `json:"filters"` } get(t, base+"/v1/status", &status) if status.Filters != tip+1 { t.Fatalf("the instance serves %d filters for %d heights", status.Filters, tip+1) } // Every filter a range serves is the node's for that block. names := rangeNamesOf(t, base, "btc", "flt", 0, uint32(tip)+1) if len(names) == 0 { t.Fatal("no filter chunks") } at := 0 var got [][]byte for _, name := range names { raw := chunkAt(t, base, name) for at < len(raw) { n := int(binary.LittleEndian.Uint32(raw[at:])) got = append(got, raw[at+4:at+4+n]) at += 4 + n } } if len(got) != tip+1 { t.Fatalf("%d filters served", len(got)) } for h := 0; h <= tip; h++ { hash := r.must(t, "getblockhash", itoa(h)) var node struct { Filter string `json:"filter"` } raw, err := r.cli("getblockfilter", hash) if err != nil { t.Fatalf("getblockfilter %d: %v", h, err) } if err := json.Unmarshal([]byte(raw), &node); err != nil { t.Fatalf("height %d: %v", h, err) } want, err := hex.DecodeString(node.Filter) if err != nil { t.Fatal(err) } if string(got[h]) != string(want) { t.Fatalf("height %d: the served filter is not the node's", h) } } t.Logf("%d filters computed from bodies with no UTXO set and served to a client", len(got)) }