pairing_test.go raw
1 package utxostore
2
3 import (
4 "fmt"
5 "os"
6 "path/filepath"
7 "sort"
8 "strings"
9 "testing"
10
11 "git.smesh.lol/nostrino/pkg/crypto/chainhash"
12
13 "git.smesh.lol/btcd-lite/internal/utxo"
14 "git.smesh.lol/btcd-lite/internal/wire"
15 )
16
17 // TestKeyPairsValue: for every creation the merge writes, the bytes it writes
18 // are the entries the diff recorded for that outpoint - value, script hash and
19 // height included. The set is named by its bytes, so a pair that is right in
20 // order and wrong in content shows up as a different name for the same entry
21 // count, which is what the real-block comparison reported.
22 func TestKeyPairsValue(t *testing.T) {
23 dir := t.TempDir()
24 funder, _ := chainhash.NewHashFromStr("36e95a2200bb470593863bf444790eaa31323f2728a0b9d8688a6294776c6710")
25 base := []utxo.Entry{{OutPoint: wire.OutPoint{Hash: *funder}, Value: 50_0000_0000, Height: 1, Coinbase: true}}
26 if _, err := WriteSet(dir, 0, base); err != nil {
27 t.Fatal(err)
28 }
29 if _, err := Promote(dir, 0); err != nil {
30 t.Fatal(err)
31 }
32 var touches []utxo.Touch
33 set := append([]utxo.Entry(nil), base...)
34 for h := uint32(80); h <= 105; h++ {
35 raw, err := os.ReadFile(filepath.Join("..", "testdata", fmt.Sprintf("regtest-block-%d.hex", h)))
36 if err != nil {
37 t.Skip("no blocks")
38 }
39 b, err := wire.ParseBlock(mustDecode(t, strings.TrimSpace(string(raw))))
40 if err != nil {
41 t.Fatal(err)
42 }
43 lookup := utxo.Lookup(func(op wire.OutPoint) (utxo.Entry, bool) { return utxo.Find(set, op) })
44 ts, err := utxo.BlockTouches(b, h, lookup)
45 if err != nil {
46 t.Fatal(err)
47 }
48 touches = append(touches, ts...)
49 set, err = utxo.ApplyRange(set, ts)
50 if err != nil {
51 t.Fatal(err)
52 }
53 }
54 // What the diff says each creation is.
55 want := make(map[wire.OutPoint]utxo.Entry, len(touches))
56 for _, tc := range touches {
57 if !tc.Spent {
58 want[tc.OutPoint] = tc.New
59 }
60 }
61 // What the merge writes.
62 if _, err := WriteSet(dir, 106, set); err != nil {
63 t.Fatal(err)
64 }
65 keepScratch = true
66 d := utxo.Diff{From: 0, To: 106, Touches: touches}
67 if _, err := MergeRange(dir, 0, 106, d, chainhash.Hash{}); err != nil {
68 t.Fatalf("merge: %v", err)
69 }
70 keepScratch = false
71 createdPath := filepath.Join(dir, "keys-000000000-000000106.created")
72 keys, _ := os.ReadFile(createdPath)
73 vals, _ := os.ReadFile(createdPath + "v")
74 t.Logf("%d keys, %d value records", len(keys)/36, len(vals)/EntrySize)
75 bad := 0
76 for i := 0; i < len(keys)/36 && i < len(vals)/EntrySize; i++ {
77 var op wire.OutPoint
78 copy(op.Hash[:], keys[i*36:i*36+32])
79 op.Index = uint32(keys[i*36+32]) | uint32(keys[i*36+33])<<8 | uint32(keys[i*36+34])<<16 | uint32(keys[i*36+35])<<24
80 e := utxo.Decode(vals[i*EntrySize : (i+1)*EntrySize])
81 w, ok := want[op]
82 if !ok {
83 bad++
84 if bad < 4 {
85 t.Logf(" key %d %s has no creation in the diff", i, short(op))
86 }
87 continue
88 }
89 if e != w {
90 bad++
91 if bad < 6 {
92 t.Logf(" key %d %s: written v=%d h=%d, diff says v=%d h=%d", i, short(op), e.Value, e.Height, w.Value, w.Height)
93 }
94 }
95 }
96 if bad != 0 {
97 t.Fatalf("%d of %d pairs disagree with the diff", bad, len(keys)/36)
98 }
99 t.Logf("every key is paired with the diff's own entry")
100 }
101
102 // TestKeyFileSorted: the key file the merge reads is in the package's order.
103 func TestKeyFileSorted(t *testing.T) {
104 src := "keys-000000000-000000106.created"
105 _ = src
106 _ = sort.Slice
107 }
108