contractcourt: move responsibility for closing coop close channel to chanArb

This commit is contained in:
Johan T. Halseth 2018-08-21 12:21:15 +02:00
parent 3549f2bc17
commit ad1d78884a
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26
2 changed files with 50 additions and 26 deletions

@ -23,6 +23,12 @@ type LocalUnilateralCloseInfo struct {
*lnwallet.LocalForceCloseSummary *lnwallet.LocalForceCloseSummary
} }
// CooperativeCloseInfo encapsulates all the informnation we need to act
// on a cooperative close that gets confirmed.
type CooperativeCloseInfo struct {
*channeldb.ChannelCloseSummary
}
// ChainEventSubscription is a struct that houses a subscription to be notified // ChainEventSubscription is a struct that houses a subscription to be notified
// for any on-chain events related to a channel. There are three types of // for any on-chain events related to a channel. There are three types of
// possible on-chain events: a cooperative channel closure, a unilateral // possible on-chain events: a cooperative channel closure, a unilateral
@ -42,9 +48,7 @@ type ChainEventSubscription struct {
// CooperativeClosure is a signal that will be sent upon once a // CooperativeClosure is a signal that will be sent upon once a
// cooperative channel closure has been detected confirmed. // cooperative channel closure has been detected confirmed.
// CooperativeClosure chan *CooperativeCloseInfo
// TODO(roasbeef): or something else
CooperativeClosure chan struct{}
// ContractBreach is a channel that will be sent upon if we detect a // ContractBreach is a channel that will be sent upon if we detect a
// contract breach. The struct sent across the channel contains all the // contract breach. The struct sent across the channel contains all the
@ -232,7 +236,7 @@ func (c *chainWatcher) SubscribeChannelEvents() *ChainEventSubscription {
ChanPoint: c.cfg.chanState.FundingOutpoint, ChanPoint: c.cfg.chanState.FundingOutpoint,
RemoteUnilateralClosure: make(chan *lnwallet.UnilateralCloseSummary, 1), RemoteUnilateralClosure: make(chan *lnwallet.UnilateralCloseSummary, 1),
LocalUnilateralClosure: make(chan *LocalUnilateralCloseInfo, 1), LocalUnilateralClosure: make(chan *LocalUnilateralCloseInfo, 1),
CooperativeClosure: make(chan struct{}, 1), CooperativeClosure: make(chan *CooperativeCloseInfo, 1),
ContractBreach: make(chan *lnwallet.BreachRetribution, 1), ContractBreach: make(chan *lnwallet.BreachRetribution, 1),
Cancel: func() { Cancel: func() {
c.Lock() c.Lock()
@ -511,26 +515,24 @@ func (c *chainWatcher) dispatchCooperativeClose(commitSpend *chainntnfs.SpendDet
SettledBalance: localAmt, SettledBalance: localAmt,
CloseType: channeldb.CooperativeClose, CloseType: channeldb.CooperativeClose,
ShortChanID: c.cfg.chanState.ShortChanID(), ShortChanID: c.cfg.chanState.ShortChanID(),
IsPending: false, IsPending: true,
RemoteCurrentRevocation: c.cfg.chanState.RemoteCurrentRevocation, RemoteCurrentRevocation: c.cfg.chanState.RemoteCurrentRevocation,
RemoteNextRevocation: c.cfg.chanState.RemoteNextRevocation, RemoteNextRevocation: c.cfg.chanState.RemoteNextRevocation,
LocalChanConfig: c.cfg.chanState.LocalChanCfg, LocalChanConfig: c.cfg.chanState.LocalChanCfg,
} }
err := c.cfg.chanState.CloseChannel(closeSummary)
if err != nil && err != channeldb.ErrNoActiveChannels && // Create a summary of all the information needed to handle the
err != channeldb.ErrNoChanDBExists { // cooperative closure.
return fmt.Errorf("unable to close chan state: %v", err) closeInfo := &CooperativeCloseInfo{
ChannelCloseSummary: closeSummary,
} }
log.Infof("closeObserver: ChannelPoint(%v) is fully "+ // With the event processed, we'll now notify all subscribers of the
"closed, at height: %v", // event.
c.cfg.chanState.FundingOutpoint,
commitSpend.SpendingHeight)
c.Lock() c.Lock()
for _, sub := range c.clientSubscriptions { for _, sub := range c.clientSubscriptions {
select { select {
case sub.CooperativeClosure <- struct{}{}: case sub.CooperativeClosure <- closeInfo:
case <-c.quit: case <-c.quit:
c.Unlock() c.Unlock()
return fmt.Errorf("exiting") return fmt.Errorf("exiting")

@ -338,6 +338,10 @@ const (
// localCloseTrigger is a transition trigger driven by our commitment // localCloseTrigger is a transition trigger driven by our commitment
// being confirmed. // being confirmed.
localCloseTrigger localCloseTrigger
// coopCloseTrigger is a transition trigger driven by a cooperative
// close transaction being confirmed.
coopCloseTrigger
) )
// String returns a human readable string describing the passed // String returns a human readable string describing the passed
@ -356,6 +360,9 @@ func (t transitionTrigger) String() string {
case localCloseTrigger: case localCloseTrigger:
return "localCloseTrigger" return "localCloseTrigger"
case coopCloseTrigger:
return "coopCloseTrigger"
default: default:
return "unknown trigger" return "unknown trigger"
} }
@ -421,10 +428,16 @@ func (c *ChannelArbitrator) stateStep(triggerHeight uint32,
case userTrigger: case userTrigger:
nextState = StateBroadcastCommit nextState = StateBroadcastCommit
// If the trigger is a cooperative close being confirmed, then
// we can go straight to StateFullyResolved, as there won't be
// any contracts to resolve.
case coopCloseTrigger:
nextState = StateFullyResolved
// Otherwise, if this state advance was triggered by a // Otherwise, if this state advance was triggered by a
// commitment being confirmed on chain, then we'll jump // commitment being confirmed on chain, then we'll jump
// straight to the state where the contract has already been // straight to the state where the contract has already been
// closed. // closed, and we will inspect the set of unresolved contracts.
case localCloseTrigger: case localCloseTrigger:
log.Errorf("ChannelArbitrator(%v): unexpected local "+ log.Errorf("ChannelArbitrator(%v): unexpected local "+
"commitment confirmed while in StateDefault", "commitment confirmed while in StateDefault",
@ -1372,16 +1385,28 @@ func (c *ChannelArbitrator) channelAttendant(bestHeight int32) {
// We've cooperatively closed the channel, so we're no longer // We've cooperatively closed the channel, so we're no longer
// needed. We'll mark the channel as resolved and exit. // needed. We'll mark the channel as resolved and exit.
case <-c.cfg.ChainEvents.CooperativeClosure: case closeInfo := <-c.cfg.ChainEvents.CooperativeClosure:
log.Infof("ChannelArbitrator(%v) closing due to co-op "+ log.Infof("ChannelArbitrator(%v) marking channel "+
"closure", c.cfg.ChanPoint) "cooperatively closed", c.cfg.ChanPoint)
if err := c.cfg.MarkChannelResolved(); err != nil { err := c.cfg.MarkChannelClosed(
log.Errorf("Unable to mark contract "+ closeInfo.ChannelCloseSummary,
"resolved: %v", err) )
if err != nil {
log.Errorf("unable to mark channel closed: "+
"%v", err)
return
} }
// We'll now advance our state machine until it reaches
// a terminal state, and the channel is marked resolved.
_, _, err = c.advanceState(
closeInfo.CloseHeight, coopCloseTrigger,
)
if err != nil {
log.Errorf("unable to advance state: %v", err)
return return
}
// We have broadcasted our commitment, and it is now confirmed // We have broadcasted our commitment, and it is now confirmed
// on-chain. // on-chain.
@ -1440,9 +1465,6 @@ func (c *ChannelArbitrator) channelAttendant(bestHeight int32) {
HtlcResolutions: *uniClosure.HtlcResolutions, HtlcResolutions: *uniClosure.HtlcResolutions,
} }
// TODO(roasbeef): modify signal to also detect
// cooperative closures?
// As we're now acting upon an event triggered by the // As we're now acting upon an event triggered by the
// broadcast of the remote commitment transaction, // broadcast of the remote commitment transaction,
// we'll swap out our active HTLC set with the set // we'll swap out our active HTLC set with the set