From 95de1098447bb4bd417227300cde86a94910efb3 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Mon, 8 Jan 2018 17:42:13 -0800 Subject: [PATCH] lnwallet: return our final local balance from CompleteCooperativeClose MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In this commit, add an additional return value to CompleteCooperativeClose. We’ll now report to the caller our final balance in the cooperative closure transaction. We report this as depending on if we’re the initiator or not, our final balance may not exactly match the balance we had in the last state. --- lnwallet/channel.go | 21 +++++++++++---------- lnwallet/channel_test.go | 10 +++++----- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 46cd97eb..05d8b847 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -3930,8 +3930,8 @@ func (lc *LightningChannel) AddHTLC(htlc *lnwire.UpdateAddHTLC) (uint64, error) } // To ensure that we can actually fully accept this new HTLC, we'll - // calculate the current available bandwidth, and subtract the value - // ofthe HTLC from it. + // calculate the current available bandwidth, and subtract the value of + // the HTLC from it. initialBalance, _ := lc.availableBalance() availableBalance := initialBalance availableBalance -= htlc.Amount @@ -4739,13 +4739,14 @@ func (lc *LightningChannel) CreateCloseProposal(proposedFee btcutil.Amount, // CompleteCooperativeClose completes the cooperative closure of the target // active lightning channel. A fully signed closure transaction as well as the -// signature itself are returned. +// signature itself are returned. Additionally, we also return our final +// settled balance, which reflects any fees we may have paid. // // NOTE: The passed local and remote sigs are expected to be fully complete // signatures including the proper sighash byte. -func (lc *LightningChannel) CompleteCooperativeClose(localSig, remoteSig, +func (lc *LightningChannel) CompleteCooperativeClose(localSig, remoteSig []byte, localDeliveryScript, remoteDeliveryScript []byte, - proposedFee btcutil.Amount) (*wire.MsgTx, error) { + proposedFee btcutil.Amount) (*wire.MsgTx, btcutil.Amount, error) { lc.Lock() defer lc.Unlock() @@ -4753,7 +4754,7 @@ func (lc *LightningChannel) CompleteCooperativeClose(localSig, remoteSig, // If the channel is already closed, then ignore this request. if lc.status == channelClosed { // TODO(roasbeef): check to ensure no pending payments - return nil, ErrChanClosing + return nil, 0, ErrChanClosing } // Subtract the proposed fee from the appropriate balance, taking care @@ -4785,7 +4786,7 @@ func (lc *LightningChannel) CompleteCooperativeClose(localSig, remoteSig, // negative output. tx := btcutil.NewTx(closeTx) if err := blockchain.CheckTransactionSanity(tx); err != nil { - return nil, err + return nil, 0, err } hashCache := txscript.NewTxSigHashes(closeTx) @@ -4803,10 +4804,10 @@ func (lc *LightningChannel) CompleteCooperativeClose(localSig, remoteSig, txscript.StandardVerifyFlags, nil, hashCache, int64(lc.channelState.Capacity)) if err != nil { - return nil, err + return nil, 0, err } if err := vm.Execute(); err != nil { - return nil, err + return nil, 0, err } // As the transaction is sane, and the scripts are valid we'll mark the @@ -4814,7 +4815,7 @@ func (lc *LightningChannel) CompleteCooperativeClose(localSig, remoteSig, // chain in a timely manner and possibly be re-broadcast by the wallet. lc.status = channelClosed - return closeTx, nil + return closeTx, ourBalance, nil } // DeleteState deletes all state concerning the channel from the underlying diff --git a/lnwallet/channel_test.go b/lnwallet/channel_test.go index 9ad4a4ca..5feed1f3 100644 --- a/lnwallet/channel_test.go +++ b/lnwallet/channel_test.go @@ -904,7 +904,7 @@ func TestCooperativeChannelClosure(t *testing.T) { // With the proposals created, both sides should be able to properly // process the other party's signature. This indicates that the // transaction is well formed, and the signatures verify. - aliceCloseTx, err := bobChannel.CompleteCooperativeClose( + aliceCloseTx, _, err := bobChannel.CompleteCooperativeClose( bobCloseSig, aliceCloseSig, bobDeliveryScript, aliceDeliveryScript, bobFee) if err != nil { @@ -912,7 +912,7 @@ func TestCooperativeChannelClosure(t *testing.T) { } bobCloseSha := aliceCloseTx.TxHash() - bobCloseTx, err := aliceChannel.CompleteCooperativeClose( + bobCloseTx, _, err := aliceChannel.CompleteCooperativeClose( aliceCloseSig, bobCloseSig, aliceDeliveryScript, bobDeliveryScript, aliceFee) if err != nil { @@ -2019,7 +2019,7 @@ func TestCooperativeCloseDustAdherence(t *testing.T) { } bobCloseSig := append(bobSig, byte(txscript.SigHashAll)) - closeTx, err := bobChannel.CompleteCooperativeClose( + closeTx, _, err := bobChannel.CompleteCooperativeClose( bobCloseSig, aliceCloseSig, bobDeliveryScript, aliceDeliveryScript, bobFee) if err != nil { @@ -2057,7 +2057,7 @@ func TestCooperativeCloseDustAdherence(t *testing.T) { } bobCloseSig = append(bobSig, byte(txscript.SigHashAll)) - closeTx, err = bobChannel.CompleteCooperativeClose( + closeTx, _, err = bobChannel.CompleteCooperativeClose( bobCloseSig, aliceCloseSig, bobDeliveryScript, aliceDeliveryScript, bobFee) if err != nil { @@ -2099,7 +2099,7 @@ func TestCooperativeCloseDustAdherence(t *testing.T) { } bobCloseSig = append(bobSig, byte(txscript.SigHashAll)) - closeTx, err = bobChannel.CompleteCooperativeClose( + closeTx, _, err = bobChannel.CompleteCooperativeClose( bobCloseSig, aliceCloseSig, bobDeliveryScript, aliceDeliveryScript, bobFee) if err != nil {