diff --git a/lntest/harness.go b/lntest/harness.go index 16a40821..9029c130 100644 --- a/lntest/harness.go +++ b/lntest/harness.go @@ -723,7 +723,11 @@ func (n *NetworkHarness) ConnectNodes(t *testing.T, a, b *HarnessNode) { // DisconnectNodes disconnects node a from node b by sending RPC message // from a node to b node -func (n *NetworkHarness) DisconnectNodes(ctx context.Context, a, b *HarnessNode) error { +func (n *NetworkHarness) DisconnectNodes(a, b *HarnessNode) error { + ctxb := context.Background() + ctx, cancel := context.WithTimeout(ctxb, DefaultTimeout) + defer cancel() + bobInfo, err := b.GetInfo(ctx, &lnrpc.GetInfoRequest{}) if err != nil { return err @@ -988,9 +992,12 @@ type OpenChannelParams struct { // if the timeout is reached before the channel pending notification is // received, an error is returned. The confirmed boolean determines whether we // should fund the channel with confirmed outputs or not. -func (n *NetworkHarness) OpenChannel(ctx context.Context, - srcNode, destNode *HarnessNode, p OpenChannelParams) ( - lnrpc.Lightning_OpenChannelClient, error) { +func (n *NetworkHarness) OpenChannel(srcNode, destNode *HarnessNode, + p OpenChannelParams) (lnrpc.Lightning_OpenChannelClient, error) { + + ctxb := context.Background() + ctx, cancel := context.WithTimeout(ctxb, ChannelOpenTimeout) + defer cancel() // Wait until srcNode and destNode have the latest chain synced. // Otherwise, we may run into a check within the funding manager that @@ -1062,10 +1069,14 @@ func (n *NetworkHarness) OpenChannel(ctx context.Context, // passed channel funding parameters. If the passed context has a timeout, then // if the timeout is reached before the channel pending notification is // received, an error is returned. -func (n *NetworkHarness) OpenPendingChannel(ctx context.Context, - srcNode, destNode *HarnessNode, amt btcutil.Amount, +func (n *NetworkHarness) OpenPendingChannel(srcNode, destNode *HarnessNode, + amt btcutil.Amount, pushAmt btcutil.Amount) (*lnrpc.PendingUpdate, error) { + ctxb := context.Background() + ctx, cancel := context.WithTimeout(ctxb, ChannelOpenTimeout) + defer cancel() + // Wait until srcNode and destNode have blockchain synced if err := srcNode.WaitForBlockchainSync(ctx); err != nil { return nil, fmt.Errorf("unable to sync srcNode chain: %v", err) diff --git a/lntest/itest/assertions.go b/lntest/itest/assertions.go index c51e41dc..dbfc0249 100644 --- a/lntest/itest/assertions.go +++ b/lntest/itest/assertions.go @@ -43,17 +43,13 @@ func openChannelStream(t *harnessTest, net *lntest.NetworkHarness, t.t.Helper() - ctxb := context.Background() - ctx, cancel := context.WithTimeout(ctxb, channelOpenTimeout) - defer cancel() - // Wait until we are able to fund a channel successfully. This wait // prevents us from erroring out when trying to create a channel while // the node is starting up. var chanOpenUpdate lnrpc.Lightning_OpenChannelClient err := wait.NoError(func() error { var err error - chanOpenUpdate, err = net.OpenChannel(ctx, alice, bob, p) + chanOpenUpdate, err = net.OpenChannel(alice, bob, p) return err }, defaultTimeout) require.NoError(t.t, err, "unable to open channel") diff --git a/lntest/itest/lnd_channel_backup_test.go b/lntest/itest/lnd_channel_backup_test.go index 95e805df..9ac87a8b 100644 --- a/lntest/itest/lnd_channel_backup_test.go +++ b/lntest/itest/lnd_channel_backup_test.go @@ -902,9 +902,8 @@ func testChanRestoreScenario(t *harnessTest, net *lntest.NetworkHarness, var chanPoint *lnrpc.ChannelPoint switch { case testCase.unconfirmed: - ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout) _, err := net.OpenPendingChannel( - ctxt, from, to, chanAmt, pushAmt, + from, to, chanAmt, pushAmt, ) if err != nil { t.Fatalf("couldn't open pending channel: %v", err) diff --git a/lntest/itest/lnd_channel_graph_test.go b/lntest/itest/lnd_channel_graph_test.go index a06cb78f..f357696b 100644 --- a/lntest/itest/lnd_channel_graph_test.go +++ b/lntest/itest/lnd_channel_graph_test.go @@ -190,8 +190,7 @@ func testUpdateChanStatus(net *lntest.NetworkHarness, t *harnessTest) { // disconnections from automatically disabling the channel again // (we don't want to clutter the network with channels that are // falsely advertised as enabled when they don't work). - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) - if err := net.DisconnectNodes(ctxt, alice, bob); err != nil { + if err := net.DisconnectNodes(alice, bob); err != nil { t.Fatalf("unable to disconnect Alice from Bob: %v", err) } expectedPolicy.Disabled = true @@ -230,8 +229,7 @@ func testUpdateChanStatus(net *lntest.NetworkHarness, t *harnessTest) { }, ) - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) - if err := net.DisconnectNodes(ctxt, alice, bob); err != nil { + if err := net.DisconnectNodes(alice, bob); err != nil { t.Fatalf("unable to disconnect Alice from Bob: %v", err) } @@ -261,8 +259,7 @@ func testUpdateChanStatus(net *lntest.NetworkHarness, t *harnessTest) { // note the asymmetry between manual enable and manual disable! assertEdgeDisabled(alice, chanPoint, true) - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) - if err := net.DisconnectNodes(ctxt, alice, bob); err != nil { + if err := net.DisconnectNodes(alice, bob); err != nil { t.Fatalf("unable to disconnect Alice from Bob: %v", err) } @@ -583,8 +580,7 @@ out: // that a node that does not have any channels open is ignored, so first // we disconnect Alice and Bob, open a channel between Bob and Carol, // and finally connect Alice to Bob again. - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) - if err := net.DisconnectNodes(ctxt, alice, bob); err != nil { + if err := net.DisconnectNodes(alice, bob); err != nil { t.Fatalf("unable to disconnect alice and bob: %v", err) } carol := net.NewNode(t.t, "Carol", nil) diff --git a/lntest/itest/lnd_funding_test.go b/lntest/itest/lnd_funding_test.go index e1616ef2..a5a5fd93 100644 --- a/lntest/itest/lnd_funding_test.go +++ b/lntest/itest/lnd_funding_test.go @@ -512,9 +512,9 @@ func testChannelFundingPersistence(net *lntest.NetworkHarness, t *harnessTest) { // Create a new channel that requires 5 confs before it's considered // open, then broadcast the funding transaction - ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout) - pendingUpdate, err := net.OpenPendingChannel(ctxt, net.Alice, carol, - chanAmt, pushAmt) + pendingUpdate, err := net.OpenPendingChannel( + net.Alice, carol, chanAmt, pushAmt, + ) if err != nil { t.Fatalf("unable to open channel: %v", err) } @@ -572,6 +572,7 @@ func testChannelFundingPersistence(net *lntest.NetworkHarness, t *harnessTest) { // Assert that our wallet has our opening transaction with a label // that does not have a channel ID set yet, because we have not // reached our required confirmations. + ctxt, _ := context.WithTimeout(ctxb, defaultTimeout) tx := findTxAtHeight(ctxt, t, height, fundingTxStr, net.Alice) // At this stage, we expect the transaction to be labelled, but not with diff --git a/lntest/itest/lnd_max_channel_size_test.go b/lntest/itest/lnd_max_channel_size_test.go index 4491e61c..f2612639 100644 --- a/lntest/itest/lnd_max_channel_size_test.go +++ b/lntest/itest/lnd_max_channel_size_test.go @@ -3,7 +3,6 @@ package itest import ( - "context" "fmt" "strings" @@ -29,7 +28,6 @@ func testMaxChannelSize(net *lntest.NetworkHarness, t *harnessTest) { defer shutdownAndAssert(net, t, wumboNode2) // We'll send 11 BTC to the wumbo node so it can test the wumbo soft limit. - ctxb := context.Background() net.SendCoins(t.t, 11*btcutil.SatoshiPerBitcoin, wumboNode) // Next we'll connect both nodes, then attempt to make a wumbo channel @@ -39,7 +37,7 @@ func testMaxChannelSize(net *lntest.NetworkHarness, t *harnessTest) { chanAmt := funding.MaxBtcFundingAmountWumbo + 1 _, err := net.OpenChannel( - ctxb, wumboNode, wumboNode2, lntest.OpenChannelParams{ + wumboNode, wumboNode2, lntest.OpenChannelParams{ Amt: chanAmt, }, ) @@ -61,7 +59,7 @@ func testMaxChannelSize(net *lntest.NetworkHarness, t *harnessTest) { net.EnsureConnected(t.t, wumboNode, miniNode) _, err = net.OpenChannel( - ctxb, wumboNode, miniNode, lntest.OpenChannelParams{ + wumboNode, miniNode, lntest.OpenChannelParams{ Amt: chanAmt, }, ) diff --git a/lntest/itest/lnd_misc_test.go b/lntest/itest/lnd_misc_test.go index c76a01c4..bca8ca38 100644 --- a/lntest/itest/lnd_misc_test.go +++ b/lntest/itest/lnd_misc_test.go @@ -31,8 +31,6 @@ import ( // Bob-peer and then re-connects them again. We expect Alice to be able to // disconnect at any point. func testDisconnectingTargetPeer(net *lntest.NetworkHarness, t *harnessTest) { - ctxb := context.Background() - // We'll start both nodes with a high backoff so that they don't // reconnect automatically during our test. args := []string{ @@ -61,9 +59,8 @@ func testDisconnectingTargetPeer(net *lntest.NetworkHarness, t *harnessTest) { // Create a new channel that requires 1 confs before it's considered // open, then broadcast the funding transaction const numConfs = 1 - ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout) pendingUpdate, err := net.OpenPendingChannel( - ctxt, alice, bob, chanAmt, pushAmt, + alice, bob, chanAmt, pushAmt, ) if err != nil { t.Fatalf("unable to open channel: %v", err) @@ -76,7 +73,7 @@ func testDisconnectingTargetPeer(net *lntest.NetworkHarness, t *harnessTest) { // Disconnect Alice-peer from Bob-peer and get error causes by one // pending channel with detach node is existing. - if err := net.DisconnectNodes(ctxt, alice, bob); err != nil { + if err := net.DisconnectNodes(alice, bob); err != nil { t.Fatalf("Bob's peer was disconnected from Alice's"+ " while one pending channel is existing: err %v", err) } @@ -124,7 +121,7 @@ func testDisconnectingTargetPeer(net *lntest.NetworkHarness, t *harnessTest) { // Disconnect Alice-peer from Bob-peer and get error causes by one // active channel with detach node is existing. - if err := net.DisconnectNodes(ctxt, alice, bob); err != nil { + if err := net.DisconnectNodes(alice, bob); err != nil { t.Fatalf("Bob's peer was disconnected from Alice's"+ " while one active channel is existing: err %v", err) } @@ -149,7 +146,7 @@ func testDisconnectingTargetPeer(net *lntest.NetworkHarness, t *harnessTest) { // Disconnect Alice-peer from Bob-peer without getting error about // existing channels. - if err := net.DisconnectNodes(ctxt, alice, bob); err != nil { + if err := net.DisconnectNodes(alice, bob); err != nil { t.Fatalf("unable to disconnect Bob's peer from Alice's: err %v", err) } @@ -517,10 +514,8 @@ func testMaxPendingChannels(net *lntest.NetworkHarness, t *harnessTest) { // Carol exhausted available amount of pending channels, next open // channel request should cause ErrorGeneric to be sent back to Alice. - ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout) _, err := net.OpenChannel( - ctxt, net.Alice, carol, - lntest.OpenChannelParams{ + net.Alice, carol, lntest.OpenChannelParams{ Amt: amount, }, ) @@ -558,7 +553,7 @@ func testMaxPendingChannels(net *lntest.NetworkHarness, t *harnessTest) { // Ensure that the funding transaction enters a block, and is // properly advertised by Alice. assertTxInBlock(t, block, fundingTxID) - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) + ctxt, _ := context.WithTimeout(ctxb, defaultTimeout) err = net.Alice.WaitForNetworkChannelOpen(ctxt, fundingChanPoint) if err != nil { t.Fatalf("channel not seen on network before "+ @@ -1452,8 +1447,7 @@ func testSendUpdateDisableChannel(net *lntest.NetworkHarness, t *harnessTest) { // Now we'll test a long disconnection. Disconnect Carol and Eve and // ensure they both detect each other as disabled. Their min backoffs // are high enough to not interfere with disabling logic. - ctxt, _ := context.WithTimeout(ctxb, defaultTimeout) - if err := net.DisconnectNodes(ctxt, carol, eve); err != nil { + if err := net.DisconnectNodes(carol, eve); err != nil { t.Fatalf("unable to disconnect Carol from Eve: %v", err) } @@ -1486,8 +1480,7 @@ func testSendUpdateDisableChannel(net *lntest.NetworkHarness, t *harnessTest) { // should allow for the disconnect to be detected, but still leave time // to cancel the announcement before the 3 second inactive timeout is // hit. - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) - if err := net.DisconnectNodes(ctxt, carol, eve); err != nil { + if err := net.DisconnectNodes(carol, eve); err != nil { t.Fatalf("unable to disconnect Carol from Eve: %v", err) } time.Sleep(time.Second) diff --git a/lntest/itest/lnd_onchain_test.go b/lntest/itest/lnd_onchain_test.go index cc11c91e..ee15730a 100644 --- a/lntest/itest/lnd_onchain_test.go +++ b/lntest/itest/lnd_onchain_test.go @@ -189,10 +189,8 @@ func testAnchorReservedValue(net *lntest.NetworkHarness, t *harnessTest) { // wallet, without a change output. This should not be allowed. resErr := lnwallet.ErrReservedValueInvalidated.Error() - ctxt, _ := context.WithTimeout(context.Background(), defaultTimeout) _, err := net.OpenChannel( - ctxt, alice, bob, - lntest.OpenChannelParams{ + alice, bob, lntest.OpenChannelParams{ Amt: chanAmt, }, ) @@ -232,7 +230,7 @@ func testAnchorReservedValue(net *lntest.NetworkHarness, t *harnessTest) { aliceChanPoint1, aliceChanPoint2, aliceChanPoint3, } for _, chanPoint := range chanPoints { - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) + ctxt, _ := context.WithTimeout(ctxb, defaultTimeout) err = alice.WaitForNetworkChannelOpen(ctxt, chanPoint) require.NoError(t.t, err) @@ -247,7 +245,7 @@ func testAnchorReservedValue(net *lntest.NetworkHarness, t *harnessTest) { addrReq := &lnrpc.NewAddressRequest{ Type: lnrpc.AddressType_WITNESS_PUBKEY_HASH, } - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) + ctxt, _ := context.WithTimeout(ctxb, defaultTimeout) resp, err := alice.NewAddress(ctxt, addrReq) require.NoError(t.t, err) diff --git a/lntest/itest/lnd_open_channel_test.go b/lntest/itest/lnd_open_channel_test.go index 7287749e..0271a431 100644 --- a/lntest/itest/lnd_open_channel_test.go +++ b/lntest/itest/lnd_open_channel_test.go @@ -82,9 +82,9 @@ func testOpenChannelAfterReorg(net *lntest.NetworkHarness, t *harnessTest) { // open, then broadcast the funding transaction chanAmt := funding.MaxBtcFundingAmount pushAmt := btcutil.Amount(0) - ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout) - pendingUpdate, err := net.OpenPendingChannel(ctxt, net.Alice, net.Bob, - chanAmt, pushAmt) + pendingUpdate, err := net.OpenPendingChannel( + net.Alice, net.Bob, chanAmt, pushAmt, + ) if err != nil { t.Fatalf("unable to open channel: %v", err) } @@ -125,7 +125,7 @@ func testOpenChannelAfterReorg(net *lntest.NetworkHarness, t *harnessTest) { if err != nil { t.Fatalf("unable to get current blockheight %v", err) } - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) + ctxt, _ := context.WithTimeout(ctxb, defaultTimeout) err = waitForNodeBlockHeight(ctxt, net.Alice, minerHeight) if err != nil { t.Fatalf("unable to sync to chain: %v", err) diff --git a/lntest/itest/lnd_payment_test.go b/lntest/itest/lnd_payment_test.go index 358636bb..b437fb4f 100644 --- a/lntest/itest/lnd_payment_test.go +++ b/lntest/itest/lnd_payment_test.go @@ -171,10 +171,8 @@ func testPaymentFollowingChannelOpen(net *lntest.NetworkHarness, t *harnessTest) channelCapacity := paymentAmt * 1000 // We first establish a channel between Alice and Bob. - ctxt, cancel := context.WithTimeout(ctxb, channelOpenTimeout) - defer cancel() pendingUpdate, err := net.OpenPendingChannel( - ctxt, net.Alice, net.Bob, channelCapacity, 0, + net.Alice, net.Bob, channelCapacity, 0, ) if err != nil { t.Fatalf("unable to open channel: %v", err) @@ -221,7 +219,7 @@ func testPaymentFollowingChannelOpen(net *lntest.NetworkHarness, t *harnessTest) // At this point we want to make sure the channel is opened and not // pending. - ctxt, cancel = context.WithTimeout(ctxb, defaultTimeout) + ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout) defer cancel() res, err := net.Bob.ListChannels(ctxt, &lnrpc.ListChannelsRequest{}) if err != nil { diff --git a/lntest/itest/lnd_switch_test.go b/lntest/itest/lnd_switch_test.go index a3b411a6..3361a7c9 100644 --- a/lntest/itest/lnd_switch_test.go +++ b/lntest/itest/lnd_switch_test.go @@ -464,8 +464,7 @@ func testSwitchOfflineDelivery(net *lntest.NetworkHarness, t *harnessTest) { } // First, disconnect Dave and Alice so that their link is broken. - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) - if err := net.DisconnectNodes(ctxt, dave, net.Alice); err != nil { + if err := net.DisconnectNodes(dave, net.Alice); err != nil { t.Fatalf("unable to disconnect alice from dave: %v", err) } @@ -485,8 +484,7 @@ func testSwitchOfflineDelivery(net *lntest.NetworkHarness, t *harnessTest) { // Now, disconnect Dave from Alice again before settling back the // payment. - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) - if err := net.DisconnectNodes(ctxt, dave, net.Alice); err != nil { + if err := net.DisconnectNodes(dave, net.Alice); err != nil { t.Fatalf("unable to disconnect alice from dave: %v", err) } diff --git a/lntest/itest/lnd_wumbo_channels_test.go b/lntest/itest/lnd_wumbo_channels_test.go index 5284db56..a8fc626b 100644 --- a/lntest/itest/lnd_wumbo_channels_test.go +++ b/lntest/itest/lnd_wumbo_channels_test.go @@ -1,7 +1,6 @@ package itest import ( - "context" "strings" "github.com/btcsuite/btcutil" @@ -28,7 +27,6 @@ func testWumboChannels(net *lntest.NetworkHarness, t *harnessTest) { // We'll send coins to the wumbo node, as it'll be the one imitating // the channel funding. - ctxb := context.Background() net.SendCoins(t.t, btcutil.SatoshiPerBitcoin, wumboNode) // Next we'll connect both nodes, then attempt to make a wumbo channel @@ -38,7 +36,7 @@ func testWumboChannels(net *lntest.NetworkHarness, t *harnessTest) { chanAmt := funding.MaxBtcFundingAmount + 1 _, err := net.OpenChannel( - ctxb, wumboNode, miniNode, lntest.OpenChannelParams{ + wumboNode, miniNode, lntest.OpenChannelParams{ Amt: chanAmt, }, ) diff --git a/lntest/itest/test_harness.go b/lntest/itest/test_harness.go index cf14a3ff..905eb135 100644 --- a/lntest/itest/test_harness.go +++ b/lntest/itest/test_harness.go @@ -40,7 +40,6 @@ const ( defaultTimeout = lntest.DefaultTimeout minerMempoolTimeout = lntest.MinerMempoolTimeout channelCloseTimeout = lntest.ChannelCloseTimeout - channelOpenTimeout = lntest.ChannelOpenTimeout itestLndBinary = "../../lnd-itest" anchorSize = 330 noFeeLimitMsat = math.MaxInt64