mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-03-04 09:48:19 +01:00
lnwallet/channel: break up availableBalance
This commit is contained in:
parent
0d9a1b8656
commit
58dec10680
1 changed files with 46 additions and 12 deletions
|
@ -5997,11 +5997,14 @@ func (lc *LightningChannel) CompleteCooperativeClose(localSig, remoteSig []byte,
|
||||||
return closeTx, ourBalance, nil
|
return closeTx, ourBalance, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AvailableBalance returns the current available balance within the channel.
|
// AvailableBalance returns the current balance available for sending within
|
||||||
// By available balance, we mean that if at this very instance s new commitment
|
// the channel. By available balance, we mean that if at this very instance a
|
||||||
// were to be created which evals all the log entries, what would our available
|
// new commitment were to be created which evals all the log entries, what
|
||||||
// balance me. This method is useful when deciding if a given channel can
|
// would our available balance for adding an additional HTLC be. It takes into
|
||||||
// accept an HTLC in the multi-hop forwarding scenario.
|
// account the fee that must be paid for adding this HTLC (if we're the
|
||||||
|
// initiator), and that we cannot spend from the channel reserve. This method
|
||||||
|
// is useful when deciding if a given channel can accept an HTLC in the
|
||||||
|
// multi-hop forwarding scenario.
|
||||||
func (lc *LightningChannel) AvailableBalance() lnwire.MilliSatoshi {
|
func (lc *LightningChannel) AvailableBalance() lnwire.MilliSatoshi {
|
||||||
lc.RLock()
|
lc.RLock()
|
||||||
defer lc.RUnlock()
|
defer lc.RUnlock()
|
||||||
|
@ -6021,19 +6024,50 @@ func (lc *LightningChannel) availableBalance() (lnwire.MilliSatoshi, int64) {
|
||||||
htlcView := lc.fetchHTLCView(remoteACKedIndex,
|
htlcView := lc.fetchHTLCView(remoteACKedIndex,
|
||||||
lc.localUpdateLog.logIndex)
|
lc.localUpdateLog.logIndex)
|
||||||
|
|
||||||
// Then compute our current balance for that view.
|
// Calculate our available balance from our local commitment.
|
||||||
ourBalance, _, commitWeight, filteredView, err :=
|
//
|
||||||
lc.computeView(htlcView, false, false)
|
// NOTE: This is not always accurate, since the remote node can always
|
||||||
|
// add updates concurrently, causing our balance to go down if we're
|
||||||
|
// the initiator, but this is a problem on the protocol level.
|
||||||
|
ourLocalCommitBalance, commitWeight := lc.availableCommitmentBalance(
|
||||||
|
htlcView,
|
||||||
|
)
|
||||||
|
|
||||||
|
return ourLocalCommitBalance, commitWeight
|
||||||
|
}
|
||||||
|
|
||||||
|
// availableCommitmentBalance attempts to calculate the balance we have
|
||||||
|
// available for HTLCs on the local/remote commitment given the htlcView. To
|
||||||
|
// account for sending HTLCs of different sizes, it will report the balance
|
||||||
|
// available for sending non-dust HTLCs, which will be manifested on the
|
||||||
|
// commitment, increasing the commitment fee we must pay as an initiator,
|
||||||
|
// eating into our balance. It will make sure we won't violate the channel
|
||||||
|
// reserve constraints for this amount.
|
||||||
|
func (lc *LightningChannel) availableCommitmentBalance(view *htlcView) (
|
||||||
|
lnwire.MilliSatoshi, int64) {
|
||||||
|
|
||||||
|
// Compute the current balances for this commitment. This will take
|
||||||
|
// into account HTLCs to determine the commit weight, which the
|
||||||
|
// initiator must pay the fee for.
|
||||||
|
ourBalance, _, commitWeight, filteredView, err := lc.computeView(
|
||||||
|
view, false, false,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.log.Errorf("Unable to fetch available balance: %v", err)
|
lc.log.Errorf("Unable to fetch available balance: %v", err)
|
||||||
return 0, 0
|
return 0, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we are the channel initiator, we must remember to subtract the
|
// Given the commitment weight, find the commitment fee in case of no
|
||||||
// commitment fee from our available balance.
|
// added HTLC output.
|
||||||
commitFee := filteredView.feePerKw.FeeForWeight(commitWeight)
|
feePerKw := filteredView.feePerKw
|
||||||
|
baseCommitFee := lnwire.NewMSatFromSatoshis(
|
||||||
|
feePerKw.FeeForWeight(commitWeight),
|
||||||
|
)
|
||||||
|
|
||||||
|
// If we are the channel initiator, we must to subtract the commitment
|
||||||
|
// fee from our available balance.
|
||||||
if lc.channelState.IsInitiator {
|
if lc.channelState.IsInitiator {
|
||||||
ourBalance -= lnwire.NewMSatFromSatoshis(commitFee)
|
ourBalance -= baseCommitFee
|
||||||
}
|
}
|
||||||
|
|
||||||
return ourBalance, commitWeight
|
return ourBalance, commitWeight
|
||||||
|
|
Loading…
Add table
Reference in a new issue