package gate import ( "os" "path/filepath" "testing" "git.smesh.lol/btcd-lite/internal/chains" "git.smesh.lol/btcd-lite/internal/utxo" "git.smesh.lol/btcd-lite/internal/utxo/store" "git.smesh.lol/btcd-lite/internal/utxobuild" "git.smesh.lol/btcd-lite/internal/wire" ) // TestStreamedWalkMatchesTheReplay: the walk that never holds the set must // produce the same dataset as the walk that does, down to the name each // snapshot gets. Two builds of 301 blocks, one in memory and one epoch at a // time, and every snapshot file and every per-block diff has to be identical. func TestStreamedWalkMatchesTheReplay(t *testing.T) { r := nodeOn(t) r.createWallet(t) r.mine(t, 150) to := r.must(t, "getnewaddress") if _, err := r.cli("sendtoaddress", to, "1"); err != nil { t.Fatalf("sendtoaddress: %v", err) } r.mine(t, 150) tip := r.height(t) src := &mapSource{bodies: rpcBodies(t, r, tip)} interval := chains.Regtest().Exported.Retarget replay := t.TempDir() res, err := utxobuild.Build(utxobuild.Config{ Dir: filepath.Join(replay, "utxo"), DiffDir: filepath.Join(replay, "diff"), Chain: "btc", Interval: interval, From: 0, To: uint32(tip) + 1, Blocks: src, Logf: t.Logf, }) if err != nil { t.Fatalf("replay: %v", err) } streamed := t.TempDir() sres, err := utxobuild.BuildStream(utxobuild.Config{ Dir: filepath.Join(streamed, "utxo"), DiffDir: filepath.Join(streamed, "diff"), Chain: "btc", Interval: interval, From: 0, To: uint32(tip) + 1, Blocks: src, Logf: t.Logf, }) if err != nil { t.Fatalf("streamed: %v", err) } // Diagnose the first snapshot's entries, entry by entry. a144, err := utxostore.LoadSet(filepath.Join(replay, "utxo"), 144) if err != nil { t.Fatalf("replay set 144: %v", err) } b144, err := utxostore.LoadSet(filepath.Join(streamed, "utxo"), 144) if err != nil { t.Fatalf("streamed set 144: %v", err) } if len(a144) != len(b144) { inA := map[wire.OutPoint]bool{} for _, e := range a144 { inA[e.OutPoint] = true } inB := map[wire.OutPoint]bool{} for _, e := range b144 { inB[e.OutPoint] = true } n := 0 for op := range inA { if !inB[op] { t.Logf("only in replay: %s height %d", op, opHeight(a144, op)) if n++; n == 4 { break } } } for op := range inB { if !inA[op] { t.Logf("only in streamed: %s", op) } } t.Fatalf("set 144: replay %d entries, streamed %d", len(a144), len(b144)) } for i := range a144 { if a144[i] != b144[i] { t.Fatalf("set 144 entry %d: replay %+v, streamed %+v", i, a144[i], b144[i]) } } if res.Name != sres.Name || res.Height != sres.Height { t.Fatalf("the walks end differently: %s at %d, %s at %d", res.Name, res.Height, sres.Name, sres.Height) } // Every snapshot file, by height, byte for byte. for h := uint32(0); h <= uint32(tip); h += interval { a := utxostore.SetFile(h) ab, err := os.ReadFile(filepath.Join(replay, "utxo", a)) if err != nil { t.Fatalf("replay snapshot %d: %v", h, err) } bb, err := os.ReadFile(filepath.Join(streamed, "utxo", a)) if err != nil { t.Fatalf("streamed snapshot %d: %v", h, err) } if string(ab) != string(bb) { t.Fatalf("snapshot %d differs between the walks", h) } } // Every per-block diff, by range. diffs, err := filepath.Glob(filepath.Join(replay, "diff", "diff-*.dat")) if err != nil || len(diffs) == 0 { t.Fatalf("no diffs to compare: %v", err) } for _, path := range diffs { name := filepath.Base(path) a, err := os.ReadFile(path) if err != nil { t.Fatal(err) } b, err := os.ReadFile(filepath.Join(streamed, "diff", name)) if err != nil { t.Fatalf("streamed diff %s: %v", name, err) } if string(a) != string(b) { t.Fatalf("diff %s differs between the walks", name) } } t.Logf("%d snapshots and %d diffs identical between the in-memory walk and the streamed one, ending at %s", int(uint32(tip)/interval)+1, len(diffs), res.Name) } // opHeight is the height of the entry for an outpoint, for a diagnostic line. func opHeight(set []utxo.Entry, op wire.OutPoint) uint32 { for _, e := range set { if e.OutPoint == op { return e.Height } } return 0 }