diff --git a/lnwallet/channel.go b/lnwallet/channel.go index b914ac789..ea3a9ca56 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -6998,9 +6998,10 @@ func (lc *LightningChannel) CalcFee(feeRate chainfee.SatPerKWeight) btcutil.Amou } // MaxFeeRate returns the maximum fee rate given an allocation of the channel -// initiator's spendable balance. This can be useful to determine when we should -// stop proposing fee updates that exceed our maximum allocation. We also take -// a fee rate cap that should be used for anchor type channels. +// initiator's spendable balance along with the local reserve amount. This can +// be useful to determine when we should stop proposing fee updates that exceed +// our maximum allocation. We also take a fee rate cap that should be used for +// anchor type channels. // // NOTE: This should only be used for channels in which the local commitment is // the initiator. @@ -7010,16 +7011,19 @@ func (lc *LightningChannel) MaxFeeRate(maxAllocation float64, lc.RLock() defer lc.RUnlock() - // The maximum fee depends of the available balance that can be - // committed towards fees. - commit := lc.channelState.LocalCommitment - feeBalance := float64( - commit.LocalBalance.ToSatoshis() + commit.CommitFee, - ) - maxFee := feeBalance * maxAllocation + // The maximum fee depends on the available balance that can be + // committed towards fees. It takes into account our local reserve + // balance. + availableBalance, weight := lc.availableBalance() + + oldFee := lc.localCommitChain.tip().feePerKw.FeeForWeight(weight) + + // baseBalance is the maximum amount available for us to spend on fees. + baseBalance := availableBalance.ToSatoshis() + oldFee + + maxFee := float64(baseBalance) * maxAllocation // Ensure the fee rate doesn't dip below the fee floor. - _, weight := lc.availableBalance() maxFeeRate := maxFee / (float64(weight) / 1000) feeRate := chainfee.SatPerKWeight( math.Max(maxFeeRate, float64(chainfee.FeePerKwFloor)), diff --git a/lnwallet/channel_test.go b/lnwallet/channel_test.go index 0ef5c8514..a490b9502 100644 --- a/lnwallet/channel_test.go +++ b/lnwallet/channel_test.go @@ -8024,9 +8024,9 @@ func TestChannelMaxFeeRate(t *testing.T) { } defer cleanUp() - assertMaxFeeRate(aliceChannel, 1.0, 0, 690607734) - assertMaxFeeRate(aliceChannel, 0.001, 0, 690607) - assertMaxFeeRate(aliceChannel, 0.000001, 0, 690) + assertMaxFeeRate(aliceChannel, 1.0, 0, 676794154) + assertMaxFeeRate(aliceChannel, 0.001, 0, 676794) + assertMaxFeeRate(aliceChannel, 0.000001, 0, 676) assertMaxFeeRate(aliceChannel, 0.0000001, 0, chainfee.FeePerKwFloor) // Check that anchor channels are capped at their max fee rate. @@ -8044,9 +8044,9 @@ func TestChannelMaxFeeRate(t *testing.T) { anchorChannel, 1.0, chainfee.FeePerKwFloor, chainfee.FeePerKwFloor, ) - assertMaxFeeRate(anchorChannel, 0.001, 1000000, 444839) + assertMaxFeeRate(anchorChannel, 0.001, 1000000, 435941) assertMaxFeeRate(anchorChannel, 0.001, 300000, 300000) - assertMaxFeeRate(anchorChannel, 0.000001, 700, 444) + assertMaxFeeRate(anchorChannel, 0.000001, 700, 435) assertMaxFeeRate( anchorChannel, 0.0000001, 1000000, chainfee.FeePerKwFloor, )