Skip to content

Commit

Permalink
Add hash check to governance vote create
Browse files Browse the repository at this point in the history
  • Loading branch information
palas committed Oct 15, 2024
1 parent 74fe174 commit 6c6832c
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ data GovernanceVoteCreateCmdArgs era
, voteChoice :: Vote
, governanceAction :: (TxId, Word16)
, votingStakeCredentialSource :: AnyVotingStakeVerificationKeyOrHashOrFile
, mAnchor :: Maybe (VoteUrl, L.SafeHash L.StandardCrypto L.AnchorData)
, mAnchor
:: !( Maybe
( PotentiallyCheckedAnchor
VoteUrl
(VoteUrl, L.SafeHash L.StandardCrypto L.AnchorData)
)
)
, outFile :: VoteFile Out
}

Expand Down
3 changes: 3 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3612,6 +3612,9 @@ pMustCheckMetadataHash = pMustCheckHash "drep-metadata-hash" "DRep metadata" "--
pMustCheckStakeMetadataHash :: Parser (MustCheckHash StakePoolMetadataReference)
pMustCheckStakeMetadataHash = pMustCheckHash "metadata-hash" "stake pool metadata" "--metadata-hash" "--metadata-url"

pMustCheckVoteUrl :: Parser (MustCheckHash VoteUrl)
pMustCheckVoteUrl = pMustCheckHash "anchor-data-hash" "vote anchor data" "--anchor-data-hash" "--anchor-url"

pMustCheckResignationMetadataHash :: Parser (MustCheckHash ResignationMetadataUrl)
pMustCheckResignationMetadataHash =
pMustCheckHash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ pGovernanceVoteCreateCmdArgs cOnwards =
<$> pVoteChoice
<*> pGovernanceActionId
<*> pAnyVotingStakeVerificationKeyOrHashOrFile
<*> optional pVoteAnchor
<*> optional
( pPotentiallyCheckedAnchorData
pMustCheckVoteUrl
pVoteAnchor
)
<*> pFileOutDirection "out-file" "Output filepath of the vote."

pAnyVotingStakeVerificationKeyOrHashOrFile :: Parser AnyVotingStakeVerificationKeyOrHashOrFile
Expand Down
26 changes: 19 additions & 7 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Governance/Vote.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Cardano.Api.Shelley

import qualified Cardano.CLI.EraBased.Commands.Governance.Vote as Cmd
import Cardano.CLI.Read (readSingleVote)
import Cardano.CLI.Run.Hash (carryHashChecks)
import Cardano.CLI.Types.Common
import Cardano.CLI.Types.Errors.CmdError
import Cardano.CLI.Types.Errors.GovernanceVoteCmdError
Expand All @@ -26,6 +27,7 @@ import Cardano.CLI.Types.Key
import Data.Aeson.Encode.Pretty
import Data.Function
import qualified Data.Yaml.Pretty as Yaml
import Lens.Micro (_Just, over)

runGovernanceVoteCmds
:: ()
Expand Down Expand Up @@ -53,14 +55,24 @@ runGovernanceVoteCreateCmd
, outFile
} = do
let (govActionTxId, govActionIndex) = governanceAction
let sbe = conwayEraOnwardsToShelleyBasedEra eon -- TODO: Conway era - update vote creation related function to take ConwayEraOnwards
voteProcedure <- case mAnchor of
sbe = conwayEraOnwardsToShelleyBasedEra eon -- TODO: Conway era - update vote creation related function to take ConwayEraOnwards
mAnchor' =
over
(_Just . _pcaAnchor)
(\(VoteUrl url, voteHash) -> L.Anchor{L.anchorUrl = url, L.anchorDataHash = voteHash})
mAnchor

mapM_
(withExceptT GovernanceVoteCmdResignationCertHashCheckError . carryHashChecks)
mAnchor'

voteProcedure <- case mAnchor' of
Nothing -> pure $ createVotingProcedure eon voteChoice Nothing
Just (VoteUrl url, voteHash) -> shelleyBasedEraConstraints sbe $ do
let voteAnchor = L.Anchor{L.anchorUrl = url, L.anchorDataHash = voteHash}
VotingProcedure votingProcedureWithoutAnchor = createVotingProcedure eon voteChoice Nothing
votingProcedureWithAnchor = VotingProcedure $ votingProcedureWithoutAnchor{L.vProcAnchor = L.SJust voteAnchor}
pure votingProcedureWithAnchor
Just voteAnchor ->
shelleyBasedEraConstraints sbe $
let VotingProcedure votingProcedureWithoutAnchor = createVotingProcedure eon voteChoice Nothing
votingProcedureWithAnchor = VotingProcedure $ votingProcedureWithoutAnchor{L.vProcAnchor = L.SJust (pcaAnchor voteAnchor)}
in return votingProcedureWithAnchor

shelleyBasedEraConstraints sbe $ do
voter <- firstExceptT GovernanceVoteCmdReadVerificationKeyError $ case votingStakeCredentialSource of
Expand Down
11 changes: 11 additions & 0 deletions cardano-cli/src/Cardano/CLI/Types/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ module Cardano.CLI.Types.Common
, DRepMetadataUrl
, ResignationMetadataUrl
, PotentiallyCheckedAnchor (..)
, _pcaAnchor
)
where

Expand All @@ -104,6 +105,7 @@ import Data.String (IsString)
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Word (Word64)
import Lens.Micro.Type (Traversal)

-- | Determines the direction in which the MIR certificate will transfer ADA.
data TransferDirection
Expand Down Expand Up @@ -665,3 +667,12 @@ data PotentiallyCheckedAnchor anchorType anchor
-- ^ Whether to check the hash or not (CheckHash for checking or TrustHash for not checking)
}
deriving (Eq, Show)

_pcaAnchor
:: Traversal
(PotentiallyCheckedAnchor anchorType anchor)
(PotentiallyCheckedAnchor anchorType anchor')
anchor
anchor'
_pcaAnchor f pca@(PotentiallyCheckedAnchor{pcaAnchor = pcaAnchor'}) =
(\newAnchor -> pca{pcaAnchor = newAnchor}) <$> f pcaAnchor'
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import Cardano.Api.Shelley

import Cardano.Binary (DecoderError)
import Cardano.CLI.Read (VoteError)
import Cardano.CLI.Types.Errors.HashCmdError (HashCheckError)

import Control.Exception (displayException)
import qualified Data.Text.Lazy.Builder as TL
import qualified Formatting.Buildable as B

Expand All @@ -18,6 +20,7 @@ data GovernanceVoteCmdError
| GovernanceVoteCmdCredentialDecodeError !DecoderError
| GovernanceVoteCmdWriteError !(FileError ())
| GovernanceVoteCmdReadVoteTextError !VoteError
| GovernanceVoteCmdResignationCertHashCheckError !HashCheckError
deriving Show

instance Error GovernanceVoteCmdError where
Expand All @@ -32,5 +35,8 @@ instance Error GovernanceVoteCmdError where
"Cannot write vote: " <> prettyError e
GovernanceVoteCmdReadVoteTextError e ->
"Cannot read vote text: " <> prettyError e
GovernanceVoteCmdResignationCertHashCheckError hashCheckErr ->
"Error while checking resignation certificate metadata hash: "
<> pretty (displayException hashCheckErr)
where
renderDecoderError = pretty . TL.toLazyText . B.build
6 changes: 4 additions & 2 deletions cardano-cli/test/cardano-cli-golden/files/golden/help.cli
Original file line number Diff line number Diff line change
Expand Up @@ -7054,7 +7054,8 @@ Usage: cardano-cli conway governance vote create (--yes | --no | --abstain)
| --cc-hot-script-hash HASH
)
[--anchor-url TEXT
--anchor-data-hash HASH]
--anchor-data-hash HASH
[--check-anchor-data-hash]]
--out-file FILEPATH

Vote creation.
Expand Down Expand Up @@ -9054,7 +9055,8 @@ Usage: cardano-cli latest governance vote create (--yes | --no | --abstain)
| --cc-hot-script-hash HASH
)
[--anchor-url TEXT
--anchor-data-hash HASH]
--anchor-data-hash HASH
[--check-anchor-data-hash]]
--out-file FILEPATH

Vote creation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ Usage: cardano-cli conway governance vote create (--yes | --no | --abstain)
| --cc-hot-script-hash HASH
)
[--anchor-url TEXT
--anchor-data-hash HASH]
--anchor-data-hash HASH
[--check-anchor-data-hash]]
--out-file FILEPATH

Vote creation.
Expand Down Expand Up @@ -50,5 +51,10 @@ Available options:
--anchor-url TEXT Vote anchor URL
--anchor-data-hash HASH Hash of the vote anchor data (obtain it with
"cardano-cli hash anchor-data ...").
--check-anchor-data-hash Verify that the expected vote anchor data hash
provided in --anchor-data-hash matches the hash of
the file downloaded from the URL provided in
--anchor-url (this parameter will download the file
from the URL)
--out-file FILEPATH Output filepath of the vote.
-h,--help Show this help text
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ Usage: cardano-cli latest governance vote create (--yes | --no | --abstain)
| --cc-hot-script-hash HASH
)
[--anchor-url TEXT
--anchor-data-hash HASH]
--anchor-data-hash HASH
[--check-anchor-data-hash]]
--out-file FILEPATH

Vote creation.
Expand Down Expand Up @@ -50,5 +51,10 @@ Available options:
--anchor-url TEXT Vote anchor URL
--anchor-data-hash HASH Hash of the vote anchor data (obtain it with
"cardano-cli hash anchor-data ...").
--check-anchor-data-hash Verify that the expected vote anchor data hash
provided in --anchor-data-hash matches the hash of
the file downloaded from the URL provided in
--anchor-url (this parameter will download the file
from the URL)
--out-file FILEPATH Output filepath of the vote.
-h,--help Show this help text

0 comments on commit 6c6832c

Please sign in to comment.