package rigscan import ( "context" "encoding/json" "fmt" "os" "strconv" "strings" "sync" "testing" "time" "git.smesh.lol/nostrino/pkg/chain" "git.smesh.lol/nostrino/pkg/crypto/chainhash" "git.smesh.lol/nostrino/pkg/neutrino" ) // TestSyncStartsAtACheckpoint: a fresh store whose profile carries a checkpoint verifies the // chain from that height, not from genesis, and the sync says what it downloaded and how fast. // // The rig's two chains share only their genesis, so the checkpoint here is placed a few hundred // blocks below the tip rather than at a real fork. What the test proves is the mechanism the // deployment relies on: the light client asks for nothing below the checkpoint, stores nothing // below it, and measures its rate from it — which is the difference between syncing a forked // chain's own history and downloading Bitcoin's history twice. func TestSyncStartsAtACheckpoint(t *testing.T) { if os.Getenv("NOSTRINO_RIG") != "1" { t.Skip("NOSTRINO_RIG is not set, and this test needs the regtest rig") } profile, err := chain.ByName("regtest") if err != nil { t.Fatal(err) } tip, err := strconv.ParseInt(cli(t, "getblockcount"), 10, 64) if err != nil { t.Fatalf("getblockcount: %v", err) } const depth = 200 if tip <= depth+10 { t.Fatalf("the chain is only %d blocks, too short for a checkpoint", tip) } height := uint32(tip) - depth blockHash, err := chainhash.NewHashFromStr(cli(t, "getblockhash", fmt.Sprintf("%d", height))) if err != nil { t.Fatalf("getblockhash: %v", err) } var filter struct { Filter string `json:"filter"` Header string `json:"header"` } if err := json.Unmarshal([]byte(cli(t, "getblockfilter", blockHash.String())), &filter); err != nil { t.Fatalf("getblockfilter: %v", err) } if filter.Filter == "" || filter.Header == "" { t.Fatal("the node returned no filter and header for the checkpoint") } filterHeader, err := chainhash.NewHashFromStr(filter.Header) if err != nil { t.Fatalf("filter header: %v", err) } profile.Checkpoint = &chain.Checkpoint{ Height: height, BlockHash: *blockHash, FilterHeader: *filterHeader, } address, dial := rigDial(t) var ( mu sync.Mutex log strings.Builder ) client, err := neutrino.New(neutrino.Config{ Profile: profile, Store: neutrino.NewMemory(), Address: address, Dial: dial, Logf: func(format string, args ...any) { mu.Lock() defer mu.Unlock() log.WriteString(fmt.Sprintf(format, args...) + "\n") }, }) if err != nil { t.Fatal(err) } defer client.Close() ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) defer cancel() if err := client.Connect(ctx); err != nil { t.Fatal(err) } // Connecting installs the anchor. Nothing above it has been verified yet, and nothing // below it may be: the chain starts here. if anchor, ok := client.Store().Anchor(); !ok || anchor.Height != height { t.Fatalf("anchor %+v (set %v), want height %d", anchor, ok, height) } result, err := client.Sync(ctx) if err != nil { t.Fatalf("sync: %v", err) } if !result.Checkpoint || result.AnchorHeight != height { t.Fatalf("sync from %d (checkpoint %v), want the checkpoint at %d", result.AnchorHeight, result.Checkpoint, height) } want := int(tip) - int(height) if result.HeadersAdded != want || result.FilterHeadersAdded != want || result.FiltersAdded != want { t.Fatalf("downloaded %d headers, %d filter headers, %d filters, want %d of each", result.HeadersAdded, result.FilterHeadersAdded, result.FiltersAdded, want) } if result.HeaderBytes != want*80 { t.Fatalf("%d header bytes, want %d", result.HeaderBytes, want*80) } if result.FilterHeaderBytes != want*64 { t.Fatalf("%d filter header bytes, want %d", result.FilterHeaderBytes, want*64) } if result.FilterBytes <= 0 { t.Fatalf("%d filter bytes", result.FilterBytes) } if result.Bytes() != result.HeaderBytes+result.FilterHeaderBytes+result.FilterBytes || result.Bytes() <= result.FilterBytes { t.Fatalf("total %d from %d filter bytes", result.Bytes(), result.FilterBytes) } // Below the checkpoint is not this store's history, and a match there would be a coin // the wallet cannot verify. if _, ok := client.Store().Header(height); ok { t.Fatal("the checkpoint header was stored") } if _, ok := client.Store().Filter(height); ok { t.Fatal("the filter at the checkpoint was stored") } mu.Lock() line := log.String() mu.Unlock() if !strings.Contains(line, fmt.Sprintf("from the checkpoint at %d", height)) { t.Fatalf("the sync did not name its start:\n%s", line) } if !strings.Contains(line, "filters/s") || !strings.Contains(line, "/s") { t.Fatalf("the sync did not report a rate:\n%s", line) } t.Logf("checkpoint sync %d..%d: %s", height, result.TipHeight, strings.TrimSpace(line)) // A checkpoint the peer cannot build on is refused, not silently ignored. bad := *profile bad.Checkpoint = &chain.Checkpoint{ Height: height, BlockHash: chainhash.DoubleHashH([]byte("not this chain")), FilterHeader: *filterHeader, } other, err := neutrino.New(neutrino.Config{Profile: &bad, Store: neutrino.NewMemory(), Address: address, Dial: dial}) if err != nil { t.Fatal(err) } defer other.Close() if _, err := other.Sync(ctx); err == nil { t.Fatal("a checkpoint that is not on the chain was accepted") } }