From 9abe06f3c8523820ade00d4e3c536315433f1dc1 Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Mon, 10 Sep 2018 13:17:55 +0200 Subject: [PATCH] cnct: finish channel arbitrator in StateFullyResolved Previously the arbitrator wasn't advanced to the final stage after the last contract resolved. Also channel arbitrator now does not ignore a log error anymore unresolved contracts cannot be retrieved. --- contractcourt/channel_arbitrator.go | 43 +++++++++++++----------- contractcourt/channel_arbitrator_test.go | 5 +-- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/contractcourt/channel_arbitrator.go b/contractcourt/channel_arbitrator.go index 154821cb1..4eb499470 100644 --- a/contractcourt/channel_arbitrator.go +++ b/contractcourt/channel_arbitrator.go @@ -816,7 +816,19 @@ func (c *ChannelArbitrator) stateStep(triggerHeight uint32, log.Infof("ChannelArbitrator(%v): still awaiting contract "+ "resolution", c.cfg.ChanPoint) - nextState = StateWaitingFullResolution + numUnresolved, err := c.log.FetchUnresolvedContracts() + if err != nil { + return StateError, closeTx, err + } + + // If we still have unresolved contracts, then we'll stay alive + // to oversee their resolution. + if len(numUnresolved) != 0 { + nextState = StateWaitingFullResolution + break + } + + nextState = StateFullyResolved // If we start as fully resolved, then we'll end as fully resolved. case StateFullyResolved: @@ -1722,29 +1734,22 @@ func (c *ChannelArbitrator) channelAttendant(bestHeight int32) { log.Infof("ChannelArbitrator(%v): a contract has been "+ "fully resolved!", c.cfg.ChanPoint) - numUnresolved, err := c.log.FetchUnresolvedContracts() + nextState, _, err := c.advanceState( + uint32(bestHeight), chainTrigger, + ) if err != nil { - log.Errorf("unable to query resolved "+ - "contracts: %v", err) + log.Errorf("unable to advance state: %v", err) } - // If we still have unresolved contracts, then we'll - // stay alive to oversee their resolution. - if len(numUnresolved) != 0 { - continue - } + // If we don't have anything further to do after + // advancing our state, then we'll exit. + if nextState == StateFullyResolved { + log.Infof("ChannelArbitrator(%v): all "+ + "contracts fully resolved, exiting", + c.cfg.ChanPoint) - log.Infof("ChannelArbitrator(%v): all contracts fully "+ - "resolved, exiting", c.cfg.ChanPoint) - - // Otherwise, our job is finished here, the contract is - // now fully resolved! We'll mark it as such, then exit - // ourselves. - if err := c.cfg.MarkChannelResolved(); err != nil { - log.Errorf("unable to mark contract "+ - "resolved: %v", err) + return } - return // We've just received a request to forcibly close out the // channel. We'll diff --git a/contractcourt/channel_arbitrator_test.go b/contractcourt/channel_arbitrator_test.go index 4518fac05..3a0d68a51 100644 --- a/contractcourt/channel_arbitrator_test.go +++ b/contractcourt/channel_arbitrator_test.go @@ -611,10 +611,7 @@ func TestChannelArbitratorLocalForceClosePendingHtlc(t *testing.T) { notifier.spendChan <- &chainntnfs.SpendDetail{} // At this point channel should be marked as resolved. - - // It should transition StateWaitingFullResolution -> - // StateFullyResolved, but this isn't happening. - // assertStateTransitions(t, arbLog.newStates, StateFullyResolved) + assertStateTransitions(t, arbLog.newStates, StateFullyResolved) select { case <-resolved: