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" ) // TestMergeMatchesWalkOnRealBlocks drives the merge over a range of real blocks // and requires the walk to agree with it entry by entry. // // The blocks are the node's own bytes for heights 80..105, of which 105 spends // an output of height 1. The walk and the merge are given the same base, one // that holds that output's entry, so both sides see a range with a spend of a // prior output in it - the case the synthetic diffs never had. func TestMergeMatchesWalkOnRealBlocks(t *testing.T) { dir := t.TempDir() // Height 1's coinbase: what height 105 spends. funderTx, _ := chainhash.NewHashFromStr("36e95a2200bb470593863bf444790eaa31323f2728a0b9d8688a6294776c6710") base := []utxo.Entry{{OutPoint: wire.OutPoint{Hash: *funderTx}, Value: 50_0000_0000, Height: 1, Coinbase: true}} if _, err := utxostore.WriteSet(dir, 0, base); err != nil { t.Fatal(err) } if _, err := utxostore.Promote(dir, 0); err != nil { t.Fatal(err) } 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.Skipf("no captured block %d", h) } 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) } } if len(set) != len(base)+26*2 { t.Logf("walk holds %d entries", len(set)) } if _, err := utxostore.CutRange(dir, 0, 106, touches); err != nil { t.Fatal(err) } loaded, err := utxostore.LoadDiff(dir, 0, 106) if err != nil { t.Fatal(err) } got, err := utxostore.MergeRange(dir, 0, 106, loaded, chainhash.Hash{}) if err != nil { t.Fatalf("merge: %v", err) } want := utxostore.NameOf(set) if got != want { // written is the merge's own file, decoded and re-encoded so the // comparison is of entries and not of bytes. raw, err := os.ReadFile(utxostore.SetFile(106)) if err != nil { raw, err = os.ReadFile(filepath.Join(dir, 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)) } missed, extra := 0, 0 for _, w := range written { if _, ok := utxo.Find(set, w.OutPoint); !ok { extra++ } } for _, e := range set { if _, ok := utxo.Find(written, e.OutPoint); !ok { missed++ } } t.Logf("walk %d entries, written %d; %d entries only the walk has, %d only the merge has", len(set), len(written), missed, extra) for _, e := range set { if _, ok := utxo.Find(written, e.OutPoint); !ok { t.Logf(" only the walk: %s v=%d h=%d", e.OutPoint, e.Value, e.Height) } } for _, w := range written { if _, ok := utxo.Find(set, w.OutPoint); !ok { t.Logf(" only the merge: %s v=%d h=%d", w.OutPoint, w.Value, w.Height) } } for i := 0; i < len(set) && i < len(written); i++ { if set[i] != written[i] { lo, hi := i-2, i+3 if lo < 0 { lo = 0 } for j := lo; j < hi && j < len(set) && j < len(written); j++ { t.Logf(" %d walk %s v=%d h=%d | written %s v=%d h=%d", j, set[j].OutPoint, set[j].Value, set[j].Height, written[j].OutPoint, written[j].Value, written[j].Height) } break } } t.Fatalf("merge %s, walk %s", got, want) } t.Logf("26 real blocks with a spend: walk and merge agree on %d entries", len(set)) } // TestDiffKeysAreTheWalksCreations breaks the disagreement down to the key // lists: which outpoints the diff says were created, which it says were spent, // and which the walk's set holds that neither list names. func TestDiffKeysAreTheWalksCreations(t *testing.T) { dir := t.TempDir() funderTx, _ := chainhash.NewHashFromStr("36e95a2200bb470593863bf444790eaa31323f2728a0b9d8688a6294776c6710") base := []utxo.Entry{{OutPoint: wire.OutPoint{Hash: *funderTx}, Value: 50_0000_0000, Height: 1, Coinbase: true}} if _, err := utxostore.WriteSet(dir, 0, base); err != nil { t.Fatal(err) } if _, err := utxostore.Promote(dir, 0); err != nil { t.Fatal(err) } 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 block") } b, err := wire.ParseBlock(mustHex(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("block %d: %v", h, err) } touches = append(touches, ts...) set, err = utxo.ApplyRange(set, ts) if err != nil { t.Fatalf("block %d apply: %v", h, err) } } // What the diff calls a creation: a touch's outpoint that the walk holds // and that has no spend of it in the range. created := map[wire.OutPoint]bool{} spent := map[wire.OutPoint]bool{} for _, tc := range touches { if tc.Spent { spent[tc.OutPoint] = true } else { created[tc.OutPoint] = true } } inWalk, notFromTouches := 0, 0 for _, e := range set { if _, ok := utxo.Find(base, e.OutPoint); ok { continue } inWalk++ if !created[e.OutPoint] { notFromTouches++ if notFromTouches < 5 { t.Logf("in the walk, not a touch's creation: %s v=%d h=%d", e.OutPoint, e.Value, e.Height) } } } t.Logf("walk holds %d new entries, %d of them not named by a creation touch; diff touches %d, creations %d, spends %d", inWalk, notFromTouches, len(touches), len(created), len(spent)) if notFromTouches != 0 { t.Fatalf("%d entries in the walk have no creation touch", notFromTouches) } }