lnwallet: check coop close fee negative balance
Also modify the test to check for this condition.
This commit is contained in:
parent
09a126b29f
commit
a48c369250
@ -6182,10 +6182,13 @@ func (lc *LightningChannel) CreateCloseProposal(proposedFee btcutil.Amount,
|
|||||||
// Get the final balances after subtracting the proposed fee, taking
|
// Get the final balances after subtracting the proposed fee, taking
|
||||||
// care 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.
|
||||||
ourBalance, theirBalance := CoopCloseBalance(
|
ourBalance, theirBalance, err := CoopCloseBalance(
|
||||||
lc.channelState.ChanType, lc.channelState.IsInitiator,
|
lc.channelState.ChanType, lc.channelState.IsInitiator,
|
||||||
proposedFee, lc.channelState.LocalCommitment,
|
proposedFee, lc.channelState.LocalCommitment,
|
||||||
)
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
closeTx := CreateCooperativeCloseTx(
|
closeTx := CreateCooperativeCloseTx(
|
||||||
fundingTxIn(lc.channelState), lc.channelState.LocalChanCfg.DustLimit,
|
fundingTxIn(lc.channelState), lc.channelState.LocalChanCfg.DustLimit,
|
||||||
@ -6241,10 +6244,13 @@ func (lc *LightningChannel) CompleteCooperativeClose(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get the final balances after subtracting the proposed fee.
|
// Get the final balances after subtracting the proposed fee.
|
||||||
ourBalance, theirBalance := CoopCloseBalance(
|
ourBalance, theirBalance, err := CoopCloseBalance(
|
||||||
lc.channelState.ChanType, lc.channelState.IsInitiator,
|
lc.channelState.ChanType, lc.channelState.IsInitiator,
|
||||||
proposedFee, lc.channelState.LocalCommitment,
|
proposedFee, lc.channelState.LocalCommitment,
|
||||||
)
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
// 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,
|
||||||
|
@ -2230,11 +2230,11 @@ func TestCooperativeCloseDustAdherence(t *testing.T) {
|
|||||||
"got %v", 2, len(closeTx.TxOut))
|
"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()
|
resetChannelState()
|
||||||
|
|
||||||
// Next we'll modify the current balances and dust limits such that
|
// 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)
|
aliceBal := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin)
|
||||||
bobBal := lnwire.NewMSatFromSatoshis(250)
|
bobBal := lnwire.NewMSatFromSatoshis(250)
|
||||||
setBalances(aliceBal, bobBal)
|
setBalances(aliceBal, bobBal)
|
||||||
@ -2277,11 +2277,26 @@ func TestCooperativeCloseDustAdherence(t *testing.T) {
|
|||||||
int64(closeTx.TxOut[0].Value))
|
int64(closeTx.TxOut[0].Value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finally, we'll modify the current balances and dust limits such that
|
// We'll modify the current balances and dust limits such that
|
||||||
// Alice's current balance is _below_ his her limit.
|
// Alice's current balance is too low to pay the proposed fee.
|
||||||
setBalances(bobBal, aliceBal)
|
setBalances(bobBal, aliceBal)
|
||||||
resetChannelState()
|
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
|
// Our final attempt at another cooperative channel closure. It should
|
||||||
// succeed without any issues.
|
// succeed without any issues.
|
||||||
aliceSig, _, _, err = aliceChannel.CreateCloseProposal(
|
aliceSig, _, _, err = aliceChannel.CreateCloseProposal(
|
||||||
|
@ -667,7 +667,7 @@ func CreateCommitTx(chanType channeldb.ChannelType,
|
|||||||
// the cooperative close tx, given the channel type and transaction fee.
|
// the cooperative close tx, given the channel type and transaction fee.
|
||||||
func CoopCloseBalance(chanType channeldb.ChannelType, isInitiator bool,
|
func CoopCloseBalance(chanType channeldb.ChannelType, isInitiator bool,
|
||||||
coopCloseFee btcutil.Amount, localCommit channeldb.ChannelCommitment) (
|
coopCloseFee btcutil.Amount, localCommit channeldb.ChannelCommitment) (
|
||||||
btcutil.Amount, btcutil.Amount) {
|
btcutil.Amount, btcutil.Amount, error) {
|
||||||
|
|
||||||
// Get both parties' balances from the latest commitment.
|
// Get both parties' balances from the latest commitment.
|
||||||
ourBalance := localCommit.LocalBalance.ToSatoshis()
|
ourBalance := localCommit.LocalBalance.ToSatoshis()
|
||||||
@ -693,7 +693,15 @@ func CoopCloseBalance(chanType channeldb.ChannelType, isInitiator bool,
|
|||||||
theirBalance += initiatorDelta
|
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
|
// genHtlcScript generates the proper P2WSH public key scripts for the HTLC
|
||||||
|
Loading…
Reference in New Issue
Block a user