Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify implementation of executeQueryAnyMode #5038

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 17 additions & 22 deletions cardano-api/src/Cardano/Api/Convenience/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module Cardano.Api.Convenience.Query (
) where

import Control.Monad.Trans.Except (ExceptT (..), except, runExceptT)
import Control.Monad.Trans.Except.Extra (firstExceptT, hoistMaybe)
import Control.Monad.Trans.Except.Extra (firstExceptT, hoistMaybe, left, onLeft,
onNothing)
import Data.Bifunctor (first)
import Data.Function ((&))
import Data.Set (Set)
Expand All @@ -34,6 +35,7 @@ import Cardano.Api.ProtocolParameters
import Cardano.Api.Query
import Cardano.Api.TxBody
import Cardano.Api.Utils
import Control.Monad.Trans (MonadTrans (..))

data QueryConvenienceError
= AcqFailure AcquiringFailure
Expand Down Expand Up @@ -123,39 +125,32 @@ executeQueryCardanoMode
executeQueryCardanoMode era nid q = runExceptT $ do
SocketPath sockPath <- firstExceptT SockErr . ExceptT $ readEnvSocketPath

let localConnectInfo =
let localNodeConnInfo =
LocalNodeConnectInfo
{ localConsensusModeParams = CardanoModeParams (EpochSlots 21600)
, localNodeNetworkId = nid
, localNodeSocketPath = sockPath
}

ExceptT $ executeQueryAnyMode era localConnectInfo q
ExceptT $ executeQueryAnyMode era localNodeConnInfo q

-- | Execute a query against the local node in any mode.
executeQueryAnyMode
:: forall result era mode. CardanoEra era
-> LocalNodeConnectInfo mode
-> QueryInMode mode (Either EraMismatch result)
-> IO (Either QueryConvenienceError result)
executeQueryAnyMode era localNodeConnInfo q = do
executeQueryAnyMode era localNodeConnInfo q = runExceptT $ do
let cMode = consensusModeOnly $ localConsensusModeParams localNodeConnInfo
case toEraInMode era cMode of
Just eraInMode ->
case eraInMode of
ByronEraInByronMode -> return $ Left ByronEraNotSupported
_ -> execQuery
Nothing -> return $ Left $ EraConsensusModeMismatch
(AnyConsensusMode CardanoMode)
(getIsCardanoEraConstraint era $ AnyCardanoEra era)
where
execQuery :: IO (Either QueryConvenienceError result)
execQuery = collapse <$> queryNodeLocalState localNodeConnInfo Nothing q

collapse
:: Either AcquiringFailure (Either EraMismatch a)
-> Either QueryConvenienceError a
collapse res = do
innerRes <- first AcqFailure res
first QueryEraMismatch innerRes

eraInMode <- pure (toEraInMode era cMode)
& onNothing (left $ EraConsensusModeMismatch
(AnyConsensusMode CardanoMode)
(getIsCardanoEraConstraint era $ AnyCardanoEra era))

case eraInMode of
ByronEraInByronMode -> left ByronEraNotSupported
_ ->
lift (queryNodeLocalState localNodeConnInfo Nothing q)
& onLeft (left . AcqFailure)
& onLeft (left . QueryEraMismatch)