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.
This commit is contained in:
Olaoluwa Osuntokun 2017-02-27 20:52:23 -06:00
parent b91dae7eaf
commit 8283ff2da6
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2

View File

@ -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
}