contractcourt/channel_arbitrator: move next state commit out of stateStep

This commit is contained in:
Johan T. Halseth 2018-08-21 12:21:14 +02:00
parent 6df302d7f5
commit 06c683d48d
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26

@ -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, log.Tracef("ChannelArbitrator(%v): next_state=%v", c.cfg.ChanPoint,
nextState) nextState)
c.state = nextState
return nextState, closeTx, nil return nextState, closeTx, nil
} }
@ -656,7 +651,8 @@ func (c *ChannelArbitrator) advanceState(triggerHeight uint32,
triggerHeight, trigger, triggerHeight, trigger,
) )
if err != nil { 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 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 // our prior state back as the next state, then we'll
// terminate. // terminate.
if nextState == priorState { if nextState == priorState {
log.Tracef("ChannelArbitrator(%v): terminating at state=%v", log.Tracef("ChannelArbitrator(%v): terminating at "+
c.cfg.ChanPoint, nextState) "state=%v", c.cfg.ChanPoint, nextState)
return nextState, forceCloseTx, nil 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
} }
} }