itest: manage context in CloseChannel and AssertChannelExists

This commit is contained in:
yyforyongyu
2021-08-20 01:35:02 +08:00
parent 3e26d77f91
commit 16403da91e
7 changed files with 39 additions and 55 deletions

View File

@@ -1163,10 +1163,14 @@ func (n *NetworkHarness) WaitForChannelOpen(
// passed channel point, initiated by the passed lnNode. If the passed context // 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 // has a timeout, an error is returned if that timeout is reached before the
// channel close is pending. // channel close is pending.
func (n *NetworkHarness) CloseChannel(ctx context.Context, func (n *NetworkHarness) CloseChannel(lnNode *HarnessNode,
lnNode *HarnessNode, cp *lnrpc.ChannelPoint, cp *lnrpc.ChannelPoint,
force bool) (lnrpc.Lightning_CloseChannelClient, *chainhash.Hash, error) { 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 // Create a channel outpoint that we can use to compare to channels
// from the ListChannelsResponse. // from the ListChannelsResponse.
txidHash, err := getChanPointFundingTxid(cp) txidHash, err := getChanPointFundingTxid(cp)
@@ -1342,9 +1346,12 @@ func (n *NetworkHarness) WaitForChannelClose(
// assertions using channel's values. These functions are responsible for // assertions using channel's values. These functions are responsible for
// failing the test themselves if they do not pass. // failing the test themselves if they do not pass.
// nolint: interfacer // nolint: interfacer
func (n *NetworkHarness) AssertChannelExists(ctx context.Context, func (n *NetworkHarness) AssertChannelExists(node *HarnessNode,
node *HarnessNode, chanPoint *wire.OutPoint, chanPoint *wire.OutPoint, checks ...func(*lnrpc.Channel)) error {
checks ...func(*lnrpc.Channel)) error {
ctxb := context.Background()
ctx, cancel := context.WithTimeout(ctxb, ChannelCloseTimeout)
defer cancel()
req := &lnrpc.ListChannelsRequest{} req := &lnrpc.ListChannelsRequest{}

View File

@@ -72,10 +72,6 @@ func openChannelAndAssert(t *harnessTest, net *lntest.NetworkHarness,
t.t.Helper() t.t.Helper()
ctxb := context.Background()
ctx, cancel := context.WithTimeout(ctxb, channelOpenTimeout)
defer cancel()
chanOpenUpdate := openChannelStream(t, net, alice, bob, p) chanOpenUpdate := openChannelStream(t, net, alice, bob, p)
// Mine 6 blocks, then wait for Alice's node to notify us that the // 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, Index: fundingChanPoint.OutputIndex,
} }
require.NoError( require.NoError(
t.t, net.AssertChannelExists(ctx, alice, &chanPoint), t.t, net.AssertChannelExists(alice, &chanPoint),
"unable to assert channel existence", "unable to assert channel existence",
) )
require.NoError( require.NoError(
t.t, net.AssertChannelExists(ctx, bob, &chanPoint), t.t, net.AssertChannelExists(bob, &chanPoint),
"unable to assert channel existence", "unable to assert channel existence",
) )
@@ -238,9 +234,7 @@ func closeChannelAndAssertType(t *harnessTest,
defer close(graphSub.quit) defer close(graphSub.quit)
} }
closeUpdates, _, err := net.CloseChannel( closeUpdates, _, err := net.CloseChannel(node, fundingChanPoint, force)
ctxt, node, fundingChanPoint, force,
)
require.NoError(t.t, err, "unable to close channel") require.NoError(t.t, err, "unable to close channel")
// If the channel policy was enabled prior to the closure, wait until we // 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) ctx, cancel := context.WithTimeout(ctxb, channelCloseTimeout)
defer cancel() 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") require.NoError(t.t, err, "unable to close channel")
return assertChannelClosed( return assertChannelClosed(

View File

@@ -139,9 +139,7 @@ func testCommitmentTransactionDeadline(net *lntest.NetworkHarness,
require.NoError(t.t, err, "htlc mismatch") require.NoError(t.t, err, "htlc mismatch")
// Alice force closes the channel. // Alice force closes the channel.
ctxt, cancel = context.WithTimeout(ctxb, defaultTimeout) _, _, err = net.CloseChannel(alice, chanPoint, true)
defer cancel()
_, _, err = net.CloseChannel(ctxt, alice, chanPoint, true)
require.NoError(t.t, err, "unable to force close channel") require.NoError(t.t, err, "unable to force close channel")
// Now that the channel has been force closed, it should show // 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 const actualFeeRate = 30000
net.SetFeeEstimate(actualFeeRate) net.SetFeeEstimate(actualFeeRate)
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout) _, closingTxID, err := net.CloseChannel(alice, chanPoint, true)
_, closingTxID, err := net.CloseChannel(ctxt, alice, chanPoint, true)
if err != nil { if err != nil {
t.Fatalf("unable to execute force channel closure: %v", err) t.Fatalf("unable to execute force channel closure: %v", err)
} }

View File

@@ -454,8 +454,7 @@ func testExternalFundingChanPoint(net *lntest.NetworkHarness, t *harnessTest) {
// First, we'll try to close the channel as Carol, the initiator. This // First, we'll try to close the channel as Carol, the initiator. This
// should fail as a frozen channel only allows the responder to // should fail as a frozen channel only allows the responder to
// initiate a channel close. // initiate a channel close.
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout) _, _, err = net.CloseChannel(carol, chanPoint2, false)
_, _, err = net.CloseChannel(ctxt, carol, chanPoint2, false)
require.Error(t.t, err, require.Error(t.t, err,
"carol wasn't denied a co-op close attempt "+ "carol wasn't denied a co-op close attempt "+
"for a frozen channel", "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. // Check both nodes to ensure that the channel is ready for operation.
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) err = net.AssertChannelExists(net.Alice, &outPoint, check)
err = net.AssertChannelExists(ctxt, net.Alice, &outPoint, check)
if err != nil { if err != nil {
t.Fatalf("unable to assert channel existence: %v", err) t.Fatalf("unable to assert channel existence: %v", err)
} }
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) if err := net.AssertChannelExists(carol, &outPoint); err != nil {
if err := net.AssertChannelExists(ctxt, carol, &outPoint); err != nil {
t.Fatalf("unable to assert channel existence: %v", err) t.Fatalf("unable to assert channel existence: %v", err)
} }

View File

@@ -115,12 +115,10 @@ func testDisconnectingTargetPeer(net *lntest.NetworkHarness, t *harnessTest) {
} }
// Check both nodes to ensure that the channel is ready for operation. // Check both nodes to ensure that the channel is ready for operation.
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) if err := net.AssertChannelExists(alice, &outPoint); err != nil {
if err := net.AssertChannelExists(ctxt, alice, &outPoint); err != nil {
t.Fatalf("unable to assert channel existence: %v", err) t.Fatalf("unable to assert channel existence: %v", err)
} }
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) if err := net.AssertChannelExists(bob, &outPoint); err != nil {
if err := net.AssertChannelExists(ctxt, bob, &outPoint); err != nil {
t.Fatalf("unable to assert channel existence: %v", err) t.Fatalf("unable to assert channel existence: %v", err)
} }
@@ -573,10 +571,8 @@ func testMaxPendingChannels(net *lntest.NetworkHarness, t *harnessTest) {
Hash: *fundingTxID, Hash: *fundingTxID,
Index: fundingChanPoint.OutputIndex, Index: fundingChanPoint.OutputIndex,
} }
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) err = net.AssertChannelExists(net.Alice, &chanPoint)
if err := net.AssertChannelExists(ctxt, net.Alice, &chanPoint); err != nil { require.NoError(t.t, err, "unable to assert channel existence")
t.Fatalf("unable to assert channel existence: %v", err)
}
chanPoints[i] = fundingChanPoint chanPoints[i] = fundingChanPoint
} }
@@ -1503,14 +1499,12 @@ func testSendUpdateDisableChannel(net *lntest.NetworkHarness, t *harnessTest) {
// Close Alice's channels with Bob and Carol cooperatively and // Close Alice's channels with Bob and Carol cooperatively and
// unilaterally respectively. // unilaterally respectively.
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout) _, _, err = net.CloseChannel(net.Alice, chanPointAliceBob, false)
_, _, err = net.CloseChannel(ctxt, net.Alice, chanPointAliceBob, false)
if err != nil { if err != nil {
t.Fatalf("unable to close channel: %v", err) t.Fatalf("unable to close channel: %v", err)
} }
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout) _, _, err = net.CloseChannel(net.Alice, chanPointAliceCarol, true)
_, _, err = net.CloseChannel(ctxt, net.Alice, chanPointAliceCarol, true)
if err != nil { if err != nil {
t.Fatalf("unable to close channel: %v", err) t.Fatalf("unable to close channel: %v", err)
} }
@@ -1530,8 +1524,7 @@ func testSendUpdateDisableChannel(net *lntest.NetworkHarness, t *harnessTest) {
mineBlocks(t, net, 1, 2) mineBlocks(t, net, 1, 2)
// Also do this check for Eve's channel with Carol. // Also do this check for Eve's channel with Carol.
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout) _, _, err = net.CloseChannel(eve, chanPointEveCarol, false)
_, _, err = net.CloseChannel(ctxt, eve, chanPointEveCarol, false)
if err != nil { if err != nil {
t.Fatalf("unable to close channel: %v", err) t.Fatalf("unable to close channel: %v", err)
} }

View File

@@ -169,8 +169,9 @@ func testRevokedCloseRetribution(net *lntest.NetworkHarness, t *harnessTest) {
var closeUpdates lnrpc.Lightning_CloseChannelClient var closeUpdates lnrpc.Lightning_CloseChannelClient
force := true force := true
err = wait.Predicate(func() bool { err = wait.Predicate(func() bool {
ctxt, _ := context.WithTimeout(ctxb, channelCloseTimeout) closeUpdates, _, err = net.CloseChannel(
closeUpdates, _, err = net.CloseChannel(ctxt, net.Bob, chanPoint, force) net.Bob, chanPoint, force,
)
if err != nil { if err != nil {
predErr = err predErr = err
return false return false
@@ -404,9 +405,8 @@ func testRevokedCloseRetributionZeroValueRemoteOutput(net *lntest.NetworkHarness
force := true force := true
err = wait.Predicate(func() bool { err = wait.Predicate(func() bool {
ctxt, _ := context.WithTimeout(ctxb, channelCloseTimeout)
closeUpdates, closeTxID, closeErr = net.CloseChannel( closeUpdates, closeTxID, closeErr = net.CloseChannel(
ctxt, carol, chanPoint, force, carol, chanPoint, force,
) )
return closeErr == nil return closeErr == nil
}, defaultTimeout) }, defaultTimeout)
@@ -714,9 +714,9 @@ func testRevokedCloseRetributionRemoteHodl(net *lntest.NetworkHarness,
// commitment transaction of a prior *revoked* state, so she'll soon // commitment transaction of a prior *revoked* state, so she'll soon
// feel the wrath of Dave's retribution. // feel the wrath of Dave's retribution.
force := true force := true
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout) closeUpdates, closeTxID, err := net.CloseChannel(
closeUpdates, closeTxID, err := net.CloseChannel(ctxt, carol, carol, chanPoint, force,
chanPoint, force) )
if err != nil { if err != nil {
t.Fatalf("unable to close channel: %v", err) t.Fatalf("unable to close channel: %v", err)
} }
@@ -1154,9 +1154,7 @@ func testRevokedCloseRetributionAltruistWatchtowerCase(
// broadcasting his current channel state. This is actually the // broadcasting his current channel state. This is actually the
// commitment transaction of a prior *revoked* state, so he'll soon // commitment transaction of a prior *revoked* state, so he'll soon
// feel the wrath of Dave's retribution. // feel the wrath of Dave's retribution.
closeUpdates, closeTxID, err := net.CloseChannel( closeUpdates, closeTxID, err := net.CloseChannel(carol, chanPoint, true)
ctxb, carol, chanPoint, true,
)
if err != nil { if err != nil {
t.Fatalf("unable to close channel: %v", err) t.Fatalf("unable to close channel: %v", err)
} }

View File

@@ -856,13 +856,11 @@ func testPrivateChannels(net *lntest.NetworkHarness, t *harnessTest) {
Hash: *fundingTxID, Hash: *fundingTxID,
Index: chanPointPrivate.OutputIndex, Index: chanPointPrivate.OutputIndex,
} }
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout) err = net.AssertChannelExists(carol, &privateFundPoint)
err = net.AssertChannelExists(ctxt, carol, &privateFundPoint)
if err != nil { if err != nil {
t.Fatalf("unable to assert channel existence: %v", err) t.Fatalf("unable to assert channel existence: %v", err)
} }
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) err = net.AssertChannelExists(net.Alice, &privateFundPoint)
err = net.AssertChannelExists(ctxt, net.Alice, &privateFundPoint)
if err != nil { if err != nil {
t.Fatalf("unable to assert channel existence: %v", err) 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) time.Sleep(time.Millisecond * 50)
// Let Carol pay the invoices. // Let Carol pay the invoices.
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
err = completePaymentRequests( err = completePaymentRequests(
ctxt, carol, carol.RouterClient, payReqs, true, ctxt, carol, carol.RouterClient, payReqs, true,
) )