itest: refactor testFailingChannel

This commit is contained in:
yyforyongyu 2022-08-04 22:28:27 +08:00
parent 008ef964fc
commit 966e037d53
No known key found for this signature in database
GPG key ID: 9BCD95C4FF296868
3 changed files with 32 additions and 151 deletions

View file

@ -215,4 +215,8 @@ var allTestCasesTemp = []*lntemp.TestCase{
Name: "channel force closure",
TestFunc: testChannelForceClosure,
},
{
Name: "failing link",
TestFunc: testFailingChannel,
},
}

View file

@ -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

View file

@ -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,