link+lnwallet: move bandwidth channel reserve validation into channel

Since we want to handle the edge case where paying the HTLC fee would
take the initiator below the reserve, we move the subtraction of the
reserve into availableBalance where this calculation will be performed.
This commit is contained in:
Johan T. Halseth committed 2020-02-19 12:27:42 +01:00
1 parent 58dec10680
commit 5e89d5b6c2
3 files changed
+33 -16

No files matched your search

+17 -2
View File
@@ -6057,6 +6057,17 @@ func (lc *LightningChannel) availableCommitmentBalance(view *htlcView) (
return 0, 0
}
// We can never spend from the channel reserve, so we'll subtract it
// from our available balance.
ourReserve := lnwire.NewMSatFromSatoshis(
lc.channelState.LocalChanCfg.ChanReserve,
)
if ourReserve <= ourBalance {
ourBalance -= ourReserve
} else {
ourBalance = 0
}
// Given the commitment weight, find the commitment fee in case of no
// added HTLC output.
feePerKw := filteredView.feePerKw
@@ -6067,6 +6078,9 @@ func (lc *LightningChannel) availableCommitmentBalance(view *htlcView) (
// If we are the channel initiator, we must to subtract the commitment
// fee from our available balance.
if lc.channelState.IsInitiator {
if ourBalance < baseCommitFee {
return 0, commitWeight
}
ourBalance -= baseCommitFee
}
@@ -6278,13 +6292,14 @@ func (lc *LightningChannel) MaxFeeRate(maxAllocation float64) chainfee.SatPerKWe
// The maximum fee depends of the available balance that can be
// committed towards fees.
balance, weight := lc.availableBalance()
commit := lc.channelState.LocalCommitment
feeBalance := float64(
balance.ToSatoshis() + lc.channelState.LocalCommitment.CommitFee,
commit.LocalBalance.ToSatoshis() + commit.CommitFee,
)
maxFee := feeBalance * maxAllocation
// Ensure the fee rate doesn't dip below the fee floor.
_, weight := lc.availableBalance()
maxFeeRate := maxFee / (float64(weight) / 1000)
return chainfee.SatPerKWeight(
math.Max(maxFeeRate, float64(chainfee.FeePerKwFloor)),