checkpoint_test.go raw
1 package rigscan
2
3 import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "os"
8 "strconv"
9 "strings"
10 "sync"
11 "testing"
12 "time"
13
14 "git.smesh.lol/nostrino/pkg/chain"
15 "git.smesh.lol/nostrino/pkg/crypto/chainhash"
16 "git.smesh.lol/nostrino/pkg/neutrino"
17 )
18
19 // TestSyncStartsAtACheckpoint: a fresh store whose profile carries a checkpoint verifies the
20 // chain from that height, not from genesis, and the sync says what it downloaded and how fast.
21 //
22 // The rig's two chains share only their genesis, so the checkpoint here is placed a few hundred
23 // blocks below the tip rather than at a real fork. What the test proves is the mechanism the
24 // deployment relies on: the light client asks for nothing below the checkpoint, stores nothing
25 // below it, and measures its rate from it — which is the difference between syncing a forked
26 // chain's own history and downloading Bitcoin's history twice.
27 func TestSyncStartsAtACheckpoint(t *testing.T) {
28 if os.Getenv("NOSTRINO_RIG") != "1" {
29 t.Skip("NOSTRINO_RIG is not set, and this test needs the regtest rig")
30 }
31
32 profile, err := chain.ByName("regtest")
33
34 if err != nil {
35 t.Fatal(err)
36 }
37
38 tip, err := strconv.ParseInt(cli(t, "getblockcount"), 10, 64)
39
40 if err != nil {
41 t.Fatalf("getblockcount: %v", err)
42 }
43
44 const depth = 200
45
46 if tip <= depth+10 {
47 t.Fatalf("the chain is only %d blocks, too short for a checkpoint", tip)
48 }
49
50 height := uint32(tip) - depth
51 blockHash, err := chainhash.NewHashFromStr(cli(t, "getblockhash", fmt.Sprintf("%d", height)))
52
53 if err != nil {
54 t.Fatalf("getblockhash: %v", err)
55 }
56
57 var filter struct {
58 Filter string `json:"filter"`
59 Header string `json:"header"`
60 }
61
62 if err := json.Unmarshal([]byte(cli(t, "getblockfilter", blockHash.String())), &filter); err != nil {
63 t.Fatalf("getblockfilter: %v", err)
64 }
65
66 if filter.Filter == "" || filter.Header == "" {
67 t.Fatal("the node returned no filter and header for the checkpoint")
68 }
69
70 filterHeader, err := chainhash.NewHashFromStr(filter.Header)
71
72 if err != nil {
73 t.Fatalf("filter header: %v", err)
74 }
75
76 profile.Checkpoint = &chain.Checkpoint{
77 Height: height,
78 BlockHash: *blockHash,
79 FilterHeader: *filterHeader,
80 }
81
82 address, dial := rigDial(t)
83
84 var (
85 mu sync.Mutex
86 log strings.Builder
87 )
88
89 client, err := neutrino.New(neutrino.Config{
90 Profile: profile,
91 Store: neutrino.NewMemory(),
92 Address: address,
93 Dial: dial,
94 Logf: func(format string, args ...any) {
95 mu.Lock()
96 defer mu.Unlock()
97
98 log.WriteString(fmt.Sprintf(format, args...) + "\n")
99 },
100 })
101
102 if err != nil {
103 t.Fatal(err)
104 }
105
106 defer client.Close()
107
108 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
109 defer cancel()
110
111 if err := client.Connect(ctx); err != nil {
112 t.Fatal(err)
113 }
114
115 // Connecting installs the anchor. Nothing above it has been verified yet, and nothing
116 // below it may be: the chain starts here.
117 if anchor, ok := client.Store().Anchor(); !ok || anchor.Height != height {
118 t.Fatalf("anchor %+v (set %v), want height %d", anchor, ok, height)
119 }
120
121 result, err := client.Sync(ctx)
122
123 if err != nil {
124 t.Fatalf("sync: %v", err)
125 }
126
127 if !result.Checkpoint || result.AnchorHeight != height {
128 t.Fatalf("sync from %d (checkpoint %v), want the checkpoint at %d",
129 result.AnchorHeight, result.Checkpoint, height)
130 }
131
132 want := int(tip) - int(height)
133
134 if result.HeadersAdded != want || result.FilterHeadersAdded != want || result.FiltersAdded != want {
135 t.Fatalf("downloaded %d headers, %d filter headers, %d filters, want %d of each",
136 result.HeadersAdded, result.FilterHeadersAdded, result.FiltersAdded, want)
137 }
138
139 if result.HeaderBytes != want*80 {
140 t.Fatalf("%d header bytes, want %d", result.HeaderBytes, want*80)
141 }
142
143 if result.FilterHeaderBytes != want*64 {
144 t.Fatalf("%d filter header bytes, want %d", result.FilterHeaderBytes, want*64)
145 }
146
147 if result.FilterBytes <= 0 {
148 t.Fatalf("%d filter bytes", result.FilterBytes)
149 }
150
151 if result.Bytes() != result.HeaderBytes+result.FilterHeaderBytes+result.FilterBytes ||
152 result.Bytes() <= result.FilterBytes {
153 t.Fatalf("total %d from %d filter bytes", result.Bytes(), result.FilterBytes)
154 }
155
156 // Below the checkpoint is not this store's history, and a match there would be a coin
157 // the wallet cannot verify.
158 if _, ok := client.Store().Header(height); ok {
159 t.Fatal("the checkpoint header was stored")
160 }
161
162 if _, ok := client.Store().Filter(height); ok {
163 t.Fatal("the filter at the checkpoint was stored")
164 }
165
166 mu.Lock()
167 line := log.String()
168 mu.Unlock()
169
170 if !strings.Contains(line, fmt.Sprintf("from the checkpoint at %d", height)) {
171 t.Fatalf("the sync did not name its start:\n%s", line)
172 }
173
174 if !strings.Contains(line, "filters/s") || !strings.Contains(line, "/s") {
175 t.Fatalf("the sync did not report a rate:\n%s", line)
176 }
177
178 t.Logf("checkpoint sync %d..%d: %s", height, result.TipHeight, strings.TrimSpace(line))
179
180 // A checkpoint the peer cannot build on is refused, not silently ignored.
181 bad := *profile
182 bad.Checkpoint = &chain.Checkpoint{
183 Height: height,
184 BlockHash: chainhash.DoubleHashH([]byte("not this chain")),
185 FilterHeader: *filterHeader,
186 }
187
188 other, err := neutrino.New(neutrino.Config{Profile: &bad, Store: neutrino.NewMemory(), Address: address, Dial: dial})
189
190 if err != nil {
191 t.Fatal(err)
192 }
193
194 defer other.Close()
195
196 if _, err := other.Sync(ctx); err == nil {
197 t.Fatal("a checkpoint that is not on the chain was accepted")
198 }
199 }
200