package utxo_test import ( "fmt" "os" "path/filepath" "strings" "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/wire" ) // TestCleanRange walks the captured blocks and compares the walk with the merge // in two directories that share nothing but the base. // // The base directory is written once and copied: the walk's directory gets the // walk's replay and nothing else, the merge's directory gets the diff and the // merge's own file. A run that shares a directory with the other's output can // disagree for its own reasons, and one of the earlier attempts did. func TestCleanRange(t *testing.T) { // Height 1 creates two outputs: the coinbase and an OP_RETURN, which a // canonical set does not hold. Height 105 spends the coinbase. funderTx, _ := chainhash.NewHashFromStr("36e95a2200bb470593863bf444790eaa31323f2728a0b9d8688a6294776c6710") base := []utxo.Entry{{OutPoint: wire.OutPoint{Hash: *funderTx, Index: 0}, Value: 50_0000_0000, Height: 1, Coinbase: true}} // The walk starts from the base and applies the captured blocks to it. set := append([]utxo.Entry(nil), base...) var touches []utxo.Touch for h := uint32(80); h <= 105; h++ { raw, err := os.ReadFile(filepath.Join("testdata", fmt.Sprintf("regtest-block-%d.hex", h))) if err != nil { t.Skip("no captured blocks") } b, err := wire.ParseBlock(mustHex(t, strings.TrimSpace(string(raw)))) if err != nil { t.Fatalf("block %d: %v", h, 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("block %d touches: %v", h, err) } touches = append(touches, ts...) set, err = utxo.ApplyRange(set, ts) if err != nil { t.Fatalf("block %d apply: %v", h, err) } } // The base directory, written once. The walk applied the range to its own // set; the merge applies it to the base, so the base is the walk's set // before the range, not after it. baseDir := t.TempDir() if _, err := utxostore.WriteSet(baseDir, 0, base); err != nil { t.Fatal(err) } if _, err := utxostore.Promote(baseDir, 0); err != nil { t.Fatal(err) } // The walk's own snapshot of its result, for a name to compare against. if _, err := utxostore.WriteSet(baseDir, 106, set); err != nil { t.Fatal(err) } // The merge's directory: the base, and nothing else. mergeDir := t.TempDir() copyFile(t, filepath.Join(baseDir, utxostore.SetFile(0)), filepath.Join(mergeDir, utxostore.SetFile(0))) if _, err := utxostore.CutRange(mergeDir, 0, 106, touches); err != nil { t.Fatal(err) } d, err := utxostore.LoadDiff(mergeDir, 0, 106) if err != nil { t.Fatal(err) } got, err := utxostore.MergeRange(mergeDir, 0, 106, d, chainhash.Hash{}) if err != nil { t.Fatalf("merge: %v", err) } want := utxostore.NameOf(set) if got != want { raw, err := os.ReadFile(filepath.Join(mergeDir, utxostore.SetFile(106))) if err != nil { t.Fatal(err) } written := make([]utxo.Entry, 0, len(raw)/utxostore.EntrySize) buf := make([]byte, utxostore.EntrySize) for i := 0; i+utxostore.EntrySize <= len(raw); i += utxostore.EntrySize { copy(buf, raw[i:i+utxostore.EntrySize]) written = append(written, utxo.Decode(buf)) } // Both lists must be sorted by the package comparison; if one is not, // the fault is an order that was never imposed and not a comparison. for i := 1; i < len(set); i++ { if !utxo.LessEntry(set[i-1], set[i]) { t.Logf("the walk is out of order at %d: %s then %s", i, set[i-1].OutPoint, set[i].OutPoint) } } for i := 1; i < len(written); i++ { if !utxo.LessEntry(written[i-1], written[i]) { t.Logf("the merge is out of order at %d: %s then %s", i, written[i-1].OutPoint, written[i].OutPoint) } } have := map[wire.OutPoint]bool{} for _, e := range written { have[e.OutPoint] = true } held := map[wire.OutPoint]bool{} for _, e := range set { held[e.OutPoint] = true } onlyWalk, onlyMerge := 0, 0 for _, e := range set { if !have[e.OutPoint] { onlyWalk++ if onlyWalk < 4 { t.Logf(" only the walk: %s v=%d h=%d", e.OutPoint, e.Value, e.Height) } } } for _, e := range written { if !held[e.OutPoint] { onlyMerge++ if onlyMerge < 4 { t.Logf(" only the merge: %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] { for j := i; j < i+4 && j < len(set) && j < len(written); j++ { t.Logf(" %d walk %s v=%d h=%d cb=%v | merge %s v=%d h=%d cb=%v", j, set[j].OutPoint, set[j].Value, set[j].Height, set[j].Coinbase, written[j].OutPoint, written[j].Value, written[j].Height, written[j].Coinbase) } break } } t.Fatalf("merge %s, walk %s, %d entries each; %d only the walk, %d only the merge", got, want, len(set), onlyWalk, onlyMerge) } t.Logf("26 blocks with a real spend: merge and walk agree on %s over %d entries", got, len(set)) } func copyFile(t *testing.T, from, to string) { t.Helper() raw, err := os.ReadFile(from) if err != nil { t.Fatal(err) } if err := os.WriteFile(to, raw, 0o644); err != nil { t.Fatal(err) } }