From 06c683d48d2ea74cd604566c70fe44f6fdc3ad0c Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Tue, 21 Aug 2018 12:21:14 +0200 Subject: [PATCH] contractcourt/channel_arbitrator: move next state commit out of stateStep --- contractcourt/channel_arbitrator.go | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/contractcourt/channel_arbitrator.go b/contractcourt/channel_arbitrator.go index 6bf6c385..69700f03 100644 --- a/contractcourt/channel_arbitrator.go +++ b/contractcourt/channel_arbitrator.go @@ -619,14 +619,9 @@ func (c *ChannelArbitrator) stateStep(triggerHeight uint32, } } - if err := c.log.CommitState(nextState); err != nil { - return StateError, nil, err - } - log.Tracef("ChannelArbitrator(%v): next_state=%v", c.cfg.ChanPoint, nextState) - c.state = nextState return nextState, closeTx, nil } @@ -656,7 +651,8 @@ func (c *ChannelArbitrator) advanceState(triggerHeight uint32, triggerHeight, trigger, ) if err != nil { - log.Errorf("unable to advance state: %v", err) + log.Errorf("ChannelArbitrator(%v): unable to advance "+ + "state: %v", c.cfg.ChanPoint, err) return priorState, nil, err } @@ -668,10 +664,21 @@ func (c *ChannelArbitrator) advanceState(triggerHeight uint32, // our prior state back as the next state, then we'll // terminate. if nextState == priorState { - log.Tracef("ChannelArbitrator(%v): terminating at state=%v", - c.cfg.ChanPoint, nextState) + log.Tracef("ChannelArbitrator(%v): terminating at "+ + "state=%v", c.cfg.ChanPoint, nextState) return nextState, forceCloseTx, nil } + + // As the prior state was successfully executed, we can now + // commit the next state. This ensures that we will re-execute + // the prior state if anything fails. + if err := c.log.CommitState(nextState); err != nil { + log.Errorf("ChannelArbitrator(%v): unable to commit "+ + "next state(%v): %v", c.cfg.ChanPoint, + nextState, err) + return priorState, nil, err + } + c.state = nextState } }