itest: test all ChannelBalance fields

This commit is contained in:
yyforyongyu 2020-08-11 03:47:27 +08:00
parent 1b200c9930
commit 48f6c47b17
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868

@ -1101,6 +1101,16 @@ func channelCommitType(node *lntest.HarnessNode,
func assertChannelBalanceResp(t *harnessTest, func assertChannelBalanceResp(t *harnessTest,
node *lntest.HarnessNode, expected *lnrpc.ChannelBalanceResponse) { 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() t.t.Helper()
ctxt, _ := context.WithTimeout(context.Background(), defaultTimeout) ctxt, _ := context.WithTimeout(context.Background(), defaultTimeout)
@ -1108,9 +1118,7 @@ func assertChannelBalanceResp(t *harnessTest,
resp, err := node.ChannelBalance(ctxt, req) resp, err := node.ChannelBalance(ctxt, req)
require.NoError(t.t, err, "unable to get node's balance") require.NoError(t.t, err, "unable to get node's balance")
require.Equal( return resp
t.t, expected, resp, "balance is incorrect",
)
} }
// basicChannelFundingTest is a sub-test of the main testBasicChannelFunding // basicChannelFundingTest is a sub-test of the main testBasicChannelFunding
@ -1124,6 +1132,31 @@ func basicChannelFundingTest(t *harnessTest, net *lntest.NetworkHarness,
chanAmt := lnd.MaxBtcFundingAmount chanAmt := lnd.MaxBtcFundingAmount
pushAmt := btcutil.Amount(100000) 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 // 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 // and Bob with Alice pushing 100k satoshis to Bob's side during
// funding. This function will block until the channel itself is fully // 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 // With the channel open, ensure that the amount specified above has
// properly been pushed to Bob. // properly been pushed to Bob.
aliceLocalBalance := chanAmt - pushAmt - cType.calcStaticFee(0) aliceLocalBalance := chanAmt - pushAmt - cType.calcStaticFee(0)
expBalanceAlice := &lnrpc.ChannelBalanceResponse{ checkChannelBalance(
LocalBalance: &lnrpc.Amount{ alice, aliceChannelBalance, aliceLocalBalance, pushAmt,
Sat: uint64(aliceLocalBalance), )
Msat: uint64(lnwire.NewMSatFromSatoshis( checkChannelBalance(
aliceLocalBalance, bob, bobChannelBalance, pushAmt, 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)
req := &lnrpc.ListChannelsRequest{} req := &lnrpc.ListChannelsRequest{}
aliceChannel, err := alice.ListChannels(context.Background(), req) 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 // Confirm the channel and wait for it to be recognized by both
// parties. Two transactions should be mined, the unconfirmed spend and // parties. Two transactions should be mined, the unconfirmed spend and
// the funding tx. // the funding tx.
@ -1445,55 +1497,10 @@ func testUnconfirmedChannelFunding(net *lntest.NetworkHarness, t *harnessTest) {
t.Fatalf("error while waiting for channel open: %v", err) 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 // 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. // channel as a sanity check to ensure things worked out as intended.
carolLocalBalance := chanAmt - pushAmt - cType.calcStaticFee(0) checkChannelBalance(carol, carolLocalBalance, pushAmt, 0, 0)
expBalanceCarol := &lnrpc.ChannelBalanceResponse{ checkChannelBalance(net.Alice, pushAmt, carolLocalBalance, 0, 0)
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)
// Now that we're done with the test, the channel can be closed. // Now that we're done with the test, the channel can be closed.
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout) ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
@ -3001,6 +3008,44 @@ func testChannelUnsettledBalance(net *lntest.NetworkHarness, t *harnessTest) {
const chanAmt = btcutil.Amount(1000000) const chanAmt = btcutil.Amount(1000000)
ctxb := context.Background() 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. // Create carol in hodl mode.
carol, err := net.NewNode("Carol", []string{"--hodl.exit-settle"}) carol, err := net.NewNode("Carol", []string{"--hodl.exit-settle"})
if err != nil { if err != nil {
@ -3039,6 +3084,17 @@ func testChannelUnsettledBalance(net *lntest.NetworkHarness, t *harnessTest) {
"timeout: %v", err) "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. // Channel should be ready for payments.
const ( const (
payAmt = 100 payAmt = 100
@ -3115,6 +3171,17 @@ func testChannelUnsettledBalance(net *lntest.NetworkHarness, t *harnessTest) {
default: 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. // Force and assert the channel closure.
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout) ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointAlice, true) closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointAlice, true)