Skip to content

Commit

Permalink
Simplify implementation of executeQueryAnyMode (No oops is involved)
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Mar 31, 2023
1 parent 38d3791 commit f88f8ce
Showing 1 changed file with 17 additions and 22 deletions.
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)

0 comments on commit f88f8ce

Please sign in to comment.