forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nursery_store_test.go
554 lines (469 loc) · 16.9 KB
/
nursery_store_test.go
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
// +build !rpctest
package lnd
import (
"reflect"
"testing"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/channeldb"
)
type incubateTest struct {
nOutputs int
chanPoint *wire.OutPoint
commOutput *kidOutput
htlcOutputs []babyOutput
err error
}
// incubateTests holds the test vectors used to test the state transitions of
// outputs stored in the nursery store.
var incubateTests []incubateTest
// initIncubateTests instantiates the test vectors during package init, which
// properly captures the sign descriptors and public keys.
func initIncubateTests() {
incubateTests = []incubateTest{
{
nOutputs: 0,
chanPoint: &outPoints[3],
},
{
nOutputs: 1,
chanPoint: &outPoints[0],
commOutput: &kidOutputs[0],
},
{
nOutputs: 4,
chanPoint: &outPoints[0],
commOutput: &kidOutputs[0],
htlcOutputs: babyOutputs,
},
}
}
// TestNurseryStoreInit verifies basic properties of the nursery store before
// any modifying calls are made.
func TestNurseryStoreInit(t *testing.T) {
cdb, cleanUp, err := channeldb.MakeTestDB()
if err != nil {
t.Fatalf("unable to open channel db: %v", err)
}
defer cleanUp()
ns, err := newNurseryStore(&bitcoinTestnetGenesis, cdb)
if err != nil {
t.Fatalf("unable to open nursery store: %v", err)
}
assertNumChannels(t, ns, 0)
assertNumPreschools(t, ns, 0)
}
// TestNurseryStoreIncubate tests the primary state transitions taken by outputs
// in the nursery store. The test is designed to walk both commitment or htlc
// outputs through the nursery store, verifying the properties of the
// intermediate states.
func TestNurseryStoreIncubate(t *testing.T) {
cdb, cleanUp, err := channeldb.MakeTestDB()
if err != nil {
t.Fatalf("unable to open channel db: %v", err)
}
defer cleanUp()
ns, err := newNurseryStore(&bitcoinTestnetGenesis, cdb)
if err != nil {
t.Fatalf("unable to open nursery store: %v", err)
}
for i, test := range incubateTests {
// At the beginning of each test, we do not expect to the
// nursery store to be tracking any outputs for this channel
// point.
assertNumChanOutputs(t, ns, test.chanPoint, 0)
// Nursery store should be completely empty.
assertNumChannels(t, ns, 0)
assertNumPreschools(t, ns, 0)
// Begin incubating all of the outputs provided in this test
// vector.
var kids []kidOutput
if test.commOutput != nil {
kids = append(kids, *test.commOutput)
}
err = ns.Incubate(kids, test.htlcOutputs)
if err != nil {
t.Fatalf("unable to incubate outputs"+
"on test #%d: %v", i, err)
}
// Now that the outputs have been inserted, the nursery store
// should see exactly that many outputs under this channel
// point.
// NOTE: This property should remain intact after every state
// change until the channel has been completely removed.
assertNumChanOutputs(t, ns, test.chanPoint, test.nOutputs)
// If there were no inputs to be incubated, just check that the
// no trace of the channel was left.
if test.nOutputs == 0 {
assertNumChannels(t, ns, 0)
continue
}
// The test vector has a non-zero number of outputs, we will
// expect to only see the one channel from this test case.
assertNumChannels(t, ns, 1)
// The channel should be shown as immature, since none of the
// outputs should be graduated directly after being inserted.
// It should also be impossible to remove the channel, if it is
// also immature.
// NOTE: These two tests properties should hold between every
// state change until all outputs have been fully graduated.
assertChannelMaturity(t, ns, test.chanPoint, false)
assertCanRemoveChannel(t, ns, test.chanPoint, false)
// Verify that the htlc outputs, if any, reside in the height
// index at their first stage CLTV's expiry height.
for _, htlcOutput := range test.htlcOutputs {
assertCribAtExpiryHeight(t, ns, &htlcOutput)
}
// If the commitment output was not dust, we will move it from
// the preschool bucket to the kindergarten bucket.
if test.commOutput != nil {
// If the commitment output was not considered dust, we
// should see exactly one preschool output in the
// nursery store.
assertNumPreschools(t, ns, 1)
// Now, move the commitment output to the kindergarten
// bucket.
err = ns.PreschoolToKinder(test.commOutput, 0)
if err != test.err {
t.Fatalf("unable to move commitment output from "+
"pscl to kndr: %v", err)
}
// The total number of outputs for this channel should
// not have changed, and the kindergarten output should
// reside at its maturity height.
assertNumChanOutputs(t, ns, test.chanPoint, test.nOutputs)
assertKndrAtMaturityHeight(t, ns, test.commOutput)
// The total number of channels should not have changed.
assertNumChannels(t, ns, 1)
// Channel maturity and removal should reflect that the
// channel still has non-graduated outputs.
assertChannelMaturity(t, ns, test.chanPoint, false)
assertCanRemoveChannel(t, ns, test.chanPoint, false)
// Moving the preschool output should have no effect on
// the placement of crib outputs in the height index.
for _, htlcOutput := range test.htlcOutputs {
assertCribAtExpiryHeight(t, ns, &htlcOutput)
}
}
// At this point, we should see no more preschool outputs in the
// nursery store. Either it was moved to the kindergarten
// bucket, or never inserted.
assertNumPreschools(t, ns, 0)
// If the commitment output is not-dust, we will graduate the
// class at its maturity height.
if test.commOutput != nil {
// Compute the commitment output's maturity height, and
// move proceed to graduate that class.
maturityHeight := test.commOutput.ConfHeight() +
test.commOutput.BlocksToMaturity()
err = ns.GraduateKinder(maturityHeight, test.commOutput)
if err != nil {
t.Fatalf("unable to graduate kindergarten class at "+
"height %d: %v", maturityHeight, err)
}
// The total number of outputs for this channel should
// not have changed, but the kindergarten output should
// have been removed from its maturity height.
assertNumChanOutputs(t, ns, test.chanPoint, test.nOutputs)
assertKndrNotAtMaturityHeight(t, ns, test.commOutput)
// The total number of channels should not have changed.
assertNumChannels(t, ns, 1)
// Moving the preschool output should have no effect on
// the placement of crib outputs in the height index.
for _, htlcOutput := range test.htlcOutputs {
assertCribAtExpiryHeight(t, ns, &htlcOutput)
}
}
// If there are any htlc outputs to incubate, we will walk them
// through their two-stage incubation process.
if len(test.htlcOutputs) > 0 {
for i, htlcOutput := range test.htlcOutputs {
// Begin by moving each htlc output from the
// crib to kindergarten state.
err = ns.CribToKinder(&htlcOutput)
if err != nil {
t.Fatalf("unable to move htlc output from "+
"crib to kndr: %v", err)
}
// Number of outputs for this channel should
// remain unchanged.
assertNumChanOutputs(t, ns, test.chanPoint,
test.nOutputs)
// If the output hasn't moved to kndr, it should
// be at its crib expiry height, otherwise is
// should have been removed.
for j := range test.htlcOutputs {
if j > i {
assertCribAtExpiryHeight(t, ns,
&test.htlcOutputs[j])
assertKndrNotAtMaturityHeight(t,
ns, &test.htlcOutputs[j].kidOutput)
} else {
assertCribNotAtExpiryHeight(t, ns,
&test.htlcOutputs[j])
assertKndrAtMaturityHeight(t,
ns, &test.htlcOutputs[j].kidOutput)
}
}
}
// Total number of channels in the nursery store should
// be the same, no outputs should be marked as
// preschool.
assertNumChannels(t, ns, 1)
assertNumPreschools(t, ns, 0)
// Channel should also not be mature, as it we should
// still have outputs in kindergarten.
assertChannelMaturity(t, ns, test.chanPoint, false)
assertCanRemoveChannel(t, ns, test.chanPoint, false)
// Now, graduate each htlc kindergarten output,
// asserting the invariant number of outputs being
// tracked in this channel
for _, htlcOutput := range test.htlcOutputs {
maturityHeight := htlcOutput.ConfHeight() +
htlcOutput.BlocksToMaturity()
err = ns.GraduateKinder(maturityHeight,
&htlcOutput.kidOutput)
if err != nil {
t.Fatalf("unable to graduate htlc output "+
"from kndr to grad: %v", err)
}
assertNumChanOutputs(t, ns, test.chanPoint,
test.nOutputs)
}
}
// All outputs have been advanced through the nursery store, but
// no attempt has been made to clean up this channel. We expect
// to see the same channel remaining, and no kindergarten
// outputs.
assertNumChannels(t, ns, 1)
assertNumPreschools(t, ns, 0)
// Since all outputs have now been graduated, the nursery store
// should recognize that the channel is mature, and attempting
// to remove it should succeed.
assertChannelMaturity(t, ns, test.chanPoint, true)
assertCanRemoveChannel(t, ns, test.chanPoint, true)
// Now that the channel has been removed, the nursery store
// should be no channels in the nursery store, and no outputs
// being tracked for this channel point.
assertNumChannels(t, ns, 0)
assertNumChanOutputs(t, ns, test.chanPoint, 0)
// If we had a commitment output, ensure it was removed from the
// height index.
if test.commOutput != nil {
assertKndrNotAtMaturityHeight(t, ns, test.commOutput)
}
// Check that all htlc outputs are no longer stored in their
// crib or kindergarten height buckets.
for _, htlcOutput := range test.htlcOutputs {
assertCribNotAtExpiryHeight(t, ns, &htlcOutput)
assertKndrNotAtMaturityHeight(t, ns, &htlcOutput.kidOutput)
}
// Lastly, there should be no lingering preschool outputs.
assertNumPreschools(t, ns, 0)
}
}
// TestNurseryStoreGraduate verifies that the nursery store properly removes
// populated entries from the height index as it is purged, and that the last
// purged height is set appropriately.
func TestNurseryStoreGraduate(t *testing.T) {
cdb, cleanUp, err := channeldb.MakeTestDB()
if err != nil {
t.Fatalf("unable to open channel db: %v", err)
}
defer cleanUp()
ns, err := newNurseryStore(&bitcoinTestnetGenesis, cdb)
if err != nil {
t.Fatalf("unable to open nursery store: %v", err)
}
kid := &kidOutputs[3]
// Compute the height at which this output will be inserted in the
// height index.
maturityHeight := kid.ConfHeight() + kid.BlocksToMaturity()
// First, add a commitment output to the nursery store, which is
// initially inserted in the preschool bucket.
err = ns.Incubate([]kidOutput{*kid}, nil)
if err != nil {
t.Fatalf("unable to incubate commitment output: %v", err)
}
// Then, move the commitment output to the kindergarten bucket, such
// that it resides in the height index at its maturity height.
err = ns.PreschoolToKinder(kid, 0)
if err != nil {
t.Fatalf("unable to move pscl output to kndr: %v", err)
}
// Now, iteratively purge all height below the target maturity height,
// checking that each class is now empty, and that the last purged
// height is set correctly.
for i := 0; i < int(maturityHeight); i++ {
assertHeightIsPurged(t, ns, uint32(i))
}
// Check that the commitment output currently exists at its maturity
// height.
assertKndrAtMaturityHeight(t, ns, kid)
err = ns.GraduateKinder(maturityHeight, kid)
if err != nil {
t.Fatalf("unable to graduate kindergarten outputs at height=%d: "+
"%v", maturityHeight, err)
}
assertHeightIsPurged(t, ns, maturityHeight)
}
// assertNumChanOutputs checks that the channel bucket has the expected number
// of outputs.
func assertNumChanOutputs(t *testing.T, ns NurseryStore,
chanPoint *wire.OutPoint, expectedNum int) {
var count int
err := ns.ForChanOutputs(chanPoint, func([]byte, []byte) error {
count++
return nil
})
if count == 0 && err == ErrContractNotFound {
return
} else if err != nil {
t.Fatalf("unable to count num outputs for channel %v: %v",
chanPoint, err)
}
if count != expectedNum {
t.Fatalf("nursery store should have %d outputs, found %d",
expectedNum, count)
}
}
// assertNumPreschools loads all preschool outputs and verifies their count
// matches the expected number.
func assertNumPreschools(t *testing.T, ns NurseryStore, expected int) {
psclOutputs, err := ns.FetchPreschools()
if err != nil {
t.Fatalf("unable to retrieve preschool outputs: %v", err)
}
if len(psclOutputs) != expected {
t.Fatalf("expected number of pscl outputs to be %d, got %v",
expected, len(psclOutputs))
}
}
// assertNumChannels checks that the nursery has a given number of active
// channels.
func assertNumChannels(t *testing.T, ns NurseryStore, expected int) {
channels, err := ns.ListChannels()
if err != nil {
t.Fatalf("unable to fetch channels from nursery store: %v",
err)
}
if len(channels) != expected {
t.Fatalf("expected number of active channels to be %d, got %d",
expected, len(channels))
}
}
// assertHeightIsPurged checks that the finalized transaction, kindergarten, and
// htlc outputs at a particular height are all nil.
func assertHeightIsPurged(t *testing.T, ns NurseryStore,
height uint32) {
kndrOutputs, cribOutputs, err := ns.FetchClass(height)
if err != nil {
t.Fatalf("unable to retrieve class at height=%d: %v",
height, err)
}
if kndrOutputs != nil {
t.Fatalf("height=%d not purged, kndr outputs should be nil", height)
}
if cribOutputs != nil {
t.Fatalf("height=%d not purged, crib outputs should be nil", height)
}
}
// assertCribAtExpiryHeight loads the class at the given height, and verifies
// that the given htlc output is one of the crib outputs.
func assertCribAtExpiryHeight(t *testing.T, ns NurseryStore,
htlcOutput *babyOutput) {
expiryHeight := htlcOutput.expiry
_, cribOutputs, err := ns.FetchClass(expiryHeight)
if err != nil {
t.Fatalf("unable to retrieve class at height=%d: %v",
expiryHeight, err)
}
for _, crib := range cribOutputs {
if reflect.DeepEqual(&crib, htlcOutput) {
return
}
}
t.Fatalf("could not find crib output %v at height %d",
htlcOutput.OutPoint(), expiryHeight)
}
// assertCribNotAtExpiryHeight loads the class at the given height, and verifies
// that the given htlc output is not one of the crib outputs.
func assertCribNotAtExpiryHeight(t *testing.T, ns NurseryStore,
htlcOutput *babyOutput) {
expiryHeight := htlcOutput.expiry
_, cribOutputs, err := ns.FetchClass(expiryHeight)
if err != nil {
t.Fatalf("unable to retrieve class at height %d: %v",
expiryHeight, err)
}
for _, crib := range cribOutputs {
if reflect.DeepEqual(&crib, htlcOutput) {
t.Fatalf("found find crib output %v at height %d",
htlcOutput.OutPoint(), expiryHeight)
}
}
}
// assertKndrAtMaturityHeight loads the class at the provided height and
// verifies that the provided kid output is one of the kindergarten outputs
// returned.
func assertKndrAtMaturityHeight(t *testing.T, ns NurseryStore,
kndrOutput *kidOutput) {
maturityHeight := kndrOutput.ConfHeight() +
kndrOutput.BlocksToMaturity()
kndrOutputs, _, err := ns.FetchClass(maturityHeight)
if err != nil {
t.Fatalf("unable to retrieve class at height %d: %v",
maturityHeight, err)
}
for _, kndr := range kndrOutputs {
if reflect.DeepEqual(&kndr, kndrOutput) {
return
}
}
t.Fatalf("could not find kndr output %v at height %d",
kndrOutput.OutPoint(), maturityHeight)
}
// assertKndrNotAtMaturityHeight loads the class at the provided height and
// verifies that the provided kid output is not one of the kindergarten outputs
// returned.
func assertKndrNotAtMaturityHeight(t *testing.T, ns NurseryStore,
kndrOutput *kidOutput) {
maturityHeight := kndrOutput.ConfHeight() +
kndrOutput.BlocksToMaturity()
kndrOutputs, _, err := ns.FetchClass(maturityHeight)
if err != nil {
t.Fatalf("unable to retrieve class at height %d: %v",
maturityHeight, err)
}
for _, kndr := range kndrOutputs {
if reflect.DeepEqual(&kndr, kndrOutput) {
t.Fatalf("found find kndr output %v at height %d",
kndrOutput.OutPoint(), maturityHeight)
}
}
}
// assertChannelMaturity queries the nursery store for the maturity of the given
// channel, failing if the result does not match the expectedMaturity.
func assertChannelMaturity(t *testing.T, ns NurseryStore,
chanPoint *wire.OutPoint, expectedMaturity bool) {
isMature, err := ns.IsMatureChannel(chanPoint)
if err != nil {
t.Fatalf("unable to fetch channel maturity: %v", err)
}
if isMature != expectedMaturity {
t.Fatalf("expected channel maturity: %v, actual: %v",
expectedMaturity, isMature)
}
}
// assertCanRemoveChannel tries to remove a channel from the nursery store,
// failing if the result does match expected canRemove.
func assertCanRemoveChannel(t *testing.T, ns NurseryStore,
chanPoint *wire.OutPoint, canRemove bool) {
err := ns.RemoveChannel(chanPoint)
if canRemove && err != nil {
t.Fatalf("expected nil when removing active channel, got: %v",
err)
} else if !canRemove && err != ErrImmatureChannel {
t.Fatalf("expected ErrImmatureChannel when removing "+
"active channel: %v", err)
}
}