itest: refactor testChannelUnsettledBalance

This commit is contained in:
yyforyongyu 2022-08-04 20:02:07 +08:00
parent 8f472bf063
commit f7f259b92a
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868
3 changed files with 51 additions and 95 deletions

View File

@ -203,4 +203,8 @@ var allTestCasesTemp = []*lntemp.TestCase{
Name: "channel balance", Name: "channel balance",
TestFunc: testChannelBalance, TestFunc: testChannelBalance,
}, },
{
Name: "channel unsettled balance",
TestFunc: testChannelUnsettledBalance,
},
} }

View File

@ -1,17 +1,14 @@
package itest package itest
import ( import (
"context"
"fmt" "fmt"
"github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/btcutil"
"github.com/lightningnetwork/lnd/chainreg"
"github.com/lightningnetwork/lnd/funding" "github.com/lightningnetwork/lnd/funding"
"github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/routerrpc" "github.com/lightningnetwork/lnd/lnrpc/routerrpc"
"github.com/lightningnetwork/lnd/lntemp" "github.com/lightningnetwork/lnd/lntemp"
"github.com/lightningnetwork/lnd/lntemp/node" "github.com/lightningnetwork/lnd/lntemp/node"
"github.com/lightningnetwork/lnd/lntest"
"github.com/lightningnetwork/lnd/lntest/wait" "github.com/lightningnetwork/lnd/lntest/wait"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -76,13 +73,12 @@ func testChannelBalance(ht *lntemp.HarnessTest) {
// Alice will send Htlcs to Carol while she is in hodl mode. This will result // Alice will send Htlcs to Carol while she is in hodl mode. This will result
// in a build of pending Htlcs. We expect the channels unsettled balance to // in a build of pending Htlcs. We expect the channels unsettled balance to
// equal the sum of all the Pending Htlcs. // equal the sum of all the Pending Htlcs.
func testChannelUnsettledBalance(net *lntest.NetworkHarness, t *harnessTest) { func testChannelUnsettledBalance(ht *lntemp.HarnessTest) {
const chanAmt = btcutil.Amount(1000000) const chanAmt = btcutil.Amount(1000000)
ctxb := context.Background()
// Creates a helper closure to be used below which asserts the proper // Creates a helper closure to be used below which asserts the proper
// response to a channel balance RPC. // response to a channel balance RPC.
checkChannelBalance := func(node *lntest.HarnessNode, checkChannelBalance := func(node *node.HarnessNode,
local, remote, unsettledLocal, unsettledRemote btcutil.Amount) { local, remote, unsettledLocal, unsettledRemote btcutil.Amount) {
expectedResponse := &lnrpc.ChannelBalanceResponse{ expectedResponse := &lnrpc.ChannelBalanceResponse{
@ -115,44 +111,25 @@ func testChannelUnsettledBalance(net *lntest.NetworkHarness, t *harnessTest) {
// Deprecated fields. // Deprecated fields.
Balance: int64(local), Balance: int64(local),
} }
assertChannelBalanceResp(t, node, expectedResponse) ht.AssertChannelBalanceResp(node, expectedResponse)
} }
// Create carol in hodl mode. // Create carol in hodl mode.
carol := net.NewNode(t.t, "Carol", []string{"--hodl.exit-settle"}) carol := ht.NewNode("Carol", []string{"--hodl.exit-settle"})
defer shutdownAndAssert(net, t, carol)
// Connect Alice to Carol. // Connect Alice to Carol.
net.ConnectNodes(t.t, net.Alice, carol) alice := ht.Alice
ht.ConnectNodes(alice, carol)
// Open a channel between Alice and Carol. // Open a channel between Alice and Carol.
chanPointAlice := openChannelAndAssert( chanPointAlice := ht.OpenChannel(
t, net, net.Alice, carol, alice, carol, lntemp.OpenChannelParams{Amt: chanAmt},
lntest.OpenChannelParams{
Amt: chanAmt,
},
) )
cType := ht.GetChannelCommitType(alice, chanPointAlice)
// Wait for Alice and Carol to receive the channel edge from the
// funding manager.
err := net.Alice.WaitForNetworkChannelOpen(chanPointAlice)
if err != nil {
t.Fatalf("alice didn't see the alice->carol channel before "+
"timeout: %v", err)
}
err = carol.WaitForNetworkChannelOpen(chanPointAlice)
if err != nil {
t.Fatalf("alice didn't see the alice->carol channel before "+
"timeout: %v", err)
}
cType, err := channelCommitType(net.Alice, chanPointAlice)
require.NoError(t.t, err, "unable to get channel type")
// Check alice's channel balance, which should have zero remote and zero // Check alice's channel balance, which should have zero remote and zero
// pending balance. // pending balance.
checkChannelBalance(net.Alice, chanAmt-calcStaticFee(cType, 0), 0, 0, 0) checkChannelBalance(alice, chanAmt-calcStaticFee(cType, 0), 0, 0, 0)
// Check carol's channel balance, which should have zero local and zero // Check carol's channel balance, which should have zero local and zero
// pending balance. // pending balance.
@ -165,79 +142,61 @@ func testChannelUnsettledBalance(net *lntest.NetworkHarness, t *harnessTest) {
) )
// Simulateneously send numInvoices payments from Alice to Carol. // Simulateneously send numInvoices payments from Alice to Carol.
carolPubKey := carol.PubKey[:]
errChan := make(chan error)
for i := 0; i < numInvoices; i++ { for i := 0; i < numInvoices; i++ {
go func() { go func() {
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout) req := &routerrpc.SendPaymentRequest{
_, err := net.Alice.RouterClient.SendPaymentV2(ctxt, Dest: carol.PubKey[:],
&routerrpc.SendPaymentRequest{ Amt: int64(payAmt),
Dest: carolPubKey, PaymentHash: ht.Random32Bytes(),
Amt: int64(payAmt), FinalCltvDelta: finalCltvDelta,
PaymentHash: makeFakePayHash(t), TimeoutSeconds: 60,
FinalCltvDelta: chainreg.DefaultBitcoinTimeLockDelta, FeeLimitMsat: noFeeLimitMsat,
TimeoutSeconds: 60,
FeeLimitMsat: noFeeLimitMsat,
})
if err != nil {
errChan <- err
} }
alice.RPC.SendPayment(req)
}() }()
} }
// There should be a number of PendingHtlcs equal
// to the amount of Invoices sent.
ht.AssertNumActiveHtlcs(alice, numInvoices)
ht.AssertNumActiveHtlcs(carol, numInvoices)
// Set the amount expected for the Unsettled Balance for this channel.
expectedBalance := numInvoices * payAmt
// Test that the UnsettledBalance for both Alice and Carol // Test that the UnsettledBalance for both Alice and Carol
// is equal to the amount of invoices * payAmt. // is equal to the amount of invoices * payAmt.
var unsettledErr error checkUnsettledBalance := func() error {
nodes := []*lntest.HarnessNode{net.Alice, carol} // Get channel info for the Alice.
err = wait.Predicate(func() bool { chanInfo := ht.QueryChannelByChanPoint(alice, chanPointAlice)
// There should be a number of PendingHtlcs equal
// to the amount of Invoices sent. // Check that UnsettledBalance is what we expect.
unsettledErr = assertNumActiveHtlcs(nodes, numInvoices) if int(chanInfo.UnsettledBalance) != expectedBalance {
if unsettledErr != nil { return fmt.Errorf("unsettled balance failed "+
return false "expected: %v, received: %v", expectedBalance,
chanInfo.UnsettledBalance)
} }
// Set the amount expected for the Unsettled Balance for // Get channel info for the Carol.
// this channel. chanInfo = ht.QueryChannelByChanPoint(carol, chanPointAlice)
expectedBalance := numInvoices * payAmt
// Check each nodes UnsettledBalance field. // Check that UnsettledBalance is what we expect.
for _, node := range nodes { if int(chanInfo.UnsettledBalance) != expectedBalance {
// Get channel info for the node. return fmt.Errorf("unsettled balance failed "+
chanInfo, err := getChanInfo(node) "expected: %v, received: %v", expectedBalance,
if err != nil { chanInfo.UnsettledBalance)
unsettledErr = err
return false
}
// Check that UnsettledBalance is what we expect.
if int(chanInfo.UnsettledBalance) != expectedBalance {
unsettledErr = fmt.Errorf("unsettled balance failed "+
"expected: %v, received: %v", expectedBalance,
chanInfo.UnsettledBalance)
return false
}
} }
return true return nil
}, defaultTimeout)
if err != nil {
t.Fatalf("unsettled balace error: %v", unsettledErr)
}
// Check for payment errors.
select {
case err := <-errChan:
t.Fatalf("payment error: %v", err)
default:
} }
require.NoError(ht, wait.NoError(checkUnsettledBalance, defaultTimeout),
"timeout while checking unsettled balance")
// Check alice's channel balance, which should have a remote unsettled // Check alice's channel balance, which should have a remote unsettled
// balance that equals to the amount of invoices * payAmt. The remote // balance that equals to the amount of invoices * payAmt. The remote
// balance remains zero. // balance remains zero.
aliceLocal := chanAmt - calcStaticFee(cType, 0) - numInvoices*payAmt aliceLocal := chanAmt - calcStaticFee(cType, 0) - numInvoices*payAmt
checkChannelBalance(net.Alice, aliceLocal, 0, 0, numInvoices*payAmt) checkChannelBalance(alice, aliceLocal, 0, 0, numInvoices*payAmt)
// Check carol's channel balance, which should have a local unsettled // Check carol's channel balance, which should have a local unsettled
// balance that equals to the amount of invoices * payAmt. The local // balance that equals to the amount of invoices * payAmt. The local
@ -245,8 +204,5 @@ func testChannelUnsettledBalance(net *lntest.NetworkHarness, t *harnessTest) {
checkChannelBalance(carol, 0, aliceLocal, numInvoices*payAmt, 0) checkChannelBalance(carol, 0, aliceLocal, numInvoices*payAmt, 0)
// Force and assert the channel closure. // Force and assert the channel closure.
closeChannelAndAssert(t, net, net.Alice, chanPointAlice, true) ht.ForceCloseChannel(alice, chanPointAlice)
// Cleanup by mining the force close and sweep transaction.
cleanupForceClose(t, net, net.Alice, chanPointAlice)
} }

View File

@ -12,10 +12,6 @@ var allTestCases = []*testCase{
name: "channel force closure", name: "channel force closure",
test: testChannelForceClosure, test: testChannelForceClosure,
}, },
{
name: "channel unsettled balance",
test: testChannelUnsettledBalance,
},
{ {
name: "single hop invoice", name: "single hop invoice",
test: testSingleHopInvoice, test: testSingleHopInvoice,