@@ -43,7 +43,9 @@ module Cardano.Api.LedgerState
4343 , chainSyncClientPipelinedWithLedgerState
4444
4545 -- * Ledger state conditions
46- , LedgerStateCondition (.. )
46+ , ConditionResult (.. )
47+ , fromConditionResult
48+ , toConditionResult
4749 , foldEpochState
4850
4951 -- * Errors
@@ -166,6 +168,7 @@ import Ouroboros.Consensus.Storage.Serialisation
166168import Ouroboros.Consensus.TypeFamilyWrappers (WrapLedgerEvent (WrapLedgerEvent ))
167169import Ouroboros.Network.Block (blockNo )
168170import qualified Ouroboros.Network.Block
171+ import Ouroboros.Network.Mux (MuxError )
169172import qualified Ouroboros.Network.Protocol.ChainSync.Client as CS
170173import qualified Ouroboros.Network.Protocol.ChainSync.ClientPipelined as CSP
171174import Ouroboros.Network.Protocol.ChainSync.PipelineDecision
@@ -356,13 +359,15 @@ data FoldBlocksError
356359 = FoldBlocksInitialLedgerStateError ! InitialLedgerStateError
357360 | FoldBlocksApplyBlockError ! LedgerStateError
358361 | FoldBlocksIOException ! IOException
362+ | FoldBlocksMuxError ! MuxError
359363 deriving Show
360364
361365instance Error FoldBlocksError where
362366 prettyError = \ case
363367 FoldBlocksInitialLedgerStateError err -> prettyError err
364368 FoldBlocksApplyBlockError err -> " Failed when applying a block:" <+> prettyError err
365369 FoldBlocksIOException err -> " IOException:" <+> prettyException err
370+ FoldBlocksMuxError err -> " FoldBlocks error:" <+> prettyException err
366371
367372-- | Type that lets us decide whether to continue or stop
368373-- the fold from within our accumulation function.
@@ -406,7 +411,7 @@ foldBlocks
406411 -- truncating the last k blocks before the node's tip.
407412 -> t m a
408413 -- ^ The final state
409- foldBlocks nodeConfigFilePath socketPath validationMode state0 accumulate = handleIOExceptions $ do
414+ foldBlocks nodeConfigFilePath socketPath validationMode state0 accumulate = handleExceptions $ do
410415 -- NOTE this was originally implemented with a non-pipelined client then
411416 -- changed to a pipelined client for a modest speedup:
412417 -- * Non-pipelined: 1h 0m 19s
@@ -1758,10 +1763,19 @@ constructGlobals sGen eInfo (Ledger.ProtVer majorPParamsVer _) =
17581763
17591764--------------------------------------------------------------------------
17601765
1761- data LedgerStateCondition
1762- = ConditionMet
1763- | ConditionNotMet
1764- deriving (Show , Eq )
1766+ -- | Type isomorphic to bool, representing condition check result
1767+ data ConditionResult
1768+ = ConditionNotMet
1769+ | ConditionMet
1770+ deriving (Read , Show , Enum , Bounded , Ord , Eq )
1771+
1772+ toConditionResult :: Bool -> ConditionResult
1773+ toConditionResult False = ConditionNotMet
1774+ toConditionResult True = ConditionMet
1775+
1776+ fromConditionResult :: ConditionResult -> Bool
1777+ fromConditionResult ConditionNotMet = False
1778+ fromConditionResult ConditionMet = True
17651779
17661780data AnyNewEpochState where
17671781 AnyNewEpochState
@@ -1791,7 +1805,7 @@ foldEpochState
17911805 -> ( AnyNewEpochState
17921806 -> SlotNo
17931807 -> BlockNo
1794- -> StateT s IO LedgerStateCondition
1808+ -> StateT s IO ConditionResult
17951809 )
17961810 -- ^ Condition you want to check against the new epoch state.
17971811 --
@@ -1804,9 +1818,9 @@ foldEpochState
18041818 -- rollback. This is achieved by only calling the accumulator on states/blocks
18051819 -- that are older than the security parameter, k. This has the side effect of
18061820 -- truncating the last k blocks before the node's tip.
1807- -> t m (LedgerStateCondition , s )
1821+ -> t m (ConditionResult , s )
18081822 -- ^ The final state
1809- foldEpochState nodeConfigFilePath socketPath validationMode terminationEpoch initialResult checkCondition = handleIOExceptions $ do
1823+ foldEpochState nodeConfigFilePath socketPath validationMode terminationEpoch initialResult checkCondition = handleExceptions $ do
18101824 -- NOTE this was originally implemented with a non-pipelined client then
18111825 -- changed to a pipelined client for a modest speedup:
18121826 -- * Non-pipelined: 1h 0m 19s
@@ -1858,7 +1872,7 @@ foldEpochState nodeConfigFilePath socketPath validationMode terminationEpoch ini
18581872 Nothing -> modifyError FoldBlocksIOException . liftIO $ readMVar stateMv
18591873 where
18601874 protocols :: ()
1861- => MVar (LedgerStateCondition , s )
1875+ => MVar (ConditionResult , s )
18621876 -> IORef (Maybe LedgerStateError )
18631877 -> Env
18641878 -> LedgerState
@@ -1874,7 +1888,7 @@ foldEpochState nodeConfigFilePath socketPath validationMode terminationEpoch ini
18741888 -- | Defines the client side of the chain sync protocol.
18751889 chainSyncClient :: Word16
18761890 -- ^ The maximum number of concurrent requests.
1877- -> MVar (LedgerStateCondition , s )
1891+ -> MVar (ConditionResult , s )
18781892 -- ^ State accumulator. Written to on every block.
18791893 -> IORef (Maybe LedgerStateError )
18801894 -- ^ Resulting error if any. Written to once on protocol
@@ -2002,5 +2016,11 @@ atTerminationEpoch terminationEpoch events =
20022016 , currentEpoch' >= terminationEpoch
20032017 ]
20042018
2005- handleIOExceptions :: MonadIOTransError FoldBlocksError t m => ExceptT FoldBlocksError IO a -> t m a
2006- handleIOExceptions = liftEither <=< liftIO . fmap (join . first FoldBlocksIOException ) . try . runExceptT
2019+ handleExceptions :: MonadIOTransError FoldBlocksError t m
2020+ => ExceptT FoldBlocksError IO a
2021+ -> t m a
2022+ handleExceptions = liftEither <=< liftIO . runExceptT . flip catches handlers
2023+ where
2024+ handlers = [ Handler $ throwError . FoldBlocksIOException
2025+ , Handler $ throwError . FoldBlocksMuxError
2026+ ]
0 commit comments