mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-10-10 19:43:43 +02:00
itest: refactor testWipeForwardingPackages
This commit is contained in:
@@ -365,4 +365,8 @@ var allTestCasesTemp = []*lntemp.TestCase{
|
|||||||
Name: "single hop invoice",
|
Name: "single hop invoice",
|
||||||
TestFunc: testSingleHopInvoice,
|
TestFunc: testSingleHopInvoice,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "wipe forwarding packages",
|
||||||
|
TestFunc: testWipeForwardingPackages,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
@@ -96,10 +96,6 @@ var allTestCases = []*testCase{
|
|||||||
name: "wallet import pubkey",
|
name: "wallet import pubkey",
|
||||||
test: testWalletImportPubKey,
|
test: testWalletImportPubKey,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "wipe forwarding packages",
|
|
||||||
test: testWipeForwardingPackages,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: "remote signer",
|
name: "remote signer",
|
||||||
test: testRemoteSigner,
|
test: testRemoteSigner,
|
||||||
|
@@ -1,14 +1,11 @@
|
|||||||
package itest
|
package itest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"testing"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/lightningnetwork/lnd/chainreg"
|
"github.com/lightningnetwork/lnd/chainreg"
|
||||||
"github.com/lightningnetwork/lnd/lnrpc"
|
"github.com/lightningnetwork/lnd/lnrpc"
|
||||||
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
|
"github.com/lightningnetwork/lnd/lntemp"
|
||||||
"github.com/lightningnetwork/lnd/lntest"
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -22,91 +19,9 @@ type pendingChan *lnrpc.PendingChannelsResponse_PendingChannel
|
|||||||
// - Bob force closes the channel Alice->Bob, and checks from both Bob's
|
// - Bob force closes the channel Alice->Bob, and checks from both Bob's
|
||||||
// PoV(local force close) and Alice's Pov(remote close) that the forwarding
|
// PoV(local force close) and Alice's Pov(remote close) that the forwarding
|
||||||
// packages are wiped.
|
// packages are wiped.
|
||||||
// - Bob coop closes the channel Bob->Carol, and checks from both Bob PoVs that
|
// - Bob coop closes the channel Bob->Carol, and checks from both Bob PoVs
|
||||||
// the forwarding packages are wiped.
|
// that the forwarding packages are wiped.
|
||||||
func testWipeForwardingPackages(net *lntest.NetworkHarness,
|
func testWipeForwardingPackages(ht *lntemp.HarnessTest) {
|
||||||
t *harnessTest) {
|
|
||||||
|
|
||||||
// Setup the test and get the channel points.
|
|
||||||
pointAB, pointBC, carol, cleanUp := setupFwdPkgTest(net, t)
|
|
||||||
defer cleanUp()
|
|
||||||
|
|
||||||
// Firstly, Bob force closes the channel.
|
|
||||||
_, _, err := net.CloseChannel(net.Bob, pointAB, true)
|
|
||||||
require.NoError(t.t, err, "unable to force close channel")
|
|
||||||
|
|
||||||
// Now that the channel has been force closed, it should show up in
|
|
||||||
// bob's PendingChannels RPC under the waiting close section.
|
|
||||||
pendingChan := assertWaitingCloseChannel(t.t, net.Bob)
|
|
||||||
|
|
||||||
// Check that Bob has created forwarding packages. We don't care the
|
|
||||||
// exact number here as long as these packages are deleted when the
|
|
||||||
// channel is closed.
|
|
||||||
require.NotZero(t.t, pendingChan.NumForwardingPackages)
|
|
||||||
|
|
||||||
// Mine 1 block to get the closing transaction confirmed.
|
|
||||||
_, err = net.Miner.Client.Generate(1)
|
|
||||||
require.NoError(t.t, err, "unable to mine blocks")
|
|
||||||
|
|
||||||
// Now that the closing transaction is confirmed, the above waiting
|
|
||||||
// close channel should now become pending force closed channel.
|
|
||||||
pendingChan = assertPendingForceClosedChannel(t.t, net.Bob)
|
|
||||||
|
|
||||||
// Check the forwarding packages are deleted.
|
|
||||||
require.Zero(t.t, pendingChan.NumForwardingPackages)
|
|
||||||
|
|
||||||
// For Alice, the forwarding packages should have been wiped too.
|
|
||||||
pendingChanAlice := assertPendingForceClosedChannel(t.t, net.Alice)
|
|
||||||
require.Zero(t.t, pendingChanAlice.NumForwardingPackages)
|
|
||||||
|
|
||||||
// Secondly, Bob coop closes the channel.
|
|
||||||
_, _, err = net.CloseChannel(net.Bob, pointBC, false)
|
|
||||||
require.NoError(t.t, err, "unable to coop close channel")
|
|
||||||
|
|
||||||
// Now that the channel has been coop closed, it should show up in
|
|
||||||
// bob's PendingChannels RPC under the waiting close section.
|
|
||||||
pendingChan = assertWaitingCloseChannel(t.t, net.Bob)
|
|
||||||
|
|
||||||
// Check that Bob has created forwarding packages. We don't care the
|
|
||||||
// exact number here as long as these packages are deleted when the
|
|
||||||
// channel is closed.
|
|
||||||
require.NotZero(t.t, pendingChan.NumForwardingPackages)
|
|
||||||
|
|
||||||
// Since it's a coop close, Carol should see the waiting close channel
|
|
||||||
// too.
|
|
||||||
pendingChanCarol := assertWaitingCloseChannel(t.t, carol)
|
|
||||||
require.NotZero(t.t, pendingChanCarol.NumForwardingPackages)
|
|
||||||
|
|
||||||
// Mine 1 block to get the closing transaction confirmed.
|
|
||||||
_, err = net.Miner.Client.Generate(1)
|
|
||||||
require.NoError(t.t, err, "unable to mine blocks")
|
|
||||||
|
|
||||||
// Now that the closing transaction is confirmed, the above waiting
|
|
||||||
// close channel should now become pending closed channel. Note that
|
|
||||||
// the name PendingForceClosingChannels is a bit confusing, what it
|
|
||||||
// really contains is channels whose closing tx has been broadcast.
|
|
||||||
pendingChan = assertPendingForceClosedChannel(t.t, net.Bob)
|
|
||||||
|
|
||||||
// Check the forwarding packages are deleted.
|
|
||||||
require.Zero(t.t, pendingChan.NumForwardingPackages)
|
|
||||||
|
|
||||||
// Mine a block to confirm sweep transactions such that they
|
|
||||||
// don't remain in the mempool for any subsequent tests.
|
|
||||||
_, err = net.Miner.Client.Generate(1)
|
|
||||||
require.NoError(t.t, err, "unable to mine blocks")
|
|
||||||
}
|
|
||||||
|
|
||||||
// setupFwdPkgTest prepares the wipe forwarding packages tests. It creates a
|
|
||||||
// network topology that has a channel direction: Alice -> Bob -> Carol, sends
|
|
||||||
// several payments from Alice to Carol, and returns the two channel points(one
|
|
||||||
// for Alice and Bob, the other for Bob and Carol), the node Carol, and a
|
|
||||||
// cleanup function to be used when the test finishes.
|
|
||||||
func setupFwdPkgTest(net *lntest.NetworkHarness,
|
|
||||||
t *harnessTest) (*lnrpc.ChannelPoint, *lnrpc.ChannelPoint,
|
|
||||||
*lntest.HarnessNode, func()) {
|
|
||||||
|
|
||||||
ctxb := context.Background()
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
chanAmt = 10e6
|
chanAmt = 10e6
|
||||||
paymentAmt = 10e4
|
paymentAmt = 10e4
|
||||||
@@ -114,114 +29,96 @@ func setupFwdPkgTest(net *lntest.NetworkHarness,
|
|||||||
numInvoices = 3
|
numInvoices = 3
|
||||||
)
|
)
|
||||||
|
|
||||||
// Grab Alice and Bob from harness net.
|
// Grab Alice and Bob from HarnessTest.
|
||||||
alice, bob := net.Alice, net.Bob
|
alice, bob := ht.Alice, ht.Bob
|
||||||
|
|
||||||
// Create a new node Carol, which will create invoices that require
|
// Create a new node Carol, which will create invoices that require
|
||||||
// Alice to pay.
|
// Alice to pay.
|
||||||
carol := net.NewNode(t.t, "Carol", nil)
|
carol := ht.NewNode("Carol", nil)
|
||||||
|
|
||||||
// Connect Bob to Carol.
|
// Connect Bob to Carol.
|
||||||
net.ConnectNodes(t.t, bob, carol)
|
ht.ConnectNodes(bob, carol)
|
||||||
|
|
||||||
// Open a channel between Alice and Bob.
|
// Open a channel between Alice and Bob.
|
||||||
chanPointAB := openChannelAndAssert(
|
chanPointAB := ht.OpenChannel(
|
||||||
t, net, alice, bob, lntest.OpenChannelParams{
|
alice, bob, lntemp.OpenChannelParams{Amt: chanAmt},
|
||||||
Amt: chanAmt,
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Open a channel between Bob and Carol.
|
// Open a channel between Bob and Carol.
|
||||||
chanPointBC := openChannelAndAssert(
|
chanPointBC := ht.OpenChannel(
|
||||||
t, net, bob, carol, lntest.OpenChannelParams{
|
bob, carol, lntemp.OpenChannelParams{Amt: chanAmt},
|
||||||
Amt: chanAmt,
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout)
|
// Before we continue, make sure Alice has seen the channel between Bob
|
||||||
defer cancel()
|
// and Carol.
|
||||||
|
ht.AssertTopologyChannelOpen(alice, chanPointBC)
|
||||||
|
|
||||||
// Alice sends several payments to Carol through Bob, which triggers
|
// Alice sends several payments to Carol through Bob, which triggers
|
||||||
// Bob to create forwarding packages.
|
// Bob to create forwarding packages.
|
||||||
for i := 0; i < numInvoices; i++ {
|
for i := 0; i < numInvoices; i++ {
|
||||||
// Add an invoice for Carol.
|
// Add an invoice for Carol.
|
||||||
invoice := &lnrpc.Invoice{Memo: "testing", Value: paymentAmt}
|
invoice := &lnrpc.Invoice{Memo: "testing", Value: paymentAmt}
|
||||||
invoiceResp, err := carol.AddInvoice(ctxt, invoice)
|
resp := carol.RPC.AddInvoice(invoice)
|
||||||
require.NoError(t.t, err, "unable to add invoice")
|
|
||||||
|
|
||||||
// Alice sends a payment to Carol through Bob.
|
// Alice sends a payment to Carol through Bob.
|
||||||
sendAndAssertSuccess(
|
ht.CompletePaymentRequests(alice, []string{resp.PaymentRequest})
|
||||||
t, net.Alice, &routerrpc.SendPaymentRequest{
|
|
||||||
PaymentRequest: invoiceResp.PaymentRequest,
|
|
||||||
TimeoutSeconds: 60,
|
|
||||||
FeeLimitSat: noFeeLimitMsat,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return chanPointAB, chanPointBC, carol, func() {
|
// TODO(yy): remove the sleep once the following bug is fixed.
|
||||||
shutdownAndAssert(net, t, alice)
|
// When the invoice is reported settled, the commitment dance is not
|
||||||
shutdownAndAssert(net, t, bob)
|
// yet finished, which can cause an error when closing the channel,
|
||||||
shutdownAndAssert(net, t, carol)
|
// saying there's active HTLCs. We need to investigate this issue and
|
||||||
}
|
// reverse the order to, first finish the commitment dance, then report
|
||||||
}
|
// the invoice as settled.
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
// assertWaitingCloseChannel checks there is a single channel that is waiting
|
|
||||||
// for close and returns the channel found.
|
// Firstly, Bob force closes the channel.
|
||||||
func assertWaitingCloseChannel(t *testing.T,
|
ht.CloseChannelAssertPending(bob, chanPointAB, true)
|
||||||
node *lntest.HarnessNode) pendingChan {
|
|
||||||
|
// Now that the channel has been force closed, it should show up in
|
||||||
ctxb := context.Background()
|
// bob's PendingChannels RPC under the waiting close section.
|
||||||
|
pendingAB := ht.AssertChannelWaitingClose(bob, chanPointAB).Channel
|
||||||
var channel pendingChan
|
|
||||||
require.Eventually(t, func() bool {
|
// Check that Bob has created forwarding packages. We don't care the
|
||||||
ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout)
|
// exact number here as long as these packages are deleted when the
|
||||||
defer cancel()
|
// channel is closed.
|
||||||
|
require.NotZero(ht, pendingAB.NumForwardingPackages)
|
||||||
req := &lnrpc.PendingChannelsRequest{}
|
|
||||||
resp, err := node.PendingChannels(ctxt, req)
|
// Secondly, Bob coop closes the channel.
|
||||||
|
ht.CloseChannelAssertPending(bob, chanPointBC, false)
|
||||||
// We require the RPC call to be succeeded and won't retry upon
|
|
||||||
// an error.
|
// Now that the channel has been coop closed, it should show up in
|
||||||
require.NoError(t, err, "unable to query for pending channels")
|
// bob's PendingChannels RPC under the waiting close section.
|
||||||
|
pendingBC := ht.AssertChannelWaitingClose(bob, chanPointBC).Channel
|
||||||
if err := checkNumWaitingCloseChannels(resp, 1); err != nil {
|
|
||||||
return false
|
// Check that Bob has created forwarding packages. We don't care the
|
||||||
}
|
// exact number here as long as these packages are deleted when the
|
||||||
|
// channel is closed.
|
||||||
channel = resp.WaitingCloseChannels[0].Channel
|
require.NotZero(ht, pendingBC.NumForwardingPackages)
|
||||||
return true
|
|
||||||
}, defaultTimeout, 200*time.Millisecond)
|
// Since it's a coop close, Carol should see the waiting close channel
|
||||||
|
// too.
|
||||||
return channel
|
pendingBC = ht.AssertChannelWaitingClose(carol, chanPointBC).Channel
|
||||||
}
|
require.NotZero(ht, pendingBC.NumForwardingPackages)
|
||||||
|
|
||||||
// assertForceClosedChannel checks there is a single channel that is pending
|
// Mine 1 block to get the two closing transactions confirmed.
|
||||||
// force closed and returns the channel found.
|
ht.MineBlocksAndAssertNumTxes(1, 2)
|
||||||
func assertPendingForceClosedChannel(t *testing.T,
|
|
||||||
node *lntest.HarnessNode) pendingChan {
|
// Now that the closing transaction is confirmed, the above waiting
|
||||||
|
// close channel should now become pending force closed channel.
|
||||||
ctxb := context.Background()
|
pendingAB = ht.AssertChannelPendingForceClose(bob, chanPointAB).Channel
|
||||||
|
|
||||||
var channel pendingChan
|
// Check the forwarding pacakges are deleted.
|
||||||
require.Eventually(t, func() bool {
|
require.Zero(ht, pendingAB.NumForwardingPackages)
|
||||||
ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout)
|
|
||||||
defer cancel()
|
// For Alice, the forwarding packages should have been wiped too.
|
||||||
|
pending := ht.AssertChannelPendingForceClose(alice, chanPointAB)
|
||||||
req := &lnrpc.PendingChannelsRequest{}
|
pendingAB = pending.Channel
|
||||||
resp, err := node.PendingChannels(ctxt, req)
|
require.Zero(ht, pendingAB.NumForwardingPackages)
|
||||||
|
|
||||||
// We require the RPC call to be succeeded and won't retry upon
|
// Mine 1 block to get Alice's sweeping tx confirmed.
|
||||||
// an error.
|
ht.MineBlocksAndAssertNumTxes(1, 1)
|
||||||
require.NoError(t, err, "unable to query for pending channels")
|
|
||||||
|
// Clean up the force closed channel.
|
||||||
if err := checkNumForceClosedChannels(resp, 1); err != nil {
|
ht.CleanupForceClose(bob, chanPointAB)
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
channel = resp.PendingForceClosingChannels[0].Channel
|
|
||||||
return true
|
|
||||||
}, defaultTimeout, 200*time.Millisecond)
|
|
||||||
|
|
||||||
return channel
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user