-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtree_path_shape_test.go
More file actions
297 lines (262 loc) · 7.66 KB
/
Copy pathtree_path_shape_test.go
File metadata and controls
297 lines (262 loc) · 7.66 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package arkrpc
import (
"fmt"
"testing"
"github.com/btcsuite/btcd/chainhash/v2"
"github.com/stretchr/testify/require"
)
// shapeTestOutputs is the number of outputs every synthetic node in this
// file carries. Two is the smallest count that lets a node name two
// distinct children, which is what the DAG shapes below need.
const shapeTestOutputs = 2
// shapeNode builds a minimal, structurally valid TreePathNode carrying
// the given child wiring. Only the shape matters to these tests, so the
// payload fields are filled with the cheapest values the decoder
// accepts.
func shapeNode(idx uint32, children map[uint32]uint32) *TreePathNode {
hash := chainhash.Hash{byte(idx + 1)}
outputs := make([]*TxOut, shapeTestOutputs)
for i := range outputs {
outputs[i] = &TxOut{
Value: 1000,
PkScript: []byte{
0x51,
},
}
}
return &TreePathNode{
Input: &OutPoint{
Txid: hash[:],
Vout: idx,
},
Outputs: outputs,
Children: children,
Amount: 2000,
}
}
// shapePath wraps nodes into a TreePath with a valid batch outpoint and
// output, so any error a test observes comes from the node wiring rather
// than from the surrounding fields.
func shapePath(nodes ...*TreePathNode) *TreePath {
hash := chainhash.Hash{0xba}
return &TreePath{
Nodes: nodes,
BatchOutpoint: &OutPoint{
Txid: hash[:],
Vout: 0,
},
BatchOutput: &TxOut{
Value: 2000,
PkScript: []byte{
0x51,
},
},
}
}
// TestTreePathToTreeRejectsSharedChild pins the single-parent invariant
// on the indexer decode path. Forward references alone admit a DAG: two
// parents at different indices can both name the same higher-indexed
// child and both satisfy childIdx > i. That shape is the expensive one
// here, because the receive path walks the decoded tree to validate the
// claimed ancestry depth and that walk re-visits a shared subtree once
// per path reaching it.
func TestTreePathToTreeRejectsSharedChild(t *testing.T) {
t.Parallel()
// Root fans out to 1 and 2; both then name 3.
tp := shapePath(
shapeNode(
0, map[uint32]uint32{
0: 1,
1: 2,
},
),
shapeNode(
1, map[uint32]uint32{
0: 3,
},
),
shapeNode(
2, map[uint32]uint32{
0: 3,
},
),
shapeNode(3, nil),
)
got, err := TreePathToTree(tp)
require.Error(t, err)
require.ErrorContains(t, err, "must not share children")
require.Nil(t, got)
}
// TestTreePathToTreeRejectsSharedChildViaSiblingOutputs covers the
// cheapest form of the same shape: one node pointing several of its own
// outputs at the same next node. Children is a map keyed by output
// index, so this needs no second parent at all, and it is what turns a
// depth-d chain into 2^d walk paths from only d nodes.
func TestTreePathToTreeRejectsSharedChildViaSiblingOutputs(t *testing.T) {
t.Parallel()
tp := shapePath(
shapeNode(
0, map[uint32]uint32{
0: 1,
1: 1,
},
),
shapeNode(1, nil),
)
got, err := TreePathToTree(tp)
require.Error(t, err)
require.ErrorContains(t, err, "must not share children")
require.Nil(t, got)
}
// TestTreePathToTreeRejectsUnreachableNodes covers the shapes that
// single-parent plus forward-edges still admits: those describe a
// forest, so a sender can pad a message with nodes no walk from the root
// will ever reach, or attach a whole second root with its own subtree.
func TestTreePathToTreeRejectsUnreachableNodes(t *testing.T) {
t.Parallel()
tests := []struct {
name string
nodes []*TreePathNode
}{{
// The root claims nothing, so node 1 is simply detached.
name: "orphan node",
nodes: []*TreePathNode{
shapeNode(0, nil),
shapeNode(1, nil),
},
}, {
// Node 1 is a second root carrying its own subtree.
name: "detached second root",
nodes: []*TreePathNode{
shapeNode(0, nil),
shapeNode(1, map[uint32]uint32{0: 2}),
shapeNode(2, nil),
},
}}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got, err := TreePathToTree(shapePath(tc.nodes...))
require.Error(t, err)
require.ErrorContains(
t, err, "unreachable nodes or multiple roots",
)
require.Nil(t, got)
})
}
}
// TestTreePathToTreeReportsWildIndexAsOutOfRange checks the diagnostic
// ordering: an index past the end of the node slice must be named as
// out of range even when it repeats, rather than being reported as a
// sharing violation that sends a reader hunting for a tree-shape bug.
func TestTreePathToTreeReportsWildIndexAsOutOfRange(t *testing.T) {
t.Parallel()
tp := shapePath(
shapeNode(
0, map[uint32]uint32{
0: 999,
1: 999,
},
),
shapeNode(1, nil),
)
got, err := TreePathToTree(tp)
require.Error(t, err)
require.ErrorContains(t, err, "out of range")
require.NotContains(t, err.Error(), "share children")
require.Nil(t, got)
}
// TestTreePathToTreeNodeCap covers the node bound. The decoder had none
// at all, so a single response could carry an arbitrary number of nodes
// to deserialize before any structural check ran.
func TestTreePathToTreeNodeCap(t *testing.T) {
t.Parallel()
// A linear chain long enough to exceed the cap set below.
const chainLen = 8
nodes := make([]*TreePathNode, chainLen)
for i := range nodes {
var children map[uint32]uint32
if i < chainLen-1 {
children = map[uint32]uint32{0: uint32(i + 1)}
}
nodes[i] = shapeNode(uint32(i), children)
}
tp := shapePath(nodes...)
// Over the cap: refused before any node is deserialized.
got, err := TreePathToTree(tp, WithMaxTreePathNodes(chainLen-1))
require.Error(t, err)
require.ErrorContains(t, err, "exceeds maximum")
require.Nil(t, got)
// At the cap, and with the cap disabled, the same chain decodes.
for _, maxNodes := range []int{chainLen, 0} {
t.Run(fmt.Sprintf("max=%d", maxNodes), func(t *testing.T) {
t.Parallel()
got, err := TreePathToTree(
tp, WithMaxTreePathNodes(maxNodes),
)
require.NoError(t, err)
require.NotNil(t, got)
})
}
}
// TestTreePathToTreeAcceptsWellFormedTree guards against the new checks
// rejecting anything a legitimate producer emits: a plain branching tree
// where every node other than the root is claimed exactly once must
// still decode, and the wiring must survive.
func TestTreePathToTreeAcceptsWellFormedTree(t *testing.T) {
t.Parallel()
tp := shapePath(
shapeNode(
0, map[uint32]uint32{
0: 1,
1: 2,
},
),
shapeNode(
1, map[uint32]uint32{
0: 3,
},
),
shapeNode(2, nil),
shapeNode(3, nil),
)
got, err := TreePathToTree(tp)
require.NoError(t, err)
require.NotNil(t, got)
require.Len(t, got.Root.Children, 2)
require.Len(t, got.Root.Children[0].Children, 1)
require.Empty(t, got.Root.Children[1].Children)
}
// TestAncestryPathDepthWalkOnSharedChild pins the reason the check
// belongs at this boundary rather than in each walker. nodeMaxDepth,
// which the receive path runs on every decoded ancestry path, has no
// memoization, so a shared child multiplies its work by the number of
// paths reaching it. Refusing the shape at decode time means the walk
// is never handed one.
func TestAncestryPathDepthWalkOnSharedChild(t *testing.T) {
t.Parallel()
// A chain where every node points both of its outputs at the
// next one: linear in nodes, exponential in root-to-leaf paths.
const chainLen = 20
nodes := make([]*TreePathNode, chainLen)
for i := range nodes {
var children map[uint32]uint32
if i < chainLen-1 {
children = map[uint32]uint32{
0: uint32(i + 1),
1: uint32(i + 1),
}
}
nodes[i] = shapeNode(uint32(i), children)
}
hash := chainhash.Hash{0xac}
ap := &AncestryPath{
TreePath: shapePath(nodes...),
CommitmentTxid: hash[:],
TreeDepth: chainLen,
}
got, err := AncestryPathToTree(ap)
require.Error(t, err)
require.ErrorContains(t, err, "must not share children")
require.Nil(t, got)
}