store_test.go raw
1 package utxostore_test
2
3 import (
4 "encoding/binary"
5 "io"
6 "os"
7 "path/filepath"
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 func op(i uint32) wire.OutPoint {
18 var h [32]byte
19 binary.LittleEndian.PutUint32(h[:4], i)
20 binary.BigEndian.PutUint32(h[28:], i)
21 return wire.OutPoint{Hash: h, Index: 0}
22 }
23
24 // chain simulates heights: d creations per height, then spends of the oldest
25 // quarter of what the earlier heights left, so every block walks inside what
26 // earlier ones built.
27 func chain(n, d int) [][]utxo.Touch {
28 out := make([][]utxo.Touch, n)
29 var held []uint32
30 next := uint32(0)
31 for h := 0; h < n; h++ {
32 var ts []utxo.Touch
33 for k := 0; k < d; k++ {
34 id := next
35 next++
36 ts = append(ts, utxo.Touch{OutPoint: op(id), Height: uint32(h),
37 New: utxo.Entry{OutPoint: op(id), Value: int64(id), Height: uint32(h)}})
38 held = append(held, id)
39 }
40 cut := len(held) / 4
41 for k := 0; k < cut; k++ {
42 ts = append(ts, utxo.Touch{OutPoint: op(held[k]), Height: uint32(h), Spent: true})
43 }
44 held = held[cut:]
45 out[h] = ts
46 }
47 return out
48 }
49
50 // TestCheckpointChain: build sets and diffs on disk from a replay, then
51 // rebuild the tip from the base and the diffs and require the same name; then
52 // change one byte in a diff and require a different name.
53 func TestCheckpointChain(t *testing.T) {
54 dir := t.TempDir()
55 heights := chain(60, 8)
56 var set []utxo.Entry
57 // The base at height 0.
58 if _, err := utxostore.WriteSet(dir, 0, set); err != nil {
59 t.Fatal(err)
60 }
61 name0, err := utxostore.Promote(dir, 0)
62 if err != nil {
63 t.Fatal(err)
64 }
65 boundaries := []uint32{0, 10, 25, 40, 60}
66 prev := uint32(0)
67 var touches []utxo.Touch
68 names := map[uint32]chainhash.Hash{0: name0}
69 for h := 0; h < len(heights); h++ {
70 var err error
71 set, err = utxo.ApplyRange(set, heights[h])
72 if err != nil {
73 t.Fatalf("height %d: %v", h, err)
74 }
75 touches = append(touches, heights[h]...)
76 if next := uint32(h + 1); next == nextBoundary(boundaries, prev) {
77 if _, err := utxostore.CutRange(dir, prev, next, touches); err != nil {
78 t.Fatal(err)
79 }
80 name, err := utxostore.WriteSet(dir, next, set)
81 if err != nil {
82 t.Fatal(err)
83 }
84 names[next] = name
85 prev = next
86 touches = nil
87 }
88 }
89 // The base and diffs rebuild every checkpoint.
90 for i := 0; i+1 < len(boundaries); i++ {
91 from, to := boundaries[i], boundaries[i+1]
92 d, err := utxostore.LoadDiff(dir, from, to)
93 if err != nil {
94 t.Fatal(err)
95 }
96 got, err := utxostore.MergeRange(dir, from, to, d, names[to])
97 if err != nil {
98 t.Fatalf("merge %d..%d: %v", from, to, err)
99 }
100 if got != names[to] {
101 t.Fatalf("apply %d..%d gives %s, the replay wrote %s", from, to, got, names[to])
102 }
103 }
104 // A changed diff gives a different name.
105 d, err := utxostore.LoadDiff(dir, 0, 10)
106 if err != nil {
107 t.Fatal(err)
108 }
109 d.Touches[0].Height++
110 raw := d.Encode()
111 if chainhash.DoubleHashH(raw) == names[10] {
112 t.Fatal("a changed diff kept the set's name")
113 }
114 }
115
116 func nextBoundary(b []uint32, cur uint32) uint32 {
117 for _, x := range b {
118 if x > cur {
119 return x
120 }
121 }
122 return ^uint32(0)
123 }
124
125 // TestSetRoundTrip: a set written and read back is the same set under the same
126 // name.
127 func TestSetRoundTrip(t *testing.T) {
128 dir := t.TempDir()
129 set := []utxo.Entry{
130 {OutPoint: op(1), Value: 5, Height: 3, ScriptHash: [8]byte{1}},
131 {OutPoint: op(2), Value: 9, Height: 4, Coinbase: true},
132 }
133 name, err := utxostore.WriteSet(dir, 7, set)
134 if err != nil {
135 t.Fatal(err)
136 }
137 back, err := utxostore.LoadReplay(dir, 7)
138 if err != nil {
139 t.Fatal(err)
140 }
141 if len(back) != 2 || back[0] != set[0] || back[1] != set[1] {
142 t.Fatalf("set round trip differs: %+v", back)
143 }
144 onDisk, err := utxostore.NameReplay(dir, 7)
145 if err != nil || onDisk != name {
146 t.Fatalf("file name %s, written name %s", onDisk, name)
147 }
148 }
149
150 // TestStreamMerge: the streaming merge from the base and a range's diff
151 // reproduces the set the replay wrote, entry for entry, at every boundary.
152 func TestStreamMerge(t *testing.T) {
153 dir := t.TempDir()
154 heights := chain(60, 8)
155 var set []utxo.Entry
156 if _, err := utxostore.WriteSet(dir, 0, set); err != nil {
157 t.Fatal(err)
158 }
159 if _, err := utxostore.Promote(dir, 0); err != nil {
160 t.Fatal(err)
161 }
162 boundaries := []uint32{0, 10, 25, 40, 60}
163 prev := uint32(0)
164 var touches []utxo.Touch
165 for h := 0; h < len(heights); h++ {
166 var err error
167 set, err = utxo.ApplyRange(set, heights[h])
168 if err != nil {
169 t.Fatalf("height %d: %v", h, err)
170 }
171 touches = append(touches, heights[h]...)
172 next := uint32(h + 1)
173 if next != nextBoundary(boundaries, prev) {
174 continue
175 }
176 if _, err := utxostore.CutRange(dir, prev, next, touches); err != nil {
177 t.Fatal(err)
178 }
179 if _, err := utxostore.WriteSet(dir, next, set); err != nil {
180 t.Fatal(err)
181 }
182 replayName := utxostore.NameOf(set)
183 replay, err := utxostore.LoadReplay(dir, next)
184 if err != nil {
185 t.Fatal(err)
186 }
187 d, err := utxostore.LoadDiff(dir, prev, next)
188 if err != nil {
189 t.Fatal(err)
190 }
191 // The merge writes its result over the replay's file, so the replay is
192 // put back after the merge to be compared with.
193 mergedName, err := utxostore.MergeRange(dir, prev, next, d, replayName)
194 if err != nil {
195 t.Fatalf("merge %d..%d: %v", prev, next, err)
196 }
197 if mergedName != replayName {
198 t.Fatalf("merge gives %s, replay gives %s", mergedName, replayName)
199 }
200 merged, err := utxostore.LoadReplay(dir, next)
201 if err != nil {
202 t.Fatal(err)
203 }
204 if len(merged) != len(replay) {
205 t.Fatalf("%d..%d: merge holds %d, replay holds %d", prev, next, len(merged), len(replay))
206 }
207 prev = next
208 touches = nil
209 }
210 }
211
212 // LoadSetFile reads a checkpoint's merged set file, which is what a client
213 // that applied the diffs holds.
214 func LoadSetFile(dir string, height uint32) ([]utxo.Entry, error) {
215 f, err := os.Open(filepath.Join(dir, utxostore.SetFile(height)))
216 if err != nil {
217 return nil, err
218 }
219 defer f.Close()
220 var out []utxo.Entry
221 buf := make([]byte, utxostore.EntrySize)
222 for {
223 if _, err := io.ReadFull(f, buf); err != nil {
224 if err == io.EOF {
225 return out, nil
226 }
227 return nil, err
228 }
229 out = append(out, utxo.Decode(buf))
230 }
231 }
232
233 // TestCheckpointTilesTheChain: the merge writes every boundary's file, so a
234 // checkpoint's set is a file the next merge reads. This is the property the
235 // chain of diffs rests on: a client applies base and diffs, and an instance
236 // applying them one at a time walks the same files.
237 func TestCheckpointTilesTheChain(t *testing.T) {
238 dir := t.TempDir()
239 heights := chain(30, 5)
240 var set []utxo.Entry
241 if _, err := utxostore.WriteSet(dir, 0, set); err != nil {
242 t.Fatal(err)
243 }
244 name, err := utxostore.Promote(dir, 0)
245 if err != nil {
246 t.Fatal(err)
247 }
248 names := map[uint32]chainhash.Hash{0: name}
249 var touches []utxo.Touch
250 prev := uint32(0)
251 for h := 0; h < len(heights); h++ {
252 var err error
253 set, err = utxo.ApplyRange(set, heights[h])
254 if err != nil {
255 t.Fatal(err)
256 }
257 touches = append(touches, heights[h]...)
258 if (h+1)%10 != 0 {
259 continue
260 }
261 next := uint32(h + 1)
262 if _, err := utxostore.CutRange(dir, prev, next, touches); err != nil {
263 t.Fatal(err)
264 }
265 d, err := utxostore.LoadDiff(dir, prev, next)
266 if err != nil {
267 t.Fatal(err)
268 }
269 n, err := utxostore.MergeRange(dir, prev, next, d, utxostore.NameOf(set))
270 if err != nil {
271 t.Fatalf("merge %d..%d: %v", prev, next, err)
272 }
273 names[next] = n
274 prev = next
275 touches = nil
276 }
277 // A client with only the base and the diffs reaches each boundary, and
278 // each name it computes is the name the chain recorded.
279 for _, to := range []uint32{10, 20, 30} {
280 scratch := t.TempDir()
281 raw, err := os.ReadFile(filepath.Join(dir, utxostore.SetFile(0)))
282 if err != nil {
283 t.Fatal(err)
284 }
285 if err := os.WriteFile(filepath.Join(scratch, utxostore.SetFile(0)), raw, 0o644); err != nil {
286 t.Fatal(err)
287 }
288 from := uint32(0)
289 for _, next := range []uint32{10, 20, 30} {
290 d, err := utxostore.LoadDiff(dir, from, next)
291 if err != nil {
292 t.Fatal(err)
293 }
294 got, err := utxostore.MergeRange(scratch, from, next, d, chainhash.Hash{})
295 if err != nil {
296 t.Fatalf("client merge %d..%d: %v", from, next, err)
297 }
298 if got != names[next] {
299 t.Fatalf("client at %d has %s, the chain recorded %s", next, got, names[next])
300 }
301 from = next
302 if next == to {
303 break
304 }
305 }
306 }
307 }
308