@@ -43,7 +43,9 @@ module Cardano.Api.LedgerState
43
43
, chainSyncClientPipelinedWithLedgerState
44
44
45
45
-- * Ledger state conditions
46
- , LedgerStateCondition (.. )
46
+ , ConditionResult (.. )
47
+ , fromConditionResult
48
+ , toConditionResult
47
49
, foldEpochState
48
50
49
51
-- * Errors
@@ -166,6 +168,7 @@ import Ouroboros.Consensus.Storage.Serialisation
166
168
import Ouroboros.Consensus.TypeFamilyWrappers (WrapLedgerEvent (WrapLedgerEvent ))
167
169
import Ouroboros.Network.Block (blockNo )
168
170
import qualified Ouroboros.Network.Block
171
+ import Ouroboros.Network.Mux (MuxError )
169
172
import qualified Ouroboros.Network.Protocol.ChainSync.Client as CS
170
173
import qualified Ouroboros.Network.Protocol.ChainSync.ClientPipelined as CSP
171
174
import Ouroboros.Network.Protocol.ChainSync.PipelineDecision
@@ -356,13 +359,15 @@ data FoldBlocksError
356
359
= FoldBlocksInitialLedgerStateError ! InitialLedgerStateError
357
360
| FoldBlocksApplyBlockError ! LedgerStateError
358
361
| FoldBlocksIOException ! IOException
362
+ | FoldBlocksMuxError ! MuxError
359
363
deriving Show
360
364
361
365
instance Error FoldBlocksError where
362
366
prettyError = \ case
363
367
FoldBlocksInitialLedgerStateError err -> prettyError err
364
368
FoldBlocksApplyBlockError err -> " Failed when applying a block:" <+> prettyError err
365
369
FoldBlocksIOException err -> " IOException:" <+> prettyException err
370
+ FoldBlocksMuxError err -> " FoldBlocks error:" <+> prettyException err
366
371
367
372
-- | Type that lets us decide whether to continue or stop
368
373
-- the fold from within our accumulation function.
@@ -406,7 +411,7 @@ foldBlocks
406
411
-- truncating the last k blocks before the node's tip.
407
412
-> t m a
408
413
-- ^ The final state
409
- foldBlocks nodeConfigFilePath socketPath validationMode state0 accumulate = handleIOExceptions $ do
414
+ foldBlocks nodeConfigFilePath socketPath validationMode state0 accumulate = handleExceptions $ do
410
415
-- NOTE this was originally implemented with a non-pipelined client then
411
416
-- changed to a pipelined client for a modest speedup:
412
417
-- * Non-pipelined: 1h 0m 19s
@@ -1758,10 +1763,19 @@ constructGlobals sGen eInfo (Ledger.ProtVer majorPParamsVer _) =
1758
1763
1759
1764
--------------------------------------------------------------------------
1760
1765
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
1765
1779
1766
1780
data AnyNewEpochState where
1767
1781
AnyNewEpochState
@@ -1791,7 +1805,7 @@ foldEpochState
1791
1805
-> ( AnyNewEpochState
1792
1806
-> SlotNo
1793
1807
-> BlockNo
1794
- -> StateT s IO LedgerStateCondition
1808
+ -> StateT s IO ConditionResult
1795
1809
)
1796
1810
-- ^ Condition you want to check against the new epoch state.
1797
1811
--
@@ -1804,9 +1818,9 @@ foldEpochState
1804
1818
-- rollback. This is achieved by only calling the accumulator on states/blocks
1805
1819
-- that are older than the security parameter, k. This has the side effect of
1806
1820
-- truncating the last k blocks before the node's tip.
1807
- -> t m (LedgerStateCondition , s )
1821
+ -> t m (ConditionResult , s )
1808
1822
-- ^ The final state
1809
- foldEpochState nodeConfigFilePath socketPath validationMode terminationEpoch initialResult checkCondition = handleIOExceptions $ do
1823
+ foldEpochState nodeConfigFilePath socketPath validationMode terminationEpoch initialResult checkCondition = handleExceptions $ do
1810
1824
-- NOTE this was originally implemented with a non-pipelined client then
1811
1825
-- changed to a pipelined client for a modest speedup:
1812
1826
-- * Non-pipelined: 1h 0m 19s
@@ -1858,7 +1872,7 @@ foldEpochState nodeConfigFilePath socketPath validationMode terminationEpoch ini
1858
1872
Nothing -> modifyError FoldBlocksIOException . liftIO $ readMVar stateMv
1859
1873
where
1860
1874
protocols :: ()
1861
- => MVar (LedgerStateCondition , s )
1875
+ => MVar (ConditionResult , s )
1862
1876
-> IORef (Maybe LedgerStateError )
1863
1877
-> Env
1864
1878
-> LedgerState
@@ -1874,7 +1888,7 @@ foldEpochState nodeConfigFilePath socketPath validationMode terminationEpoch ini
1874
1888
-- | Defines the client side of the chain sync protocol.
1875
1889
chainSyncClient :: Word16
1876
1890
-- ^ The maximum number of concurrent requests.
1877
- -> MVar (LedgerStateCondition , s )
1891
+ -> MVar (ConditionResult , s )
1878
1892
-- ^ State accumulator. Written to on every block.
1879
1893
-> IORef (Maybe LedgerStateError )
1880
1894
-- ^ Resulting error if any. Written to once on protocol
@@ -2002,5 +2016,11 @@ atTerminationEpoch terminationEpoch events =
2002
2016
, currentEpoch' >= terminationEpoch
2003
2017
]
2004
2018
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