Skip to content

Commit 6e13e85

Browse files
committed
convert panic to exceptions in Ledger/State.hs
1 parent 791da67 commit 6e13e85

File tree

2 files changed

+56
-42
lines changed

2 files changed

+56
-42
lines changed

cardano-db-sync/src/Cardano/DbSync/Error.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ data SyncNodeError
4949
| SNErrAlonzoConfig !FilePath !Text
5050
| SNErrCardanoConfig !Text
5151
| SNErrInsertGenesis !String
52+
| SNErrLedgerState !String
5253

5354
instance Exception SyncNodeError
5455

@@ -99,6 +100,7 @@ instance Show SyncNodeError where
99100
, show err
100101
]
101102
SNErrInsertGenesis err -> "Error InsertGenesis: " <> err
103+
SNErrLedgerState err -> "Error Ledger State: " <> err
102104

103105
annotateInvariantTx :: Byron.Tx -> SyncInvariant -> SyncInvariant
104106
annotateInvariantTx tx ei =

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

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import qualified Control.Exception as Exception
6565
import qualified Data.ByteString.Base16 as Base16
6666

6767
import Cardano.DbSync.Api.Types (LedgerEnv (..), SyncOptions (..))
68+
import Cardano.DbSync.Error (SyncNodeError (..))
6869
import qualified Data.ByteString.Char8 as BS
6970
import qualified Data.ByteString.Lazy.Char8 as LBS
7071
import qualified Data.ByteString.Short as SBS
@@ -209,48 +210,59 @@ applyBlock env blk = do
209210
atomically $ do
210211
!ledgerDB <- readStateUnsafe env
211212
let oldState = ledgerDbCurrent ledgerDB
212-
let !result = applyBlk (ExtLedgerCfg (getTopLevelconfigHasLedger env)) blk (clsState oldState)
213+
!result <- applyBlk (ExtLedgerCfg (getTopLevelconfigHasLedger env)) blk (clsState oldState)
213214
let !ledgerEvents = mapMaybe convertAuxLedgerEvent (lrEvents result)
214215
let !newLedgerState = lrResult result
215216
!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)
231235
where
232236
applyBlk ::
233237
ExtLedgerCfg CardanoBlock ->
234238
CardanoBlock ->
235239
ExtLedgerState CardanoBlock ->
236-
LedgerResult (ExtLedgerState CardanoBlock) (ExtLedgerState CardanoBlock)
240+
STM (LedgerResult (ExtLedgerState CardanoBlock) (ExtLedgerState CardanoBlock))
237241
applyBlk cfg block lsb =
238242
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+
}
254266

255267
applyToEpochBlockNo :: Bool -> Bool -> EpochBlockNo -> EpochBlockNo
256268
applyToEpochBlockNo True _ _ = EBBEpochBlockNo
@@ -709,14 +721,14 @@ getRegisteredPoolShelley lState =
709721
Shelley.nesEs $
710722
Consensus.shelleyLedgerState lState
711723

712-
ledgerEpochNo :: HasLedgerEnv -> ExtLedgerState CardanoBlock -> EpochNo
724+
ledgerEpochNo :: HasLedgerEnv -> ExtLedgerState CardanoBlock -> Either SyncNodeError EpochNo
713725
ledgerEpochNo env cls =
714726
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
716728
NotOrigin slot ->
717729
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
720732
where
721733
epochInfo :: EpochInfo (Except Consensus.PastHorizonException)
722734
epochInfo = epochInfoLedger (configLedger $ getTopLevelconfigHasLedger env) (hardForkLedgerStatePerEra $ ledgerState cls)
@@ -727,21 +739,21 @@ tickThenReapplyCheckHash ::
727739
ExtLedgerCfg CardanoBlock ->
728740
CardanoBlock ->
729741
ExtLedgerState CardanoBlock ->
730-
Either Text (LedgerResult (ExtLedgerState CardanoBlock) (ExtLedgerState CardanoBlock))
742+
Either SyncNodeError (LedgerResult (ExtLedgerState CardanoBlock) (ExtLedgerState CardanoBlock))
731743
tickThenReapplyCheckHash cfg block lsb =
732744
if blockPrevHash block == ledgerTipHash (ledgerState lsb)
733745
then Right $ tickThenReapplyLedgerResult cfg block lsb
734746
else
735-
Left $
747+
Left $ SNErrLedgerState $
736748
mconcat
737749
[ "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))
739751
, " hash "
740-
, renderByteArray (Cardano.unChainHash (ledgerTipHash $ ledgerState lsb))
752+
, Text.unpack $ renderByteArray (Cardano.unChainHash (ledgerTipHash $ ledgerState lsb))
741753
, " but block previous hash is "
742-
, renderByteArray (Cardano.unChainHash $ blockPrevHash block)
754+
, Text.unpack $ renderByteArray (Cardano.unChainHash $ blockPrevHash block)
743755
, " and block current hash is "
744-
, renderByteArray (SBS.fromShort . Consensus.getOneEraHash $ blockHash block)
756+
, Text.unpack $ renderByteArray (SBS.fromShort . Consensus.getOneEraHash $ blockHash block)
745757
, "."
746758
]
747759

0 commit comments

Comments
 (0)