dbgspend2_test.go raw
1 package utxostore
2
3 import (
4 "encoding/binary"
5 "testing"
6
7 "git.smesh.lol/nostrino/pkg/crypto/chainhash"
8
9 "git.smesh.lol/btcd-lite/internal/utxo"
10 "git.smesh.lol/btcd-lite/internal/wire"
11 )
12
13 func qop2(i uint32) wire.OutPoint {
14 var h [32]byte
15 binary.LittleEndian.PutUint32(h[:4], i)
16 binary.BigEndian.PutUint32(h[28:], i)
17 return wire.OutPoint{Hash: h}
18 }
19
20 // TestDbgSpendMiddle2 varies one thing at a time from a passing case to the
21 // failing one, so the trigger is named.
22 func TestDbgSpendMiddle2(t *testing.T) {
23 cases := []struct {
24 name string
25 base []uint32
26 touch []utxo.Touch
27 }{
28 {"create-then-spend-one-base",
29 []uint32{0, 100, 200, 300, 400},
30 []utxo.Touch{
31 {OutPoint: qop2(50), Height: 1, New: utxo.Entry{OutPoint: qop2(50), Value: 50, Height: 1}},
32 {OutPoint: qop2(200), Height: 1, Spent: true},
33 }},
34 {"spend-then-create",
35 []uint32{0, 100, 200, 300, 400},
36 []utxo.Touch{
37 {OutPoint: qop2(200), Height: 1, Spent: true},
38 {OutPoint: qop2(50), Height: 1, New: utxo.Entry{OutPoint: qop2(50), Value: 50, Height: 1}},
39 }},
40 {"create-between-spend",
41 []uint32{0, 100, 200, 300, 400},
42 []utxo.Touch{
43 {OutPoint: qop2(150), Height: 1, New: utxo.Entry{OutPoint: qop2(150), Value: 150, Height: 1}},
44 {OutPoint: qop2(200), Height: 1, Spent: true},
45 {OutPoint: qop2(250), Height: 1, New: utxo.Entry{OutPoint: qop2(250), Value: 250, Height: 1}},
46 }},
47 }
48 for _, c := range cases {
49 t.Run(c.name, func(t *testing.T) {
50 dir := t.TempDir()
51 var base []utxo.Entry
52 for _, id := range c.base {
53 base = append(base, utxo.Entry{OutPoint: qop2(id), Value: int64(id), Height: 0})
54 }
55 if _, err := WriteSet(dir, 0, base); err != nil {
56 t.Fatal(err)
57 }
58 if _, err := Promote(dir, 0); err != nil {
59 t.Fatal(err)
60 }
61 want, err := utxo.ApplyRange(base, c.touch)
62 if err != nil {
63 t.Fatalf("walk: %v", err)
64 }
65 if _, err := CutRange(dir, 0, 1, c.touch); err != nil {
66 t.Fatal(err)
67 }
68 d, _ := LoadDiff(dir, 0, 1)
69 got, err := MergeRange(dir, 0, 1, d, chainhash.Hash{})
70 if err != nil {
71 t.Fatalf("merge: %v", err)
72 }
73 if got != NameOf(want) {
74 t.Fatalf("merge %s walk %s (%d entries)", got, NameOf(want), len(want))
75 }
76 })
77 }
78 }
79