lnwallet: check coop close fee negative balance

Also modify the test to check for this condition.
This commit is contained in:
Johan T. Halseth 2020-08-24 15:44:13 +02:00
parent 09a126b29f
commit a48c369250
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26
3 changed files with 37 additions and 8 deletions

@ -6182,10 +6182,13 @@ func (lc *LightningChannel) CreateCloseProposal(proposedFee btcutil.Amount,
// 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.
ourBalance, theirBalance := CoopCloseBalance(
ourBalance, theirBalance, err := CoopCloseBalance(
lc.channelState.ChanType, lc.channelState.IsInitiator,
proposedFee, lc.channelState.LocalCommitment,
)
if err != nil {
return nil, nil, 0, err
}
closeTx := CreateCooperativeCloseTx(
fundingTxIn(lc.channelState), lc.channelState.LocalChanCfg.DustLimit,
@ -6241,10 +6244,13 @@ func (lc *LightningChannel) CompleteCooperativeClose(
}
// Get the final balances after subtracting the proposed fee.
ourBalance, theirBalance := CoopCloseBalance(
ourBalance, theirBalance, err := CoopCloseBalance(
lc.channelState.ChanType, lc.channelState.IsInitiator,
proposedFee, lc.channelState.LocalCommitment,
)
if err != nil {
return nil, 0, err
}
// Create the transaction used to return the current settled balance
// on this active channel back to both parties. In this current model,

@ -2230,11 +2230,11 @@ func TestCooperativeCloseDustAdherence(t *testing.T) {
"got %v", 2, len(closeTx.TxOut))
}
// We'll reset the channel states before proceeding to our nest test.
// We'll reset the channel states before proceeding to our next test.
resetChannelState()
// Next we'll modify the current balances and dust limits such that
// Bob's current balance is above _below_ his dust limit.
// Bob's current balance is _below_ his dust limit.
aliceBal := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin)
bobBal := lnwire.NewMSatFromSatoshis(250)
setBalances(aliceBal, bobBal)
@ -2277,11 +2277,26 @@ func TestCooperativeCloseDustAdherence(t *testing.T) {
int64(closeTx.TxOut[0].Value))
}
// Finally, we'll modify the current balances and dust limits such that
// Alice's current balance is _below_ his her limit.
// We'll modify the current balances and dust limits such that
// Alice's current balance is too low to pay the proposed fee.
setBalances(bobBal, aliceBal)
resetChannelState()
// Attempting to close with this fee now should fail, since Alice
// cannot afford it.
_, _, _, err = aliceChannel.CreateCloseProposal(
aliceFee, aliceDeliveryScript, bobDeliveryScript,
)
if err == nil {
t.Fatalf("expected error")
}
// Finally, we'll modify the current balances and dust limits such that
// Alice's balance after paying the coop fee is _below_ her dust limit.
lowBalance := lnwire.NewMSatFromSatoshis(aliceFee) + 1000
setBalances(lowBalance, aliceBal)
resetChannelState()
// Our final attempt at another cooperative channel closure. It should
// succeed without any issues.
aliceSig, _, _, err = aliceChannel.CreateCloseProposal(

@ -667,7 +667,7 @@ func CreateCommitTx(chanType channeldb.ChannelType,
// 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) {
btcutil.Amount, btcutil.Amount, error) {
// Get both parties' balances from the latest commitment.
ourBalance := localCommit.LocalBalance.ToSatoshis()
@ -693,7 +693,15 @@ func CoopCloseBalance(chanType channeldb.ChannelType, isInitiator bool,
theirBalance += initiatorDelta
}
return ourBalance, theirBalance
// During fee negotiation it should always be verified that the
// initiator can pay the proposed fee, but we do a sanity check just to
// be sure here.
if ourBalance < 0 || theirBalance < 0 {
return 0, 0, fmt.Errorf("initiator cannot afford proposed " +
"coop close fee")
}
return ourBalance, theirBalance, nil
}
// genHtlcScript generates the proper P2WSH public key scripts for the HTLC