From 60529cc2090713e0b007b3731490d352a36f610f Mon Sep 17 00:00:00 2001 From: John Ky Date: Tue, 14 Feb 2023 22:52:21 +1100 Subject: [PATCH 1/2] Straight line code. --- .../src/Cardano/CLI/Shelley/Run/Governance.hs | 47 ++++++++++--------- update.proposal | 5 ++ 2 files changed, 29 insertions(+), 23 deletions(-) create mode 100644 update.proposal diff --git a/cardano-cli/src/Cardano/CLI/Shelley/Run/Governance.hs b/cardano-cli/src/Cardano/CLI/Shelley/Run/Governance.hs index a5db5cd67fa..d25e2ee903b 100644 --- a/cardano-cli/src/Cardano/CLI/Shelley/Run/Governance.hs +++ b/cardano-cli/src/Cardano/CLI/Shelley/Run/Governance.hs @@ -6,7 +6,8 @@ module Cardano.CLI.Shelley.Run.Governance import Control.Monad (unless, when) import Control.Monad.Trans.Except (ExceptT) -import Control.Monad.Trans.Except.Extra (firstExceptT, handleIOExceptT, left, newExceptT) +import Control.Monad.Trans.Except.Extra (firstExceptT, handleIOExceptT, left, newExceptT, + onLeft) import Data.Aeson (eitherDecode) import qualified Data.ByteString.Lazy as LB import Data.Text (Text) @@ -21,6 +22,8 @@ import Cardano.CLI.Shelley.Parsers import Cardano.CLI.Types import qualified Cardano.Ledger.Shelley.TxBody as Shelley +import Control.Monad.IO.Unlift (MonadIO (..)) +import Data.Function ((&)) data ShelleyGovernanceCmdError @@ -152,26 +155,24 @@ runGovernanceUpdateProposal -> Maybe FilePath -- ^ Cost models file path -> ExceptT ShelleyGovernanceCmdError IO () runGovernanceUpdateProposal (OutputFile upFile) eNo genVerKeyFiles upPprams mCostModelFp = do - finalUpPprams - <- case mCostModelFp of - Nothing -> return upPprams - Just fp -> do - costModelsBs <- - handleIOExceptT (ShelleyGovernanceCmdCostModelReadError . FileIOError fp) - $ LB.readFile fp - case eitherDecode costModelsBs of - Right cModels -> return $ upPprams {protocolUpdateCostModels = fromAlonzoCostModels cModels} - Left err -> left $ ShelleyGovernanceCmdCostModelsJsonDecodeErr fp $ Text.pack err - - when (finalUpPprams == mempty) $ left ShelleyGovernanceCmdEmptyUpdateProposalError - genVKeys <- sequence - [ firstExceptT ShelleyGovernanceCmdTextEnvReadError . newExceptT $ - readFileTextEnvelope - (AsVerificationKey AsGenesisKey) - vkeyFile - | VerificationKeyFile vkeyFile <- genVerKeyFiles ] - let genKeyHashes = fmap verificationKeyHash genVKeys - upProp = makeShelleyUpdateProposal finalUpPprams genKeyHashes eNo - firstExceptT ShelleyGovernanceCmdTextEnvWriteError . newExceptT $ - writeFileTextEnvelope upFile Nothing upProp + finalUpPprams <- case mCostModelFp of + Nothing -> return upPprams + Just fp -> do + costModelsBs <- handleIOExceptT (ShelleyGovernanceCmdCostModelReadError . FileIOError fp) $ LB.readFile fp + + cModels <- pure (eitherDecode costModelsBs) + & onLeft (left . ShelleyGovernanceCmdCostModelsJsonDecodeErr fp . Text.pack) + + return $ upPprams {protocolUpdateCostModels = fromAlonzoCostModels cModels} + + when (finalUpPprams == mempty) $ left ShelleyGovernanceCmdEmptyUpdateProposalError + + genVKeys <- sequence + [ firstExceptT ShelleyGovernanceCmdTextEnvReadError . newExceptT $ readFileTextEnvelope (AsVerificationKey AsGenesisKey) vkeyFile + | VerificationKeyFile vkeyFile <- genVerKeyFiles + ] + let genKeyHashes = fmap verificationKeyHash genVKeys + upProp = makeShelleyUpdateProposal finalUpPprams genKeyHashes eNo + + firstExceptT ShelleyGovernanceCmdTextEnvWriteError . newExceptT $ writeFileTextEnvelope upFile Nothing upProp diff --git a/update.proposal b/update.proposal new file mode 100644 index 00000000000..4fa8aa8319a --- /dev/null +++ b/update.proposal @@ -0,0 +1,5 @@ +{ + "type": "UpdateProposalShelley", + "description": "", + "cborHex": "82a1581c5c5dbf04f506922566f737301c824847df1e2ceadb979e8ccb29070b981a818208008080808080808080808080808080808080a0808081821b00000004a817c8001a03b20b80808080801832" +} From 59f6751dfcf466ce5a32793293f8bfc17fc6de55 Mon Sep 17 00:00:00 2001 From: John Ky Date: Tue, 14 Feb 2023 23:41:35 +1100 Subject: [PATCH 2/2] Error when costmodel is empty in create-update-proposal --- cardano-api/ChangeLog.md | 2 ++ .../src/Cardano/CLI/Shelley/Run/Governance.hs | 14 ++++++++++---- update.proposal | 5 ----- 3 files changed, 12 insertions(+), 9 deletions(-) delete mode 100644 update.proposal diff --git a/cardano-api/ChangeLog.md b/cardano-api/ChangeLog.md index 8341e8b8f19..f4313b0bc8c 100644 --- a/cardano-api/ChangeLog.md +++ b/cardano-api/ChangeLog.md @@ -16,6 +16,8 @@ - **Breaking change** - `deserialiseFromRawBytes` method of the `SerialiseAsRawBytes` type class to return `Either` instead of `Maybe`. Deprecate `eitherDeserialiseFromRawBytes`. Use `deserialiseFromRawBytes` instead. +- The `cardano-cli governance create-update-proposal` command to reject empty cost model. + ### Bugs - Allow reading text envelopes from pipes ([PR 4384](https://github.com/input-output-hk/cardano-node/pull/4384)) diff --git a/cardano-cli/src/Cardano/CLI/Shelley/Run/Governance.hs b/cardano-cli/src/Cardano/CLI/Shelley/Run/Governance.hs index d25e2ee903b..4d54e3e734d 100644 --- a/cardano-cli/src/Cardano/CLI/Shelley/Run/Governance.hs +++ b/cardano-cli/src/Cardano/CLI/Shelley/Run/Governance.hs @@ -10,6 +10,8 @@ import Control.Monad.Trans.Except.Extra (firstExceptT, handleIOExceptT onLeft) import Data.Aeson (eitherDecode) import qualified Data.ByteString.Lazy as LB +import Data.Function ((&)) +import qualified Data.List as List import Data.Text (Text) import qualified Data.Text as Text @@ -21,9 +23,8 @@ import Cardano.CLI.Shelley.Key (VerificationKeyOrHashOrFile, import Cardano.CLI.Shelley.Parsers import Cardano.CLI.Types +import Cardano.Ledger.Alonzo.Scripts (CostModels (..)) import qualified Cardano.Ledger.Shelley.TxBody as Shelley -import Control.Monad.IO.Unlift (MonadIO (..)) -import Data.Function ((&)) data ShelleyGovernanceCmdError @@ -39,6 +40,7 @@ data ShelleyGovernanceCmdError !Int -- ^ Number of reward amounts | ShelleyGovernanceCmdCostModelsJsonDecodeErr !FilePath !Text + | ShelleyGovernanceCmdEmptyCostModel !FilePath deriving Show renderShelleyGovernanceError :: ShelleyGovernanceCmdError -> Text @@ -55,8 +57,10 @@ renderShelleyGovernanceError err = <> " The number of staking keys: " <> textShow numVKeys <> " and the number of reward amounts: " <> textShow numRwdAmts <> " are not equivalent." - ShelleyGovernanceCmdCostModelsJsonDecodeErr err' fp -> - "Error decoding cost model: " <> Text.pack err' <> " at: " <> fp + ShelleyGovernanceCmdCostModelsJsonDecodeErr fp err' -> + "Error decoding cost model: " <> err' <> " at: " <> Text.pack fp + ShelleyGovernanceCmdEmptyCostModel fp -> + "The decoded cost model was empty at: " <> Text.pack fp ShelleyGovernanceCmdCostModelReadError err' -> "Error reading the cost model: " <> Text.pack (displayError err') @@ -163,6 +167,8 @@ runGovernanceUpdateProposal (OutputFile upFile) eNo genVerKeyFiles upPprams mCos cModels <- pure (eitherDecode costModelsBs) & onLeft (left . ShelleyGovernanceCmdCostModelsJsonDecodeErr fp . Text.pack) + when (List.null (unCostModels cModels)) $ left (ShelleyGovernanceCmdEmptyCostModel fp) + return $ upPprams {protocolUpdateCostModels = fromAlonzoCostModels cModels} when (finalUpPprams == mempty) $ left ShelleyGovernanceCmdEmptyUpdateProposalError diff --git a/update.proposal b/update.proposal deleted file mode 100644 index 4fa8aa8319a..00000000000 --- a/update.proposal +++ /dev/null @@ -1,5 +0,0 @@ -{ - "type": "UpdateProposalShelley", - "description": "", - "cborHex": "82a1581c5c5dbf04f506922566f737301c824847df1e2ceadb979e8ccb29070b981a818208008080808080808080808080808080808080a0808081821b00000004a817c8001a03b20b80808080801832" -}