Skip to content

Commit

Permalink
Add --include-stake convenience flag to drep-state query
Browse files Browse the repository at this point in the history
Use custom sum type to turn stake info on or off

Make the "stake" field only show up when used

Restore DRep credentials to the output of `drep-state`
  • Loading branch information
carlhammann committed Jan 19, 2024
1 parent 24dd2e1 commit a9656ef
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
7 changes: 7 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Commands/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module Cardano.CLI.EraBased.Commands.Query
, QueryDRepStateCmdArgs(..)
, QueryDRepStakeDistributionCmdArgs(..)
, renderQueryCmds
, IncludeStake (..)
) where

import Cardano.Api.Shelley hiding (QueryInShelleyBasedEra (..))
Expand Down Expand Up @@ -188,9 +189,15 @@ data QueryDRepStateCmdArgs era = QueryDRepStateCmdArgs
, consensusModeParams :: !ConsensusModeParams
, networkId :: !NetworkId
, drepKeys :: !(AllOrOnly (VerificationKeyOrHashOrFile DRepKey))
, includeStake :: !IncludeStake
, mOutFile :: !(Maybe (File () Out))
} deriving Show

-- | Whether to include the stake, as queried by drep-stake-distribution, in
-- the output of drep-state. This is (computationally) expensive, but sometimes
-- convenient.
data IncludeStake = WithStake | NoStake deriving Show

data QueryDRepStakeDistributionCmdArgs era = QueryDRepStakeDistributionCmdArgs
{ eon :: !(ConwayEraOnwards era)
, nodeSocketPath :: !SocketPath
Expand Down
10 changes: 10 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,16 @@ pQueryDRepStateCmd era envCli = do
<*> pConsensusModeParams
<*> pNetworkId envCli
<*> pAllOrOnlyDRepVerificationKeyOrHashOrFile
<*> Opt.flag WithStake NoStake (mconcat
[ Opt.long "include-stake"
, Opt.help $ mconcat
[ "Also return the stake associated with each DRep. "
, "The result is the same as with \"drep-stake-distribution\"; "
, "this is a convenience option to obtain all information concerning a DRep at once. "
, "This is a potentially expensive query."
]
]
)
<*> optional pOutputFile

pQueryDRepStakeDistributionCmd :: ()
Expand Down
33 changes: 25 additions & 8 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}

Expand Down Expand Up @@ -1394,24 +1395,40 @@ runQueryDRepState
, Cmd.consensusModeParams
, Cmd.networkId
, Cmd.drepKeys = drepKeys'
, Cmd.includeStake
, Cmd.mOutFile
} = conwayEraOnwardsConstraints eon $ do
let localNodeConnInfo = LocalNodeConnectInfo consensusModeParams networkId nodeSocketPath

let drepKeys = case drepKeys' of
All -> []
Only l -> l
drepCreds <- Set.fromList <$> mapM (firstExceptT QueryCmdDRepKeyError . getDRepCredentialFromVerKeyHashOrFile) drepKeys
drepCreds <- mapM (firstExceptT QueryCmdDRepKeyError . getDRepCredentialFromVerKeyHashOrFile) drepKeys

drepState <- runQuery localNodeConnInfo $ queryDRepState eon $ Set.fromList drepCreds

drepStakeDistribution <-
case includeStake of
Cmd.WithStake -> runQuery localNodeConnInfo $
queryDRepStakeDistribution eon (Set.fromList $ Ledger.DRepCredential <$> drepCreds)
Cmd.NoStake -> return mempty

drepState <- runQuery localNodeConnInfo $ queryDRepState eon drepCreds
writeOutput mOutFile $
second drepStateToJson <$> Map.assocs drepState
drepStateToJson drepStakeDistribution <$> Map.assocs drepState
where
drepStateToJson ds = A.object
[ "expiry" .= (ds ^. Ledger.drepExpiryL)
, "anchor" .= (ds ^. Ledger.drepAnchorL)
, "deposit" .= (ds ^. Ledger.drepDepositL)
]
drepStateToJson stakeDistr (cred, ds) = (cred,) . A.object $
if Map.null stakeDistr
then
[ "expiry" .= (ds ^. Ledger.drepExpiryL)
, "anchor" .= (ds ^. Ledger.drepAnchorL)
, "deposit" .= (ds ^. Ledger.drepDepositL)
]
else
[ "expiry" .= (ds ^. Ledger.drepExpiryL)
, "anchor" .= (ds ^. Ledger.drepAnchorL)
, "deposit" .= (ds ^. Ledger.drepDepositL)
, "stake" .= Map.lookup (Ledger.DRepCredential cred) stakeDistr
]

runQueryDRepStakeDistribution
:: Cmd.QueryDRepStakeDistributionCmdArgs era
Expand Down
1 change: 1 addition & 0 deletions cardano-cli/test/cardano-cli-golden/files/golden/help.cli
Original file line number Diff line number Diff line change
Expand Up @@ -6721,6 +6721,7 @@ Usage: cardano-cli conway query drep-state --socket-path SOCKET_PATH
| --drep-key-hash HASH
)
)
[--include-stake]
[--out-file FILE]

Get the DRep state. If no DRep credentials are provided, return states for all
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Usage: cardano-cli conway query drep-state --socket-path SOCKET_PATH
| --drep-key-hash HASH
)
)
[--include-stake]
[--out-file FILE]

Get the DRep state. If no DRep credentials are provided, return states for all
Expand All @@ -37,5 +38,10 @@ Available options:
Filepath of the DRep verification key.
--drep-key-hash HASH DRep verification key hash (either Bech32-encoded or
hex-encoded).
--include-stake Also return the stake associated with each DRep. The
result is the same as with "drep-stake-distribution";
this is a convenience option to obtain all
information concerning a DRep at once. This is a
potentially expensive query.
--out-file FILE The output file.
-h,--help Show this help text

0 comments on commit a9656ef

Please sign in to comment.