Skip to content

Commit

Permalink
Add 'conway governance vote view' command
Browse files Browse the repository at this point in the history
  • Loading branch information
carlhammann committed Oct 9, 2023
1 parent 2f6f50b commit 25fd1bb
Show file tree
Hide file tree
Showing 16 changed files with 207 additions and 18 deletions.
1 change: 1 addition & 0 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ test-suite cardano-cli-golden
Test.Golden.Governance.Action
Test.Golden.Governance.Committee
Test.Golden.Governance.DRep
Test.Golden.Governance.Vote
Test.Golden.Help
Test.Golden.Key.NonExtendedKey
Test.Golden.Shelley.Address.Build
Expand Down
18 changes: 17 additions & 1 deletion cardano-cli/src/Cardano/CLI/EraBased/Commands/Governance/Vote.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,37 @@

module Cardano.CLI.EraBased.Commands.Governance.Vote
( GovernanceVoteCmds(..)
, AnyVoteViewCmd(..)
, renderGovernanceVoteCmds
) where


import Cardano.Api.Shelley

import Cardano.CLI.Types.Governance

import Data.Text (Text)

newtype GovernanceVoteCmds era
data GovernanceVoteCmds era
= GovernanceVoteCreateCmd
AnyVote
| GovernanceVoteViewCmd
(AnyVoteViewCmd era)

data AnyVoteViewCmd era
= AnyVoteViewCmd
{ governanceVoteViewCmdYamlOutput :: Bool
, governanceVoteViewCmdEra :: ConwayEraOnwards era
, governanceVoteViewCmdVoteFile :: VoteFile In
, governanceVoteViewCmdOutputFile :: Maybe (File () Out)
}


renderGovernanceVoteCmds :: ()
=> GovernanceVoteCmds era
-> Text
renderGovernanceVoteCmds = \case
GovernanceVoteCreateCmd {} ->
"governance vote create"
GovernanceVoteViewCmd {} ->
"governance vote view"
35 changes: 29 additions & 6 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Governance/Vote.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ pGovernanceVoteCmds :: ()
-> Maybe (Parser (GovernanceVoteCmds era))
pGovernanceVoteCmds era =
subInfoParser "vote"
( Opt.progDesc
$ mconcat
[ "Vote commands."
]
)
[ pGovernanceVoteCreateCmd era
(Opt.progDesc "Vote commands.")
[ pGovernanceVoteCreateCmd era,
pGovernanceVoteViewCmd era
]

pGovernanceVoteCreateCmd :: ()
Expand Down Expand Up @@ -54,3 +51,29 @@ pAnyVotingStakeVerificationKeyOrHashOrFile =
asum [ AnyDRepVerificationKeyOrHashOrFile <$> pDRepVerificationKeyOrHashOrFile
, AnyStakePoolVerificationKeyOrHashOrFile <$> pStakePoolVerificationKeyOrHashOrFile Nothing
]

pGovernanceVoteViewCmd :: ()
=> CardanoEra era
-> Maybe (Parser (GovernanceVoteCmds era))
pGovernanceVoteViewCmd era = do
w <- forEraMaybeEon era
pure
$ subParser "view"
$ Opt.info
(GovernanceVoteViewCmd <$> pAnyVoteViewCmd w)
$ Opt.progDesc "Vote viewing."

pAnyVoteViewCmd :: ConwayEraOnwards era -> Parser (AnyVoteViewCmd era)
pAnyVoteViewCmd cOnwards =
AnyVoteViewCmd
<$> pYamlOutput
<*> pure cOnwards
<*> pFileInDirection "vote-file" "Input filepath of the vote."
<*> pMaybeOutputFile
where
pYamlOutput :: Parser Bool
pYamlOutput =
Opt.switch
( Opt.long "yaml"
<> Opt.help "Output vote in YAML format (and not JSON)."
)
34 changes: 30 additions & 4 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Governance/Vote.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ import qualified Cardano.Api.Ledger as Ledger
import Cardano.Api.Shelley

import Cardano.CLI.EraBased.Commands.Governance.Vote
import Cardano.CLI.Read (readVotingProceduresFile)
import Cardano.CLI.Types.Errors.CmdError
import Cardano.CLI.Types.Errors.GovernanceVoteCmdError
import Cardano.CLI.Types.Governance
import Cardano.CLI.Types.Key
import Cardano.Ledger.Keys (coerceKeyRole)

import Control.Monad.Except
import Control.Monad.Trans.Except
import Control.Monad.Trans.Except.Extra
import Data.Aeson.Encode.Pretty
import Data.Bifunctor
import Data.Function
import qualified Data.Yaml.Pretty as Yaml

runGovernanceVoteCmds :: ()
=> GovernanceVoteCmds era
Expand All @@ -31,6 +34,9 @@ runGovernanceVoteCmds = \case
GovernanceVoteCreateCmd anyVote ->
runGovernanceVoteCreateCmd anyVote
& firstExceptT CmdGovernanceVoteError
GovernanceVoteViewCmd (AnyVoteViewCmd printYaml w voteFile mOutFile) ->
runGovernanceVoteViewCmd printYaml w voteFile mOutFile
& firstExceptT CmdGovernanceVoteError

runGovernanceVoteCreateCmd
:: AnyVote
Expand All @@ -41,7 +47,7 @@ runGovernanceVoteCreateCmd (ConwayOnwardsVote cOnwards voteChoice (govActionTxId
shelleyBasedEraConstraints sbe $ do
case voteStakeCred of
AnyDRepVerificationKeyOrHashOrFile stake -> do
DRepKeyHash h <- firstExceptT GovernanceVoteCmdReadError
DRepKeyHash h <- firstExceptT GovernanceVoteCmdReadVerificationKeyError
. newExceptT $ readVerificationKeyOrHashOrTextEnvFile AsDRepKey stake
let vStakeCred = StakeCredentialByKey . StakeKeyHash $ coerceKeyRole h

Expand All @@ -53,7 +59,7 @@ runGovernanceVoteCreateCmd (ConwayOnwardsVote cOnwards voteChoice (govActionTxId
firstExceptT GovernanceVoteCmdWriteError . newExceptT $ writeFileTextEnvelope oFp Nothing votingProcedures

AnyStakePoolVerificationKeyOrHashOrFile stake -> do
h <- firstExceptT GovernanceVoteCmdReadError
h <- firstExceptT GovernanceVoteCmdReadVerificationKeyError
. newExceptT $ readVerificationKeyOrHashOrTextEnvFile AsStakePoolKey stake

let voter = Ledger.StakePoolVoter (unStakePoolKeyHash h)
Expand All @@ -63,7 +69,7 @@ runGovernanceVoteCreateCmd (ConwayOnwardsVote cOnwards voteChoice (govActionTxId
firstExceptT GovernanceVoteCmdWriteError . newExceptT $ writeFileTextEnvelope oFp Nothing votingProcedures

AnyCommitteeHotVerificationKeyOrHashOrFile stake -> do
CommitteeHotKeyHash h <- firstExceptT GovernanceVoteCmdReadError
CommitteeHotKeyHash h <- firstExceptT GovernanceVoteCmdReadVerificationKeyError
. newExceptT $ readVerificationKeyOrHashOrTextEnvFile AsCommitteeHotKey stake
let vStakeCred = StakeCredentialByKey . StakeKeyHash $ coerceKeyRole h
votingCred <- hoistEither $ first GovernanceVoteCmdCredentialDecodeError $ toVotingCredential cOnwards vStakeCred
Expand All @@ -72,3 +78,23 @@ runGovernanceVoteCreateCmd (ConwayOnwardsVote cOnwards voteChoice (govActionTxId
voteProcedure = createVotingProcedure cOnwards voteChoice Nothing
votingProcedures = singletonVotingProcedures cOnwards voter govActIdentifier (unVotingProcedure voteProcedure)
firstExceptT GovernanceVoteCmdWriteError . newExceptT $ writeFileTextEnvelope oFp Nothing votingProcedures

runGovernanceVoteViewCmd
:: Bool
-> ConwayEraOnwards era
-> VoteFile In
-> Maybe (File () Out)
-> ExceptT GovernanceVoteCmdError IO ()
runGovernanceVoteViewCmd outputYaml w fp mOutFile = do
let sbe = conwayEraOnwardsToShelleyBasedEra w

shelleyBasedEraConstraints sbe $ do
voteProcedures <- firstExceptT GovernanceVoteCmdReadVoteFileError . newExceptT $
readVotingProceduresFile w fp
firstExceptT GovernanceVoteCmdWriteError .
newExceptT .
(if outputYaml
then writeByteStringOutput mOutFile . Yaml.encodePretty (Yaml.setConfCompare compare Yaml.defConfig)
else writeLazyByteStringOutput mOutFile . encodePretty' (defConfig {confCompare = compare})) .
unVotingProcedures $
voteProcedures
7 changes: 5 additions & 2 deletions cardano-cli/src/Cardano/CLI/Read.hs
Original file line number Diff line number Diff line change
Expand Up @@ -757,11 +757,14 @@ readRequiredSigner (RequiredSignerSkeyFile skFile) = do
getHash (ShelleyNormalSigningKey sk) =
verificationKeyHash . getVerificationKey $ PaymentSigningKey sk

data VoteError
newtype VoteError
= VoteErrorFile (FileError TextEnvelopeError)
| VotesNotSupportedInEra AnyCardanoEra
deriving Show

instance Error VoteError where
displayError = \case
VoteErrorFile e -> displayError e

readVotingProceduresFiles :: ()
=> ConwayEraOnwards era
-> [VoteFile In]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@ import Cardano.Api.Shelley

import Cardano.Binary (DecoderError)

import Cardano.CLI.Read (VoteError)

import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Builder as TL
import qualified Formatting.Buildable as B

data GovernanceVoteCmdError
= GovernanceVoteCmdReadError !(FileError InputDecodeError)
= GovernanceVoteCmdReadVerificationKeyError !(FileError InputDecodeError)
| GovernanceVoteCmdReadVoteFileError !VoteError
| GovernanceVoteCmdCredentialDecodeError !DecoderError
| GovernanceVoteCmdWriteError !(FileError ())
deriving Show

instance Error GovernanceVoteCmdError where
displayError = \case
GovernanceVoteCmdReadError e ->
GovernanceVoteCmdReadVerificationKeyError e ->
"Cannot read verification key: " <> displayError e
GovernanceVoteCmdReadVoteFileError e ->
"Cannot read vote file: " <> displayError e
GovernanceVoteCmdCredentialDecodeError e ->
"Cannot decode voting credential: " <> renderDecoderError e
GovernanceVoteCmdWriteError e ->
Expand Down
6 changes: 4 additions & 2 deletions cardano-cli/test/cardano-cli-golden/Test/Golden/ErrorsSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ test_VoteReadError =
[ ("GovernanceVoteCmdCredentialDecodeError"
, GovernanceVoteCmdCredentialDecodeError
$ DecoderErrorCustom "<todecode>" "<decodeeerror>")
, ("GovernanceVoteCmdReadError"
, GovernanceVoteCmdReadError $ FileError "path/file.txt" InputInvalidError)
, ("GovernanceVoteCmdReadVerificationKeyError"
, GovernanceVoteCmdReadVerificationKeyError $ FileError "path/file.txt" InputInvalidError)
, ("GovernanceVoteCmdReadVoteFileError"
, GovernanceVoteCmdReadVoteFileError $ VoteErrorFile $ FileError "path/file.txt" $ TextEnvelopeAesonDecodeError "some error description")
, ("GovernanceVoteCmdWriteError"
, GovernanceVoteCmdWriteError $ FileError "path/file.txt" ())
]
Expand Down
68 changes: 68 additions & 0 deletions cardano-cli/test/cardano-cli-golden/Test/Golden/Governance/Vote.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{- HLINT ignore "Use camelCase" -}

module Test.Golden.Governance.Vote where

import Control.Monad (void)

import Test.Cardano.CLI.Util (execCardanoCLI, noteInputFile, propertyOnce)

import Hedgehog
import qualified Hedgehog.Extras.Test.Base as H
import qualified Hedgehog.Extras.Test.Golden as H

hprop_golden_governance_governance_vote_create :: Property
hprop_golden_governance_governance_vote_create =
propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
vkeyFile <- noteInputFile "test/cardano-cli-golden/files/input/drep.vkey"
voteFile <- H.noteTempFile tempDir "vote"
voteGold <- H.note "test/cardano-cli-golden/files/golden/governance/vote/vote"

void $ execCardanoCLI
[ "conway", "governance", "vote", "create"
, "--yes"
, "--governance-action-tx-id", "b1015258a99351c143a7a40b7b58f033ace10e3cc09c67780ed5b2b0992aa60a"
, "--governance-action-index", "5"
, "--drep-verification-key-file", vkeyFile
, "--out-file", voteFile
]

H.diffFileVsGoldenFile voteFile voteGold

hprop_golden_governance_governance_vote_view_json_stdout :: Property
hprop_golden_governance_governance_vote_view_json_stdout =
propertyOnce $ do
voteFile <- noteInputFile "test/cardano-cli-golden/files/golden/governance/vote/vote"
voteViewGold <- H.note "test/cardano-cli-golden/files/golden/governance/vote/voteViewJSON"
voteView <- execCardanoCLI
[ "conway", "governance", "vote", "view"
, "--vote-file", voteFile
]

H.diffVsGoldenFile voteView voteViewGold

hprop_golden_governance_governance_vote_view_json_outfile :: Property
hprop_golden_governance_governance_vote_view_json_outfile =
propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
voteFile <- noteInputFile "test/cardano-cli-golden/files/golden/governance/vote/vote"
voteViewFile <- H.noteTempFile tempDir "voteView"
voteViewGold <- H.note "test/cardano-cli-golden/files/golden/governance/vote/voteViewJSON"
void $ execCardanoCLI
[ "conway", "governance", "vote", "view"
, "--vote-file", voteFile
, "--out-file", voteViewFile
]

H.diffFileVsGoldenFile voteViewFile voteViewGold

hprop_golden_governance_governance_vote_view_yaml :: Property
hprop_golden_governance_governance_vote_view_yaml =
propertyOnce $ do
voteFile <- noteInputFile "test/cardano-cli-golden/files/golden/governance/vote/vote"
voteViewGold <- H.note "test/cardano-cli-golden/files/golden/governance/vote/voteViewYAML"
voteView <- execCardanoCLI
[ "conway", "governance", "vote", "view"
, "--yaml"
, "--vote-file", voteFile
]

H.diffVsGoldenFile voteView voteViewGold
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cannot read vote file: path/file.txt: TextEnvelope aeson decode error: some error description
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "Governance voting procedures",
"description": "",
"cborHex": "a18202581ceb09d5556a8bce421074e394d02c79ced96741657b4cf7ca8995294da1825820b1015258a99351c143a7a40b7b58f033ace10e3cc09c67780ed5b2b0992aa60a058201f6"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"drep-keyHash-eb09d5556a8bce421074e394d02c79ced96741657b4cf7ca8995294d": {
"b1015258a99351c143a7a40b7b58f033ace10e3cc09c67780ed5b2b0992aa60a#5": {
"anchor": null,
"decision": "VoteYes"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
drep-keyHash-eb09d5556a8bce421074e394d02c79ced96741657b4cf7ca8995294d:
b1015258a99351c143a7a40b7b58f033ace10e3cc09c67780ed5b2b0992aa60a#5:
anchor: null
decision: VoteYes
8 changes: 7 additions & 1 deletion cardano-cli/test/cardano-cli-golden/files/golden/help.cli
Original file line number Diff line number Diff line change
Expand Up @@ -6525,7 +6525,7 @@ Usage: cardano-cli conway governance drep registration-certificate

Create a registration certificate.

Usage: cardano-cli conway governance vote create
Usage: cardano-cli conway governance vote (create | view)

Vote commands.

Expand All @@ -6543,6 +6543,12 @@ Usage: cardano-cli conway governance vote create (--yes | --no | --abstain)

Vote creation.

Usage: cardano-cli conway governance vote view [--yaml]
--vote-file FILE
[--out-file FILE]

Vote viewing.

Usage: cardano-cli conway node
( key-gen
| key-gen-KES
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Usage: cardano-cli conway governance vote (create | view)

Vote commands.

Available options:
-h,--help Show this help text

Available commands:
create Vote creation.
view Vote viewing.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Usage: cardano-cli conway governance vote view [--yaml]
--vote-file FILE
[--out-file FILE]

Vote viewing.

Available options:
--yaml Output vote in YAML format (and not JSON).
--vote-file FILE Input filepath of the vote.
--out-file FILE Optional output file. Default is to write to stdout.
-h,--help Show this help text

0 comments on commit 25fd1bb

Please sign in to comment.