lnwallet: modify CoopCloseBalance to not depend on chan commit

This commit is contained in:
Olaoluwa Osuntokun 2024-05-29 19:57:43 +02:00 committed by Oliver Gugger
parent e536cfad0f
commit 8b97d4f833
No known key found for this signature in database
GPG Key ID: 8E4256593F177720
2 changed files with 11 additions and 9 deletions

View File

@ -7781,7 +7781,10 @@ func (lc *LightningChannel) CreateCloseProposal(proposedFee btcutil.Amount,
// during the channel closing process.
ourBalance, theirBalance, err := CoopCloseBalance(
lc.channelState.ChanType, lc.channelState.IsInitiator,
proposedFee, lc.channelState.LocalCommitment,
proposedFee,
lc.channelState.LocalCommitment.LocalBalance.ToSatoshis(),
lc.channelState.LocalCommitment.RemoteBalance.ToSatoshis(),
lc.channelState.LocalCommitment.CommitFee,
)
if err != nil {
return nil, nil, 0, err
@ -7879,7 +7882,10 @@ func (lc *LightningChannel) CompleteCooperativeClose(
// Get the final balances after subtracting the proposed fee.
ourBalance, theirBalance, err := CoopCloseBalance(
lc.channelState.ChanType, lc.channelState.IsInitiator,
proposedFee, lc.channelState.LocalCommitment,
proposedFee,
lc.channelState.LocalCommitment.LocalBalance.ToSatoshis(),
lc.channelState.LocalCommitment.RemoteBalance.ToSatoshis(),
lc.channelState.LocalCommitment.CommitFee,
)
if err != nil {
return nil, 0, err

View File

@ -1033,16 +1033,12 @@ func CreateCommitTx(chanType channeldb.ChannelType,
// 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, error) {
// Get both parties' balances from the latest commitment.
ourBalance := localCommit.LocalBalance.ToSatoshis()
theirBalance := localCommit.RemoteBalance.ToSatoshis()
coopCloseFee, ourBalance, theirBalance,
commitFee btcutil.Amount) (btcutil.Amount, btcutil.Amount, error) {
// We'll make sure we account for the complete balance by adding the
// current dangling commitment fee to the balance of the initiator.
initiatorDelta := localCommit.CommitFee
initiatorDelta := commitFee
// Since the initiator's balance also is stored after subtracting the
// anchor values, add that back in case this was an anchor commitment.