package utxostore import ( "fmt" "os" "path/filepath" "strings" "testing" "git.smesh.lol/nostrino/pkg/crypto/chainhash" "git.smesh.lol/btcd-lite/internal/utxo" "git.smesh.lol/btcd-lite/internal/wire" ) // TestRealBlocksTrace drives the merge over the captured blocks with the step // trace on, so the cursor action that loses an entry is visible rather than // inferred from the output. func TestRealBlocksTrace(t *testing.T) { dir := t.TempDir() funder, _ := chainhash.NewHashFromStr("36e95a2200bb470593863bf444790eaa31323f2728a0b9d8688a6294776c6710") base := []utxo.Entry{{OutPoint: wire.OutPoint{Hash: *funder}, Value: 50_0000_0000, Height: 1, Coinbase: true}} if _, err := WriteSet(dir, 0, base); err != nil { t.Fatal(err) } if _, err := Promote(dir, 0); err != nil { t.Fatal(err) } var touches []utxo.Touch set := append([]utxo.Entry(nil), base...) 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 blocks") } b, err := wire.ParseBlock([]byte(mustDecode(t, strings.TrimSpace(string(raw))))) 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("%d: %v", h, err) } touches = append(touches, ts...) set, err = utxo.ApplyRange(set, ts) if err != nil { t.Fatalf("%d: %v", h, err) } } if _, err := WriteSet(dir, 106, set); err != nil { t.Fatal(err) } if _, err := CutRange(dir, 0, 106, touches); err != nil { t.Fatal(err) } d, err := LoadDiff(dir, 0, 106) if err != nil { t.Fatal(err) } steps := 0 debugStep = func(b wire.OutPoint, ci int, c wire.OutPoint, ch bool, s wire.OutPoint, sh bool) { if steps < 6 { t.Logf("base %s | created[%d] %s have=%v | spent %s have=%v", short(b), ci, short(c), ch, short(s), sh) } steps++ } defer func() { debugStep = func(wire.OutPoint, int, wire.OutPoint, bool, wire.OutPoint, bool) {} }() got, err := MergeRange(dir, 0, 106, d, chainhash.Hash{}) raw, _ := os.ReadFile(filepath.Join(dir, SetFile(106))) written := make([]utxo.Entry, 0, len(raw)/EntrySize) buf := make([]byte, EntrySize) for i := 0; i+EntrySize <= len(raw); i += EntrySize { copy(buf, raw[i:i+EntrySize]) written = append(written, utxo.Decode(buf)) } t.Logf("merge %s err %v walk %s, %d entries written %d", got, err, NameOf(set), len(set), len(written)) for i := 0; i < len(set) && i < len(written); i++ { if set[i] != written[i] { t.Logf("first difference at %d: walk %s v=%d h=%d, written %s v=%d h=%d", i, short(set[i].OutPoint), set[i].Value, set[i].Height, short(written[i].OutPoint), written[i].Value, written[i].Height) break } } }