Skip to content

Commit 14345b2

Browse files
committed
Use New Epoch for epoch 0
Fixes #1715
1 parent 87c4529 commit 14345b2

File tree

1 file changed

+33
-29
lines changed
  • cardano-db-sync/src/Cardano/DbSync/Ledger

1 file changed

+33
-29
lines changed

cardano-db-sync/src/Cardano/DbSync/Ledger/State.hs

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ applyBlock env blk = do
228228
let (ledgerEvents, deposits) = splitDeposits ledgerEventsFull
229229
let !newLedgerState = finaliseDrepDistr (lrResult result)
230230
!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)
232232
let !newEpochBlockNo = applyToEpochBlockNo (isJust $ blockIsEBB blk) (isJust newEpoch) (clsEpochBlockNo oldState)
233233
let !newState = CardanoLedgerState newLedgerState newEpochBlockNo
234234
let !ledgerDB' = pushLedgerDB ledgerDB newState
@@ -252,28 +252,31 @@ applyBlock env blk = do
252252
else defaultApplyResult details
253253
pure (oldState, appResult)
254254
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
259257
-- pass on error when trying to get ledgerEpochNo
260-
case (currEpochE, prevEpochE) of
258+
case (prevEpochE, currEpochE) of
261259
(Left err, _) -> Left err
262260
(_, 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+
}
277280

278281
applyToEpochBlockNo :: Bool -> Bool -> EpochBlockNo -> EpochBlockNo
279282
applyToEpochBlockNo True _ _ = EBBEpochBlockNo
@@ -326,12 +329,13 @@ storeSnapshotAndCleanupMaybe ::
326329
IO Bool
327330
storeSnapshotAndCleanupMaybe env oldState appResult blkNo isCons syncState =
328331
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+
_ ->
335339
if timeToSnapshot syncState blkNo && isCons
336340
then do
337341
liftIO $ saveCleanupState env oldState Nothing
@@ -751,14 +755,14 @@ getRegisteredPoolShelley lState =
751755
Shelley.nesEs $
752756
Consensus.shelleyLedgerState lState
753757

754-
ledgerEpochNo :: HasLedgerEnv -> ExtLedgerState CardanoBlock -> Either SyncNodeError EpochNo
758+
ledgerEpochNo :: HasLedgerEnv -> ExtLedgerState CardanoBlock -> Either SyncNodeError (Maybe EpochNo)
755759
ledgerEpochNo env cls =
756760
case ledgerTipSlot (ledgerState cls) of
757-
Origin -> Right $ EpochNo 0 -- An empty chain is in epoch 0
761+
Origin -> Right Nothing
758762
NotOrigin slot ->
759763
case runExcept $ epochInfoEpoch epochInfo slot of
760764
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)
762766
where
763767
epochInfo :: EpochInfo (Except Consensus.PastHorizonException)
764768
epochInfo = epochInfoLedger (configLedger $ getTopLevelconfigHasLedger env) (hardForkLedgerStatePerEra $ ledgerState cls)

0 commit comments

Comments
 (0)