4
4
{-# LANGUAGE FlexibleContexts #-}
5
5
{-# LANGUAGE FlexibleInstances #-}
6
6
{-# LANGUAGE GADTs #-}
7
+ {-# LANGUAGE LambdaCase #-}
7
8
{-# LANGUAGE RankNTypes #-}
8
9
{-# LANGUAGE ScopedTypeVariables #-}
9
10
{-# LANGUAGE TypeApplications #-}
@@ -130,6 +131,7 @@ data ShelleyQueryCmdError
130
131
-- ^ Operational certificate of the unknown stake pool.
131
132
| ShelleyQueryCmdPoolStateDecodeError DecoderError
132
133
| ShelleyQueryCmdStakeSnapshotDecodeError DecoderError
134
+ | ShelleyQueryCmdUnsupportedNtcVersion ! UnsupportedNtcVersionError
133
135
134
136
deriving Show
135
137
@@ -169,6 +171,10 @@ renderShelleyQueryCmdError err =
169
171
" Failed to decode PoolState. Error: " <> Text. pack (show decoderError)
170
172
ShelleyQueryCmdStakeSnapshotDecodeError decoderError ->
171
173
" Failed to decode StakeSnapshot. Error: " <> Text. pack (show decoderError)
174
+ ShelleyQueryCmdUnsupportedNtcVersion (UnsupportedNtcVersionError minNtcVersion ntcVersion) ->
175
+ " Unsupported feature for the node-to-client protocol version.\n " <>
176
+ " This query requires at least " <> show minNtcVersion <> " but the node negotiated " <> show ntcVersion <> " .\n " <>
177
+ " Later node versions support later protocol versions (but development protocol versions are not enabled in the node by default)."
172
178
173
179
runQueryCmd :: QueryCmd -> ExceptT ShelleyQueryCmdError IO ()
174
180
runQueryCmd cmd =
@@ -211,7 +217,11 @@ runQueryProtocolParameters (AnyConsensusModeParams cModeParams) network mOutFile
211
217
let localNodeConnInfo = LocalNodeConnectInfo cModeParams network sockPath
212
218
213
219
result <- liftIO $ executeLocalStateQueryExpr localNodeConnInfo Nothing $ \ _ntcVersion -> runExceptT $ do
214
- anyE@ (AnyCardanoEra era) <- lift $ determineEraExpr cModeParams
220
+ eraResult <- lift $ determineEraExpr cModeParams
221
+
222
+ anyE@ (AnyCardanoEra era) <- case eraResult of
223
+ Right a -> pure a
224
+ Left e -> throwE $ ShelleyQueryCmdUnsupportedNtcVersion e
215
225
216
226
case cardanoEraStyle era of
217
227
LegacyByronEra -> left ShelleyQueryCmdByronEra
@@ -223,7 +233,10 @@ runQueryProtocolParameters (AnyConsensusModeParams cModeParams) network mOutFile
223
233
224
234
ppResult <- lift . queryExpr $ QueryInEra eInMode $ QueryInShelleyBasedEra sbe QueryProtocolParameters
225
235
226
- except ppResult & firstExceptT ShelleyQueryCmdEraMismatch
236
+ case ppResult of
237
+ Right (Right a) -> pure a
238
+ Right (Left e) -> throwE $ ShelleyQueryCmdEraMismatch e
239
+ Left e -> throwE $ ShelleyQueryCmdUnsupportedNtcVersion e
227
240
228
241
writeProtocolParameters mOutFile =<< except (join (first ShelleyQueryCmdAcquireFailure result))
229
242
where
@@ -263,6 +276,12 @@ percentage tolerance a b = Text.pack (printf "%.2f" pc)
263
276
relativeTimeSeconds :: RelativeTime -> Integer
264
277
relativeTimeSeconds (RelativeTime dt) = floor (nominalDiffTimeToSeconds dt)
265
278
279
+ swapEither :: Either e (Either x a ) -> Either x (Either e a )
280
+ swapEither = \ case
281
+ Left e -> Right (Left e)
282
+ Right (Left e) -> Left e
283
+ Right (Right a) -> Right (Right a)
284
+
266
285
-- | Query the chain tip via the chain sync protocol.
267
286
--
268
287
-- This is a fallback query to support older versions of node to client protocol.
@@ -285,25 +304,30 @@ runQueryTip (AnyConsensusModeParams cModeParams) network mOutFile = do
285
304
CardanoMode -> do
286
305
let localNodeConnInfo = LocalNodeConnectInfo cModeParams network sockPath
287
306
288
- eLocalState <- liftIO $ executeLocalStateQueryExpr localNodeConnInfo Nothing $ \ ntcVersion -> do
289
- era <- queryExpr (QueryCurrentEra CardanoModeIsMultiEra )
290
- eraHistory <- queryExpr (QueryEraHistory CardanoModeIsMultiEra )
291
- mChainBlockNo <- if ntcVersion >= NodeToClientV_10
292
- then Just <$> queryExpr QueryChainBlockNo
293
- else return Nothing
294
- mChainPoint <- if ntcVersion >= NodeToClientV_10
295
- then Just <$> queryExpr (QueryChainPoint CardanoMode )
296
- else return Nothing
297
- mSystemStart <- if ntcVersion >= NodeToClientV_9
298
- then Just <$> queryExpr QuerySystemStart
299
- else return Nothing
300
-
301
- return O. QueryTipLocalState
302
- { O. era = era
303
- , O. eraHistory = eraHistory
304
- , O. mSystemStart = mSystemStart
305
- , O. mChainTip = makeChainTip <$> mChainBlockNo <*> mChainPoint
306
- }
307
+ eLocalState <- ExceptT $ fmap swapEither $
308
+ executeLocalStateQueryExpr localNodeConnInfo Nothing $ \ ntcVersion -> runExceptT $ do
309
+ era <- ExceptT $ fmap (first ShelleyQueryCmdUnsupportedNtcVersion ) $
310
+ queryExpr (QueryCurrentEra CardanoModeIsMultiEra )
311
+
312
+ eraHistory <- ExceptT $ fmap (first ShelleyQueryCmdUnsupportedNtcVersion ) $
313
+ queryExpr (QueryEraHistory CardanoModeIsMultiEra )
314
+
315
+ mChainBlockNo <- if ntcVersion >= NodeToClientV_10
316
+ then fmap Just $ ExceptT $ fmap (first ShelleyQueryCmdUnsupportedNtcVersion ) $ queryExpr QueryChainBlockNo
317
+ else return Nothing
318
+ mChainPoint <- if ntcVersion >= NodeToClientV_10
319
+ then fmap Just $ ExceptT $ fmap (first ShelleyQueryCmdUnsupportedNtcVersion ) $ queryExpr (QueryChainPoint CardanoMode )
320
+ else return Nothing
321
+ mSystemStart <- if ntcVersion >= NodeToClientV_9
322
+ then fmap Just $ ExceptT $ fmap (first ShelleyQueryCmdUnsupportedNtcVersion ) $ queryExpr QuerySystemStart
323
+ else return Nothing
324
+
325
+ return O. QueryTipLocalState
326
+ { O. era = era
327
+ , O. eraHistory = eraHistory
328
+ , O. mSystemStart = mSystemStart
329
+ , O. mChainTip = makeChainTip <$> mChainBlockNo <*> mChainPoint
330
+ }
307
331
308
332
mLocalState <- hushM (first ShelleyQueryCmdAcquireFailure eLocalState) $ \ e ->
309
333
liftIO . T. hPutStrLn IO. stderr $ " Warning: Local state unavailable: " <> renderShelleyQueryCmdError e
@@ -1035,16 +1059,20 @@ runQueryStakePools (AnyConsensusModeParams cModeParams)
1035
1059
anyE@ (AnyCardanoEra era) <- case consensusModeOnly cModeParams of
1036
1060
ByronMode -> return $ AnyCardanoEra ByronEra
1037
1061
ShelleyMode -> return $ AnyCardanoEra ShelleyEra
1038
- CardanoMode -> lift . queryExpr $ QueryCurrentEra CardanoModeIsMultiEra
1062
+ CardanoMode -> ExceptT $ fmap (first ShelleyQueryCmdUnsupportedNtcVersion ) $ queryExpr $ QueryCurrentEra CardanoModeIsMultiEra
1039
1063
1040
1064
let cMode = consensusModeOnly cModeParams
1041
1065
1042
1066
case toEraInMode era cMode of
1043
1067
Just eInMode -> do
1044
1068
sbe <- getSbe $ cardanoEraStyle era
1045
1069
1046
- firstExceptT ShelleyQueryCmdEraMismatch . ExceptT $
1047
- queryExpr . QueryInEra eInMode . QueryInShelleyBasedEra sbe $ QueryStakePools
1070
+ r <- ExceptT $ fmap (first ShelleyQueryCmdUnsupportedNtcVersion ) $ queryExpr $
1071
+ QueryInEra eInMode $ QueryInShelleyBasedEra sbe $ QueryStakePools
1072
+
1073
+ case r of
1074
+ Right a -> pure a
1075
+ Left e -> throwE (ShelleyQueryCmdEraMismatch e)
1048
1076
1049
1077
Nothing -> left $ ShelleyQueryCmdEraConsensusModeMismatch (AnyConsensusMode cMode) anyE
1050
1078
0 commit comments