mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-03-04 01:36:24 +01:00
itest: respect the reserved wallet balance when using fundmax
This commit is contained in:
parent
8c1cf21707
commit
ff45fc7e71
1 changed files with 60 additions and 9 deletions
|
@ -1,6 +1,7 @@
|
||||||
package itest
|
package itest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -8,8 +9,10 @@ import (
|
||||||
"github.com/lightningnetwork/lnd"
|
"github.com/lightningnetwork/lnd"
|
||||||
"github.com/lightningnetwork/lnd/input"
|
"github.com/lightningnetwork/lnd/input"
|
||||||
"github.com/lightningnetwork/lnd/lnrpc"
|
"github.com/lightningnetwork/lnd/lnrpc"
|
||||||
|
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
|
||||||
"github.com/lightningnetwork/lnd/lntest"
|
"github.com/lightningnetwork/lnd/lntest"
|
||||||
"github.com/lightningnetwork/lnd/lntest/node"
|
"github.com/lightningnetwork/lnd/lntest/node"
|
||||||
|
"github.com/lightningnetwork/lnd/lnwallet"
|
||||||
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
|
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
|
||||||
"github.com/lightningnetwork/lnd/lnwire"
|
"github.com/lightningnetwork/lnd/lnwire"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
@ -39,6 +42,10 @@ type chanFundMaxTestCase struct {
|
||||||
// is set to true.
|
// is set to true.
|
||||||
expectedErrStr string
|
expectedErrStr string
|
||||||
|
|
||||||
|
// commitmentType allows to define the exact type when opening the
|
||||||
|
// channel.
|
||||||
|
commitmentType lnrpc.CommitmentType
|
||||||
|
|
||||||
// private denotes if the channel opening is announced to the network or
|
// private denotes if the channel opening is announced to the network or
|
||||||
// not.
|
// not.
|
||||||
private bool
|
private bool
|
||||||
|
@ -49,16 +56,25 @@ type chanFundMaxTestCase struct {
|
||||||
func testChannelFundMax(ht *lntest.HarnessTest) {
|
func testChannelFundMax(ht *lntest.HarnessTest) {
|
||||||
// Create two new nodes that open a channel between each other for these
|
// Create two new nodes that open a channel between each other for these
|
||||||
// tests.
|
// tests.
|
||||||
alice := ht.NewNode("Alice", nil)
|
args := lntest.NodeArgsForCommitType(lnrpc.CommitmentType_ANCHORS)
|
||||||
|
alice := ht.NewNode("Alice", args)
|
||||||
defer ht.Shutdown(alice)
|
defer ht.Shutdown(alice)
|
||||||
|
|
||||||
bob := ht.NewNode("Bob", nil)
|
bob := ht.NewNode("Bob", args)
|
||||||
defer ht.Shutdown(bob)
|
defer ht.Shutdown(bob)
|
||||||
|
|
||||||
// Ensure both sides are connected so the funding flow can be properly
|
// Ensure both sides are connected so the funding flow can be properly
|
||||||
// executed.
|
// executed.
|
||||||
ht.EnsureConnected(alice, bob)
|
ht.EnsureConnected(alice, bob)
|
||||||
|
|
||||||
|
// Calculate reserve amount for one channel.
|
||||||
|
reserveResp, _ := alice.RPC.WalletKit.RequiredReserve(
|
||||||
|
context.Background(), &walletrpc.RequiredReserveRequest{
|
||||||
|
AdditionalPublicChannels: 1,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
reserveAmount := btcutil.Amount(reserveResp.RequiredReserve)
|
||||||
|
|
||||||
var testCases = []*chanFundMaxTestCase{
|
var testCases = []*chanFundMaxTestCase{
|
||||||
{
|
{
|
||||||
name: "wallet amount is dust",
|
name: "wallet amount is dust",
|
||||||
|
@ -132,6 +148,25 @@ func testChannelFundMax(ht *lntest.HarnessTest) {
|
||||||
pushAmt: 16_766_000,
|
pushAmt: 16_766_000,
|
||||||
expectedBalanceAlice: lnd.MaxFundingAmount - 16_766_000,
|
expectedBalanceAlice: lnd.MaxFundingAmount - 16_766_000,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
name: "anchor reserved value",
|
||||||
|
initialWalletBalance: 100_000,
|
||||||
|
commitmentType: lnrpc.CommitmentType_ANCHORS,
|
||||||
|
expectedBalanceAlice: btcutil.Amount(100_000) -
|
||||||
|
fundingFee(1, true) - reserveAmount,
|
||||||
|
},
|
||||||
|
// Funding a private anchor channel should omit the achor
|
||||||
|
// reserve and produce no change output.
|
||||||
|
{
|
||||||
|
name: "private anchor no reserved " +
|
||||||
|
"value",
|
||||||
|
private: true,
|
||||||
|
initialWalletBalance: 100_000,
|
||||||
|
commitmentType: lnrpc.CommitmentType_ANCHORS,
|
||||||
|
expectedBalanceAlice: btcutil.Amount(100_000) -
|
||||||
|
fundingFee(1, false),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, testCase := range testCases {
|
for _, testCase := range testCases {
|
||||||
|
@ -139,6 +174,7 @@ func testChannelFundMax(ht *lntest.HarnessTest) {
|
||||||
testCase.name, func(tt *testing.T) {
|
testCase.name, func(tt *testing.T) {
|
||||||
runFundMaxTestCase(
|
runFundMaxTestCase(
|
||||||
ht, tt, alice, bob, testCase,
|
ht, tt, alice, bob, testCase,
|
||||||
|
reserveAmount,
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
@ -153,7 +189,8 @@ func testChannelFundMax(ht *lntest.HarnessTest) {
|
||||||
|
|
||||||
// runTestCase runs a single test case asserting that test conditions are met.
|
// runTestCase runs a single test case asserting that test conditions are met.
|
||||||
func runFundMaxTestCase(ht *lntest.HarnessTest, t *testing.T, alice,
|
func runFundMaxTestCase(ht *lntest.HarnessTest, t *testing.T, alice,
|
||||||
bob *node.HarnessNode, testCase *chanFundMaxTestCase) {
|
bob *node.HarnessNode, testCase *chanFundMaxTestCase,
|
||||||
|
reserveAmount btcutil.Amount) {
|
||||||
|
|
||||||
ht.FundCoins(testCase.initialWalletBalance, alice)
|
ht.FundCoins(testCase.initialWalletBalance, alice)
|
||||||
|
|
||||||
|
@ -167,19 +204,24 @@ func runFundMaxTestCase(ht *lntest.HarnessTest, t *testing.T, alice,
|
||||||
sweepNodeWalletAndAssert(ht, alice)
|
sweepNodeWalletAndAssert(ht, alice)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
commitType := testCase.commitmentType
|
||||||
|
if commitType == lnrpc.CommitmentType_UNKNOWN_COMMITMENT_TYPE {
|
||||||
|
commitType = lnrpc.CommitmentType_STATIC_REMOTE_KEY
|
||||||
|
}
|
||||||
|
|
||||||
// The parameters to try opening the channel with.
|
// The parameters to try opening the channel with.
|
||||||
chanParams := lntest.OpenChannelParams{
|
chanParams := lntest.OpenChannelParams{
|
||||||
Amt: 0,
|
Amt: 0,
|
||||||
PushAmt: testCase.pushAmt,
|
PushAmt: testCase.pushAmt,
|
||||||
SatPerVByte: testCase.feeRate,
|
SatPerVByte: testCase.feeRate,
|
||||||
FundMax: true,
|
CommitmentType: commitType,
|
||||||
Private: testCase.private,
|
FundMax: true,
|
||||||
|
Private: testCase.private,
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we don't expect the channel opening to be
|
// If we don't expect the channel opening to be
|
||||||
// successful, simply check for an error.
|
// successful, simply check for an error.
|
||||||
if testCase.chanOpenShouldFail {
|
if testCase.chanOpenShouldFail {
|
||||||
|
|
||||||
expectedErr := fmt.Errorf(testCase.expectedErrStr)
|
expectedErr := fmt.Errorf(testCase.expectedErrStr)
|
||||||
ht.OpenChannelAssertErr(
|
ht.OpenChannelAssertErr(
|
||||||
alice, bob, chanParams, expectedErr,
|
alice, bob, chanParams, expectedErr,
|
||||||
|
@ -210,6 +252,15 @@ func runFundMaxTestCase(ht *lntest.HarnessTest, t *testing.T, alice,
|
||||||
ht, bob, testCase.pushAmt,
|
ht, bob, testCase.pushAmt,
|
||||||
testCase.expectedBalanceAlice-lntest.CalcStaticFee(cType, 0),
|
testCase.expectedBalanceAlice-lntest.CalcStaticFee(cType, 0),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if lntest.CommitTypeHasAnchors(testCase.commitmentType) &&
|
||||||
|
!testCase.private {
|
||||||
|
|
||||||
|
ht.AssertWalletAccountBalance(
|
||||||
|
alice, lnwallet.DefaultAccountName,
|
||||||
|
int64(reserveAmount), 0,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates a helper closure to be used below which asserts the proper
|
// Creates a helper closure to be used below which asserts the proper
|
||||||
|
|
Loading…
Add table
Reference in a new issue