From 966e037d53c5b31c2e7eef453484b86b551cc55a Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Thu, 4 Aug 2022 22:28:27 +0800 Subject: [PATCH] itest: refactor `testFailingChannel` --- lntest/itest/list_on_test.go | 4 + lntest/itest/lnd_channel_force_close_test.go | 175 +++---------------- lntest/itest/lnd_test_list_on_test.go | 4 - 3 files changed, 32 insertions(+), 151 deletions(-) diff --git a/lntest/itest/list_on_test.go b/lntest/itest/list_on_test.go index b6ff5840f..c8a2d0266 100644 --- a/lntest/itest/list_on_test.go +++ b/lntest/itest/list_on_test.go @@ -215,4 +215,8 @@ var allTestCasesTemp = []*lntemp.TestCase{ Name: "channel force closure", TestFunc: testChannelForceClosure, }, + { + Name: "failing link", + TestFunc: testFailingChannel, + }, } diff --git a/lntest/itest/lnd_channel_force_close_test.go b/lntest/itest/lnd_channel_force_close_test.go index e3c4e9b17..26d425c6f 100644 --- a/lntest/itest/lnd_channel_force_close_test.go +++ b/lntest/itest/lnd_channel_force_close_test.go @@ -2,7 +2,6 @@ package itest import ( "bytes" - "context" "fmt" "testing" @@ -1036,31 +1035,23 @@ func findCommitAndAnchor(t *harnessTest, net *lntest.NetworkHarness, return commitSweep, anchorSweep } -// testFailingChannel tests that we will fail the channel by force closing ii +// testFailingChannel tests that we will fail the channel by force closing it // in the case where a counterparty tries to settle an HTLC with the wrong // preimage. -func testFailingChannel(net *lntest.NetworkHarness, t *harnessTest) { - ctxb := context.Background() - - const ( - paymentAmt = 10000 - ) +func testFailingChannel(ht *lntemp.HarnessTest) { + const paymentAmt = 10000 chanAmt := lnd.MaxFundingAmount // We'll introduce Carol, which will settle any incoming invoice with a // totally unrelated preimage. - carol := net.NewNode(t.t, "Carol", []string{"--hodl.bogus-settle"}) - defer shutdownAndAssert(net, t, carol) + carol := ht.NewNode("Carol", []string{"--hodl.bogus-settle"}) + + alice := ht.Alice + ht.ConnectNodes(alice, carol) // Let Alice connect and open a channel to Carol, - net.ConnectNodes(t.t, net.Alice, carol) - chanPoint := openChannelAndAssert( - t, net, net.Alice, carol, - lntest.OpenChannelParams{ - Amt: chanAmt, - }, - ) + ht.OpenChannel(alice, carol, lntemp.OpenChannelParams{Amt: chanAmt}) // With the channel open, we'll create a invoice for Carol that Alice // will attempt to pay. @@ -1070,159 +1061,49 @@ func testFailingChannel(net *lntest.NetworkHarness, t *harnessTest) { RPreimage: preimage, Value: paymentAmt, } - ctxt, _ := context.WithTimeout(ctxb, defaultTimeout) - resp, err := carol.AddInvoice(ctxt, invoice) - if err != nil { - t.Fatalf("unable to add invoice: %v", err) - } - carolPayReqs := []string{resp.PaymentRequest} - - // Wait for Alice to receive the channel edge from the funding manager. - err = net.Alice.WaitForNetworkChannelOpen(chanPoint) - if err != nil { - t.Fatalf("alice didn't see the alice->carol channel before "+ - "timeout: %v", err) - } + resp := carol.RPC.AddInvoice(invoice) // Send the payment from Alice to Carol. We expect Carol to attempt to // settle this payment with the wrong preimage. - err = completePaymentRequests( - net.Alice, net.Alice.RouterClient, carolPayReqs, false, - ) - if err != nil { - t.Fatalf("unable to send payments: %v", err) + // + // NOTE: cannot use `CompletePaymentRequestsNoWait` here as the channel + // will be force closed, so the num of updates check in that function + // won't work as the channel cannot be found. + req := &routerrpc.SendPaymentRequest{ + PaymentRequest: resp.PaymentRequest, + TimeoutSeconds: 60, + FeeLimitMsat: noFeeLimitMsat, } + ht.SendPaymentAndAssertStatus(alice, req, lnrpc.Payment_IN_FLIGHT) // Since Alice detects that Carol is trying to trick her by providing a // fake preimage, she should fail and force close the channel. - var predErr error - err = wait.Predicate(func() bool { - pendingChansRequest := &lnrpc.PendingChannelsRequest{} - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) - pendingChanResp, err := net.Alice.PendingChannels(ctxt, - pendingChansRequest) - if err != nil { - predErr = fmt.Errorf("unable to query for pending "+ - "channels: %v", err) - return false - } - n := len(pendingChanResp.WaitingCloseChannels) - if n != 1 { - predErr = fmt.Errorf("expected to find %d channels "+ - "waiting close, found %d", 1, n) - return false - } - return true - }, defaultTimeout) - if err != nil { - t.Fatalf("%v", predErr) - } + ht.AssertNumWaitingClose(alice, 1) // Mine a block to confirm the broadcasted commitment. - block := mineBlocks(t, net, 1, 1)[0] - if len(block.Transactions) != 2 { - t.Fatalf("transaction wasn't mined") - } + block := ht.MineBlocksAndAssertNumTxes(1, 1)[0] + require.Len(ht, block.Transactions, 2, "transaction wasn't mined") // The channel should now show up as force closed both for Alice and // Carol. - err = wait.Predicate(func() bool { - pendingChansRequest := &lnrpc.PendingChannelsRequest{} - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) - pendingChanResp, err := net.Alice.PendingChannels(ctxt, - pendingChansRequest) - if err != nil { - predErr = fmt.Errorf("unable to query for pending "+ - "channels: %v", err) - return false - } - n := len(pendingChanResp.WaitingCloseChannels) - if n != 0 { - predErr = fmt.Errorf("expected to find %d channels "+ - "waiting close, found %d", 0, n) - return false - } - n = len(pendingChanResp.PendingForceClosingChannels) - if n != 1 { - predErr = fmt.Errorf("expected to find %d channel "+ - "pending force close, found %d", 1, n) - return false - } - return true - }, defaultTimeout) - if err != nil { - t.Fatalf("%v", predErr) - } - - err = wait.Predicate(func() bool { - pendingChansRequest := &lnrpc.PendingChannelsRequest{} - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) - pendingChanResp, err := carol.PendingChannels(ctxt, - pendingChansRequest) - if err != nil { - predErr = fmt.Errorf("unable to query for pending "+ - "channels: %v", err) - return false - } - n := len(pendingChanResp.PendingForceClosingChannels) - if n != 1 { - predErr = fmt.Errorf("expected to find %d channel "+ - "pending force close, found %d", 1, n) - return false - } - return true - }, defaultTimeout) - if err != nil { - t.Fatalf("%v", predErr) - } + ht.AssertNumPendingForceClose(alice, 1) + ht.AssertNumPendingForceClose(carol, 1) // Carol will use the correct preimage to resolve the HTLC on-chain. - _, err = waitForTxInMempool(net.Miner.Client, minerMempoolTimeout) - if err != nil { - t.Fatalf("unable to find Carol's resolve tx in mempool: %v", err) - } + ht.Miner.AssertNumTxsInMempool(1) // Mine enough blocks for Alice to sweep her funds from the force // closed channel. - _, err = net.Miner.Client.Generate(defaultCSV - 1) - if err != nil { - t.Fatalf("unable to generate blocks: %v", err) - } + ht.MineBlocks(defaultCSV - 1) // Wait for the sweeping tx to be broadcast. - _, err = waitForTxInMempool(net.Miner.Client, minerMempoolTimeout) - if err != nil { - t.Fatalf("unable to find Alice's sweep tx in mempool: %v", err) - } + ht.Miner.AssertNumTxsInMempool(1) // Mine the sweep. - _, err = net.Miner.Client.Generate(1) - if err != nil { - t.Fatalf("unable to generate blocks: %v", err) - } + ht.MineBlocks(1) // No pending channels should be left. - err = wait.Predicate(func() bool { - pendingChansRequest := &lnrpc.PendingChannelsRequest{} - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) - pendingChanResp, err := net.Alice.PendingChannels(ctxt, - pendingChansRequest) - if err != nil { - predErr = fmt.Errorf("unable to query for pending "+ - "channels: %v", err) - return false - } - n := len(pendingChanResp.PendingForceClosingChannels) - if n != 0 { - predErr = fmt.Errorf("expected to find %d channel "+ - "pending force close, found %d", 0, n) - return false - } - return true - }, defaultTimeout) - if err != nil { - t.Fatalf("%v", predErr) - } + ht.AssertNumPendingForceClose(alice, 0) } // assertReports checks that the count of resolutions we have present per diff --git a/lntest/itest/lnd_test_list_on_test.go b/lntest/itest/lnd_test_list_on_test.go index df7c87cb4..c2d0ace9f 100644 --- a/lntest/itest/lnd_test_list_on_test.go +++ b/lntest/itest/lnd_test_list_on_test.go @@ -90,10 +90,6 @@ var allTestCases = []*testCase{ name: "revoked uncooperative close retribution", test: testRevokedCloseRetribution, }, - { - name: "failing link", - test: testFailingChannel, - }, { name: "revoked uncooperative close retribution zero value remote output", test: testRevokedCloseRetributionZeroValueRemoteOutput,