lnwallet: extract coop close balance calc into method
This commit is contained in:
parent
724f439b0b
commit
07a57ae778
@ -6179,21 +6179,13 @@ func (lc *LightningChannel) CreateCloseProposal(proposedFee btcutil.Amount,
|
|||||||
return nil, nil, 0, ErrChanClosing
|
return nil, nil, 0, ErrChanClosing
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subtract the proposed fee from the appropriate balance, taking care
|
// Get the final balances after subtracting the proposed fee, taking
|
||||||
// not to persist the adjusted balance, as the feeRate may change
|
// care not to persist the adjusted balance, as the feeRate may change
|
||||||
// during the channel closing process.
|
// during the channel closing process.
|
||||||
localCommit := lc.channelState.LocalCommitment
|
ourBalance, theirBalance := CoopCloseBalance(
|
||||||
ourBalance := localCommit.LocalBalance.ToSatoshis()
|
lc.channelState.ChanType, lc.channelState.IsInitiator,
|
||||||
theirBalance := localCommit.RemoteBalance.ToSatoshis()
|
proposedFee, lc.channelState.LocalCommitment,
|
||||||
|
)
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
|
||||||
closeTx := CreateCooperativeCloseTx(
|
closeTx := CreateCooperativeCloseTx(
|
||||||
fundingTxIn(lc.channelState), lc.channelState.LocalChanCfg.DustLimit,
|
fundingTxIn(lc.channelState), lc.channelState.LocalChanCfg.DustLimit,
|
||||||
@ -6248,21 +6240,11 @@ func (lc *LightningChannel) CompleteCooperativeClose(
|
|||||||
return nil, 0, ErrChanClosing
|
return nil, 0, ErrChanClosing
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subtract the proposed fee from the appropriate balance, taking care
|
// Get the final balances after subtracting the proposed fee.
|
||||||
// not to persist the adjusted balance, as the feeRate may change
|
ourBalance, theirBalance := CoopCloseBalance(
|
||||||
// during the channel closing process.
|
lc.channelState.ChanType, lc.channelState.IsInitiator,
|
||||||
localCommit := lc.channelState.LocalCommitment
|
proposedFee, 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
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the transaction used to return the current settled balance
|
// Create the transaction used to return the current settled balance
|
||||||
// on this active channel back to both parties. In this current model,
|
// on this active channel back to both parties. In this current model,
|
||||||
|
@ -663,6 +663,28 @@ func CreateCommitTx(chanType channeldb.ChannelType,
|
|||||||
return commitTx, nil
|
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
|
// 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
|
// 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.
|
// HTLC is being applied to their commitment transaction or ours.
|
||||||
|
Loading…
Reference in New Issue
Block a user