From 8283ff2da6facf18c24ec27a4fa84a3a2c8e4a0e Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Mon, 27 Feb 2017 20:52:23 -0600 Subject: [PATCH] lnwallet: during cooperative closure don't shift status until end This commit avoids a class of bug wherein the state of the channel would be marked as closing enough though an error occurred somewhere in the function. The bug was due to the fact that the channel `status` was shifted before any actual logic within the function(s) were executed. We fix this bug by _only_ shifting the channel status once the function has completed without any error. --- lnwallet/channel.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 34775d07..5a36f44a 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -2234,10 +2234,6 @@ func (lc *LightningChannel) InitCooperativeClose() ([]byte, *chainhash.Hash, err return nil, nil, ErrChanClosing } - // Otherwise, indicate in the channel status that a channel closure has - // been initiated. - lc.status = channelClosing - closeTx := CreateCooperativeCloseTx(lc.fundingTxIn, lc.channelState.OurBalance, lc.channelState.TheirBalance, lc.channelState.OurDeliveryScript, lc.channelState.TheirDeliveryScript, @@ -2254,6 +2250,10 @@ func (lc *LightningChannel) InitCooperativeClose() ([]byte, *chainhash.Hash, err return nil, nil, err } + // As everything checks out, indicate in the channel status that a + // channel closure has been initiated. + lc.status = channelClosing + return closeSig, &closeTxSha, nil } @@ -2275,8 +2275,6 @@ func (lc *LightningChannel) CompleteCooperativeClose(remoteSig []byte) (*wire.Ms return nil, ErrChanClosing } - lc.status = channelClosed - // Create the transaction used to return the current settled balance // on this active channel back to both parties. In this current model, // the initiator pays full fees for the cooperative close transaction. @@ -2315,6 +2313,11 @@ func (lc *LightningChannel) CompleteCooperativeClose(remoteSig []byte) (*wire.Ms return nil, err } + // As the transaction is sane, and the scripts are valid we'll mark the + // channel now as closed as the closure transaction should get into the + // chain in a timely manner and possibly be re-broadcast by the wallet. + lc.status = channelClosed + return closeTx, nil }