Skip to content

Commit d854c80

Browse files
committed
itest+lntest: fix itest re the new sweeping behavior
1 parent e0f0f5c commit d854c80

6 files changed

+281
-142
lines changed

itest/lnd_channel_force_close_test.go

+57-16
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ func testChannelForceClosure(ht *lntest.HarnessTest) {
6565
// order to fund the channel.
6666
st.FundCoins(btcutil.SatoshiPerBitcoin, alice)
6767

68+
// NOTE: Alice needs 3 more UTXOs to sweep her
69+
// second-layer txns after a restart - after a restart
70+
// all the time-sensitive sweeps are swept immediately
71+
// without being aggregated.
72+
//
73+
// TODO(yy): remove this once the can recover its state
74+
// from restart.
75+
st.FundCoins(btcutil.SatoshiPerBitcoin, alice)
76+
st.FundCoins(btcutil.SatoshiPerBitcoin, alice)
77+
st.FundCoins(btcutil.SatoshiPerBitcoin, alice)
78+
st.FundCoins(btcutil.SatoshiPerBitcoin, alice)
79+
6880
// Also give Carol some coins to allow her to sweep her
6981
// anchor.
7082
st.FundCoins(btcutil.SatoshiPerBitcoin, carol)
@@ -198,7 +210,7 @@ func channelForceClosureTest(ht *lntest.HarnessTest,
198210
// To give the neutrino backend some time to catch up with the chain,
199211
// we wait here until we have enough UTXOs to actually sweep the local
200212
// and remote anchor.
201-
const expectedUtxos = 2
213+
const expectedUtxos = 6
202214
ht.AssertNumUTXOs(alice, expectedUtxos)
203215

204216
// We expect to see Alice's force close tx in the mempool.
@@ -324,6 +336,28 @@ func channelForceClosureTest(ht *lntest.HarnessTest,
324336
// commit and anchor outputs.
325337
ht.MineBlocksAndAssertNumTxes(1, 1)
326338

339+
// Once Alice's anchor sweeping is mined, she should have no pending
340+
// sweep requests atm.
341+
ht.AssertNumPendingSweeps(alice, 0)
342+
343+
// TODO(yy): fix the case in 0.18.1 - the CPFP anchor sweeping may be
344+
// replaced with a following request after the above restart - the
345+
// anchor will be offered to the sweeper again with updated params,
346+
// which cannot be swept due to it being uneconomical.
347+
var anchorRecovered bool
348+
err = wait.NoError(func() error {
349+
sweepResp := alice.RPC.ListSweeps(false, 0)
350+
txns := sweepResp.GetTransactionIds().TransactionIds
351+
352+
if len(txns) >= 1 {
353+
anchorRecovered = true
354+
return nil
355+
}
356+
357+
return fmt.Errorf("expected 1 sweep tx, got %d", len(txns))
358+
}, wait.DefaultTimeout)
359+
ht.Logf("waiting for Alice's anchor sweep to be broadcast: %v", err)
360+
327361
// The following restart checks to ensure that outputs in the
328362
// kindergarten bucket are persisted while waiting for the required
329363
// number of confirmations to be reported.
@@ -365,6 +399,9 @@ func channelForceClosureTest(ht *lntest.HarnessTest,
365399
return errors.New("all funds should still be in " +
366400
"limbo")
367401
}
402+
if !anchorRecovered {
403+
return nil
404+
}
368405
if forceClose.RecoveredBalance != anchorSize {
369406
return fmt.Errorf("expected %v to be recovered",
370407
anchorSize)
@@ -487,6 +524,10 @@ func channelForceClosureTest(ht *lntest.HarnessTest,
487524
// experiencing a while waiting for the htlc outputs to incubate.
488525
ht.RestartNode(alice)
489526

527+
// To give the neutrino backend some time to catch up with the chain,
528+
// we wait here until we have enough UTXOs to
529+
// ht.AssertNumUTXOs(alice, expectedUtxos)
530+
490531
// Alice should now see the channel in her set of pending force closed
491532
// channels with one pending HTLC.
492533
err = wait.NoError(func() error {
@@ -528,19 +569,17 @@ func channelForceClosureTest(ht *lntest.HarnessTest,
528569
// one.
529570
ht.AssertNumPendingSweeps(alice, numInvoices)
530571

531-
// Mine a block to trigger the sweeps.
532-
ht.MineEmptyBlocks(1)
533-
534-
// Wait for them all to show up in the mempool and expect the timeout
535-
// txs to be aggregated into one.
536-
htlcTxIDs := ht.Miner.AssertNumTxsInMempool(1)
572+
// Wait for them all to show up in the mempool
573+
//
574+
// NOTE: after restart, all the htlc timeout txns will be offered to
575+
// the sweeper with `Immediate` set to true, so they won't be
576+
// aggregated.
577+
htlcTxIDs := ht.Miner.AssertNumTxsInMempool(numInvoices)
537578

538579
// Retrieve each htlc timeout txn from the mempool, and ensure it is
539580
// well-formed. This entails verifying that each only spends from
540-
// output, and that output is from the commitment txn. In case this is
541-
// an anchor channel, the transactions are aggregated by the sweeper
542-
// into one.
543-
numInputs := numInvoices + 1
581+
// output, and that output is from the commitment txn.
582+
numInputs := 2
544583

545584
// Construct a map of the already confirmed htlc timeout outpoints,
546585
// that will count the number of times each is spent by the sweep txn.
@@ -641,7 +680,7 @@ func channelForceClosureTest(ht *lntest.HarnessTest,
641680

642681
// Generate a block that mines the htlc timeout txns. Doing so now
643682
// activates the 2nd-stage CSV delayed outputs.
644-
ht.MineBlocksAndAssertNumTxes(1, 1)
683+
ht.MineBlocksAndAssertNumTxes(1, numInvoices)
645684

646685
// Alice is restarted here to ensure that she promptly moved the crib
647686
// outputs to the kindergarten bucket after the htlc timeout txns were
@@ -651,7 +690,9 @@ func channelForceClosureTest(ht *lntest.HarnessTest,
651690
// Advance the chain until just before the 2nd-layer CSV delays expire.
652691
// For anchor channels this is one block earlier.
653692
_, currentHeight = ht.Miner.GetBestBlock()
654-
numBlocks := int(htlcCsvMaturityHeight - uint32(currentHeight) - 1)
693+
ht.Logf("current height: %v, htlcCsvMaturityHeight=%v", currentHeight,
694+
htlcCsvMaturityHeight)
695+
numBlocks := int(htlcCsvMaturityHeight - uint32(currentHeight) - 2)
655696
ht.MineEmptyBlocks(numBlocks)
656697

657698
// Restart Alice to ensure that she can recover from a failure before
@@ -738,8 +779,8 @@ func channelForceClosureTest(ht *lntest.HarnessTest,
738779
ht.AssertSweepFound(alice, htlcSweepTx.Hash().String(), true, 0)
739780

740781
// The following restart checks to ensure that the nursery store is
741-
// storing the txid of the previously broadcast htlc sweep txn, and that
742-
// it begins watching that txid after restarting.
782+
// storing the txid of the previously broadcast htlc sweep txn, and
783+
// that it begins watching that txid after restarting.
743784
ht.RestartNode(alice)
744785

745786
// Now that the channel has been fully swept, it should no longer show
@@ -755,7 +796,7 @@ func channelForceClosureTest(ht *lntest.HarnessTest,
755796
}
756797

757798
err = checkPendingHtlcStageAndMaturity(
758-
forceClose, 2, htlcCsvMaturityHeight, -1,
799+
forceClose, 2, htlcCsvMaturityHeight-1, -1,
759800
)
760801
if err != nil {
761802
return err

0 commit comments

Comments
 (0)