Skip to content

Commit 59c6a02

Browse files
committed
Add ReaderT of NodeToClientVersion to LocalStateQueryExpr
1 parent 53873ce commit 59c6a02

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

cardano-api/cardano-api.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ library
140140
, iproute
141141
, memory
142142
, microlens
143+
, mtl
143144
, network
144145
, nothunks
145146
, optparse-applicative-fork

cardano-api/src/Cardano/Api/IPC/Monad.hs

+9-7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Control.Applicative
1313
import Control.Concurrent.STM
1414
import Control.Monad
1515
import Control.Monad.IO.Class
16+
import Control.Monad.Reader
1617
import Control.Monad.Trans.Cont
1718
import Data.Bifunctor (first)
1819
import Data.Either
@@ -45,14 +46,14 @@ import Cardano.Api.Modes
4546
-- In order to make pipelining still possible we can explore the use of Selective Functors
4647
-- which would allow us to straddle both worlds.
4748
newtype LocalStateQueryExpr block point query r m a = LocalStateQueryExpr
48-
{ runLocalStateQueryExpr :: ContT (Net.Query.ClientStAcquired block point query m r) m a
49-
} deriving (Functor, Applicative, Monad, MonadIO)
49+
{ runLocalStateQueryExpr :: ReaderT NodeToClientVersion (ContT (Net.Query.ClientStAcquired block point query m r) m) a
50+
} deriving (Functor, Applicative, Monad, MonadReader NodeToClientVersion, MonadIO)
5051

5152
-- | Execute a local state query expression.
5253
executeLocalStateQueryExpr
5354
:: LocalNodeConnectInfo mode
5455
-> Maybe ChainPoint
55-
-> (NodeToClientVersion -> LocalStateQueryExpr (BlockInMode mode) ChainPoint (QueryInMode mode) () IO a)
56+
-> LocalStateQueryExpr (BlockInMode mode) ChainPoint (QueryInMode mode) () IO a
5657
-> IO (Either AcquiringFailure a)
5758
executeLocalStateQueryExpr connectInfo mpoint f = do
5859
tmvResultLocalState <- newEmptyTMVarIO
@@ -63,7 +64,7 @@ executeLocalStateQueryExpr connectInfo mpoint f = do
6364
(\ntcVersion ->
6465
LocalNodeClientProtocols
6566
{ localChainSyncClient = NoLocalChainSyncClient
66-
, localStateQueryClient = Just $ setupLocalStateQueryExpr waitResult mpoint tmvResultLocalState (f ntcVersion)
67+
, localStateQueryClient = Just $ setupLocalStateQueryExpr waitResult mpoint tmvResultLocalState ntcVersion f
6768
, localTxSubmissionClient = Nothing
6869
, localTxMonitoringClient = Nothing
6970
}
@@ -79,12 +80,13 @@ setupLocalStateQueryExpr ::
7980
-- cause other incomplete protocols to abort which may lead to deadlock.
8081
-> Maybe ChainPoint
8182
-> TMVar (Either Net.Query.AcquireFailure a)
83+
-> NodeToClientVersion
8284
-> LocalStateQueryExpr (BlockInMode mode) ChainPoint (QueryInMode mode) () IO a
8385
-> Net.Query.LocalStateQueryClient (BlockInMode mode) ChainPoint (QueryInMode mode) IO ()
84-
setupLocalStateQueryExpr waitDone mPointVar' resultVar' f =
86+
setupLocalStateQueryExpr waitDone mPointVar' resultVar' ntcVersion f =
8587
LocalStateQueryClient . pure . Net.Query.SendMsgAcquire mPointVar' $
8688
Net.Query.ClientStAcquiring
87-
{ Net.Query.recvMsgAcquired = runContT (runLocalStateQueryExpr f) $ \result -> do
89+
{ Net.Query.recvMsgAcquired = runContT (runReaderT (runLocalStateQueryExpr f) ntcVersion) $ \result -> do
8890
atomically $ putTMVar resultVar' (Right result)
8991
void $ atomically waitDone -- Wait for all protocols to complete before exiting.
9092
pure $ Net.Query.SendMsgRelease $ pure $ Net.Query.SendMsgDone ()
@@ -98,7 +100,7 @@ setupLocalStateQueryExpr waitDone mPointVar' resultVar' f =
98100
-- | Use 'queryExpr' in a do block to construct monadic local state queries.
99101
queryExpr :: QueryInMode mode a -> LocalStateQueryExpr block point (QueryInMode mode) r IO a
100102
queryExpr q =
101-
LocalStateQueryExpr . ContT $ \f -> pure $
103+
LocalStateQueryExpr . ReaderT $ \_ -> ContT $ \f -> pure $
102104
Net.Query.SendMsgQuery q $
103105
Net.Query.ClientStQuerying
104106
{ Net.Query.recvMsgResult = f

cardano-cli/src/Cardano/CLI/Shelley/Run/Query.hs

+4-3
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ runQueryProtocolParameters (AnyConsensusModeParams cModeParams) network mOutFile
210210
$ newExceptT readEnvSocketPath
211211
let localNodeConnInfo = LocalNodeConnectInfo cModeParams network sockPath
212212

213-
result <- liftIO $ executeLocalStateQueryExpr localNodeConnInfo Nothing $ \_ntcVersion -> runExceptT $ do
213+
result <- liftIO $ executeLocalStateQueryExpr localNodeConnInfo Nothing $ runExceptT $ do
214214
anyE@(AnyCardanoEra era) <- lift $ determineEraExpr cModeParams
215215

216216
case cardanoEraStyle era of
@@ -285,7 +285,8 @@ runQueryTip (AnyConsensusModeParams cModeParams) network mOutFile = do
285285
CardanoMode -> do
286286
let localNodeConnInfo = LocalNodeConnectInfo cModeParams network sockPath
287287

288-
eLocalState <- liftIO $ executeLocalStateQueryExpr localNodeConnInfo Nothing $ \ntcVersion -> do
288+
eLocalState <- liftIO $ executeLocalStateQueryExpr localNodeConnInfo Nothing $ do
289+
ntcVersion <- ask
289290
era <- queryExpr (QueryCurrentEra CardanoModeIsMultiEra)
290291
eraHistory <- queryExpr (QueryEraHistory CardanoModeIsMultiEra)
291292
mChainBlockNo <- if ntcVersion >= NodeToClientV_10
@@ -1031,7 +1032,7 @@ runQueryStakePools (AnyConsensusModeParams cModeParams)
10311032
let localNodeConnInfo = LocalNodeConnectInfo cModeParams network sockPath
10321033

10331034
result <- ExceptT . fmap (join . first ShelleyQueryCmdAcquireFailure) $
1034-
executeLocalStateQueryExpr localNodeConnInfo Nothing $ \_ntcVersion -> runExceptT @ShelleyQueryCmdError $ do
1035+
executeLocalStateQueryExpr localNodeConnInfo Nothing $ runExceptT @ShelleyQueryCmdError $ do
10351036
anyE@(AnyCardanoEra era) <- case consensusModeOnly cModeParams of
10361037
ByronMode -> return $ AnyCardanoEra ByronEra
10371038
ShelleyMode -> return $ AnyCardanoEra ShelleyEra

0 commit comments

Comments
 (0)