lnwallet+lnrpc: use maxFeeRatio across coin selection tests

This commit is contained in:
George Tsagkarelis
2024-11-05 19:54:42 +01:00
parent f2b76325d4
commit 55c3da3d13
2 changed files with 135 additions and 11 deletions

View File

@@ -316,7 +316,8 @@ func TestCoinSelect(t *testing.T) {
selected, changeAmt, err := CoinSelect(
feeRate, test.outputValue, dustLimit,
test.coins, wallet.CoinSelectionLargest,
fundingOutputEstimate, test.changeType, 0,
fundingOutputEstimate, test.changeType,
DefaultMaxFeeRatio,
)
if test.expectErr {
@@ -356,6 +357,7 @@ func TestCalculateChangeAmount(t *testing.T) {
feeWithChange btcutil.Amount
dustLimit btcutil.Amount
changeType ChangeAddressType
maxFeeRatio float64
expectErr string
expectChangeAmt btcutil.Amount
@@ -369,6 +371,7 @@ func TestCalculateChangeAmount(t *testing.T) {
totalInputAmt: 500,
requiredAmt: 490,
feeNoChange: 12,
maxFeeRatio: DefaultMaxFeeRatio,
expectNeedMore: 502,
}, {
@@ -383,6 +386,7 @@ func TestCalculateChangeAmount(t *testing.T) {
feeWithChange: 10,
dustLimit: 100,
changeType: ExistingChangeAddress,
maxFeeRatio: DefaultMaxFeeRatio,
expectChangeAmt: 90,
}, {
@@ -392,6 +396,7 @@ func TestCalculateChangeAmount(t *testing.T) {
feeNoChange: 40,
feeWithChange: 50,
dustLimit: 100,
maxFeeRatio: DefaultMaxFeeRatio,
expectChangeAmt: 150,
}, {
@@ -401,6 +406,7 @@ func TestCalculateChangeAmount(t *testing.T) {
requiredAmt: 460,
feeNoChange: 40,
feeWithChange: 50,
maxFeeRatio: DefaultMaxFeeRatio,
expectChangeAmt: 0,
}, {
@@ -410,14 +416,36 @@ func TestCalculateChangeAmount(t *testing.T) {
feeNoChange: 10,
feeWithChange: 45,
dustLimit: 5,
maxFeeRatio: DefaultMaxFeeRatio,
expectErr: "fee (0.00000045 BTC) exceeds 20% of total output " +
"(0.00000055 BTC)",
expectErr: "fee 0.00000045 BTC exceeds max fee (0.00000011 " +
"BTC) on total output value 0.00000055 BTC",
}, {
name: "fee percent ok",
totalInputAmt: 100,
requiredAmt: 50,
feeNoChange: 10,
feeWithChange: 45,
dustLimit: 5,
maxFeeRatio: 0.95,
expectChangeAmt: 5,
}, {
name: "invalid max fee ratio",
totalInputAmt: 100,
requiredAmt: 50,
feeNoChange: 10,
feeWithChange: 45,
dustLimit: 5,
maxFeeRatio: 3.14,
expectErr: "maxFeeRatio must be between 0.00 and 1.00",
}, {
name: "invalid usage of function",
feeNoChange: 5,
feeWithChange: 10,
changeType: ExistingChangeAddress,
maxFeeRatio: DefaultMaxFeeRatio,
expectErr: "fees for with or without change must be the same",
}}
@@ -428,7 +456,7 @@ func TestCalculateChangeAmount(t *testing.T) {
changeAmt, needMore, err := CalculateChangeAmount(
tc.totalInputAmt, tc.requiredAmt,
tc.feeNoChange, tc.feeWithChange, tc.dustLimit,
tc.changeType, 0,
tc.changeType, tc.maxFeeRatio,
)
if tc.expectErr != "" {
@@ -606,8 +634,9 @@ func TestCoinSelectSubtractFees(t *testing.T) {
},
spendValue: 5 * fundingFee(highFeeRate, 1, false),
expectErr: "fee (<amt> BTC) exceeds <amt>% of total " +
"output (<amt> BTC)",
expectErr: "fee <amt> BTC exceeds max fee (<amt> " +
"BTC) on total output value <amt> BTC with " +
"max fee ratio of <amt>",
},
}
@@ -627,7 +656,8 @@ func TestCoinSelectSubtractFees(t *testing.T) {
feeRate, test.spendValue, dustLimit, test.coins,
wallet.CoinSelectionLargest,
fundingOutputEstimate,
defaultChanFundingChangeType, 0,
defaultChanFundingChangeType,
DefaultMaxFeeRatio,
)
if err != nil {
switch {
@@ -813,8 +843,9 @@ func TestCoinSelectUpToAmount(t *testing.T) {
minValue: minValue,
maxValue: 16 * fundingFee(feeRate, 1, false),
expectErr: "fee (0.00000192 BTC) exceeds 20% of total output " +
"(0.00000768 BTC)",
expectErr: "fee 0.00000192 BTC exceeds max fee (0.00000153 " +
"BTC) on total output value 0.00000768 BTC with max " +
"fee ratio of 0.20",
}, {
// This test makes sure that the implementation detail of using
// CoinSelect and CoinSelectSubtractFees is done correctly.
@@ -872,7 +903,8 @@ func TestCoinSelectUpToAmount(t *testing.T) {
test.reserved, dustLimit, test.coins,
wallet.CoinSelectionLargest,
fundingOutputEstimate,
defaultChanFundingChangeType, 0,
defaultChanFundingChangeType,
DefaultMaxFeeRatio,
)
if len(test.expectErr) == 0 && err != nil {
t.Fatal(err.Error())