From 48f6c47b1795df2d9ec16edc9ad51c3842149a5f Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Tue, 11 Aug 2020 03:47:27 +0800 Subject: [PATCH] itest: test all ChannelBalance fields --- lntest/itest/lnd_test.go | 245 +++++++++++++++++++++++++-------------- 1 file changed, 156 insertions(+), 89 deletions(-) diff --git a/lntest/itest/lnd_test.go b/lntest/itest/lnd_test.go index f53786bf..9755f8b4 100644 --- a/lntest/itest/lnd_test.go +++ b/lntest/itest/lnd_test.go @@ -1101,6 +1101,16 @@ func channelCommitType(node *lntest.HarnessNode, func assertChannelBalanceResp(t *harnessTest, node *lntest.HarnessNode, expected *lnrpc.ChannelBalanceResponse) { + resp := getChannelBalance(t, node) + require.Equal( + t.t, expected, resp, "balance is incorrect", + ) +} + +// getChannelBalance gets the channel balance. +func getChannelBalance(t *harnessTest, + node *lntest.HarnessNode) *lnrpc.ChannelBalanceResponse { + t.t.Helper() ctxt, _ := context.WithTimeout(context.Background(), defaultTimeout) @@ -1108,9 +1118,7 @@ func assertChannelBalanceResp(t *harnessTest, resp, err := node.ChannelBalance(ctxt, req) require.NoError(t.t, err, "unable to get node's balance") - require.Equal( - t.t, expected, resp, "balance is incorrect", - ) + return resp } // basicChannelFundingTest is a sub-test of the main testBasicChannelFunding @@ -1124,6 +1132,31 @@ func basicChannelFundingTest(t *harnessTest, net *lntest.NetworkHarness, chanAmt := lnd.MaxBtcFundingAmount pushAmt := btcutil.Amount(100000) + // Record nodes' channel balance before testing. + aliceChannelBalance := getChannelBalance(t, alice) + bobChannelBalance := getChannelBalance(t, bob) + + // Creates a helper closure to be used below which asserts the proper + // response to a channel balance RPC. + checkChannelBalance := func(node *lntest.HarnessNode, + oldChannelBalance *lnrpc.ChannelBalanceResponse, + local, remote btcutil.Amount) { + + newResp := oldChannelBalance + + newResp.LocalBalance.Sat += uint64(local) + newResp.LocalBalance.Msat += uint64( + lnwire.NewMSatFromSatoshis(local), + ) + newResp.RemoteBalance.Sat += uint64(remote) + newResp.RemoteBalance.Msat += uint64( + lnwire.NewMSatFromSatoshis(remote), + ) + // Deprecated fields. + newResp.Balance += int64(local) + assertChannelBalanceResp(t, node, newResp) + } + // First establish a channel with a capacity of 0.5 BTC between Alice // and Bob with Alice pushing 100k satoshis to Bob's side during // funding. This function will block until the channel itself is fully @@ -1162,45 +1195,12 @@ func basicChannelFundingTest(t *harnessTest, net *lntest.NetworkHarness, // With the channel open, ensure that the amount specified above has // properly been pushed to Bob. aliceLocalBalance := chanAmt - pushAmt - cType.calcStaticFee(0) - expBalanceAlice := &lnrpc.ChannelBalanceResponse{ - LocalBalance: &lnrpc.Amount{ - Sat: uint64(aliceLocalBalance), - Msat: uint64(lnwire.NewMSatFromSatoshis( - aliceLocalBalance, - )), - }, - RemoteBalance: &lnrpc.Amount{ - Sat: uint64(pushAmt), - Msat: uint64(lnwire.NewMSatFromSatoshis(pushAmt)), - }, - UnsettledLocalBalance: &lnrpc.Amount{}, - UnsettledRemoteBalance: &lnrpc.Amount{}, - PendingOpenLocalBalance: &lnrpc.Amount{}, - PendingOpenRemoteBalance: &lnrpc.Amount{}, - // Deprecated fields. - Balance: int64(aliceLocalBalance), - } - assertChannelBalanceResp(t, alice, expBalanceAlice) - - expBalanceBob := &lnrpc.ChannelBalanceResponse{ - LocalBalance: &lnrpc.Amount{ - Sat: uint64(pushAmt), - Msat: uint64(lnwire.NewMSatFromSatoshis(pushAmt)), - }, - RemoteBalance: &lnrpc.Amount{ - Sat: uint64(aliceLocalBalance), - Msat: uint64(lnwire.NewMSatFromSatoshis( - aliceLocalBalance, - )), - }, - UnsettledLocalBalance: &lnrpc.Amount{}, - UnsettledRemoteBalance: &lnrpc.Amount{}, - PendingOpenLocalBalance: &lnrpc.Amount{}, - PendingOpenRemoteBalance: &lnrpc.Amount{}, - // Deprecated fields. - Balance: int64(pushAmt), - } - assertChannelBalanceResp(t, bob, expBalanceBob) + checkChannelBalance( + alice, aliceChannelBalance, aliceLocalBalance, pushAmt, + ) + checkChannelBalance( + bob, bobChannelBalance, pushAmt, aliceLocalBalance, + ) req := &lnrpc.ListChannelsRequest{} aliceChannel, err := alice.ListChannels(context.Background(), req) @@ -1435,6 +1435,58 @@ func testUnconfirmedChannelFunding(net *lntest.NetworkHarness, t *harnessTest) { }, ) + // Creates a helper closure to be used below which asserts the proper + // response to a channel balance RPC. + checkChannelBalance := func(node *lntest.HarnessNode, + local, remote, pendingLocal, pendingRemote btcutil.Amount) { + expectedResponse := &lnrpc.ChannelBalanceResponse{ + LocalBalance: &lnrpc.Amount{ + Sat: uint64(local), + Msat: uint64(lnwire.NewMSatFromSatoshis( + local, + )), + }, + RemoteBalance: &lnrpc.Amount{ + Sat: uint64(remote), + Msat: uint64(lnwire.NewMSatFromSatoshis( + remote, + )), + }, + PendingOpenLocalBalance: &lnrpc.Amount{ + Sat: uint64(pendingLocal), + Msat: uint64(lnwire.NewMSatFromSatoshis( + pendingLocal, + )), + }, + PendingOpenRemoteBalance: &lnrpc.Amount{ + Sat: uint64(pendingRemote), + Msat: uint64(lnwire.NewMSatFromSatoshis( + pendingRemote, + )), + }, + UnsettledLocalBalance: &lnrpc.Amount{}, + UnsettledRemoteBalance: &lnrpc.Amount{}, + // Deprecated fields. + Balance: int64(local), + PendingOpenBalance: int64(pendingLocal), + } + assertChannelBalanceResp(t, node, expectedResponse) + } + + // As the channel is pending open, it's expected Carol has both zero + // local and remote balances, and pending local/remote should not be + // zero. + // + // Note that atm we haven't obtained the chanPoint yet, so we use the + // type directly. + cType := commitTypeTweakless + carolLocalBalance := chanAmt - pushAmt - cType.calcStaticFee(0) + checkChannelBalance(carol, 0, 0, carolLocalBalance, pushAmt) + + // For Alice, her local/remote balances should be zero, and the + // local/remote balances are the mirror of Carol's. + checkChannelBalance(net.Alice, 0, 0, pushAmt, carolLocalBalance) + // Confirm the channel and wait for it to be recognized by both // parties. Two transactions should be mined, the unconfirmed spend and // the funding tx. @@ -1445,55 +1497,10 @@ func testUnconfirmedChannelFunding(net *lntest.NetworkHarness, t *harnessTest) { t.Fatalf("error while waiting for channel open: %v", err) } - cType, err := channelCommitType(net.Alice, chanPoint) - if err != nil { - t.Fatalf("unable to get channel type: %v", err) - } - // With the channel open, we'll check the balances on each side of the // channel as a sanity check to ensure things worked out as intended. - carolLocalBalance := chanAmt - pushAmt - cType.calcStaticFee(0) - expBalanceCarol := &lnrpc.ChannelBalanceResponse{ - LocalBalance: &lnrpc.Amount{ - Sat: uint64(carolLocalBalance), - Msat: uint64(lnwire.NewMSatFromSatoshis( - carolLocalBalance, - )), - }, - RemoteBalance: &lnrpc.Amount{ - Sat: uint64(pushAmt), - Msat: uint64(lnwire.NewMSatFromSatoshis( - pushAmt, - )), - }, - UnsettledLocalBalance: &lnrpc.Amount{}, - UnsettledRemoteBalance: &lnrpc.Amount{}, - PendingOpenLocalBalance: &lnrpc.Amount{}, - PendingOpenRemoteBalance: &lnrpc.Amount{}, - // Deprecated fields. - Balance: int64(carolLocalBalance), - } - assertChannelBalanceResp(t, carol, expBalanceCarol) - - expBalanceAlice := &lnrpc.ChannelBalanceResponse{ - LocalBalance: &lnrpc.Amount{ - Sat: uint64(pushAmt), - Msat: uint64(lnwire.NewMSatFromSatoshis(pushAmt)), - }, - RemoteBalance: &lnrpc.Amount{ - Sat: uint64(carolLocalBalance), - Msat: uint64(lnwire.NewMSatFromSatoshis( - carolLocalBalance, - )), - }, - UnsettledLocalBalance: &lnrpc.Amount{}, - UnsettledRemoteBalance: &lnrpc.Amount{}, - PendingOpenLocalBalance: &lnrpc.Amount{}, - PendingOpenRemoteBalance: &lnrpc.Amount{}, - // Deprecated fields. - Balance: int64(pushAmt), - } - assertChannelBalanceResp(t, net.Alice, expBalanceAlice) + checkChannelBalance(carol, carolLocalBalance, pushAmt, 0, 0) + checkChannelBalance(net.Alice, pushAmt, carolLocalBalance, 0, 0) // Now that we're done with the test, the channel can be closed. ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout) @@ -3001,6 +3008,44 @@ func testChannelUnsettledBalance(net *lntest.NetworkHarness, t *harnessTest) { const chanAmt = btcutil.Amount(1000000) ctxb := context.Background() + // Creates a helper closure to be used below which asserts the proper + // response to a channel balance RPC. + checkChannelBalance := func(node *lntest.HarnessNode, + local, remote, unsettledLocal, unsettledRemote btcutil.Amount) { + + expectedResponse := &lnrpc.ChannelBalanceResponse{ + LocalBalance: &lnrpc.Amount{ + Sat: uint64(local), + Msat: uint64(lnwire.NewMSatFromSatoshis( + local, + )), + }, + RemoteBalance: &lnrpc.Amount{ + Sat: uint64(remote), + Msat: uint64(lnwire.NewMSatFromSatoshis( + remote, + )), + }, + UnsettledLocalBalance: &lnrpc.Amount{ + Sat: uint64(unsettledLocal), + Msat: uint64(lnwire.NewMSatFromSatoshis( + unsettledLocal, + )), + }, + UnsettledRemoteBalance: &lnrpc.Amount{ + Sat: uint64(unsettledRemote), + Msat: uint64(lnwire.NewMSatFromSatoshis( + unsettledRemote, + )), + }, + PendingOpenLocalBalance: &lnrpc.Amount{}, + PendingOpenRemoteBalance: &lnrpc.Amount{}, + // Deprecated fields. + Balance: int64(local), + } + assertChannelBalanceResp(t, node, expectedResponse) + } + // Create carol in hodl mode. carol, err := net.NewNode("Carol", []string{"--hodl.exit-settle"}) if err != nil { @@ -3039,6 +3084,17 @@ func testChannelUnsettledBalance(net *lntest.NetworkHarness, t *harnessTest) { "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 + // pending balance. + checkChannelBalance(net.Alice, chanAmt-cType.calcStaticFee(0), 0, 0, 0) + + // Check carol's channel balance, which should have zero local and zero + // pending balance. + checkChannelBalance(carol, 0, chanAmt-cType.calcStaticFee(0), 0, 0) + // Channel should be ready for payments. const ( payAmt = 100 @@ -3115,6 +3171,17 @@ func testChannelUnsettledBalance(net *lntest.NetworkHarness, t *harnessTest) { default: } + // Check alice's channel balance, which should have a remote unsettled + // balance that equals to the amount of invoices * payAmt. The remote + // balance remains zero. + aliceLocal := chanAmt - cType.calcStaticFee(0) - numInvoices*payAmt + checkChannelBalance(net.Alice, aliceLocal, 0, 0, numInvoices*payAmt) + + // Check carol's channel balance, which should have a local unsettled + // balance that equals to the amount of invoices * payAmt. The local + // balance remains zero. + checkChannelBalance(carol, 0, aliceLocal, numInvoices*payAmt, 0) + // Force and assert the channel closure. ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout) closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointAlice, true)