Skip to content

Commit c615f8a

Browse files
committed
Add MuxError handling in FoldBlocksError
1 parent 3d885dc commit c615f8a

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

cardano-api/internal/Cardano/Api/LedgerState.hs

+33-13
Original file line numberDiff line numberDiff line change
@@ -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
166168
import Ouroboros.Consensus.TypeFamilyWrappers (WrapLedgerEvent (WrapLedgerEvent))
167169
import Ouroboros.Network.Block (blockNo)
168170
import qualified Ouroboros.Network.Block
171+
import Ouroboros.Network.Mux (MuxError)
169172
import qualified Ouroboros.Network.Protocol.ChainSync.Client as CS
170173
import qualified Ouroboros.Network.Protocol.ChainSync.ClientPipelined as CSP
171174
import 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

361365
instance 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

17661780
data 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+
]

cardano-api/internal/Cardano/Api/Orphans.hs

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
module Cardano.Api.Orphans () where
1818

19-
import Cardano.Api.Pretty (Pretty (..), (<+>))
19+
import Cardano.Api.Pretty (Pretty (..), prettyException, (<+>))
2020
import Cardano.Api.Via.ShowOf
2121

2222
import Cardano.Binary (DecoderError (..))
@@ -74,6 +74,7 @@ import qualified Ouroboros.Consensus.Shelley.Eras as Consensus
7474
import Ouroboros.Consensus.Shelley.Ledger.Block (ShelleyHash (..))
7575
import qualified Ouroboros.Consensus.Shelley.Ledger.Query as Consensus
7676
import Ouroboros.Network.Block (HeaderHash, Tip (..))
77+
import Ouroboros.Network.Mux (MuxError)
7778

7879
import qualified Codec.Binary.Bech32 as Bech32
7980
import qualified Codec.CBOR.Read as CBOR
@@ -474,3 +475,7 @@ instance Semigroup (Ledger.ConwayPParams StrictMaybe era) where
474475

475476
lastMappendWithTHKD :: (a -> Ledger.THKD g StrictMaybe b) -> a -> a -> Ledger.THKD g StrictMaybe b
476477
lastMappendWithTHKD f a b = Ledger.THKD $ lastMappendWith (Ledger.unTHKD . f) a b
478+
479+
instance Pretty MuxError where
480+
pretty err = "Mux layer error:" <+> prettyException err
481+

cardano-api/src/Cardano/Api.hs

+3-1
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,9 @@ module Cardano.Api (
751751
chainSyncClientPipelinedWithLedgerState,
752752

753753
-- *** Ledger state conditions
754-
LedgerStateCondition(..),
754+
ConditionResult(..),
755+
fromConditionResult,
756+
toConditionResult,
755757
AnyNewEpochState(..),
756758
foldEpochState,
757759
getAnyNewEpochState,

0 commit comments

Comments
 (0)