1 package utxo_test
2 3 import (
4 "fmt"
5 "os"
6 "path/filepath"
7 "strings"
8 "testing"
9 10 "git.smesh.lol/nostrino/pkg/crypto/chainhash"
11 12 "git.smesh.lol/btcd-lite/internal/utxo"
13 utxostore "git.smesh.lol/btcd-lite/internal/utxo/store"
14 "git.smesh.lol/btcd-lite/internal/wire"
15 )
16 17 // TestCleanRange walks the captured blocks and compares the walk with the merge
18 // in two directories that share nothing but the base.
19 //
20 // The base directory is written once and copied: the walk's directory gets the
21 // walk's replay and nothing else, the merge's directory gets the diff and the
22 // merge's own file. A run that shares a directory with the other's output can
23 // disagree for its own reasons, and one of the earlier attempts did.
24 func TestCleanRange(t *testing.T) {
25 // Height 1 creates two outputs: the coinbase and an OP_RETURN, which a
26 // canonical set does not hold. Height 105 spends the coinbase.
27 funderTx, _ := chainhash.NewHashFromStr("36e95a2200bb470593863bf444790eaa31323f2728a0b9d8688a6294776c6710")
28 base := []utxo.Entry{{OutPoint: wire.OutPoint{Hash: *funderTx, Index: 0}, Value: 50_0000_0000, Height: 1, Coinbase: true}}
29 // The walk starts from the base and applies the captured blocks to it.
30 set := append([]utxo.Entry(nil), base...)
31 var touches []utxo.Touch
32 for h := uint32(80); h <= 105; h++ {
33 raw, err := os.ReadFile(filepath.Join("testdata", fmt.Sprintf("regtest-block-%d.hex", h)))
34 if err != nil {
35 t.Skip("no captured blocks")
36 }
37 b, err := wire.ParseBlock(mustHex(t, strings.TrimSpace(string(raw))))
38 if err != nil {
39 t.Fatalf("block %d: %v", h, err)
40 }
41 lookup := utxo.Lookup(func(op wire.OutPoint) (utxo.Entry, bool) { return utxo.Find(set, op) })
42 ts, err := utxo.BlockTouches(b, h, lookup)
43 if err != nil {
44 t.Fatalf("block %d touches: %v", h, err)
45 }
46 touches = append(touches, ts...)
47 set, err = utxo.ApplyRange(set, ts)
48 if err != nil {
49 t.Fatalf("block %d apply: %v", h, err)
50 }
51 }
52 // The base directory, written once. The walk applied the range to its own
53 // set; the merge applies it to the base, so the base is the walk's set
54 // before the range, not after it.
55 baseDir := t.TempDir()
56 if _, err := utxostore.WriteSet(baseDir, 0, base); err != nil {
57 t.Fatal(err)
58 }
59 if _, err := utxostore.Promote(baseDir, 0); err != nil {
60 t.Fatal(err)
61 }
62 // The walk's own snapshot of its result, for a name to compare against.
63 if _, err := utxostore.WriteSet(baseDir, 106, set); err != nil {
64 t.Fatal(err)
65 }
66 // The merge's directory: the base, and nothing else.
67 mergeDir := t.TempDir()
68 copyFile(t, filepath.Join(baseDir, utxostore.SetFile(0)), filepath.Join(mergeDir, utxostore.SetFile(0)))
69 if _, err := utxostore.CutRange(mergeDir, 0, 106, touches); err != nil {
70 t.Fatal(err)
71 }
72 d, err := utxostore.LoadDiff(mergeDir, 0, 106)
73 if err != nil {
74 t.Fatal(err)
75 }
76 got, err := utxostore.MergeRange(mergeDir, 0, 106, d, chainhash.Hash{})
77 if err != nil {
78 t.Fatalf("merge: %v", err)
79 }
80 want := utxostore.NameOf(set)
81 if got != want {
82 raw, err := os.ReadFile(filepath.Join(mergeDir, utxostore.SetFile(106)))
83 if err != nil {
84 t.Fatal(err)
85 }
86 written := make([]utxo.Entry, 0, len(raw)/utxostore.EntrySize)
87 buf := make([]byte, utxostore.EntrySize)
88 for i := 0; i+utxostore.EntrySize <= len(raw); i += utxostore.EntrySize {
89 copy(buf, raw[i:i+utxostore.EntrySize])
90 written = append(written, utxo.Decode(buf))
91 }
92 // Both lists must be sorted by the package comparison; if one is not,
93 // the fault is an order that was never imposed and not a comparison.
94 for i := 1; i < len(set); i++ {
95 if !utxo.LessEntry(set[i-1], set[i]) {
96 t.Logf("the walk is out of order at %d: %s then %s", i, set[i-1].OutPoint, set[i].OutPoint)
97 }
98 }
99 for i := 1; i < len(written); i++ {
100 if !utxo.LessEntry(written[i-1], written[i]) {
101 t.Logf("the merge is out of order at %d: %s then %s", i, written[i-1].OutPoint, written[i].OutPoint)
102 }
103 }
104 have := map[wire.OutPoint]bool{}
105 for _, e := range written {
106 have[e.OutPoint] = true
107 }
108 held := map[wire.OutPoint]bool{}
109 for _, e := range set {
110 held[e.OutPoint] = true
111 }
112 onlyWalk, onlyMerge := 0, 0
113 for _, e := range set {
114 if !have[e.OutPoint] {
115 onlyWalk++
116 if onlyWalk < 4 {
117 t.Logf(" only the walk: %s v=%d h=%d", e.OutPoint, e.Value, e.Height)
118 }
119 }
120 }
121 for _, e := range written {
122 if !held[e.OutPoint] {
123 onlyMerge++
124 if onlyMerge < 4 {
125 t.Logf(" only the merge: %s v=%d h=%d", e.OutPoint, e.Value, e.Height)
126 }
127 }
128 }
129 for i := 0; i < len(set) && i < len(written); i++ {
130 if set[i] != written[i] {
131 for j := i; j < i+4 && j < len(set) && j < len(written); j++ {
132 t.Logf(" %d walk %s v=%d h=%d cb=%v | merge %s v=%d h=%d cb=%v",
133 j, set[j].OutPoint, set[j].Value, set[j].Height, set[j].Coinbase,
134 written[j].OutPoint, written[j].Value, written[j].Height, written[j].Coinbase)
135 }
136 break
137 }
138 }
139 t.Fatalf("merge %s, walk %s, %d entries each; %d only the walk, %d only the merge",
140 got, want, len(set), onlyWalk, onlyMerge)
141 }
142 t.Logf("26 blocks with a real spend: merge and walk agree on %s over %d entries", got, len(set))
143 }
144 145 func copyFile(t *testing.T, from, to string) {
146 t.Helper()
147 raw, err := os.ReadFile(from)
148 if err != nil {
149 t.Fatal(err)
150 }
151 if err := os.WriteFile(to, raw, 0o644); err != nil {
152 t.Fatal(err)
153 }
154 }
155