@@ -123,7 +123,7 @@ resolveForkInfo logg bhdb candidateHdrs provider hints finfo = do
123123 -- payload provider that is not caught up yet)
124124 --
125125 else do
126- resolveForkInfoForProviderState logg bhdb candidateHdrs provider hints finfo (_consensusStateLatest r)
126+ resolveForkInfoForProviderState logg bhdb candidateHdrs provider hints finfo r
127127 where
128128 h = _latestRankedBlockHash . _forkInfoTargetState $ finfo
129129
@@ -146,10 +146,11 @@ resolveForkInfoForProviderState
146146 -> p
147147 -> Maybe Hints
148148 -> ForkInfo
149- -> SyncState
149+ -> ConsensusState
150150 -> IO ()
151151resolveForkInfoForProviderState logg bhdb candidateHdrs provider hints finfo ppKnownState
152- | ppRBH == trgHash = do
152+ | ppLatestBlock == trgHash = do
153+ -- nothing to do, we are at the target
153154 logg Info " resolveForkInfo: payload provider is at target block"
154155 -- If no payload production is requested we are done. Otherwise we
155156 -- still need to issue the syncToBlock call to initiate payload
@@ -164,28 +165,43 @@ resolveForkInfoForProviderState logg bhdb candidateHdrs provider hints finfo ppK
164165 logg Info $ " resolveForkInfo: payload provider state: " <> brief ppKnownState
165166 <> " ; target state: " <> brief (_forkInfoTargetState finfo)
166167
168+ let lookupBhdb rbh = lookupRanked bhdb (int $ _rankedHeight rbh) (_ranked rbh)
169+
167170 hdr :: BlockHeader <- runMaybeT
168- (MaybeT (tableLookup candidateHdrs (_ranked trgHash))
169- <|> MaybeT (lookupRanked bhdb (int $ _rankedHeight trgHash) (_ranked trgHash)))
171+ (MaybeT (tableLookup candidateHdrs (_ranked trgHash)) <|> MaybeT (lookupBhdb trgHash))
170172 >>= \ case
171173 Nothing -> do
172- throwM $ InternalInvariantViolation $ " validatePayload: target block is missing: " <> brief trgHash
174+ throwM $ InternalInvariantViolation $
175+ " validatePayload: target block is missing: " <> brief trgHash
173176 Just hdr -> return hdr
174177
178+ -- Finding a fork from the latest block can fail if we don't know the
179+ -- state of the payload provider.
180+ -- This can happen if:
181+ -- 1. the payload provider db comes from an external source.
182+ -- 2. the payload provider has moved to a fork that catchup has not
183+ -- finished validating.
184+ -- In this case we can only guess where a common fork point might be --
185+ -- in the worst case this is genesis. The more common case is 2. in
186+ -- which case the "safe" block is probably known.
187+ let ppSafeBlock = _syncStateRankedBlockHash $ _consensusStateSafe ppKnownState
188+ let ppFinalBlock = _syncStateRankedBlockHash $ _consensusStateFinal ppKnownState
189+ ppBlock <- runMaybeT
190+ (MaybeT (lookupBhdb ppLatestBlock)
191+ <|> MaybeT (lookupBhdb ppSafeBlock)
192+ <|> MaybeT (lookupBhdb ppFinalBlock)) >>= \ case
193+ Nothing ->
194+ throwM $ InternalInvariantViolation $ " Final payload provider block is missing: " <> brief ppFinalBlock
195+ Just ppBlock -> return ppBlock
196+
175197 -- Lookup the state of the Payload Provider and query the trace
176198 -- from the fork point to the target block.
177199 --
178- -- This can fail if we don't know the state of the payload provider.
179- -- This really should only happen if the payload provider db comes from
180- -- an external source or the payload provider db is outdated and resides
181- -- on a fork that got already pruned. In this case we can only guess
182- -- where a common fork point might be -- which, in worst case, might be
183- -- the genesis. We should either do an exponential search or just fail.
184200
185201 -- Before we do the potentially expensive branch diff call, we check
186202 -- whether the ppKnownState is in the trace of the finfo.
187203 -- TODO this could be done more efficiently, but for now it is fine.
188- let idx = L. elemIndex ppRBH
204+ let idx = L. elemIndex (view rankedBlockHash ppBlock)
189205 $ unwrapParent . _evaluationCtxRankedParentHash <$> _forkInfoTrace finfo
190206
191207 newForkInfo <- case idx of
@@ -199,13 +215,10 @@ resolveForkInfoForProviderState logg bhdb candidateHdrs provider hints finfo ppK
199215 -- The the payload provider does a fast forward without rewind.
200216 return finfo
201217 { _forkInfoTrace = drop i (_forkInfoTrace finfo)
202- , _forkInfoBasePayloadHash = Parent $ _ranked $ _syncStateRankedBlockPayloadHash ppKnownState
218+ , _forkInfoBasePayloadHash = Parent $ _ranked $ view rankedBlockPayloadHash ppBlock
203219 }
204220 Nothing -> do
205221 logg Info " resolveForkInfo: payload provider state not in trace, computing fork point"
206- ppBlock <- lookupRanked bhdb (int $ _rankedHeight ppRBH) (_ranked ppRBH) >>= \ case
207- Nothing -> throwM $ InternalInvariantViolation $ " Payload provider block is missing: " <> brief ppRBH
208- Just ppBlock -> return ppBlock
209222
210223 -- FIXME: this stream can be very long if the payload provider
211224 -- is out of sync. We should limit it and proceed iteratively if
@@ -293,25 +306,18 @@ resolveForkInfoForProviderState logg bhdb candidateHdrs provider hints finfo ppK
293306 e
294307 throwM e
295308
296- -- check if we made progress
297- --
298- let delta :: Int = int (_rankedHeight (_latestRankedBlockHash newState))
299- - int (_rankedHeight (_syncStateRankedBlockHash ppKnownState))
300-
301309 -- TODO: when this function is incremental, we will manage this more correctly.
302- if ppKnownState /= _consensusStateLatest newState
310+ if ppKnownState /= newState
303311 then do
304312 logg Info $ " resolveForkInfo: made progress"
305- <> " ; delta: " <> sshow delta
306313 <> " ; previous payload provider state: " <> brief ppKnownState
307314 <> " ; new payload provider state: " <> brief newState
308315 <> " ; target state: " <> brief (_forkInfoTargetState newForkInfo)
309316 -- continue.
310317 -- TODO compute the new fork info here.
311- resolveForkInfoForProviderState logg bhdb candidateHdrs provider hints newForkInfo (_consensusStateLatest newState)
318+ resolveForkInfoForProviderState logg bhdb candidateHdrs provider hints newForkInfo newState
312319 else do
313320 logg Warn $ " resolveForkInfo: no progress"
314- <> " ; delta: " <> sshow delta
315321 <> " ; previous payload provider state: " <> brief ppKnownState
316322 <> " ; new payload provider state: " <> brief newState
317323 <> " ; target state: " <> brief (_forkInfoTargetState newForkInfo)
@@ -321,14 +327,13 @@ resolveForkInfoForProviderState logg bhdb candidateHdrs provider hints finfo ppK
321327 -- so, we raise an exception.
322328 --
323329 throwM $ ForkInfoSyncFailure $ " unexpected result state"
324- <> " ; delta: " <> sshow delta
325330 <> " ; previous payload provider state: " <> brief ppKnownState
326331 <> " ; new payload provider state: " <> brief newState
327332 <> " ; target state: " <> brief (_forkInfoTargetState newForkInfo)
328333 <> " ; trace: " <> brief (_forkInfoTrace newForkInfo)
329334 where
330335 trgHash = _latestRankedBlockHash . _forkInfoTargetState $ finfo
331- ppRBH = _syncStateRankedBlockHash ppKnownState
336+ ppLatestBlock = _syncStateRankedBlockHash $ _consensusStateLatest ppKnownState
332337
333338-- -------------------------------------------------------------------------- --
334339-- Trace Utils
0 commit comments