1 // Command nostrino-filtercheck reports what a relay's filter cache covers.
2 //
3 // It answers the operator's question, which is not "is the cache correct" (the
4 // wallet verifies every filter against the header chain it already trusts) but
5 // "is the range I care about there, and what does it cost to read": how many
6 // heights were found, in how many requests, at what rate, and where the gaps
7 // are. A gap is a range the peer serves instead, so its edges are the numbers
8 // that matter when a backfill has to be moved.
9 //
10 // Heights above the node's tip are reported as gaps, because the tool asks the
11 // relay and not the node: a range that runs past the chain's end looks the same
12 // as a range the sidecar has not reached yet. Ask for a range that ends at the
13 // tip to read the coverage alone.
14 package main
15 16 import (
17 "context"
18 "errors"
19 "flag"
20 "fmt"
21 "os"
22 "sort"
23 "time"
24 25 "git.smesh.lol/nostrino/pkg/chain"
26 "git.smesh.lol/nostrino/pkg/filtercache"
27 "git.smesh.lol/nostrino/pkg/relay"
28 )
29 30 func main() {
31 if err := run(); err != nil {
32 fmt.Fprintln(os.Stderr, "nostrino-filtercheck:", err)
33 os.Exit(1)
34 }
35 }
36 37 func run() error {
38 var (
39 chainArg = flag.String("chain", "btc", "chain profile name or alias")
40 relayURL = flag.String("relay", "", "relay URL to ask")
41 from = flag.String("from", "", "first height (default: the profile's checkpoint)")
42 to = flag.String("to", "", "last height (required)")
43 wait = flag.Duration("wait", 0, "bound on one relay answer (default: the cache's own)")
44 )
45 flag.Parse()
46 47 profile, err := chain.ByName(*chainArg)
48 if err != nil {
49 return err
50 }
51 52 if *relayURL == "" {
53 return errors.New("-relay is required")
54 }
55 56 if *to == "" {
57 return errors.New("-to is required")
58 }
59 60 last, err := parseHeight(*to)
61 if err != nil {
62 return err
63 }
64 65 first := profile.CheckpointHeight()
66 67 if *from != "" {
68 if first, err = parseHeight(*from); err != nil {
69 return err
70 }
71 }
72 73 if last < first {
74 return fmt.Errorf("range %d..%d is reversed", first, last)
75 }
76 77 requests := 0
78 79 cache, err := filtercache.New(filtercache.Config{
80 Chain: profile.Name,
81 Relays: []string{*relayURL},
82 Dial: relay.DialTransport,
83 Wait: *wait,
84 Logf: func(string, ...any) { requests++ },
85 })
86 if err != nil {
87 return err
88 }
89 defer cache.Close()
90 91 fmt.Printf("chain=%s relay=%s asking for %d..%d (%d heights)\n",
92 profile.Name, *relayURL, first, last, last-first+1)
93 94 started := time.Now()
95 96 runs, err := cache.Filters(context.Background(), first, last)
97 if err != nil {
98 return err
99 }
100 101 elapsed := time.Since(started)
102 103 have := make(map[uint32]bool, last-first+1)
104 bytes := 0
105 106 for _, run := range runs {
107 for i, filter := range run.Filters {
108 height := run.From + uint32(i)
109 110 if height < first || height > last {
111 continue
112 }
113 114 have[height] = true
115 bytes += len(filter)
116 }
117 }
118 119 var gaps []uint32
120 121 for height := first; height <= last; height++ {
122 if !have[height] {
123 gaps = append(gaps, height)
124 }
125 }
126 127 rate := 0.0
128 if elapsed > 0 {
129 rate = float64(bytes) / (1 << 20) / elapsed.Seconds()
130 }
131 132 fmt.Printf("covered %d of %d in %d request(s), %.2f MiB in %s (%.2f MiB/s)\n",
133 len(have), last-first+1, requests, float64(bytes)/(1<<20),
134 elapsed.Round(time.Millisecond), rate)
135 136 if len(gaps) == 0 {
137 return nil
138 }
139 140 sort.Slice(gaps, func(i, j int) bool { return gaps[i] < gaps[j] })
141 142 // The edges of a gap are what an operator acts on, so print the first few
143 // and the count rather than a wall of heights.
144 shown := gaps
145 if len(shown) > 8 {
146 shown = shown[:8]
147 }
148 149 fmt.Printf("gaps: %d height(s), first %v\n", len(gaps), shown)
150 151 return nil
152 }
153 154 func parseHeight(s string) (uint32, error) {
155 var n uint64
156 if _, err := fmt.Sscan(s, &n); err != nil || n > 0xffffffff {
157 return 0, fmt.Errorf("height %q is not a block height", s)
158 }
159 160 return uint32(n), nil
161 }
162