Skip to content

Commit

Permalink
lnwallet/channel: take remote balance into availableBalance calculation
Browse files Browse the repository at this point in the history
Since the remote balance affect how much we are allowed to send (if they
are the initiator) because of the added HTLC fee, we do the available
balance caluclation also from their perspective and report our minimum
balance from the two commit views as our available balance.
  • Loading branch information
halseth committed Feb 3, 2020
1 parent e10386d commit c50dae4
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions lnwallet/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5974,9 +5974,19 @@ func (lc *LightningChannel) availableBalance() (lnwire.MilliSatoshi, int64) {
// 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,
htlcView, false,
)

// Do the same caluclation from the remote commitment point of view.
ourRemoteCommitBalance, _ := lc.availableCommitmentBalance(
htlcView, true,
)

// Return which ever balance is lowest.
if ourRemoteCommitBalance < ourLocalCommitBalance {
return ourRemoteCommitBalance, commitWeight
}

return ourLocalCommitBalance, commitWeight
}

Expand All @@ -5985,21 +5995,26 @@ func (lc *LightningChannel) availableBalance() (lnwire.MilliSatoshi, int64) {
// account for sending HTLCs of different sizes, it will take into account
// whether the HTLCs will be manifested on the commitment, increasing the
// commitment fee we must pay as an initiator, eating into our balance.
func (lc *LightningChannel) availableCommitmentBalance(view *htlcView) (
lnwire.MilliSatoshi, int64) {
func (lc *LightningChannel) availableCommitmentBalance(view *htlcView,
remoteChain bool) (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 := lc.computeView(
view, false, false,
view, remoteChain, false,
)
feePerKw := filteredView.feePerKw

// Set the dustlimit based on who's commitment we are looking at.
dustlimit := lnwire.NewMSatFromSatoshis(
lc.channelState.LocalChanCfg.DustLimit,
)
if remoteChain {
dustlimit = lnwire.NewMSatFromSatoshis(
lc.channelState.RemoteChanCfg.DustLimit,
)
}

// Given the commitment weight, find the commitment fee in case of no
// added HTLC ouput.
Expand All @@ -6019,6 +6034,13 @@ func (lc *LightningChannel) availableCommitmentBalance(view *htlcView) (
htlcTimeoutFee(feePerKw),
)

// On the remote commitment, the HTLC success transaction will be used.
if remoteChain {
htlcFee = lnwire.NewMSatFromSatoshis(
htlcSuccessFee(feePerKw),
)
}

// The HTLC output will be manifested on the commitment if it
// is non-dust after paying the HTLC fee.
nonDustHtlcAmt := dustlimit + htlcFee
Expand Down

0 comments on commit c50dae4

Please sign in to comment.