small_test.go raw
1 package utxostore
2
3 import (
4 "encoding/binary"
5 "os"
6 "path/filepath"
7 "testing"
8
9 "git.smesh.lol/nostrino/pkg/crypto/chainhash"
10
11 "git.smesh.lol/btcd-lite/internal/utxo"
12 "git.smesh.lol/btcd-lite/internal/wire"
13 )
14
15 // TestCreationBelowASpentBaseEntry is the whole of it: one base entry, spent, and
16 // one creation that sorts before it. The merge must write the creation and drop
17 // the base entry; the walk does the same.
18 func TestCreationBelowASpentBaseEntry(t *testing.T) {
19 base := []utxo.Entry{
20 {OutPoint: sop(0), Value: 0},
21 {OutPoint: sop(200), Value: 200},
22 }
23 ts := []utxo.Touch{
24 {OutPoint: sop(50), Height: 1, New: utxo.Entry{OutPoint: sop(50), Value: 50, Height: 1}},
25 {OutPoint: sop(200), Height: 1, Spent: true},
26 }
27 want, err := utxo.ApplyRange(base, ts)
28 if err != nil {
29 t.Fatal(err)
30 }
31 dir := t.TempDir()
32 if _, err := WriteSet(dir, 0, base); err != nil {
33 t.Fatal(err)
34 }
35 if _, err := Promote(dir, 0); err != nil {
36 t.Fatal(err)
37 }
38 if _, err := CutRange(dir, 0, 1, ts); err != nil {
39 t.Fatal(err)
40 }
41 d, err := LoadDiff(dir, 0, 1)
42 if err != nil {
43 t.Fatal(err)
44 }
45 got, err := MergeRange(dir, 0, 1, d, NameOf(want))
46 if err != nil {
47 t.Fatalf("merge: %v", err)
48 }
49 if got != NameOf(want) {
50 raw, _ := os.ReadFile(filepath.Join(dir, SetFile(1)))
51 buf := make([]byte, EntrySize)
52 var vals []int64
53 for i := 0; i+EntrySize <= len(raw); i += EntrySize {
54 copy(buf, raw[i:i+EntrySize])
55 vals = append(vals, utxo.Decode(buf).Value)
56 }
57 var wantVals []int64
58 for _, e := range want {
59 wantVals = append(wantVals, e.Value)
60 }
61 t.Fatalf("merge %s wrote %v, walk %s is %v", got, vals, NameOf(want), wantVals)
62 }
63 _ = chainhash.Hash{}
64 }
65
66 func sop(i uint32) wire.OutPoint {
67 var h [32]byte
68 binary.LittleEndian.PutUint32(h[:4], i)
69 binary.BigEndian.PutUint32(h[28:], i)
70 return wire.OutPoint{Hash: h}
71 }
72
73 // TestCreationBetweenBaseEntries: a creation that sorts between two base
74 // entries, one of which the range spends. The entry the writer must not lose is
75 // the base entry that survives, and the creation must appear once.
76 //
77 // The merge passed one buffer to the created path and then wrote the base entry
78 // from it, so the creation came out twice and the surviving base entry came out
79 // never. The values made it look like a duplicate; the set was wrong.
80 func TestCreationBetweenBaseEntries(t *testing.T) {
81 base := []utxo.Entry{
82 {OutPoint: sop(0), Value: 0}, {OutPoint: sop(100), Value: 100}, {OutPoint: sop(200), Value: 200},
83 {OutPoint: sop(300), Value: 300}, {OutPoint: sop(400), Value: 400},
84 }
85 ts := []utxo.Touch{
86 {OutPoint: sop(50), Height: 1, New: utxo.Entry{OutPoint: sop(50), Value: 50, Height: 1}},
87 {OutPoint: sop(200), Height: 1, Spent: true},
88 }
89 want, err := utxo.ApplyRange(base, ts)
90 if err != nil {
91 t.Fatal(err)
92 }
93 dir := t.TempDir()
94 if _, err := WriteSet(dir, 0, base); err != nil {
95 t.Fatal(err)
96 }
97 if _, err := Promote(dir, 0); err != nil {
98 t.Fatal(err)
99 }
100 if _, err := CutRange(dir, 0, 1, ts); err != nil {
101 t.Fatal(err)
102 }
103 d, err := LoadDiff(dir, 0, 1)
104 if err != nil {
105 t.Fatal(err)
106 }
107 got, err := MergeRange(dir, 0, 1, d, NameOf(want))
108 if err != nil {
109 t.Fatalf("merge: %v", err)
110 }
111 if got != NameOf(want) {
112 raw, _ := os.ReadFile(filepath.Join(dir, SetFile(1)))
113 buf := make([]byte, EntrySize)
114 var vals []int64
115 for i := 0; i+EntrySize <= len(raw); i += EntrySize {
116 copy(buf, raw[i:i+EntrySize])
117 vals = append(vals, utxo.Decode(buf).Value)
118 }
119 var wantVals []int64
120 for _, e := range want {
121 wantVals = append(wantVals, e.Value)
122 }
123 t.Fatalf("merge wrote %v, the walk is %v", vals, wantVals)
124 }
125 }
126