Skip to content

Commit

Permalink
Refactor CLI query module slightly in preparation
Browse files Browse the repository at this point in the history
For the next changes, it is very helpful to have the query errors
nested, since there are more of them.

Co-authored-by: Luke Nadur <19835357+intricate@users.noreply.github.com>
  • Loading branch information
dcoutts and intricate committed Jul 10, 2020
1 parent 8c24fd8 commit b10b8a5
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/Query.hs
Expand Up @@ -52,7 +52,7 @@ import Cardano.Config.Shelley.Orphans ()
import Cardano.Config.Types (SocketPath(..))
import Cardano.Binary (decodeFull)

import Ouroboros.Consensus.Shelley.Ledger (ShelleyBlock)
import Ouroboros.Consensus.Cardano.Block (Either (..), EraMismatch (..))
import Ouroboros.Consensus.Shelley.Protocol.Crypto (TPraosStandardCrypto)
import Ouroboros.Network.Block (Point, getTipPoint)

Expand All @@ -78,7 +78,7 @@ import Ouroboros.Network.Protocol.LocalStateQuery.Type as LocalStateQu

data ShelleyQueryCmdError
= ShelleyQueryEnvVarSocketErr !EnvSocketError
| NodeLocalStateQueryError !LocalStateQuery.AcquireFailure
| ShelleyQueryNodeLocalStateQueryError !LocalStateQueryError
| ShelleyQueryWriteProtocolParamsError !FilePath !IOException
| ShelleyQueryWriteFilteredUTxOsError !FilePath !IOException
| ShelleyQueryWriteStakeDistributionError !FilePath !IOException
Expand All @@ -91,8 +91,7 @@ renderShelleyQueryCmdError :: ShelleyQueryCmdError -> Text
renderShelleyQueryCmdError err =
case err of
ShelleyQueryEnvVarSocketErr envSockErr -> renderEnvSocketError envSockErr
NodeLocalStateQueryError lsqErr ->
"Local state query acquire failure: " <> show lsqErr
ShelleyQueryNodeLocalStateQueryError lsqErr -> renderLocalStateQueryError lsqErr
ShelleyQueryWriteProtocolParamsError fp ioException ->
"Error writing protocol parameters at: " <> show fp <> " Error: " <> show ioException
ShelleyQueryWriteFilteredUTxOsError fp ioException ->
Expand Down Expand Up @@ -122,6 +121,7 @@ runQueryCmd cmd =
runQueryUTxO qFilter networkId mOutFile
_ -> liftIO $ putStrLn $ "runQueryCmd: " ++ show cmd


runQueryProtocolParameters
:: NetworkId
-> Maybe OutputFile
Expand All @@ -135,7 +135,7 @@ runQueryProtocolParameters network mOutFile = do
localNodeConsensusMode = ShelleyMode
}
tip <- liftIO $ getLocalTip connectInfo
pparams <- firstExceptT NodeLocalStateQueryError $
pparams <- firstExceptT (ShelleyQueryNodeLocalStateQueryError . AcquireFailureError) $
queryPParamsFromLocalState connectInfo (getTipPoint tip)
writeProtocolParameters mOutFile pparams

Expand Down Expand Up @@ -179,7 +179,7 @@ runQueryUTxO qfilter network mOutFile = do
localNodeConsensusMode = ShelleyMode
}
tip <- liftIO $ getLocalTip connectInfo
filteredUtxo <- firstExceptT NodeLocalStateQueryError $
filteredUtxo <- firstExceptT (ShelleyQueryNodeLocalStateQueryError . AcquireFailureError) $
queryUTxOFromLocalState connectInfo qfilter (getTipPoint tip)
writeFilteredUTxOs mOutFile filteredUtxo

Expand All @@ -196,7 +196,7 @@ runQueryLedgerState network mOutFile = do
localNodeConsensusMode = ShelleyMode
}
tip <- liftIO $ getLocalTip connectInfo
els <- firstExceptT NodeLocalStateQueryError $
els <- firstExceptT (ShelleyQueryNodeLocalStateQueryError . AcquireFailureError) $
queryLocalLedgerState connectInfo (getTipPoint tip)
case els of
Right lstate -> writeLedgerState mOutFile lstate
Expand All @@ -218,7 +218,7 @@ runQueryStakeAddressInfo addr network mOutFile = do
localNodeConsensusMode = ShelleyMode
}
tip <- liftIO $ getLocalTip connectInfo
delegsAndRwds <- firstExceptT NodeLocalStateQueryError $
delegsAndRwds <- firstExceptT (ShelleyQueryNodeLocalStateQueryError . AcquireFailureError) $
queryDelegationsAndRewardsFromLocalState
connectInfo
(Set.singleton addr)
Expand All @@ -230,8 +230,22 @@ runQueryStakeAddressInfo addr network mOutFile = do
-- | An error that can occur while querying a node's local state.
data LocalStateQueryError
= AcquireFailureError !LocalStateQuery.AcquireFailure
| EraMismatchError !EraMismatch
-- ^ A query from a certain era was applied to a ledger from a different
-- era.
| ByronProtocolNotSupportedError
-- ^ The query does not support the Byron protocol.
deriving (Eq, Show)

renderLocalStateQueryError :: LocalStateQueryError -> Text
renderLocalStateQueryError lsqErr =
case lsqErr of
AcquireFailureError err -> "Local state query acquire failure: " <> show err
EraMismatchError err ->
"A query from a certain era was applied to a ledger from a different era: " <> show err
ByronProtocolNotSupportedError ->
"The attempted local state query does not support the Byron protocol."

writeStakeAddressInfo
:: Maybe OutputFile
-> DelegationsAndRewards
Expand Down Expand Up @@ -296,7 +310,7 @@ runQueryStakeDistribution network mOutFile = do
localNodeConsensusMode = ShelleyMode
}
tip <- liftIO $ getLocalTip connectInfo
stakeDist <- firstExceptT NodeLocalStateQueryError $
stakeDist <- firstExceptT (ShelleyQueryNodeLocalStateQueryError . AcquireFailureError) $
queryStakeDistributionFromLocalState connectInfo (getTipPoint tip)
writeStakeDistribution mOutFile stakeDist

Expand Down

0 comments on commit b10b8a5

Please sign in to comment.