From c94c1767e03a85379aa1a8c01291bf60d62617a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Hurlin?= Date: Fri, 22 Nov 2024 16:14:00 +0100 Subject: [PATCH 1/3] Implement "query proposals" --- .../Cardano/CLI/EraBased/Commands/Query.hs | 16 +++++++++++ .../Cardano/CLI/EraBased/Options/Common.hs | 27 +++++++++++++++++++ .../src/Cardano/CLI/EraBased/Options/Query.hs | 23 ++++++++++++++++ .../src/Cardano/CLI/EraBased/Run/Query.hs | 26 ++++++++++++++++++ 4 files changed, 92 insertions(+) diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Commands/Query.hs b/cardano-cli/src/Cardano/CLI/EraBased/Commands/Query.hs index 25e246fff1..dbb8969015 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Commands/Query.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Commands/Query.hs @@ -11,6 +11,7 @@ module Cardano.CLI.EraBased.Commands.Query , QueryProtocolParametersCmdArgs (..) , QueryTipCmdArgs (..) , QueryStakePoolsCmdArgs (..) + , QueryProposalsCmdArgs (..) , QueryStakeDistributionCmdArgs (..) , QueryStakeAddressInfoCmdArgs (..) , QueryUTxOCmdArgs (..) @@ -32,6 +33,7 @@ module Cardano.CLI.EraBased.Commands.Query ) where +import qualified Cardano.Api.Ledger as L import qualified Cardano.Api.Network as Consensus import Cardano.Api.Shelley hiding (QueryInShelleyBasedEra (..)) @@ -66,6 +68,7 @@ data QueryCmds era | QuerySPOStakeDistributionCmd !(QuerySPOStakeDistributionCmdArgs era) | QueryCommitteeMembersStateCmd !(QueryCommitteeMembersStateCmdArgs era) | QueryTreasuryValueCmd !(QueryTreasuryValueCmdArgs era) + | QueryProposalsCmd !(QueryProposalsCmdArgs era) deriving (Generic, Show) -- | Fields that are common to most queries @@ -204,6 +207,17 @@ data QueryDRepStateCmdArgs era = QueryDRepStateCmdArgs } deriving Show +data QueryProposalsCmdArgs era = QueryProposalsCmdArgs + { eon :: !(ConwayEraOnwards era) + , nodeSocketPath :: !SocketPath + , consensusModeParams :: !ConsensusModeParams + , networkId :: !NetworkId + , govActionIds :: !(AllOrOnly (L.GovActionId L.StandardCrypto)) + , target :: !(Consensus.Target ChainPoint) + , mOutFile :: !(Maybe (File () Out)) + } + deriving Show + data QueryDRepStakeDistributionCmdArgs era = QueryDRepStakeDistributionCmdArgs { eon :: !(ConwayEraOnwards era) , commons :: !QueryCommons @@ -269,6 +283,8 @@ renderQueryCmds = \case "query slot-number" QueryRefScriptSizeCmd{} -> "query ref-script-size" + QueryProposalsCmd{} -> + "query proposals" QueryConstitutionCmd{} -> "constitution" QueryGovStateCmd{} -> diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs b/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs index 629c70d67b..5e945d52d1 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs @@ -3483,6 +3483,33 @@ pAllOrOnlySPOHashSource = pAll <|> pOnly , Opt.help "Query for all DReps." ] +pAllOrOnlyGovActionIds + :: () + => ConwayEraOnwards era + -> Parser (AllOrOnly (L.GovActionId L.StandardCrypto)) +pAllOrOnlyGovActionIds era = pAll <|> pOnly + where + pOnly = Only <$> pGovActionIds era + pAll = + Opt.flag' All $ + mconcat + [ Opt.long "all-proposals" + , Opt.help "Query for all governance proposals." + ] + +pGovActionIds + :: forall era + . () + => ConwayEraOnwards era + -> Parser [L.GovActionId L.StandardCrypto] +pGovActionIds era = conwayEraOnwardsConstraints era (some pLedgerGovernanceAction) + where + pLedgerGovernanceAction :: Parser (L.GovActionId L.StandardCrypto) + pLedgerGovernanceAction = uncurry L.GovActionId <$> pairParser + + pairParser :: Parser (L.TxId L.StandardCrypto, L.GovActionIx) + pairParser = bimap toShelleyTxId L.GovActionIx <$> pGovernanceActionId + pDRepVerificationKeyHash :: Parser (Hash DRepKey) pDRepVerificationKeyHash = Opt.option (rBech32KeyHash AsDRepKey <|> rHexHash AsDRepKey Nothing) $ diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Options/Query.hs b/cardano-cli/src/Cardano/CLI/EraBased/Options/Query.hs index d2aee2378f..bf3241cc23 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Options/Query.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Options/Query.hs @@ -268,6 +268,7 @@ pQueryCmds era envCli = , pQuerySPOStakeDistributionCmd era envCli , pQueryGetCommitteeStateCmd era envCli , pQueryTreasuryValueCmd era envCli + , pQueryProposalsCmd era envCli ] pQueryProtocolParametersCmd :: EnvCli -> Parser (QueryCmds era) @@ -520,6 +521,28 @@ pQueryDRepStakeDistributionCmd era envCli = do <*> pAllOrOnlyDRepHashSource <*> pMaybeOutputFile +pQueryProposalsCmd + :: () + => ShelleyBasedEra era + -> EnvCli + -> Maybe (Parser (QueryCmds era)) +pQueryProposalsCmd era envCli = do + w <- forShelleyBasedEraMaybeEon era + pure $ + subParser "proposals" $ + Opt.info (QueryProposalsCmd <$> pQueryProposalsCmdArgs w) $ + Opt.progDesc "Get the governance proposals." + where + pQueryProposalsCmdArgs :: ConwayEraOnwards era -> Parser (QueryProposalsCmdArgs era) + pQueryProposalsCmdArgs w = + QueryProposalsCmdArgs w + <$> pSocketPath envCli + <*> pConsensusModeParams + <*> pNetworkId envCli + <*> pAllOrOnlyGovActionIds w + <*> pTarget era + <*> optional pOutputFile + pQuerySPOStakeDistributionCmd :: () => ShelleyBasedEra era diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs b/cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs index 8e8c16d471..c88f546df0 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs @@ -75,6 +75,7 @@ import qualified Data.List as List import Data.Map.Strict (Map) import qualified Data.Map.Strict as Map import Data.Proxy (Proxy (..)) +import qualified Data.Sequence as Seq import Data.Set (Set) import qualified Data.Set as Set import Data.String @@ -117,6 +118,7 @@ runQueryCmds = \case Cmd.QuerySPOStakeDistributionCmd args -> runQuerySPOStakeDistribution args Cmd.QueryCommitteeMembersStateCmd args -> runQueryCommitteeMembersState args Cmd.QueryTreasuryValueCmd args -> runQueryTreasuryValue args + Cmd.QueryProposalsCmd args -> runQueryProposals args runQueryProtocolParametersCmd :: () @@ -1802,6 +1804,30 @@ runQueryTreasuryValue writeLazyByteStringFile outFile $ LBS.pack treasuryString +runQueryProposals + :: Cmd.QueryProposalsCmdArgs era + -> ExceptT QueryCmdError IO () +runQueryProposals + Cmd.QueryProposalsCmdArgs + { Cmd.eon + , Cmd.nodeSocketPath + , Cmd.consensusModeParams + , Cmd.networkId + , Cmd.govActionIds = govActionIds' + , Cmd.target + , Cmd.mOutFile + } = conwayEraOnwardsConstraints eon $ do + let localNodeConnInfo = LocalNodeConnectInfo consensusModeParams networkId nodeSocketPath + + let govActionIds = case govActionIds' of + All -> [] + Only l -> l + + govActionStates :: (Seq.Seq (L.GovActionState (ShelleyLedgerEra era))) <- + runQuery localNodeConnInfo target $ queryProposals eon $ Set.fromList govActionIds + + writeOutput mOutFile govActionStates + runQuery :: LocalNodeConnectInfo -> Consensus.Target ChainPoint From 11721af70a90dcb336680e0d6e7f28eaf01f052d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Hurlin?= Date: Thu, 28 Nov 2024 17:54:20 +0100 Subject: [PATCH 2/3] Adapt golden files --- .../cardano-cli-golden/files/golden/help.cli | 32 +++++++++++++++ .../files/golden/help/conway_query.cli | 2 + .../golden/help/conway_query_proposals.cli | 39 +++++++++++++++++++ .../files/golden/help/latest_query.cli | 2 + .../golden/help/latest_query_proposals.cli | 39 +++++++++++++++++++ 5 files changed, 114 insertions(+) create mode 100644 cardano-cli/test/cardano-cli-golden/files/golden/help/conway_query_proposals.cli create mode 100644 cardano-cli/test/cardano-cli-golden/files/golden/help/latest_query_proposals.cli diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help.cli index 0aac0a2b8e..590ec29af4 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help.cli @@ -7243,6 +7243,7 @@ Usage: cardano-cli conway query | spo-stake-distribution | committee-state | treasury + | proposals ) Node query commands. Will query the local node whose Unix domain socket is @@ -7608,6 +7609,21 @@ Usage: cardano-cli conway query treasury --socket-path SOCKET_PATH Get the treasury value +Usage: cardano-cli conway query proposals --socket-path SOCKET_PATH + [--cardano-mode + [--epoch-slots SLOTS]] + ( --mainnet + | --testnet-magic NATURAL + ) + [--volatile-tip | --immutable-tip] + ( --all-proposals + | (--governance-action-tx-id TXID + --governance-action-index WORD16) + ) + [--out-file FILEPATH] + + Get the governance proposals. + Usage: cardano-cli conway stake-address ( key-gen | key-hash @@ -9243,6 +9259,7 @@ Usage: cardano-cli latest query | spo-stake-distribution | committee-state | treasury + | proposals ) Node query commands. Will query the local node whose Unix domain socket is @@ -9608,6 +9625,21 @@ Usage: cardano-cli latest query treasury --socket-path SOCKET_PATH Get the treasury value +Usage: cardano-cli latest query proposals --socket-path SOCKET_PATH + [--cardano-mode + [--epoch-slots SLOTS]] + ( --mainnet + | --testnet-magic NATURAL + ) + [--volatile-tip | --immutable-tip] + ( --all-proposals + | (--governance-action-tx-id TXID + --governance-action-index WORD16) + ) + [--out-file FILEPATH] + + Get the governance proposals. + Usage: cardano-cli latest stake-address ( key-gen | key-hash diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_query.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_query.cli index 4f2507652b..67fb235016 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_query.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_query.cli @@ -21,6 +21,7 @@ Usage: cardano-cli conway query | spo-stake-distribution | committee-state | treasury + | proposals ) Node query commands. Will query the local node whose Unix domain socket is @@ -64,3 +65,4 @@ Available commands: spo-stake-distribution Get the SPO stake distribution. committee-state Get the committee state treasury Get the treasury value + proposals Get the governance proposals. diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_query_proposals.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_query_proposals.cli new file mode 100644 index 0000000000..06e06095e9 --- /dev/null +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_query_proposals.cli @@ -0,0 +1,39 @@ +Usage: cardano-cli conway query proposals --socket-path SOCKET_PATH + [--cardano-mode + [--epoch-slots SLOTS]] + ( --mainnet + | --testnet-magic NATURAL + ) + [--volatile-tip | --immutable-tip] + ( --all-proposals + | (--governance-action-tx-id TXID + --governance-action-index WORD16) + ) + [--out-file FILEPATH] + + Get the governance proposals. + +Available options: + --socket-path SOCKET_PATH + Path to the node socket. This overrides the + CARDANO_NODE_SOCKET_PATH environment variable. The + argument is optional if CARDANO_NODE_SOCKET_PATH is + defined and mandatory otherwise. + --cardano-mode For talking to a node running in full Cardano mode + (default). + --epoch-slots SLOTS The number of slots per epoch for the Byron era. + (default: 21600) + --mainnet Use the mainnet magic id. This overrides the + CARDANO_NODE_NETWORK_ID environment variable + --testnet-magic NATURAL Specify a testnet magic id. This overrides the + CARDANO_NODE_NETWORK_ID environment variable + --volatile-tip Use the volatile tip as a target. (This is the + default) + --immutable-tip Use the immutable tip as a target. + --all-proposals Query for all governance proposals. + --governance-action-tx-id TXID + Txid of the governance action. + --governance-action-index WORD16 + Tx's governance action index. + --out-file FILEPATH The output file. + -h,--help Show this help text diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help/latest_query.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help/latest_query.cli index 29935af77a..24254c1fb8 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help/latest_query.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help/latest_query.cli @@ -21,6 +21,7 @@ Usage: cardano-cli latest query | spo-stake-distribution | committee-state | treasury + | proposals ) Node query commands. Will query the local node whose Unix domain socket is @@ -64,3 +65,4 @@ Available commands: spo-stake-distribution Get the SPO stake distribution. committee-state Get the committee state treasury Get the treasury value + proposals Get the governance proposals. diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help/latest_query_proposals.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help/latest_query_proposals.cli new file mode 100644 index 0000000000..d56bc92fb3 --- /dev/null +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help/latest_query_proposals.cli @@ -0,0 +1,39 @@ +Usage: cardano-cli latest query proposals --socket-path SOCKET_PATH + [--cardano-mode + [--epoch-slots SLOTS]] + ( --mainnet + | --testnet-magic NATURAL + ) + [--volatile-tip | --immutable-tip] + ( --all-proposals + | (--governance-action-tx-id TXID + --governance-action-index WORD16) + ) + [--out-file FILEPATH] + + Get the governance proposals. + +Available options: + --socket-path SOCKET_PATH + Path to the node socket. This overrides the + CARDANO_NODE_SOCKET_PATH environment variable. The + argument is optional if CARDANO_NODE_SOCKET_PATH is + defined and mandatory otherwise. + --cardano-mode For talking to a node running in full Cardano mode + (default). + --epoch-slots SLOTS The number of slots per epoch for the Byron era. + (default: 21600) + --mainnet Use the mainnet magic id. This overrides the + CARDANO_NODE_NETWORK_ID environment variable + --testnet-magic NATURAL Specify a testnet magic id. This overrides the + CARDANO_NODE_NETWORK_ID environment variable + --volatile-tip Use the volatile tip as a target. (This is the + default) + --immutable-tip Use the immutable tip as a target. + --all-proposals Query for all governance proposals. + --governance-action-tx-id TXID + Txid of the governance action. + --governance-action-index WORD16 + Tx's governance action index. + --out-file FILEPATH The output file. + -h,--help Show this help text From 54f4a6d8512bf7ccb6a2053b8d9fd5a4cc681599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Hurlin?= Date: Wed, 11 Dec 2024 09:58:29 +0100 Subject: [PATCH 3/3] query proposals: share code using QueryCommons --- .../src/Cardano/CLI/EraBased/Commands/Query.hs | 5 +---- cardano-cli/src/Cardano/CLI/EraBased/Options/Query.hs | 5 +---- cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs | 11 +++++++---- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Commands/Query.hs b/cardano-cli/src/Cardano/CLI/EraBased/Commands/Query.hs index dbb8969015..5b284be306 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Commands/Query.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Commands/Query.hs @@ -209,11 +209,8 @@ data QueryDRepStateCmdArgs era = QueryDRepStateCmdArgs data QueryProposalsCmdArgs era = QueryProposalsCmdArgs { eon :: !(ConwayEraOnwards era) - , nodeSocketPath :: !SocketPath - , consensusModeParams :: !ConsensusModeParams - , networkId :: !NetworkId + , commons :: !QueryCommons , govActionIds :: !(AllOrOnly (L.GovActionId L.StandardCrypto)) - , target :: !(Consensus.Target ChainPoint) , mOutFile :: !(Maybe (File () Out)) } deriving Show diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Options/Query.hs b/cardano-cli/src/Cardano/CLI/EraBased/Options/Query.hs index bf3241cc23..a8884ebe21 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Options/Query.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Options/Query.hs @@ -536,11 +536,8 @@ pQueryProposalsCmd era envCli = do pQueryProposalsCmdArgs :: ConwayEraOnwards era -> Parser (QueryProposalsCmdArgs era) pQueryProposalsCmdArgs w = QueryProposalsCmdArgs w - <$> pSocketPath envCli - <*> pConsensusModeParams - <*> pNetworkId envCli + <$> pQueryCommons (convert w) envCli <*> pAllOrOnlyGovActionIds w - <*> pTarget era <*> optional pOutputFile pQuerySPOStakeDistributionCmd diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs b/cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs index c88f546df0..e72f5ecbd5 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs @@ -1810,11 +1810,14 @@ runQueryProposals runQueryProposals Cmd.QueryProposalsCmdArgs { Cmd.eon - , Cmd.nodeSocketPath - , Cmd.consensusModeParams - , Cmd.networkId + , Cmd.commons = + Cmd.QueryCommons + { Cmd.nodeSocketPath + , Cmd.consensusModeParams + , Cmd.networkId + , Cmd.target + } , Cmd.govActionIds = govActionIds' - , Cmd.target , Cmd.mOutFile } = conwayEraOnwardsConstraints eon $ do let localNodeConnInfo = LocalNodeConnectInfo consensusModeParams networkId nodeSocketPath