package utxobuild_test import ( "os" "path/filepath" "testing" "git.smesh.lol/nostrino/pkg/crypto/chainhash" "git.smesh.lol/btcd-lite/internal/utxo" utxostore "git.smesh.lol/btcd-lite/internal/utxo/store" "git.smesh.lol/btcd-lite/internal/utxobuild" "git.smesh.lol/btcd-lite/internal/wire" ) // TestRangeTwoTrace builds to checkpoint 10, then merges 10..20 by hand with the // walk in the same process, printing the first differing entry and the key // lists the merge read. func TestRangeTwoTrace(t *testing.T) { raw, spends := chainOfBodies(22) src := bodies{} for h := range raw { src[uint32(h)] = raw[h] } dir := t.TempDir() if _, err := utxobuild.Build(utxobuild.Config{Dir: dir, Interval: 10, From: 0, To: 20, Blocks: src, Logf: t.Logf}); err != nil { t.Fatalf("build: %v", err) } // The walk from the checkpoint's own file. base, err := os.ReadFile(filepath.Join(dir, utxostore.SetFile(10))) if err != nil { t.Fatal(err) } set := make([]utxo.Entry, 0, len(base)/utxostore.EntrySize) buf := make([]byte, utxostore.EntrySize) for i := 0; i+utxostore.EntrySize <= len(base); i += utxostore.EntrySize { copy(buf, base[i:i+utxostore.EntrySize]) set = append(set, utxo.Decode(buf)) } t.Logf("base at 10: %d entries", len(set)) var touches []utxo.Touch // The file at 10 is the set after block 10, so the walk continues at 11. for h := uint32(11); h <= 20; h++ { b, err := wire.ParseBlock(raw[h]) if err != nil { t.Fatal(err) } lookup := utxo.Lookup(func(op wire.OutPoint) (utxo.Entry, bool) { return utxo.Find(set, op) }) ts, err := utxo.BlockTouches(b, h, lookup) if err != nil { t.Fatalf("body %d: %v", h, err) } for _, tc := range ts { if tc.Spent { t.Logf(" body %d spends %s (fixture says %s)", h, tc.OutPoint, spends[h]) } } touches = append(touches, ts...) set, err = utxo.ApplyRange(set, ts) if err != nil { t.Fatalf("body %d apply: %v", h, err) } } t.Logf("walk 10..20: %d entries %s", len(set), utxostore.NameOf(set)) if _, err := utxostore.CutRange(dir, 10, 20, touches); err != nil { t.Fatal(err) } d, err := utxostore.LoadDiff(dir, 10, 20) if err != nil { t.Fatal(err) } got, err := utxostore.MergeRange(dir, 10, 20, d, chainhash.Hash{}) t.Logf("merge 10..20: %s err %v", got, err) if got != utxostore.NameOf(set) { file, err := os.ReadFile(filepath.Join(dir, utxostore.SetFile(20))) if err != nil { t.Fatal(err) } written := make([]utxo.Entry, 0, len(file)/utxostore.EntrySize) for i := 0; i+utxostore.EntrySize <= len(file); i += utxostore.EntrySize { copy(buf, file[i:i+utxostore.EntrySize]) written = append(written, utxo.Decode(buf)) } t.Logf("walk %d entries, merge wrote %d", len(set), len(written)) have := map[wire.OutPoint]bool{} for _, e := range written { have[e.OutPoint] = true } for _, e := range set { if !have[e.OutPoint] { t.Logf(" not written: %s v=%d h=%d", e.OutPoint, e.Value, e.Height) } } for i := 0; i < len(set) && i < len(written); i++ { if set[i] != written[i] { t.Logf(" first order difference at %d: walk %s, merge %s", i, set[i].OutPoint, written[i].OutPoint) break } } } }