-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathwatch_model_test.go
More file actions
163 lines (137 loc) · 4.26 KB
/
Copy pathwatch_model_test.go
File metadata and controls
163 lines (137 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package fraud
import (
"testing"
"github.com/btcsuite/btcd/chainhash/v2"
"github.com/btcsuite/btcd/wire/v2"
"github.com/lightninglabs/wavelength/lib/tree"
"github.com/lightninglabs/wavelength/vtxo"
"github.com/stretchr/testify/require"
)
// TestBuildWatchPlanIncludesTreeInputsAndLeafSource verifies passive watches
// cover both tree materialization and checkpoint spend of the source VTXO.
func TestBuildWatchPlanIncludesTreeInputsAndLeafSource(t *testing.T) {
treePath, source := testBranchTree(t, 5)
target := testInput(9)
plan, err := BuildWatchPlan(testDescriptor(target, treePath))
require.NoError(t, err)
require.Equal(t, target, plan.TargetOutpoint)
watched := make(map[wire.OutPoint]struct{}, len(plan.Watches))
for _, watch := range plan.Watches {
watched[watch.Outpoint] = struct{}{}
}
require.Contains(t, watched, treePath.Root.Input)
require.Contains(t, watched, treePath.Root.Children[0].Input)
require.Contains(t, watched, source)
}
// TestBuildWatchPlanIncludesEveryAncestry verifies multi-input OOR targets arm
// passive watches for every ancestry fragment.
func TestBuildWatchPlanIncludesEveryAncestry(t *testing.T) {
treeOne, sourceOne := testLeafTree(t, 10)
treeTwo, sourceTwo := testLeafTree(t, 20)
target := testInput(30)
plan, err := BuildWatchPlan(testDescriptor(target, treeOne, treeTwo))
require.NoError(t, err)
require.Equal(t, target, plan.TargetOutpoint)
watched := make(map[wire.OutPoint]struct{}, len(plan.Watches))
for _, watch := range plan.Watches {
watched[watch.Outpoint] = struct{}{}
}
require.Contains(t, watched, treeOne.Root.Input)
require.Contains(t, watched, sourceOne)
require.Contains(t, watched, treeTwo.Root.Input)
require.Contains(t, watched, sourceTwo)
}
// TestBuildWatchPlanRejectsMalformedAncestry verifies invalid persisted
// ancestry is surfaced instead of arming an incomplete passive watch set.
func TestBuildWatchPlanRejectsMalformedAncestry(t *testing.T) {
treePath, _ := testLeafTree(t, 40)
desc := testDescriptor(testInput(41), treePath)
desc.Ancestry[0].TreePath.Root = nil
_, err := BuildWatchPlan(desc)
require.ErrorIs(t, err, ErrWatchUnavailable)
}
// testLeafTree creates a one-node tree and returns its non-anchor output.
func testLeafTree(t *testing.T, seed byte) (*tree.Tree, wire.OutPoint) {
t.Helper()
root := &tree.Node{
Input: testInput(seed),
Outputs: []*wire.TxOut{
testOut(seed, 1),
},
Children: make(map[uint32]*tree.Node),
}
source, err := root.GetNonAnchorOutpoint()
require.NoError(t, err)
return &tree.Tree{
Root: root,
BatchOutput: testOut(seed, 0),
}, *source
}
// testBranchTree creates a two-node tree and returns the leaf output.
func testBranchTree(t *testing.T, seed byte) (*tree.Tree, wire.OutPoint) {
t.Helper()
root := &tree.Node{
Input: testInput(seed),
Outputs: []*wire.TxOut{
testOut(seed, 1),
},
Children: make(map[uint32]*tree.Node),
}
rootTx, err := root.ToTx()
require.NoError(t, err)
leaf := &tree.Node{
Input: wire.OutPoint{
Hash: rootTx.TxHash(),
Index: 0,
},
Outputs: []*wire.TxOut{
testOut(seed+1, 1),
},
Children: make(map[uint32]*tree.Node),
}
root.Children[0] = leaf
source, err := leaf.GetNonAnchorOutpoint()
require.NoError(t, err)
return &tree.Tree{
Root: root,
BatchOutput: testOut(seed, 0),
}, *source
}
// testDescriptor creates the minimal descriptor shape watcher tests need.
func testDescriptor(target wire.OutPoint,
trees ...*tree.Tree) *vtxo.Descriptor {
ancestry := make([]vtxo.Ancestry, 0, len(trees))
for i := range trees {
ancestry = append(ancestry, vtxo.Ancestry{
TreePath: trees[i],
CommitmentTxID: trees[i].Root.Input.Hash,
InputIndices: []uint32{uint32(i)},
TreeDepth: 1,
})
}
return &vtxo.Descriptor{
Outpoint: target,
Ancestry: ancestry,
ChainDepth: 1,
CreatedHeight: 7,
Status: vtxo.VTXOStatusLive,
}
}
// testOut returns a deterministic output with a unique pkscript.
func testOut(seed byte, index uint32) *wire.TxOut {
return &wire.TxOut{
Value: int64(1000 + index),
PkScript: []byte{
0x51, seed, byte(index),
},
}
}
// testInput returns a deterministic outpoint for seed.
func testInput(seed byte) wire.OutPoint {
var h chainhash.Hash
h[0] = seed
return wire.OutPoint{
Hash: h,
Index: 0,
}
}