@@ -65,6 +65,7 @@ import qualified Control.Exception as Exception
65
65
import qualified Data.ByteString.Base16 as Base16
66
66
67
67
import Cardano.DbSync.Api.Types (LedgerEnv (.. ), SyncOptions (.. ))
68
+ import Cardano.DbSync.Error (SyncNodeError (.. ))
68
69
import qualified Data.ByteString.Char8 as BS
69
70
import qualified Data.ByteString.Lazy.Char8 as LBS
70
71
import qualified Data.ByteString.Short as SBS
@@ -209,48 +210,59 @@ applyBlock env blk = do
209
210
atomically $ do
210
211
! ledgerDB <- readStateUnsafe env
211
212
let oldState = ledgerDbCurrent ledgerDB
212
- let ! result = applyBlk (ExtLedgerCfg (getTopLevelconfigHasLedger env)) blk (clsState oldState)
213
+ ! result <- applyBlk (ExtLedgerCfg (getTopLevelconfigHasLedger env)) blk (clsState oldState)
213
214
let ! ledgerEvents = mapMaybe convertAuxLedgerEvent (lrEvents result)
214
215
let ! newLedgerState = lrResult result
215
216
! details <- getSlotDetails env (ledgerState newLedgerState) time (cardanoBlockSlotNo blk)
216
- let ! newEpoch = mkNewEpoch (clsState oldState) newLedgerState (findAdaPots ledgerEvents)
217
- let ! newEpochBlockNo = applyToEpochBlockNo (isJust $ blockIsEBB blk) (isJust newEpoch) (clsEpochBlockNo oldState)
218
- let ! newState = CardanoLedgerState newLedgerState newEpochBlockNo
219
- let ! ledgerDB' = pushLedgerDB ledgerDB newState
220
- writeTVar (leStateVar env) (Strict. Just ledgerDB')
221
- let ! appResult =
222
- ApplyResult
223
- { apPrices = getPrices newState
224
- , apPoolsRegistered = getRegisteredPools oldState
225
- , apNewEpoch = maybeToStrict newEpoch
226
- , apSlotDetails = details
227
- , apStakeSlice = stakeSlice newState details
228
- , apEvents = ledgerEvents
229
- }
230
- pure (oldState, appResult)
217
+ let ! newEpochE = mkNewEpoch (clsState oldState) newLedgerState (findAdaPots ledgerEvents)
218
+ case newEpochE of
219
+ Left err -> throwSTM err
220
+ Right newEpoch -> do
221
+ let ! newEpochBlockNo = applyToEpochBlockNo (isJust $ blockIsEBB blk) (isJust newEpoch) (clsEpochBlockNo oldState)
222
+ let ! newState = CardanoLedgerState newLedgerState newEpochBlockNo
223
+ let ! ledgerDB' = pushLedgerDB ledgerDB newState
224
+ writeTVar (leStateVar env) (Strict. Just ledgerDB')
225
+ let ! appResult =
226
+ ApplyResult
227
+ { apPrices = getPrices newState
228
+ , apPoolsRegistered = getRegisteredPools oldState
229
+ , apNewEpoch = maybeToStrict newEpoch
230
+ , apSlotDetails = details
231
+ , apStakeSlice = stakeSlice newState details
232
+ , apEvents = ledgerEvents
233
+ }
234
+ pure (oldState, appResult)
231
235
where
232
236
applyBlk ::
233
237
ExtLedgerCfg CardanoBlock ->
234
238
CardanoBlock ->
235
239
ExtLedgerState CardanoBlock ->
236
- LedgerResult (ExtLedgerState CardanoBlock ) (ExtLedgerState CardanoBlock )
240
+ STM ( LedgerResult (ExtLedgerState CardanoBlock ) (ExtLedgerState CardanoBlock ) )
237
241
applyBlk cfg block lsb =
238
242
case tickThenReapplyCheckHash cfg block lsb of
239
- Left err -> panic err
240
- Right result -> result
241
-
242
- mkNewEpoch :: ExtLedgerState CardanoBlock -> ExtLedgerState CardanoBlock -> Maybe AdaPots -> Maybe Generic. NewEpoch
243
- mkNewEpoch oldState newState mPots =
244
- if ledgerEpochNo env newState /= ledgerEpochNo env oldState + 1
245
- then Nothing
246
- else
247
- Just $
248
- Generic. NewEpoch
249
- { Generic. neEpoch = ledgerEpochNo env newState
250
- , Generic. neIsEBB = isJust $ blockIsEBB blk
251
- , Generic. neAdaPots = maybeToStrict mPots
252
- , Generic. neEpochUpdate = Generic. epochUpdate newState
253
- }
243
+ Left err -> throwSTM err
244
+ Right result -> pure result
245
+
246
+ mkNewEpoch :: ExtLedgerState CardanoBlock -> ExtLedgerState CardanoBlock -> Maybe AdaPots -> Either SyncNodeError (Maybe Generic. NewEpoch )
247
+ mkNewEpoch oldState newState mPots = do
248
+ let currEpochE = ledgerEpochNo env newState
249
+ prevEpochE = ledgerEpochNo env oldState
250
+ -- pass on error when trying to get ledgerEpochNo
251
+ case (currEpochE, prevEpochE) of
252
+ (Left err, _) -> Left err
253
+ (_, Left err) -> Left err
254
+ (Right currEpoch, Right prevEpoch) -> do
255
+ if currEpoch /= prevEpoch + 1
256
+ then Right Nothing
257
+ else
258
+ Right $
259
+ Just $
260
+ Generic. NewEpoch
261
+ { Generic. neEpoch = currEpoch
262
+ , Generic. neIsEBB = isJust $ blockIsEBB blk
263
+ , Generic. neAdaPots = maybeToStrict mPots
264
+ , Generic. neEpochUpdate = Generic. epochUpdate newState
265
+ }
254
266
255
267
applyToEpochBlockNo :: Bool -> Bool -> EpochBlockNo -> EpochBlockNo
256
268
applyToEpochBlockNo True _ _ = EBBEpochBlockNo
@@ -709,14 +721,14 @@ getRegisteredPoolShelley lState =
709
721
Shelley. nesEs $
710
722
Consensus. shelleyLedgerState lState
711
723
712
- ledgerEpochNo :: HasLedgerEnv -> ExtLedgerState CardanoBlock -> EpochNo
724
+ ledgerEpochNo :: HasLedgerEnv -> ExtLedgerState CardanoBlock -> Either SyncNodeError EpochNo
713
725
ledgerEpochNo env cls =
714
726
case ledgerTipSlot (ledgerState cls) of
715
- Origin -> 0 -- An empty chain is in epoch 0
727
+ Origin -> Right 0 -- An empty chain is in epoch 0
716
728
NotOrigin slot ->
717
729
case runExcept $ epochInfoEpoch epochInfo slot of
718
- Left err -> panic $ " ledgerEpochNo: " <> textShow err
719
- Right en -> en
730
+ Left err -> Left $ SNErrLedgerState $ " unable to use slot: " <> show slot <> " to get ledgerEpochNo: " <> show err
731
+ Right en -> Right en
720
732
where
721
733
epochInfo :: EpochInfo (Except Consensus. PastHorizonException )
722
734
epochInfo = epochInfoLedger (configLedger $ getTopLevelconfigHasLedger env) (hardForkLedgerStatePerEra $ ledgerState cls)
@@ -727,21 +739,21 @@ tickThenReapplyCheckHash ::
727
739
ExtLedgerCfg CardanoBlock ->
728
740
CardanoBlock ->
729
741
ExtLedgerState CardanoBlock ->
730
- Either Text (LedgerResult (ExtLedgerState CardanoBlock ) (ExtLedgerState CardanoBlock ))
742
+ Either SyncNodeError (LedgerResult (ExtLedgerState CardanoBlock ) (ExtLedgerState CardanoBlock ))
731
743
tickThenReapplyCheckHash cfg block lsb =
732
744
if blockPrevHash block == ledgerTipHash (ledgerState lsb)
733
745
then Right $ tickThenReapplyLedgerResult cfg block lsb
734
746
else
735
- Left $
747
+ Left $ SNErrLedgerState $
736
748
mconcat
737
749
[ " Ledger state hash mismatch. Ledger head is slot "
738
- , textShow (unSlotNo $ fromWithOrigin (SlotNo 0 ) (ledgerTipSlot $ ledgerState lsb))
750
+ , show (unSlotNo $ fromWithOrigin (SlotNo 0 ) (ledgerTipSlot $ ledgerState lsb))
739
751
, " hash "
740
- , renderByteArray (Cardano. unChainHash (ledgerTipHash $ ledgerState lsb))
752
+ , Text. unpack $ renderByteArray (Cardano. unChainHash (ledgerTipHash $ ledgerState lsb))
741
753
, " but block previous hash is "
742
- , renderByteArray (Cardano. unChainHash $ blockPrevHash block)
754
+ , Text. unpack $ renderByteArray (Cardano. unChainHash $ blockPrevHash block)
743
755
, " and block current hash is "
744
- , renderByteArray (SBS. fromShort . Consensus. getOneEraHash $ blockHash block)
756
+ , Text. unpack $ renderByteArray (SBS. fromShort . Consensus. getOneEraHash $ blockHash block)
745
757
, " ."
746
758
]
747
759
0 commit comments