// Command nostrino-filtercheck reports what a relay's filter cache covers. // // It answers the operator's question, which is not "is the cache correct" (the // wallet verifies every filter against the header chain it already trusts) but // "is the range I care about there, and what does it cost to read": how many // heights were found, in how many requests, at what rate, and where the gaps // are. A gap is a range the peer serves instead, so its edges are the numbers // that matter when a backfill has to be moved. // // Heights above the node's tip are reported as gaps, because the tool asks the // relay and not the node: a range that runs past the chain's end looks the same // as a range the sidecar has not reached yet. Ask for a range that ends at the // tip to read the coverage alone. package main import ( "context" "errors" "flag" "fmt" "os" "sort" "time" "git.smesh.lol/nostrino/pkg/chain" "git.smesh.lol/nostrino/pkg/filtercache" "git.smesh.lol/nostrino/pkg/relay" ) func main() { if err := run(); err != nil { fmt.Fprintln(os.Stderr, "nostrino-filtercheck:", err) os.Exit(1) } } func run() error { var ( chainArg = flag.String("chain", "btc", "chain profile name or alias") relayURL = flag.String("relay", "", "relay URL to ask") from = flag.String("from", "", "first height (default: the profile's checkpoint)") to = flag.String("to", "", "last height (required)") wait = flag.Duration("wait", 0, "bound on one relay answer (default: the cache's own)") ) flag.Parse() profile, err := chain.ByName(*chainArg) if err != nil { return err } if *relayURL == "" { return errors.New("-relay is required") } if *to == "" { return errors.New("-to is required") } last, err := parseHeight(*to) if err != nil { return err } first := profile.CheckpointHeight() if *from != "" { if first, err = parseHeight(*from); err != nil { return err } } if last < first { return fmt.Errorf("range %d..%d is reversed", first, last) } requests := 0 cache, err := filtercache.New(filtercache.Config{ Chain: profile.Name, Relays: []string{*relayURL}, Dial: relay.DialTransport, Wait: *wait, Logf: func(string, ...any) { requests++ }, }) if err != nil { return err } defer cache.Close() fmt.Printf("chain=%s relay=%s asking for %d..%d (%d heights)\n", profile.Name, *relayURL, first, last, last-first+1) started := time.Now() runs, err := cache.Filters(context.Background(), first, last) if err != nil { return err } elapsed := time.Since(started) have := make(map[uint32]bool, last-first+1) bytes := 0 for _, run := range runs { for i, filter := range run.Filters { height := run.From + uint32(i) if height < first || height > last { continue } have[height] = true bytes += len(filter) } } var gaps []uint32 for height := first; height <= last; height++ { if !have[height] { gaps = append(gaps, height) } } rate := 0.0 if elapsed > 0 { rate = float64(bytes) / (1 << 20) / elapsed.Seconds() } fmt.Printf("covered %d of %d in %d request(s), %.2f MiB in %s (%.2f MiB/s)\n", len(have), last-first+1, requests, float64(bytes)/(1<<20), elapsed.Round(time.Millisecond), rate) if len(gaps) == 0 { return nil } sort.Slice(gaps, func(i, j int) bool { return gaps[i] < gaps[j] }) // The edges of a gap are what an operator acts on, so print the first few // and the count rather than a wall of heights. shown := gaps if len(shown) > 8 { shown = shown[:8] } fmt.Printf("gaps: %d height(s), first %v\n", len(gaps), shown) return nil } func parseHeight(s string) (uint32, error) { var n uint64 if _, err := fmt.Sscan(s, &n); err != nil || n > 0xffffffff { return 0, fmt.Errorf("height %q is not a block height", s) } return uint32(n), nil }