Skip to content

Commit

Permalink
Merge #4885
Browse files Browse the repository at this point in the history
4885: Disallow empty cost model for create update proposal r=newhoggy a=newhoggy

```
$ cardano-cli governance create-update-proposal --epoch 50 --max-block-execution-units '(20000000000,62000000)' --cost-model-file ~/Downloads/cost-model-secp-preprod-mainnet.json --protocol-major-version 8 --protocol-minor-version 0 --genesis-verification-key-file example/genesis-keys/genesis1.vkey --out-file update.proposal
Command failed: governance create-update-proposal  Error: The decoded cost model was empty at: /Users/jky/Downloads/cost-model-secp-preprod-mainnet.json
```

Resolves #4867

Co-authored-by: John Ky <john.ky@iohk.io>
  • Loading branch information
iohk-bors[bot] and newhoggy authored Feb 14, 2023
2 parents c5ca9c2 + 59f6751 commit daeae61
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
2 changes: 2 additions & 0 deletions cardano-api/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
57 changes: 32 additions & 25 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/Governance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ 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.Function ((&))
import qualified Data.List as List
import Data.Text (Text)
import qualified Data.Text as Text

Expand All @@ -20,6 +23,7 @@ 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


Expand All @@ -36,6 +40,7 @@ data ShelleyGovernanceCmdError
!Int
-- ^ Number of reward amounts
| ShelleyGovernanceCmdCostModelsJsonDecodeErr !FilePath !Text
| ShelleyGovernanceCmdEmptyCostModel !FilePath
deriving Show

renderShelleyGovernanceError :: ShelleyGovernanceCmdError -> Text
Expand All @@ -52,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')

Expand Down Expand Up @@ -152,26 +159,26 @@ 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)

when (List.null (unCostModels cModels)) $ left (ShelleyGovernanceCmdEmptyCostModel fp)

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

0 comments on commit daeae61

Please sign in to comment.