lnwallet: return our final local balance from CompleteCooperativeClose

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.
This commit is contained in:
Olaoluwa Osuntokun 2018-01-08 17:42:13 -08:00
parent fa6354b223
commit 95de109844
No known key found for this signature in database
GPG Key ID: 964EA263DD637C21
2 changed files with 16 additions and 15 deletions

@ -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 // To ensure that we can actually fully accept this new HTLC, we'll
// calculate the current available bandwidth, and subtract the value // calculate the current available bandwidth, and subtract the value of
// ofthe HTLC from it. // the HTLC from it.
initialBalance, _ := lc.availableBalance() initialBalance, _ := lc.availableBalance()
availableBalance := initialBalance availableBalance := initialBalance
availableBalance -= htlc.Amount availableBalance -= htlc.Amount
@ -4739,13 +4739,14 @@ func (lc *LightningChannel) CreateCloseProposal(proposedFee btcutil.Amount,
// CompleteCooperativeClose completes the cooperative closure of the target // CompleteCooperativeClose completes the cooperative closure of the target
// active lightning channel. A fully signed closure transaction as well as the // 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 // NOTE: The passed local and remote sigs are expected to be fully complete
// signatures including the proper sighash byte. // signatures including the proper sighash byte.
func (lc *LightningChannel) CompleteCooperativeClose(localSig, remoteSig, func (lc *LightningChannel) CompleteCooperativeClose(localSig, remoteSig []byte,
localDeliveryScript, remoteDeliveryScript []byte, localDeliveryScript, remoteDeliveryScript []byte,
proposedFee btcutil.Amount) (*wire.MsgTx, error) { proposedFee btcutil.Amount) (*wire.MsgTx, btcutil.Amount, error) {
lc.Lock() lc.Lock()
defer lc.Unlock() defer lc.Unlock()
@ -4753,7 +4754,7 @@ func (lc *LightningChannel) CompleteCooperativeClose(localSig, remoteSig,
// If the channel is already closed, then ignore this request. // If the channel is already closed, then ignore this request.
if lc.status == channelClosed { if lc.status == channelClosed {
// TODO(roasbeef): check to ensure no pending payments // 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 // Subtract the proposed fee from the appropriate balance, taking care
@ -4785,7 +4786,7 @@ func (lc *LightningChannel) CompleteCooperativeClose(localSig, remoteSig,
// negative output. // negative output.
tx := btcutil.NewTx(closeTx) tx := btcutil.NewTx(closeTx)
if err := blockchain.CheckTransactionSanity(tx); err != nil { if err := blockchain.CheckTransactionSanity(tx); err != nil {
return nil, err return nil, 0, err
} }
hashCache := txscript.NewTxSigHashes(closeTx) hashCache := txscript.NewTxSigHashes(closeTx)
@ -4803,10 +4804,10 @@ func (lc *LightningChannel) CompleteCooperativeClose(localSig, remoteSig,
txscript.StandardVerifyFlags, nil, hashCache, txscript.StandardVerifyFlags, nil, hashCache,
int64(lc.channelState.Capacity)) int64(lc.channelState.Capacity))
if err != nil { if err != nil {
return nil, err return nil, 0, err
} }
if err := vm.Execute(); err != nil { 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 // 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. // chain in a timely manner and possibly be re-broadcast by the wallet.
lc.status = channelClosed lc.status = channelClosed
return closeTx, nil return closeTx, ourBalance, nil
} }
// DeleteState deletes all state concerning the channel from the underlying // DeleteState deletes all state concerning the channel from the underlying

@ -904,7 +904,7 @@ func TestCooperativeChannelClosure(t *testing.T) {
// With the proposals created, both sides should be able to properly // With the proposals created, both sides should be able to properly
// process the other party's signature. This indicates that the // process the other party's signature. This indicates that the
// transaction is well formed, and the signatures verify. // transaction is well formed, and the signatures verify.
aliceCloseTx, err := bobChannel.CompleteCooperativeClose( aliceCloseTx, _, err := bobChannel.CompleteCooperativeClose(
bobCloseSig, aliceCloseSig, bobDeliveryScript, bobCloseSig, aliceCloseSig, bobDeliveryScript,
aliceDeliveryScript, bobFee) aliceDeliveryScript, bobFee)
if err != nil { if err != nil {
@ -912,7 +912,7 @@ func TestCooperativeChannelClosure(t *testing.T) {
} }
bobCloseSha := aliceCloseTx.TxHash() bobCloseSha := aliceCloseTx.TxHash()
bobCloseTx, err := aliceChannel.CompleteCooperativeClose( bobCloseTx, _, err := aliceChannel.CompleteCooperativeClose(
aliceCloseSig, bobCloseSig, aliceDeliveryScript, aliceCloseSig, bobCloseSig, aliceDeliveryScript,
bobDeliveryScript, aliceFee) bobDeliveryScript, aliceFee)
if err != nil { if err != nil {
@ -2019,7 +2019,7 @@ func TestCooperativeCloseDustAdherence(t *testing.T) {
} }
bobCloseSig := append(bobSig, byte(txscript.SigHashAll)) bobCloseSig := append(bobSig, byte(txscript.SigHashAll))
closeTx, err := bobChannel.CompleteCooperativeClose( closeTx, _, err := bobChannel.CompleteCooperativeClose(
bobCloseSig, aliceCloseSig, bobCloseSig, aliceCloseSig,
bobDeliveryScript, aliceDeliveryScript, bobFee) bobDeliveryScript, aliceDeliveryScript, bobFee)
if err != nil { if err != nil {
@ -2057,7 +2057,7 @@ func TestCooperativeCloseDustAdherence(t *testing.T) {
} }
bobCloseSig = append(bobSig, byte(txscript.SigHashAll)) bobCloseSig = append(bobSig, byte(txscript.SigHashAll))
closeTx, err = bobChannel.CompleteCooperativeClose( closeTx, _, err = bobChannel.CompleteCooperativeClose(
bobCloseSig, aliceCloseSig, bobCloseSig, aliceCloseSig,
bobDeliveryScript, aliceDeliveryScript, bobFee) bobDeliveryScript, aliceDeliveryScript, bobFee)
if err != nil { if err != nil {
@ -2099,7 +2099,7 @@ func TestCooperativeCloseDustAdherence(t *testing.T) {
} }
bobCloseSig = append(bobSig, byte(txscript.SigHashAll)) bobCloseSig = append(bobSig, byte(txscript.SigHashAll))
closeTx, err = bobChannel.CompleteCooperativeClose( closeTx, _, err = bobChannel.CompleteCooperativeClose(
bobCloseSig, aliceCloseSig, bobCloseSig, aliceCloseSig,
bobDeliveryScript, aliceDeliveryScript, bobFee) bobDeliveryScript, aliceDeliveryScript, bobFee)
if err != nil { if err != nil {