Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,9 @@ write-ghc-environment-files: always
-- IMPORTANT
-- Do NOT add more source-repository-package stanzas here unless they are strictly
-- temporary! Please read the section in CONTRIBUTING about updating dependencies.

source-repository-package
type: git
location: https://github.com/IntersectMBO/cardano-api
subdir: cardano-api
tag: 4ab850fcdefe8536fb98274ba5eaf54030a852e8
1 change: 1 addition & 0 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ library
network-uri,
optparse-applicative-fork,
parsec,
plutus-ledger-api,
prettyprinter,
prettyprinter-ansi-terminal,
random,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,6 @@ runGovernanceActionCreateProtocolParametersUpdateCmd
=> Cmd.GovernanceActionProtocolParametersUpdateCmdArgs era
-> ExceptT GovernanceActionsError IO ()
runGovernanceActionCreateProtocolParametersUpdateCmd eraBasedPParams' = do
let sbe = uppShelleyBasedEra eraBasedPParams'
caseShelleyToBabbageOrConwayEraOnwards
( \sToB -> do
let oFp = uppFilePath eraBasedPParams'
Expand Down Expand Up @@ -387,13 +386,14 @@ runGovernanceActionCreateProtocolParametersUpdateCmd eraBasedPParams' = do
)
sbe
where
sbe = uppShelleyBasedEra eraBasedPParams'
theUpdate =
case uppCostModelsFile eraBasedPParams' of
Nothing -> pure $ uppNewPParams eraBasedPParams'
Just (Cmd.CostModelsFile alonzoOnwards costModelsFile) -> do
costModels <-
firstExceptT GovernanceActionsCmdCostModelsError $
readCostModels costModelsFile
readCostModels sbe costModelsFile
pure . addCostModelsToEraBasedProtocolParametersUpdate alonzoOnwards costModels $
uppNewPParams eraBasedPParams'

Expand Down
44 changes: 41 additions & 3 deletions cardano-cli/src/Cardano/CLI/Read.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}

module Cardano.CLI.Read
Expand Down Expand Up @@ -119,11 +120,14 @@
import Cardano.CLI.Types.Governance
import Cardano.CLI.Types.Key
import qualified Cardano.Crypto.Hash as Crypto
import qualified PlutusLedgerApi.V1.ParamName as PlutusV1
import qualified PlutusLedgerApi.V2.ParamName as PlutusV2
import qualified PlutusLedgerApi.V3.ParamName as PlutusV3

import Prelude

import Control.Exception (bracket, displayException)
import Control.Monad (forM, unless, when)
import Control.Monad (forM, forM_, unless, when)
import qualified Data.Aeson as Aeson
import Data.Bifunctor
import Data.ByteString (ByteString)
Expand All @@ -134,6 +138,7 @@
import Data.Function ((&))
import Data.IORef (IORef, newIORef, readIORef, writeIORef)
import qualified Data.List as List
import qualified Data.Map as Map
import Data.Proxy (Proxy (..))
import Data.String
import Data.Text (Text)
Expand Down Expand Up @@ -1057,6 +1062,9 @@
= CostModelsErrorReadFile (FileError ())
| CostModelsErrorJSONDecode FilePath String
| CostModelsErrorEmpty FilePath
| -- | @CostModelsErrorWrongSize expected actual@ indicates that the cost model
-- has @actual@ entries, but @expected@ entries were expected.
CostModelsErrorWrongSize Int Int
deriving Show

instance Error CostModelsError where
Expand All @@ -1067,6 +1075,11 @@
"Error decoding JSON cost model at " <> pshow fp <> ": " <> pretty err <> formatExplanation
CostModelsErrorEmpty fp ->
"The decoded cost model was empty at: " <> pshow fp <> formatExplanation
CostModelsErrorWrongSize expected actual ->
"The decoded cost model has the wrong size: expected "
<> pretty expected
<> ", but got "
<> pretty actual
where
formatExplanation =
vsep
Expand All @@ -1086,13 +1099,38 @@
]

readCostModels
:: File L.CostModels In
:: ()
=> ShelleyBasedEra era
-> File L.CostModels In
-> ExceptT CostModelsError IO L.CostModels
readCostModels (File fp) = do
readCostModels sbe (File fp) = do
bytes <- handleIOExceptT (CostModelsErrorReadFile . FileIOError fp) $ LBS.readFile fp
costModels <- firstExceptT (CostModelsErrorJSONDecode fp) . except $ Aeson.eitherDecode bytes
when (null $ fromAlonzoCostModels costModels) $ throwE $ CostModelsErrorEmpty fp
forM_ (allValues @L.Language) $ checkCostModelSize costModels
return costModels
where
checkCostModelSize :: L.CostModels -> L.Language -> ExceptT CostModelsError IO ()
checkCostModelSize models lang =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think those checks should be in cardano-api so we can reuse them in decoding genesis as well, because genesis can contain PV2 and PV3 models. This is used only in starting testnets afaik.

case Map.lookup lang (L.costModelsValid models) of
Nothing -> pure ()
Just (model :: L.CostModel) -> do
let actual = Map.size (L.costModelToMap model)
expected = languageToParameterCount lang
unless (expected == actual) $ throwE $ CostModelsErrorWrongSize expected actual
return ()
Comment on lines +1117 to +1121

Check warning

Code scanning / HLint

Redundant return Warning

cardano-cli/src/Cardano/CLI/Read.hs:(1117,38)-(1121,17): Warning: Redundant return
  
Found:
  do let actual = Map.size (L.costModelToMap model)
         expected = languageToParameterCount lang
     unless (expected == actual)
       $ throwE $ CostModelsErrorWrongSize expected actual
     return ()
  
Perhaps:
  do let actual = Map.size (L.costModelToMap model)
         expected = languageToParameterCount lang
     unless (expected == actual)
       $ throwE $ CostModelsErrorWrongSize expected actual
allValues :: forall a. (Bounded a, Enum a) => [a]
allValues = [minBound :: a .. maxBound]
languageToParameterCount :: L.Language -> Int
languageToParameterCount = \case
L.PlutusV1 -> length $ allValues @PlutusV1.ParamName -- 166
L.PlutusV2 ->
let nbParamNames = length $ allValues @PlutusV2.ParamName in -- 185
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let nbParamNames = length $ allValues @PlutusV2.ParamName in -- 185
let nParamNames = length $ allValues @PlutusV2.ParamName in -- 185

😄

caseShelleyToBabbageOrConwayEraOnwards
(const $ nbParamNames - 10) -- Ten parameters were added to V2 in Conway, need to remove them here
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not so sure about hardcoding that number 10. We should account for the fact that the number of params can change in the future, however it's unlikely. That's why there's jumping over many hoops in Cardano.Api.Genesis and Test.Cardano.Api.Genesis, so we will get a clear signal when things unexpectedly change.

(const nbParamNames)
sbe
L.PlutusV3 -> length $ allValues @PlutusV3.ParamName -- 297

-- Misc

Expand Down
106 changes: 106 additions & 0 deletions cardano-cli/test/cardano-cli-golden/Test/Golden/Governance/Action.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import Cardano.Api (MonadIO)
import Control.Monad (void)
import Control.Monad.Catch (MonadCatch)
import Control.Monad.Trans.Control (MonadBaseControl)
import Data.List (isInfixOf)
import System.Exit (ExitCode (ExitSuccess))

import Test.Cardano.CLI.Hash (exampleAnchorDataHash, exampleAnchorDataHash2,
exampleAnchorDataIpfsHash, exampleAnchorDataIpfsHash2,
Expand Down Expand Up @@ -460,6 +462,8 @@ base_golden_conway_governance_action_create_protocol_parameters_update hash temp
"test/cardano-cli-golden/files/golden/governance/action/conway-create-protocol-parameters-update.action"
H.diffFileVsGoldenFile actionFile goldenActionFile

-- | Execute me with:
-- @cabal test cardano-cli-golden --test-options '-p "/golden conway governance action create protocol parameters update partial costmodel/"'@
hprop_golden_conway_governance_action_create_protocol_parameters_update_partial_costmodel
:: Property
hprop_golden_conway_governance_action_create_protocol_parameters_update_partial_costmodel =
Expand Down Expand Up @@ -495,6 +499,108 @@ hprop_golden_conway_governance_action_create_protocol_parameters_update_partial_
"test/cardano-cli-golden/files/golden/governance/action/conway-create-protocol-parameters-update-partial-costmodels.action"
H.diffFileVsGoldenFile actionFile goldenActionFile

-- | Execute me with:
-- @cabal test cardano-cli-golden --test-options '-p "/golden conway governance action create protocol parameters too small costmodel size/"'@
hprop_golden_conway_governance_action_create_protocol_parameters_too_small_costmodel_size
:: Property
hprop_golden_conway_governance_action_create_protocol_parameters_too_small_costmodel_size =
propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
-- This file has 165 entries, whereas PV1 requires 166
runOnCostModelFile
tempDir
"test/cardano-cli-golden/files/input/governance/costmodels-v1-too-small.json"
-- This file has 184 entries, whereas PV2 requires 185
runOnCostModelFile
tempDir
"test/cardano-cli-golden/files/input/governance/costmodels-v2-too-small.json"
-- This file has 184 entries, whereas PV3 requires 297
runOnCostModelFile
tempDir
"test/cardano-cli-golden/files/input/governance/costmodels-v3-too-small.json"
where
runOnCostModelFile tempDir costModelsFile = do
stakeAddressVKeyFile <- H.note "test/cardano-cli-golden/files/input/governance/stake-address.vkey"

actionFile <- noteTempFile tempDir "action"

(exitCode, _stdout, stderr) <-
H.noteShowM $
H.execDetailCardanoCLI
[ "conway"
, "governance"
, "action"
, "create-protocol-parameters-update"
, "--anchor-url"
, "example.com"
, "--anchor-data-hash"
, "c7ddb5b493faa4d3d2d679847740bdce0c5d358d56f9b1470ca67f5652a02745"
, "--mainnet"
, "--deposit-return-stake-verification-key-file"
, stakeAddressVKeyFile
, "--governance-action-deposit"
, "12345"
, "--cost-model-file"
, costModelsFile
, "--out-file"
, actionFile
]

H.assert (exitCode /= ExitSuccess)
H.assertWith stderr $ \msg -> "The decoded cost model has the wrong size" `isInfixOf` msg

-- | Execute me with:
-- @cabal test cardano-cli-golden --test-options '-p "/golden conway governance action create protocol parameters too large costmodel size/"'@
hprop_golden_conway_governance_action_create_protocol_parameters_too_large_costmodel_size
:: Property
hprop_golden_conway_governance_action_create_protocol_parameters_too_large_costmodel_size =
propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
-- From https://input-output-rnd.slack.com/archives/CCRB7BU8Y/p1727096158830419?thread_ts=1727089226.813099&cid=CCRB7BU8Y
--
-- Having too large models is fine:
--
-- Ziyang Liu
-- There should not be any check on the upper bound. The number of parameters for V1, V2 and V3 can increase at any time as we add new builtins or other features to the languages.
-- Theoretically they can also possibly decrease but that's very unlikely, certainly not below the current numbers.

-- This file has 167 entries, whereas PV1 requires 166
runOnCostModelFile
tempDir
"test/cardano-cli-golden/files/input/governance/costmodels-v1-too-large.json"
-- This file has 186 entries, whereas PV2 requires 185
runOnCostModelFile
tempDir
"test/cardano-cli-golden/files/input/governance/costmodels-v2-too-large.json"
-- This file has 298 entries, whereas PV3 requires 297
runOnCostModelFile
tempDir
"test/cardano-cli-golden/files/input/governance/costmodels-v3-too-large.json"
where
runOnCostModelFile tempDir costModelsFile = do
stakeAddressVKeyFile <- H.note "test/cardano-cli-golden/files/input/governance/stake-address.vkey"

actionFile <- noteTempFile tempDir "action"

H.noteShowM_ $
H.execCardanoCLI
[ "conway"
, "governance"
, "action"
, "create-protocol-parameters-update"
, "--anchor-url"
, "example.com"
, "--anchor-data-hash"
, "c7ddb5b493faa4d3d2d679847740bdce0c5d358d56f9b1470ca67f5652a02745"
, "--mainnet"
, "--deposit-return-stake-verification-key-file"
, stakeAddressVKeyFile
, "--governance-action-deposit"
, "12345"
, "--cost-model-file"
, costModelsFile
, "--out-file"
, actionFile
]

hprop_golden_conway_governance_action_create_hardfork_wrong_hash_fails :: Property
hprop_golden_conway_governance_action_create_hardfork_wrong_hash_fails =
propertyOnce . expectFailure . H.moduleWorkspace "tmp" $ \tempDir -> do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"type": "Governance proposal",
"description": "Update protocol parameters proposal",
"cborHex": "84193039581de18f4a3466a404c11eb410313015b88e447d81b60089e25f611600e6058400f6a112a1019f1a0003236119032c01011903e819023b00011903e8195e7104011903e818201a0001ca761928eb041959d818641959d818641959d818641959d818641959d818641959d81864186418641959d81864194c5118201a0002acfa182019b551041a000363151901ff00011a00015c3518201a000797751936f404021a0002ff941a0006ea7818dc0001011903e8196ff604021a0003bd081a00034ec5183e011a00102e0f19312a011a00032e801901a5011a0002da781903e819cf06011a00013a34182019a8f118201903e818201a00013aac0119e143041903e80a1a00030219189c011a00030219189c011a0003207c1901d9011a000330001901ff0119ccf3182019fd40182019ffd5182019581e18201940b318201a00012adf18201a0002ff941a0006ea7818dc0001011a00010f92192da7000119eabb18201a0002ff941a0006ea7818dc0001011a0002ff941a0006ea7818dc0001011a0011b22c1a0005fdde00021a000c504e197712041a001d6af61a0001425b041a00040c660004001a00014fab18201a0003236119032c010119a0de18201a00033d7618201979f41820197fb8182019a95d1820197df718201995aa18201a0223accc0a1a009063b91903fd0a1a02515e841980b30afff6826b6578616d706c652e636f6d5820c7ddb5b493faa4d3d2d679847740bdce0c5d358d56f9b1470ca67f5652a02745"
"cborHex": "84193039581de18f4a3466a404c11eb410313015b88e447d81b60089e25f611600e6058400f6a112a1019f1a0003236119032c01011903e819023b00011903e8195e7104011903e818201a0001ca761928eb041959d818641959d818641959d818641959d818641959d818641959d81864186418641959d81864194c5118201a0002acfa182019b551041a000363151901ff00011a00015c3518201a000797751936f404021a0002ff941a0006ea7818dc0001011903e8196ff604021a0003bd081a00034ec5183e011a00102e0f19312a011a00032e801901a5011a0002da781903e819cf06011a00013a34182019a8f118201903e818201a00013aac0119e143041903e80a1a00030219189c011a00030219189c011a0003207c1901d9011a000330001901ff0119ccf3182019fd40182019ffd5182019581e18201940b318201a00012adf18201a0002ff941a0006ea7818dc0001011a00010f92192da7000119eabb18201a0002ff941a0006ea7818dc0001011a0002ff941a0006ea7818dc0001011a0011b22c1a0005fdde00021a000c504e197712041a001d6af61a0001425b041a00040c660004001a00014fab18201a0003236119032c010119a0de18201a00033d7618201979f41820197fb8182019a95d1820197df718201995aa18201a0223accc0a1a009063b91903fd0a1a02515e841980b30a00000000000000000000fff6826b6578616d706c652e636f6d5820c7ddb5b493faa4d3d2d679847740bdce0c5d358d56f9b1470ca67f5652a02745"
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@
10,
38887044,
32947,
10
10,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
]
}
Loading
Loading