From 16403da91e3d1952e47cf4a162e2da2a19c7f221 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Fri, 20 Aug 2021 01:35:02 +0800 Subject: [PATCH] itest: manage context in CloseChannel and AssertChannelExists --- lntest/harness.go | 17 ++++++++++++----- lntest/itest/assertions.go | 14 ++++---------- lntest/itest/lnd_channel_force_close.go | 7 ++----- lntest/itest/lnd_funding_test.go | 9 +++------ lntest/itest/lnd_misc_test.go | 21 +++++++-------------- lntest/itest/lnd_revocation_test.go | 18 ++++++++---------- lntest/itest/lnd_routing_test.go | 8 +++----- 7 files changed, 39 insertions(+), 55 deletions(-) diff --git a/lntest/harness.go b/lntest/harness.go index 7cc739ab7..08abf0f45 100644 --- a/lntest/harness.go +++ b/lntest/harness.go @@ -1163,10 +1163,14 @@ func (n *NetworkHarness) WaitForChannelOpen( // passed channel point, initiated by the passed lnNode. If the passed context // has a timeout, an error is returned if that timeout is reached before the // channel close is pending. -func (n *NetworkHarness) CloseChannel(ctx context.Context, - lnNode *HarnessNode, cp *lnrpc.ChannelPoint, +func (n *NetworkHarness) CloseChannel(lnNode *HarnessNode, + cp *lnrpc.ChannelPoint, force bool) (lnrpc.Lightning_CloseChannelClient, *chainhash.Hash, error) { + ctxb := context.Background() + ctx, cancel := context.WithTimeout(ctxb, ChannelCloseTimeout) + defer cancel() + // Create a channel outpoint that we can use to compare to channels // from the ListChannelsResponse. txidHash, err := getChanPointFundingTxid(cp) @@ -1342,9 +1346,12 @@ func (n *NetworkHarness) WaitForChannelClose( // assertions using channel's values. These functions are responsible for // failing the test themselves if they do not pass. // nolint: interfacer -func (n *NetworkHarness) AssertChannelExists(ctx context.Context, - node *HarnessNode, chanPoint *wire.OutPoint, - checks ...func(*lnrpc.Channel)) error { +func (n *NetworkHarness) AssertChannelExists(node *HarnessNode, + chanPoint *wire.OutPoint, checks ...func(*lnrpc.Channel)) error { + + ctxb := context.Background() + ctx, cancel := context.WithTimeout(ctxb, ChannelCloseTimeout) + defer cancel() req := &lnrpc.ListChannelsRequest{} diff --git a/lntest/itest/assertions.go b/lntest/itest/assertions.go index b2c0a6c98..c51e41dcd 100644 --- a/lntest/itest/assertions.go +++ b/lntest/itest/assertions.go @@ -72,10 +72,6 @@ func openChannelAndAssert(t *harnessTest, net *lntest.NetworkHarness, t.t.Helper() - ctxb := context.Background() - ctx, cancel := context.WithTimeout(ctxb, channelOpenTimeout) - defer cancel() - chanOpenUpdate := openChannelStream(t, net, alice, bob, p) // Mine 6 blocks, then wait for Alice's node to notify us that the @@ -99,11 +95,11 @@ func openChannelAndAssert(t *harnessTest, net *lntest.NetworkHarness, Index: fundingChanPoint.OutputIndex, } require.NoError( - t.t, net.AssertChannelExists(ctx, alice, &chanPoint), + t.t, net.AssertChannelExists(alice, &chanPoint), "unable to assert channel existence", ) require.NoError( - t.t, net.AssertChannelExists(ctx, bob, &chanPoint), + t.t, net.AssertChannelExists(bob, &chanPoint), "unable to assert channel existence", ) @@ -238,9 +234,7 @@ func closeChannelAndAssertType(t *harnessTest, defer close(graphSub.quit) } - closeUpdates, _, err := net.CloseChannel( - ctxt, node, fundingChanPoint, force, - ) + closeUpdates, _, err := net.CloseChannel(node, fundingChanPoint, force) require.NoError(t.t, err, "unable to close channel") // If the channel policy was enabled prior to the closure, wait until we @@ -277,7 +271,7 @@ func closeReorgedChannelAndAssert(t *harnessTest, ctx, cancel := context.WithTimeout(ctxb, channelCloseTimeout) defer cancel() - closeUpdates, _, err := net.CloseChannel(ctx, node, fundingChanPoint, force) + closeUpdates, _, err := net.CloseChannel(node, fundingChanPoint, force) require.NoError(t.t, err, "unable to close channel") return assertChannelClosed( diff --git a/lntest/itest/lnd_channel_force_close.go b/lntest/itest/lnd_channel_force_close.go index eeb52e906..e78ce4085 100644 --- a/lntest/itest/lnd_channel_force_close.go +++ b/lntest/itest/lnd_channel_force_close.go @@ -139,9 +139,7 @@ func testCommitmentTransactionDeadline(net *lntest.NetworkHarness, require.NoError(t.t, err, "htlc mismatch") // Alice force closes the channel. - ctxt, cancel = context.WithTimeout(ctxb, defaultTimeout) - defer cancel() - _, _, err = net.CloseChannel(ctxt, alice, chanPoint, true) + _, _, err = net.CloseChannel(alice, chanPoint, true) require.NoError(t.t, err, "unable to force close channel") // Now that the channel has been force closed, it should show @@ -431,8 +429,7 @@ func channelForceClosureTest(net *lntest.NetworkHarness, t *harnessTest, const actualFeeRate = 30000 net.SetFeeEstimate(actualFeeRate) - ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout) - _, closingTxID, err := net.CloseChannel(ctxt, alice, chanPoint, true) + _, closingTxID, err := net.CloseChannel(alice, chanPoint, true) if err != nil { t.Fatalf("unable to execute force channel closure: %v", err) } diff --git a/lntest/itest/lnd_funding_test.go b/lntest/itest/lnd_funding_test.go index f7b020527..e1616ef25 100644 --- a/lntest/itest/lnd_funding_test.go +++ b/lntest/itest/lnd_funding_test.go @@ -454,8 +454,7 @@ func testExternalFundingChanPoint(net *lntest.NetworkHarness, t *harnessTest) { // First, we'll try to close the channel as Carol, the initiator. This // should fail as a frozen channel only allows the responder to // initiate a channel close. - ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout) - _, _, err = net.CloseChannel(ctxt, carol, chanPoint2, false) + _, _, err = net.CloseChannel(carol, chanPoint2, false) require.Error(t.t, err, "carol wasn't denied a co-op close attempt "+ "for a frozen channel", @@ -619,13 +618,11 @@ func testChannelFundingPersistence(net *lntest.NetworkHarness, t *harnessTest) { } // Check both nodes to ensure that the channel is ready for operation. - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) - err = net.AssertChannelExists(ctxt, net.Alice, &outPoint, check) + err = net.AssertChannelExists(net.Alice, &outPoint, check) if err != nil { t.Fatalf("unable to assert channel existence: %v", err) } - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) - if err := net.AssertChannelExists(ctxt, carol, &outPoint); err != nil { + if err := net.AssertChannelExists(carol, &outPoint); err != nil { t.Fatalf("unable to assert channel existence: %v", err) } diff --git a/lntest/itest/lnd_misc_test.go b/lntest/itest/lnd_misc_test.go index 459a4d079..c76a01c40 100644 --- a/lntest/itest/lnd_misc_test.go +++ b/lntest/itest/lnd_misc_test.go @@ -115,12 +115,10 @@ func testDisconnectingTargetPeer(net *lntest.NetworkHarness, t *harnessTest) { } // Check both nodes to ensure that the channel is ready for operation. - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) - if err := net.AssertChannelExists(ctxt, alice, &outPoint); err != nil { + if err := net.AssertChannelExists(alice, &outPoint); err != nil { t.Fatalf("unable to assert channel existence: %v", err) } - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) - if err := net.AssertChannelExists(ctxt, bob, &outPoint); err != nil { + if err := net.AssertChannelExists(bob, &outPoint); err != nil { t.Fatalf("unable to assert channel existence: %v", err) } @@ -573,10 +571,8 @@ func testMaxPendingChannels(net *lntest.NetworkHarness, t *harnessTest) { Hash: *fundingTxID, Index: fundingChanPoint.OutputIndex, } - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) - if err := net.AssertChannelExists(ctxt, net.Alice, &chanPoint); err != nil { - t.Fatalf("unable to assert channel existence: %v", err) - } + err = net.AssertChannelExists(net.Alice, &chanPoint) + require.NoError(t.t, err, "unable to assert channel existence") chanPoints[i] = fundingChanPoint } @@ -1503,14 +1499,12 @@ func testSendUpdateDisableChannel(net *lntest.NetworkHarness, t *harnessTest) { // Close Alice's channels with Bob and Carol cooperatively and // unilaterally respectively. - ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout) - _, _, err = net.CloseChannel(ctxt, net.Alice, chanPointAliceBob, false) + _, _, err = net.CloseChannel(net.Alice, chanPointAliceBob, false) if err != nil { t.Fatalf("unable to close channel: %v", err) } - ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout) - _, _, err = net.CloseChannel(ctxt, net.Alice, chanPointAliceCarol, true) + _, _, err = net.CloseChannel(net.Alice, chanPointAliceCarol, true) if err != nil { t.Fatalf("unable to close channel: %v", err) } @@ -1530,8 +1524,7 @@ func testSendUpdateDisableChannel(net *lntest.NetworkHarness, t *harnessTest) { mineBlocks(t, net, 1, 2) // Also do this check for Eve's channel with Carol. - ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout) - _, _, err = net.CloseChannel(ctxt, eve, chanPointEveCarol, false) + _, _, err = net.CloseChannel(eve, chanPointEveCarol, false) if err != nil { t.Fatalf("unable to close channel: %v", err) } diff --git a/lntest/itest/lnd_revocation_test.go b/lntest/itest/lnd_revocation_test.go index 1332e477b..b817fb00d 100644 --- a/lntest/itest/lnd_revocation_test.go +++ b/lntest/itest/lnd_revocation_test.go @@ -169,8 +169,9 @@ func testRevokedCloseRetribution(net *lntest.NetworkHarness, t *harnessTest) { var closeUpdates lnrpc.Lightning_CloseChannelClient force := true err = wait.Predicate(func() bool { - ctxt, _ := context.WithTimeout(ctxb, channelCloseTimeout) - closeUpdates, _, err = net.CloseChannel(ctxt, net.Bob, chanPoint, force) + closeUpdates, _, err = net.CloseChannel( + net.Bob, chanPoint, force, + ) if err != nil { predErr = err return false @@ -404,9 +405,8 @@ func testRevokedCloseRetributionZeroValueRemoteOutput(net *lntest.NetworkHarness force := true err = wait.Predicate(func() bool { - ctxt, _ := context.WithTimeout(ctxb, channelCloseTimeout) closeUpdates, closeTxID, closeErr = net.CloseChannel( - ctxt, carol, chanPoint, force, + carol, chanPoint, force, ) return closeErr == nil }, defaultTimeout) @@ -714,9 +714,9 @@ func testRevokedCloseRetributionRemoteHodl(net *lntest.NetworkHarness, // commitment transaction of a prior *revoked* state, so she'll soon // feel the wrath of Dave's retribution. force := true - ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout) - closeUpdates, closeTxID, err := net.CloseChannel(ctxt, carol, - chanPoint, force) + closeUpdates, closeTxID, err := net.CloseChannel( + carol, chanPoint, force, + ) if err != nil { t.Fatalf("unable to close channel: %v", err) } @@ -1154,9 +1154,7 @@ func testRevokedCloseRetributionAltruistWatchtowerCase( // broadcasting his current channel state. This is actually the // commitment transaction of a prior *revoked* state, so he'll soon // feel the wrath of Dave's retribution. - closeUpdates, closeTxID, err := net.CloseChannel( - ctxb, carol, chanPoint, true, - ) + closeUpdates, closeTxID, err := net.CloseChannel(carol, chanPoint, true) if err != nil { t.Fatalf("unable to close channel: %v", err) } diff --git a/lntest/itest/lnd_routing_test.go b/lntest/itest/lnd_routing_test.go index 4ced50b2a..6955ad805 100644 --- a/lntest/itest/lnd_routing_test.go +++ b/lntest/itest/lnd_routing_test.go @@ -856,13 +856,11 @@ func testPrivateChannels(net *lntest.NetworkHarness, t *harnessTest) { Hash: *fundingTxID, Index: chanPointPrivate.OutputIndex, } - ctxt, _ := context.WithTimeout(ctxb, defaultTimeout) - err = net.AssertChannelExists(ctxt, carol, &privateFundPoint) + err = net.AssertChannelExists(carol, &privateFundPoint) if err != nil { t.Fatalf("unable to assert channel existence: %v", err) } - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) - err = net.AssertChannelExists(ctxt, net.Alice, &privateFundPoint) + err = net.AssertChannelExists(net.Alice, &privateFundPoint) if err != nil { t.Fatalf("unable to assert channel existence: %v", err) } @@ -886,7 +884,7 @@ func testPrivateChannels(net *lntest.NetworkHarness, t *harnessTest) { time.Sleep(time.Millisecond * 50) // Let Carol pay the invoices. - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) + ctxt, _ := context.WithTimeout(ctxb, defaultTimeout) err = completePaymentRequests( ctxt, carol, carol.RouterClient, payReqs, true, )