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" ) // TestCheckpointFileIsTheWalk: after the build's first checkpoint, the file it // wrote holds exactly the set the walk held at that height, entry for entry and // in the same order. func TestCheckpointFileIsTheWalk(t *testing.T) { raw, _ := chainOfBodies(22) src := bodies{} for h := range raw { src[uint32(h)] = raw[h] } dir := t.TempDir() var walkAt10 []utxo.Entry utxobuild.Debug(func(height uint32, entries int, name chainhash.Hash) { if height == 10 { t.Logf("walk at 10: %d entries %s", entries, name) } }, nil) _, err := utxobuild.Build(utxobuild.Config{Dir: dir, Interval: 10, From: 0, To: 21, Blocks: src, Logf: t.Logf}) utxobuild.Debug(nil, nil) if err != nil { t.Fatalf("build: %v", err) } // The walk over the same 0..10, in this process. var set []utxo.Entry // The file at 10 is the set after block 10, so the walk covers 0..10. for h := uint32(0); h <= 10; 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.Fatal(err) } set, err = utxo.ApplyRange(set, ts) if err != nil { t.Fatal(err) } } walkAt10 = set file, err := os.ReadFile(filepath.Join(dir, utxostore.SetFile(10))) if err != nil { t.Fatal(err) } written := make([]utxo.Entry, 0, len(file)/utxostore.EntrySize) buf := make([]byte, 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, file at 10 holds %d", len(walkAt10), len(written)) if len(walkAt10) != len(written) { t.Fatalf("%d vs %d", len(walkAt10), len(written)) } for i := range walkAt10 { if walkAt10[i] != written[i] { t.Fatalf("entry %d: walk %s v=%d h=%d, file %s v=%d h=%d", i, walkAt10[i].OutPoint, walkAt10[i].Value, walkAt10[i].Height, written[i].OutPoint, written[i].Value, written[i].Height) } } // And the base a later merge reads must be exactly this file, so the set // of 10..20 is merged against the walk's own set at 10. if n, err := utxostore.SetName(dir, 10); err != nil || n != utxostore.NameOf(walkAt10) { t.Fatalf("the base the next merge reads names %s, the walk at 10 is %s", n, utxostore.NameOf(walkAt10)) } t.Logf("the file at 10 is the walk's set at 10: %s", utxostore.NameOf(walkAt10)) }