channel: properly roll over fee from commit tx during co-op chan close

In this commit, we fix an existing bug within our cooperative channel
closing transaction generation. Before this commit, we wouldn’t account
for the fee already allocated within the commitment transaction. As a
result, we would calculate the evaluated balance considering the fee
incorrectly. In this commit, we fix this by adding the commitment fee
to the balance of the initiator when crafting the closing transaction
This commit is contained in:
Olaoluwa Osuntokun 2017-11-29 16:14:17 -08:00
parent 4b05bad4f1
commit 94ba7f964d
No known key found for this signature in database
GPG Key ID: 964EA263DD637C21
2 changed files with 17 additions and 9 deletions

View File

@ -4603,10 +4603,13 @@ func (lc *LightningChannel) CreateCloseProposal(proposedFee btcutil.Amount,
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
ourBalance = ourBalance - proposedFee + commitFee
} else {
theirBalance = theirBalance - proposedFee
theirBalance = theirBalance - proposedFee + commitFee
}
closeTx := CreateCooperativeCloseTx(lc.fundingTxIn,
@ -4665,10 +4668,13 @@ func (lc *LightningChannel) CompleteCooperativeClose(localSig, remoteSig,
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
ourBalance = ourBalance - proposedFee + commitFee
} else {
theirBalance = theirBalance - proposedFee
theirBalance = theirBalance - proposedFee + commitFee
}
// Create the transaction used to return the current settled balance

View File

@ -1807,7 +1807,7 @@ func TestCooperativeCloseDustAdherence(t *testing.T) {
// Both sides currently have over 1 BTC settled as part of their
// balances. As a result, performing a cooperative closure now result
// in both sides having an output within the closure transaction.
aliceFee := btcutil.Amount(aliceChannel.CalcFee(aliceFeeRate))
aliceFee := btcutil.Amount(aliceChannel.CalcFee(aliceFeeRate)) + 1000
aliceSig, err := aliceChannel.CreateCloseProposal(aliceFee,
aliceDeliveryScript, bobDeliveryScript)
if err != nil {
@ -1815,7 +1815,7 @@ func TestCooperativeCloseDustAdherence(t *testing.T) {
}
aliceCloseSig := append(aliceSig, byte(txscript.SigHashAll))
bobFee := btcutil.Amount(bobChannel.CalcFee(bobFeeRate))
bobFee := btcutil.Amount(bobChannel.CalcFee(bobFeeRate)) + 1000
bobSig, err := bobChannel.CreateCloseProposal(bobFee,
bobDeliveryScript, aliceDeliveryScript)
if err != nil {
@ -1874,10 +1874,12 @@ func TestCooperativeCloseDustAdherence(t *testing.T) {
t.Fatalf("close tx has wrong number of outputs: expected %v "+
"got %v", 1, len(closeTx.TxOut))
}
if closeTx.TxOut[0].Value != int64(aliceBal.ToSatoshis()-calcStaticFee(0)) {
commitFee := aliceChannel.channelState.LocalCommitment.CommitFee
aliceExpectedBalance := aliceBal.ToSatoshis() - aliceFee + commitFee
if closeTx.TxOut[0].Value != int64(aliceExpectedBalance) {
t.Fatalf("alice's balance is incorrect: expected %v, got %v",
int64(aliceBal.ToSatoshis()-calcStaticFee(0)),
closeTx.TxOut[0].Value)
aliceExpectedBalance,
int64(closeTx.TxOut[0].Value))
}
// Finally, we'll modify the current balances and dust limits such that