diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 94e26805..30deb5a2 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -6179,21 +6179,13 @@ func (lc *LightningChannel) CreateCloseProposal(proposedFee btcutil.Amount, return nil, nil, 0, ErrChanClosing } - // Subtract the proposed fee from the appropriate balance, taking care - // not to persist the adjusted balance, as the feeRate may change + // Get the final balances after subtracting the proposed fee, taking + // care not to persist the adjusted balance, as the feeRate may change // during the channel closing process. - localCommit := lc.channelState.LocalCommitment - ourBalance := localCommit.LocalBalance.ToSatoshis() - theirBalance := localCommit.RemoteBalance.ToSatoshis() - - // We'll make sure we account for the complete balance by adding the - // current dangling commitment fee to the balance of the initiator. - commitFee := localCommit.CommitFee - if lc.channelState.IsInitiator { - ourBalance = ourBalance - proposedFee + commitFee - } else { - theirBalance = theirBalance - proposedFee + commitFee - } + ourBalance, theirBalance := CoopCloseBalance( + lc.channelState.ChanType, lc.channelState.IsInitiator, + proposedFee, lc.channelState.LocalCommitment, + ) closeTx := CreateCooperativeCloseTx( fundingTxIn(lc.channelState), lc.channelState.LocalChanCfg.DustLimit, @@ -6248,21 +6240,11 @@ func (lc *LightningChannel) CompleteCooperativeClose( return nil, 0, ErrChanClosing } - // Subtract the proposed fee from the appropriate balance, taking care - // not to persist the adjusted balance, as the feeRate may change - // during the channel closing process. - localCommit := lc.channelState.LocalCommitment - ourBalance := localCommit.LocalBalance.ToSatoshis() - theirBalance := localCommit.RemoteBalance.ToSatoshis() - - // We'll make sure we account for the complete balance by adding the - // current dangling commitment fee to the balance of the initiator. - commitFee := localCommit.CommitFee - if lc.channelState.IsInitiator { - ourBalance = ourBalance - proposedFee + commitFee - } else { - theirBalance = theirBalance - proposedFee + commitFee - } + // Get the final balances after subtracting the proposed fee. + ourBalance, theirBalance := CoopCloseBalance( + lc.channelState.ChanType, lc.channelState.IsInitiator, + proposedFee, lc.channelState.LocalCommitment, + ) // Create the transaction used to return the current settled balance // on this active channel back to both parties. In this current model, diff --git a/lnwallet/commitment.go b/lnwallet/commitment.go index d70f3ca0..ed7121d5 100644 --- a/lnwallet/commitment.go +++ b/lnwallet/commitment.go @@ -663,6 +663,28 @@ func CreateCommitTx(chanType channeldb.ChannelType, return commitTx, nil } +// CoopCloseBalance returns the final balances that should be used to create +// the cooperative close tx, given the channel type and transaction fee. +func CoopCloseBalance(chanType channeldb.ChannelType, isInitiator bool, + coopCloseFee btcutil.Amount, localCommit channeldb.ChannelCommitment) ( + btcutil.Amount, btcutil.Amount) { + + // Get both parties' balances from the latest commitment. + ourBalance := localCommit.LocalBalance.ToSatoshis() + theirBalance := localCommit.RemoteBalance.ToSatoshis() + + // We'll make sure we account for the complete balance by adding the + // current dangling commitment fee to the balance of the initiator. + commitFee := localCommit.CommitFee + if isInitiator { + ourBalance = ourBalance - coopCloseFee + commitFee + } else { + theirBalance = theirBalance - coopCloseFee + commitFee + } + + return ourBalance, theirBalance +} + // genHtlcScript generates the proper P2WSH public key scripts for the HTLC // output modified by two-bits denoting if this is an incoming HTLC, and if the // HTLC is being applied to their commitment transaction or ours.