lnwallet: use availableBalance in max fee calc

In this commit we ensure that the max fee calculated in the MaxFeeRate
function takes the local reserve amount into account along with any
pending HTLCs. This is done by calling the avaialbeBalance function.
This commit is contained in:
Elle Mouton 2021-09-08 13:25:47 +02:00
parent af9b620854
commit 2b8a4d296e
No known key found for this signature in database
GPG Key ID: D7D916376026F177
2 changed files with 20 additions and 16 deletions

View File

@ -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)),

View File

@ -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,
)