mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-02-22 22:25:24 +01:00
itest: refactor testFailingChannel
This commit is contained in:
parent
008ef964fc
commit
966e037d53
3 changed files with 32 additions and 151 deletions
|
@ -215,4 +215,8 @@ var allTestCasesTemp = []*lntemp.TestCase{
|
||||||
Name: "channel force closure",
|
Name: "channel force closure",
|
||||||
TestFunc: testChannelForceClosure,
|
TestFunc: testChannelForceClosure,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "failing link",
|
||||||
|
TestFunc: testFailingChannel,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package itest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -1036,31 +1035,23 @@ func findCommitAndAnchor(t *harnessTest, net *lntest.NetworkHarness,
|
||||||
return commitSweep, anchorSweep
|
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
|
// in the case where a counterparty tries to settle an HTLC with the wrong
|
||||||
// preimage.
|
// preimage.
|
||||||
func testFailingChannel(net *lntest.NetworkHarness, t *harnessTest) {
|
func testFailingChannel(ht *lntemp.HarnessTest) {
|
||||||
ctxb := context.Background()
|
const paymentAmt = 10000
|
||||||
|
|
||||||
const (
|
|
||||||
paymentAmt = 10000
|
|
||||||
)
|
|
||||||
|
|
||||||
chanAmt := lnd.MaxFundingAmount
|
chanAmt := lnd.MaxFundingAmount
|
||||||
|
|
||||||
// We'll introduce Carol, which will settle any incoming invoice with a
|
// We'll introduce Carol, which will settle any incoming invoice with a
|
||||||
// totally unrelated preimage.
|
// totally unrelated preimage.
|
||||||
carol := net.NewNode(t.t, "Carol", []string{"--hodl.bogus-settle"})
|
carol := ht.NewNode("Carol", []string{"--hodl.bogus-settle"})
|
||||||
defer shutdownAndAssert(net, t, carol)
|
|
||||||
|
alice := ht.Alice
|
||||||
|
ht.ConnectNodes(alice, carol)
|
||||||
|
|
||||||
// Let Alice connect and open a channel to Carol,
|
// Let Alice connect and open a channel to Carol,
|
||||||
net.ConnectNodes(t.t, net.Alice, carol)
|
ht.OpenChannel(alice, carol, lntemp.OpenChannelParams{Amt: chanAmt})
|
||||||
chanPoint := openChannelAndAssert(
|
|
||||||
t, net, net.Alice, carol,
|
|
||||||
lntest.OpenChannelParams{
|
|
||||||
Amt: chanAmt,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
// With the channel open, we'll create a invoice for Carol that Alice
|
// With the channel open, we'll create a invoice for Carol that Alice
|
||||||
// will attempt to pay.
|
// will attempt to pay.
|
||||||
|
@ -1070,159 +1061,49 @@ func testFailingChannel(net *lntest.NetworkHarness, t *harnessTest) {
|
||||||
RPreimage: preimage,
|
RPreimage: preimage,
|
||||||
Value: paymentAmt,
|
Value: paymentAmt,
|
||||||
}
|
}
|
||||||
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
resp := carol.RPC.AddInvoice(invoice)
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send the payment from Alice to Carol. We expect Carol to attempt to
|
// Send the payment from Alice to Carol. We expect Carol to attempt to
|
||||||
// settle this payment with the wrong preimage.
|
// settle this payment with the wrong preimage.
|
||||||
err = completePaymentRequests(
|
//
|
||||||
net.Alice, net.Alice.RouterClient, carolPayReqs, false,
|
// NOTE: cannot use `CompletePaymentRequestsNoWait` here as the channel
|
||||||
)
|
// will be force closed, so the num of updates check in that function
|
||||||
if err != nil {
|
// won't work as the channel cannot be found.
|
||||||
t.Fatalf("unable to send payments: %v", err)
|
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
|
// Since Alice detects that Carol is trying to trick her by providing a
|
||||||
// fake preimage, she should fail and force close the channel.
|
// fake preimage, she should fail and force close the channel.
|
||||||
var predErr error
|
ht.AssertNumWaitingClose(alice, 1)
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mine a block to confirm the broadcasted commitment.
|
// Mine a block to confirm the broadcasted commitment.
|
||||||
block := mineBlocks(t, net, 1, 1)[0]
|
block := ht.MineBlocksAndAssertNumTxes(1, 1)[0]
|
||||||
if len(block.Transactions) != 2 {
|
require.Len(ht, block.Transactions, 2, "transaction wasn't mined")
|
||||||
t.Fatalf("transaction wasn't mined")
|
|
||||||
}
|
|
||||||
|
|
||||||
// The channel should now show up as force closed both for Alice and
|
// The channel should now show up as force closed both for Alice and
|
||||||
// Carol.
|
// Carol.
|
||||||
err = wait.Predicate(func() bool {
|
ht.AssertNumPendingForceClose(alice, 1)
|
||||||
pendingChansRequest := &lnrpc.PendingChannelsRequest{}
|
ht.AssertNumPendingForceClose(carol, 1)
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Carol will use the correct preimage to resolve the HTLC on-chain.
|
// Carol will use the correct preimage to resolve the HTLC on-chain.
|
||||||
_, err = waitForTxInMempool(net.Miner.Client, minerMempoolTimeout)
|
ht.Miner.AssertNumTxsInMempool(1)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to find Carol's resolve tx in mempool: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mine enough blocks for Alice to sweep her funds from the force
|
// Mine enough blocks for Alice to sweep her funds from the force
|
||||||
// closed channel.
|
// closed channel.
|
||||||
_, err = net.Miner.Client.Generate(defaultCSV - 1)
|
ht.MineBlocks(defaultCSV - 1)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to generate blocks: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait for the sweeping tx to be broadcast.
|
// Wait for the sweeping tx to be broadcast.
|
||||||
_, err = waitForTxInMempool(net.Miner.Client, minerMempoolTimeout)
|
ht.Miner.AssertNumTxsInMempool(1)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to find Alice's sweep tx in mempool: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mine the sweep.
|
// Mine the sweep.
|
||||||
_, err = net.Miner.Client.Generate(1)
|
ht.MineBlocks(1)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to generate blocks: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// No pending channels should be left.
|
// No pending channels should be left.
|
||||||
err = wait.Predicate(func() bool {
|
ht.AssertNumPendingForceClose(alice, 0)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// assertReports checks that the count of resolutions we have present per
|
// assertReports checks that the count of resolutions we have present per
|
||||||
|
|
|
@ -90,10 +90,6 @@ var allTestCases = []*testCase{
|
||||||
name: "revoked uncooperative close retribution",
|
name: "revoked uncooperative close retribution",
|
||||||
test: testRevokedCloseRetribution,
|
test: testRevokedCloseRetribution,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "failing link",
|
|
||||||
test: testFailingChannel,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: "revoked uncooperative close retribution zero value remote output",
|
name: "revoked uncooperative close retribution zero value remote output",
|
||||||
test: testRevokedCloseRetributionZeroValueRemoteOutput,
|
test: testRevokedCloseRetributionZeroValueRemoteOutput,
|
||||||
|
|
Loading…
Add table
Reference in a new issue