Skip to content

Commit

Permalink
lnwallet+lnrpc: parameterize chanfunding fee sanity check
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeTsagk committed Mar 28, 2024
1 parent d3e5b2f commit 0e83ed6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
3 changes: 2 additions & 1 deletion lnrpc/walletrpc/walletkit_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1546,7 +1546,7 @@ func (w *WalletKit) fundPsbtCoinSelect(account string, changeIndex int32,

changeAmt, needMore, err := chanfunding.CalculateChangeAmount(
inputSum, outputSum, packetFeeNoChange,
packetFeeWithChange, changeDustLimit, changeType,
packetFeeWithChange, changeDustLimit, changeType, true,
)
if err != nil {
return nil, fmt.Errorf("error calculating change "+
Expand Down Expand Up @@ -1604,6 +1604,7 @@ func (w *WalletKit) fundPsbtCoinSelect(account string, changeIndex int32,
selectedCoins, changeAmount, err := chanfunding.CoinSelect(
feeRate, fundingAmount, changeDustLimit, coins,
w.cfg.CoinSelectionStrategy, estimator, changeType,
true,
)
if err != nil {
return fmt.Errorf("error selecting coins: %w", err)
Expand Down
28 changes: 17 additions & 11 deletions lnwallet/chanfunding/coin_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ func sanityCheckFee(totalOut, fee btcutil.Amount) error {
func CoinSelect(feeRate chainfee.SatPerKWeight, amt, dustLimit btcutil.Amount,
coins []wallet.Coin, strategy wallet.CoinSelectionStrategy,
existingWeight input.TxWeightEstimator,
changeType ChangeAddressType) ([]wallet.Coin, btcutil.Amount, error) {
changeType ChangeAddressType, skipFeeCheck bool) ([]wallet.Coin,
btcutil.Amount, error) {

amtNeeded := amt
for {
Expand All @@ -188,6 +189,7 @@ func CoinSelect(feeRate chainfee.SatPerKWeight, amt, dustLimit btcutil.Amount,
changeAmount, newAmtNeeded, err := CalculateChangeAmount(
totalSat, amt, requiredFeeNoChange,
requiredFeeWithChange, dustLimit, changeType,
skipFeeCheck,
)
if err != nil {
return nil, 0, err
Expand Down Expand Up @@ -216,7 +218,8 @@ func CoinSelect(feeRate chainfee.SatPerKWeight, amt, dustLimit btcutil.Amount,
// fees and that more coins need to be selected.
func CalculateChangeAmount(totalInputAmt, requiredAmt, requiredFeeNoChange,
requiredFeeWithChange, dustLimit btcutil.Amount,
changeType ChangeAddressType) (btcutil.Amount, btcutil.Amount, error) {
changeType ChangeAddressType, skipFeeCheck bool) (btcutil.Amount,
btcutil.Amount, error) {

// This is just a sanity check to make sure the function is used
// correctly.
Expand Down Expand Up @@ -263,12 +266,15 @@ func CalculateChangeAmount(totalInputAmt, requiredAmt, requiredFeeNoChange,
changeAmt = 0
}

// Sanity check the resulting output values to make sure we
// don't burn a great part to fees.
totalOut := requiredAmt + changeAmt
err := sanityCheckFee(totalOut, totalInputAmt-totalOut)
if err != nil {
return 0, 0, err
if !skipFeeCheck {
// Sanity check the resulting output values to make sure we
// don't burn a great part to fees.
totalOut := requiredAmt + changeAmt

err := sanityCheckFee(totalOut, totalInputAmt-totalOut)
if err != nil {
return 0, 0, err
}
}

return changeAmt, 0, nil
Expand Down Expand Up @@ -347,8 +353,8 @@ func CoinSelectUpToAmount(feeRate chainfee.SatPerKWeight, minAmount, maxAmount,
reserved, dustLimit btcutil.Amount, coins []wallet.Coin,
strategy wallet.CoinSelectionStrategy,
existingWeight input.TxWeightEstimator,
changeType ChangeAddressType) ([]wallet.Coin, btcutil.Amount,
btcutil.Amount, error) {
changeType ChangeAddressType, skipFeeCheck bool) ([]wallet.Coin,
btcutil.Amount, btcutil.Amount, error) {

var (
// selectSubtractFee is tracking if our coin selection was
Expand All @@ -369,7 +375,7 @@ func CoinSelectUpToAmount(feeRate chainfee.SatPerKWeight, minAmount, maxAmount,
// maxAmount with or without a change output that covers the miner fee.
selected, changeAmt, err := CoinSelect(
feeRate, maxAmount, dustLimit, coins, strategy, existingWeight,
changeType,
changeType, skipFeeCheck,
)

var errInsufficientFunds *ErrInsufficientFunds
Expand Down
4 changes: 2 additions & 2 deletions lnwallet/chanfunding/coin_select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func TestCoinSelect(t *testing.T) {
selected, changeAmt, err := CoinSelect(
feeRate, test.outputValue, dustLimit,
test.coins, wallet.CoinSelectionLargest,
fundingOutputEstimate, test.changeType,
fundingOutputEstimate, test.changeType, false,
)

if test.expectErr {
Expand Down Expand Up @@ -871,7 +871,7 @@ func TestCoinSelectUpToAmount(t *testing.T) {
test.reserved, dustLimit, test.coins,
wallet.CoinSelectionLargest,
fundingOutputEstimate,
defaultChanFundingChangeType,
defaultChanFundingChangeType, false,
)
if len(test.expectErr) == 0 && err != nil {
t.Fatalf(err.Error())
Expand Down
4 changes: 2 additions & 2 deletions lnwallet/chanfunding/wallet_assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ func (w *WalletAssembler) ProvisionChannel(r *Request) (Intent, error) {
r.FeeRate, r.MinFundAmt, r.FundUpToMaxAmt,
reserve, w.cfg.DustLimit, coins,
w.cfg.CoinSelectionStrategy,
fundingOutputWeight, changeType,
fundingOutputWeight, changeType, false,
)
if err != nil {
return err
Expand Down Expand Up @@ -484,7 +484,7 @@ func (w *WalletAssembler) ProvisionChannel(r *Request) (Intent, error) {
selectedCoins, changeAmt, err = CoinSelect(
r.FeeRate, r.LocalAmt, dustLimit, coins,
w.cfg.CoinSelectionStrategy,
fundingOutputWeight, changeType,
fundingOutputWeight, changeType, false,
)
if err != nil {
return err
Expand Down

0 comments on commit 0e83ed6

Please sign in to comment.