@@ -228,7 +228,7 @@ applyBlock env blk = do
228
228
let (ledgerEvents, deposits) = splitDeposits ledgerEventsFull
229
229
let ! newLedgerState = finaliseDrepDistr (lrResult result)
230
230
! details <- getSlotDetails env (ledgerState newLedgerState) time (cardanoBlockSlotNo blk)
231
- ! newEpoch <- fromEitherSTM $ mkNewEpoch (clsState oldState) newLedgerState (findAdaPots ledgerEvents)
231
+ ! newEpoch <- fromEitherSTM $ mkOnNewEpoch (clsState oldState) newLedgerState (findAdaPots ledgerEvents)
232
232
let ! newEpochBlockNo = applyToEpochBlockNo (isJust $ blockIsEBB blk) (isJust newEpoch) (clsEpochBlockNo oldState)
233
233
let ! newState = CardanoLedgerState newLedgerState newEpochBlockNo
234
234
let ! ledgerDB' = pushLedgerDB ledgerDB newState
@@ -252,28 +252,31 @@ applyBlock env blk = do
252
252
else defaultApplyResult details
253
253
pure (oldState, appResult)
254
254
where
255
- mkNewEpoch :: ExtLedgerState CardanoBlock -> ExtLedgerState CardanoBlock -> Maybe AdaPots -> Either SyncNodeError (Maybe Generic. NewEpoch )
256
- mkNewEpoch oldState newState mPots = do
257
- let currEpochE = ledgerEpochNo env newState
258
- prevEpochE = ledgerEpochNo env oldState
255
+ mkOnNewEpoch :: ExtLedgerState CardanoBlock -> ExtLedgerState CardanoBlock -> Maybe AdaPots -> Either SyncNodeError (Maybe Generic. NewEpoch )
256
+ mkOnNewEpoch oldState newState mPots = do
259
257
-- pass on error when trying to get ledgerEpochNo
260
- case (currEpochE, prevEpochE ) of
258
+ case (prevEpochE, currEpochE ) of
261
259
(Left err, _) -> Left err
262
260
(_, Left err) -> Left err
263
- (Right currEpoch, Right prevEpoch) -> do
264
- if currEpoch /= EpochNo (unEpochNo prevEpoch + 1 )
265
- then Right Nothing
266
- else
267
- Right $
268
- Just $
269
- Generic. NewEpoch
270
- { Generic. neEpoch = currEpoch
271
- , Generic. neIsEBB = isJust $ blockIsEBB blk
272
- , Generic. neAdaPots = maybeToStrict mPots
273
- , Generic. neEpochUpdate = Generic. epochUpdate newState
274
- , Generic. neDRepState = maybeToStrict $ getDrepState newState
275
- , Generic. neEnacted = maybeToStrict $ getEnacted newState
276
- }
261
+ (Right Nothing , Right (Just (EpochNo 0 ))) -> Right $ Just $ mkNewEpoch (EpochNo 0 )
262
+ (Right (Just prevEpoch), Right (Just currEpoch))
263
+ | unEpochNo currEpoch == 1 + unEpochNo prevEpoch ->
264
+ Right $ Just $ mkNewEpoch currEpoch
265
+ _ -> Right Nothing
266
+ where
267
+ prevEpochE = ledgerEpochNo env oldState
268
+ currEpochE = ledgerEpochNo env newState
269
+
270
+ mkNewEpoch :: EpochNo -> Generic. NewEpoch
271
+ mkNewEpoch currEpoch =
272
+ Generic. NewEpoch
273
+ { Generic. neEpoch = currEpoch
274
+ , Generic. neIsEBB = isJust $ blockIsEBB blk
275
+ , Generic. neAdaPots = maybeToStrict mPots
276
+ , Generic. neEpochUpdate = Generic. epochUpdate newState
277
+ , Generic. neDRepState = maybeToStrict $ getDrepState newState
278
+ , Generic. neEnacted = maybeToStrict $ getEnacted newState
279
+ }
277
280
278
281
applyToEpochBlockNo :: Bool -> Bool -> EpochBlockNo -> EpochBlockNo
279
282
applyToEpochBlockNo True _ _ = EBBEpochBlockNo
@@ -326,12 +329,13 @@ storeSnapshotAndCleanupMaybe ::
326
329
IO Bool
327
330
storeSnapshotAndCleanupMaybe env oldState appResult blkNo isCons syncState =
328
331
case maybeFromStrict (apNewEpoch appResult) of
329
- Just newEpoch -> do
330
- let newEpochNo = Generic. neEpoch newEpoch
331
- -- TODO: Instead of newEpochNo - 1, is there any way to get the epochNo from 'lssOldState'?
332
- liftIO $ saveCleanupState env oldState (Just $ EpochNo $ unEpochNo newEpochNo - 1 )
333
- pure True
334
- Nothing ->
332
+ Just newEpoch
333
+ | newEpochNo <- unEpochNo (Generic. neEpoch newEpoch)
334
+ , newEpochNo > 0 -> do
335
+ -- TODO: Instead of newEpochNo - 1, is there any way to get the epochNo from 'lssOldState'?
336
+ liftIO $ saveCleanupState env oldState (Just $ EpochNo $ newEpochNo - 1 )
337
+ pure True
338
+ _ ->
335
339
if timeToSnapshot syncState blkNo && isCons
336
340
then do
337
341
liftIO $ saveCleanupState env oldState Nothing
@@ -751,14 +755,14 @@ getRegisteredPoolShelley lState =
751
755
Shelley. nesEs $
752
756
Consensus. shelleyLedgerState lState
753
757
754
- ledgerEpochNo :: HasLedgerEnv -> ExtLedgerState CardanoBlock -> Either SyncNodeError EpochNo
758
+ ledgerEpochNo :: HasLedgerEnv -> ExtLedgerState CardanoBlock -> Either SyncNodeError ( Maybe EpochNo )
755
759
ledgerEpochNo env cls =
756
760
case ledgerTipSlot (ledgerState cls) of
757
- Origin -> Right $ EpochNo 0 -- An empty chain is in epoch 0
761
+ Origin -> Right Nothing
758
762
NotOrigin slot ->
759
763
case runExcept $ epochInfoEpoch epochInfo slot of
760
764
Left err -> Left $ SNErrLedgerState $ " unable to use slot: " <> show slot <> " to get ledgerEpochNo: " <> show err
761
- Right en -> Right en
765
+ Right en -> Right ( Just en)
762
766
where
763
767
epochInfo :: EpochInfo (Except Consensus. PastHorizonException )
764
768
epochInfo = epochInfoLedger (configLedger $ getTopLevelconfigHasLedger env) (hardForkLedgerStatePerEra $ ledgerState cls)
0 commit comments