itest: refactor testChannelBalance

This commit is contained in:
yyforyongyu
2022-08-04 19:59:17 +08:00
parent 876bb5c066
commit 8f472bf063
3 changed files with 17 additions and 33 deletions

View File

@@ -199,4 +199,8 @@ var allTestCasesTemp = []*lntemp.TestCase{
Name: "export channel backup", Name: "export channel backup",
TestFunc: testExportChannelBackup, TestFunc: testExportChannelBackup,
}, },
{
Name: "channel balance",
TestFunc: testChannelBalance,
},
} }

View File

@@ -9,6 +9,8 @@ import (
"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/node"
"github.com/lightningnetwork/lnd/lntest" "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"
@@ -17,14 +19,14 @@ import (
// testChannelBalance creates a new channel between Alice and Bob, then checks // testChannelBalance creates a new channel between Alice and Bob, then checks
// channel balance to be equal amount specified while creation of channel. // channel balance to be equal amount specified while creation of channel.
func testChannelBalance(net *lntest.NetworkHarness, t *harnessTest) { func testChannelBalance(ht *lntemp.HarnessTest) {
// Open a channel with 0.16 BTC between Alice and Bob, ensuring the // Open a channel with 0.16 BTC between Alice and Bob, ensuring the
// channel has been opened properly. // channel has been opened properly.
amount := funding.MaxBtcFundingAmount amount := funding.MaxBtcFundingAmount
// 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 btcutil.Amount) { local, remote btcutil.Amount) {
expectedResponse := &lnrpc.ChannelBalanceResponse{ expectedResponse := &lnrpc.ChannelBalanceResponse{
@@ -45,46 +47,28 @@ func testChannelBalance(net *lntest.NetworkHarness, t *harnessTest) {
// Deprecated fields. // Deprecated fields.
Balance: int64(local), Balance: int64(local),
} }
assertChannelBalanceResp(t, node, expectedResponse) ht.AssertChannelBalanceResp(node, expectedResponse)
} }
// Before beginning, make sure alice and bob are connected. // Before beginning, make sure alice and bob are connected.
net.EnsureConnected(t.t, net.Alice, net.Bob) alice, bob := ht.Alice, ht.Bob
ht.EnsureConnected(alice, bob)
chanPoint := openChannelAndAssert( chanPoint := ht.OpenChannel(
t, net, net.Alice, net.Bob, alice, bob, lntemp.OpenChannelParams{Amt: amount},
lntest.OpenChannelParams{
Amt: amount,
},
) )
cType := ht.GetChannelCommitType(alice, chanPoint)
// Wait for both Alice and Bob to recognize this new channel.
err := net.Alice.WaitForNetworkChannelOpen(chanPoint)
if err != nil {
t.Fatalf("alice didn't advertise channel before "+
"timeout: %v", err)
}
err = net.Bob.WaitForNetworkChannelOpen(chanPoint)
if err != nil {
t.Fatalf("bob didn't advertise channel before "+
"timeout: %v", err)
}
cType, err := channelCommitType(net.Alice, chanPoint)
if err != nil {
t.Fatalf("unable to get channel type: %v", err)
}
// As this is a single funder channel, Alice's balance should be // As this is a single funder channel, Alice's balance should be
// exactly 0.5 BTC since now state transitions have taken place yet. // exactly 0.5 BTC since now state transitions have taken place yet.
checkChannelBalance(net.Alice, amount-calcStaticFee(cType, 0), 0) checkChannelBalance(alice, amount-calcStaticFee(cType, 0), 0)
// Ensure Bob currently has no available balance within the channel. // Ensure Bob currently has no available balance within the channel.
checkChannelBalance(net.Bob, 0, amount-calcStaticFee(cType, 0)) checkChannelBalance(bob, 0, amount-calcStaticFee(cType, 0))
// Finally close the channel between Alice and Bob, asserting that the // Finally close the channel between Alice and Bob, asserting that the
// channel has been properly closed on-chain. // channel has been properly closed on-chain.
closeChannelAndAssert(t, net, net.Alice, chanPoint, false) ht.CloseChannel(alice, chanPoint)
} }
// testChannelUnsettledBalance will test that the UnsettledBalance field // testChannelUnsettledBalance will test that the UnsettledBalance field

View File

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