From 94ba7f964d8ba586e4a812d966c98f6d94f00160 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Wed, 29 Nov 2017 16:14:17 -0800 Subject: [PATCH] channel: properly roll over fee from commit tx during co-op chan close MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- lnwallet/channel.go | 14 ++++++++++---- lnwallet/channel_test.go | 12 +++++++----- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 55cf9fb6..1eb1b0f1 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -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 diff --git a/lnwallet/channel_test.go b/lnwallet/channel_test.go index 0a44c212..b8b603dc 100644 --- a/lnwallet/channel_test.go +++ b/lnwallet/channel_test.go @@ -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