From 8b97d4f833f0462893a6a21144ff0cb5500ee38c Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Wed, 29 May 2024 19:57:43 +0200 Subject: [PATCH] lnwallet: modify CoopCloseBalance to not depend on chan commit --- lnwallet/channel.go | 10 ++++++++-- lnwallet/commitment.go | 10 +++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 0e6300a0e..8f7513f10 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -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 diff --git a/lnwallet/commitment.go b/lnwallet/commitment.go index 73565ea18..170efece1 100644 --- a/lnwallet/commitment.go +++ b/lnwallet/commitment.go @@ -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.