lnwallet/channel: account for HTLC fee when reporting available balance

This commit is contained in:
Johan T. Halseth committed 2020-02-19 12:27:42 +01:00
1 parent 5e89d5b6c2
commit 9ff79ae595
3 files changed
+166 -18

No files matched your search

+18 -8
View File
@@ -6068,20 +6068,30 @@ func (lc *LightningChannel) availableCommitmentBalance(view *htlcView) (
ourBalance = 0
}
// Given the commitment weight, find the commitment fee in case of no
// added HTLC output.
// Calculate the commitment fee in the case where we would add another
// HTLC to the commitment, as only the balance remaining after this fee
// has been paid is actually available for sending.
feePerKw := filteredView.feePerKw
baseCommitFee := lnwire.NewMSatFromSatoshis(
feePerKw.FeeForWeight(commitWeight),
htlcCommitFee := lnwire.NewMSatFromSatoshis(
feePerKw.FeeForWeight(commitWeight + input.HTLCWeight),
)
// If we are the channel initiator, we must to subtract the commitment
// fee from our available balance.
// If we are the channel initiator, we must to subtract this commitment
// fee from our available balance in order to ensure we can afford both
// the value of the HTLC and the additional commitment fee from adding
// the HTLC.
if lc.channelState.IsInitiator {
if ourBalance < baseCommitFee {
// There is an edge case where our non-zero balance is lower
// than the htlcCommitFee, where we could still be sending dust
// HTLCs, but we return 0 in this case. This is to avoid
// lowering our balance even further, as this takes us into a
// bad state wehere neither we nor our channel counterparty can
// add HTLCs.
if ourBalance < htlcCommitFee {
return 0, commitWeight
}
ourBalance -= baseCommitFee
ourBalance -= htlcCommitFee
}
return ourBalance, commitWeight