Skip to content

Commit

Permalink
Deprecate decodeCostModelFailHard in favor of new decodeCostModel
Browse files Browse the repository at this point in the history
  • Loading branch information
lehins committed May 7, 2024
1 parent ef0f210 commit f9324de
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
4 changes: 2 additions & 2 deletions eras/conway/impl/src/Cardano/Ledger/Conway/PParams.hs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ import Cardano.Ledger.HKD (
)
import Cardano.Ledger.Plutus.CostModels (
CostModel,
decodeCostModelFailHard,
decodeCostModel,
encodeCostModel,
mkCostModel,
mkCostModels,
Expand Down Expand Up @@ -673,7 +673,7 @@ instance DecCBOR (UpgradeConwayPParams Identity) where
<! From
<! From
<! From
<! D (decodeCostModelFailHard PlutusV3)
<! D (decodeCostModel PlutusV3)

instance Crypto c => EraPParams (ConwayEra c) where
type PParamsHKD f (ConwayEra c) = ConwayPParams f (ConwayEra c)
Expand Down
1 change: 1 addition & 0 deletions libs/cardano-ledger-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
signatures of all functions that work on raw cost model parameter values.
* `mkCostModelsLenient` was changed to work in `MonadFail`, since failures during
construction is now again possible.
* Deprecate `decodeCostModelFailHard` in favor of new `decodeCostModel`.

### `testlib`

Expand Down
53 changes: 32 additions & 21 deletions libs/cardano-ledger-core/src/Cardano/Ledger/Plutus/CostModels.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module Cardano.Ledger.Plutus.CostModels (
costModelToMap,
costModelFromMap,
costModelParamsCount,
decodeCostModel,
decodeCostModelFailHard,

-- * Cost Models
Expand Down Expand Up @@ -241,7 +242,7 @@ decodeCostModelsLenient = decCBOR >>= mkCostModelsLenient

decodeCostModelsFailing :: Decoder s CostModels
decodeCostModelsFailing =
CostModels <$> decodeMapByKey decCBOR legacyDecodeCostModel <*> pure mempty
CostModels <$> decodeMapByKey decCBOR decodeCostModelLegacy <*> pure mempty
{-# INLINE decodeCostModelsFailing #-}

decodeValidAndUnknownCostModels :: Decoder s CostModels
Expand All @@ -264,19 +265,8 @@ costModelParamsCount PlutusV1 = 166
costModelParamsCount PlutusV2 = 175
costModelParamsCount PlutusV3 = 233

-- | Prior to version 9, each 'CostModel' was expected to be serialized as an array of
-- integers of a specific length (depending on the version of Plutus). Starting in
-- version 9, we allow the decoders to accept lists longer or shorter than what they
-- require, so that new fields can be added in the future. For this reason, we must hard
-- code the length expectation into the deserializers prior to version 9.
--
-- Note that the number of elements in the V1 and V2 cost models may change in the future,
-- they are only fixed prior to version 9.
--
-- See https://github.com/intersectmbo/cardano-ledger/issues/2902
-- and https://github.com/intersectmbo/cardano-ledger/blob/master/docs/adr/2022-12-05_006-cost-model-serialization.md
legacyDecodeCostModel :: Language -> Decoder s CostModel
legacyDecodeCostModel lang = do
decodeCostModelLegacy :: Language -> Decoder s CostModel
decodeCostModelLegacy lang = do
when (lang > PlutusV2) $
fail $
"Legacy CostModel decoding is not supported for " ++ show lang ++ " language version"
Expand All @@ -293,15 +283,36 @@ legacyDecodeCostModel lang = do
case mkCostModel lang values of
Left e -> fail $ show e
Right cm -> pure cm
{-# INLINEABLE legacyDecodeCostModel #-}
{-# INLINEABLE decodeCostModelLegacy #-}

-- | Prior to version 9, each 'CostModel' was expected to be serialized as an array of
-- integers of a specific length (depending on the version of Plutus). Starting in
-- version 9, we allow the decoders to accept lists longer or shorter than what they
-- require, so that new fields can be added in the future. For this reason, we must hard
-- code the length expectation into the deserializers prior to version 9.
--
-- Note that the number of elements in the V1 and V2 cost models are allowed to change in
-- the future, they are only fixed prior to version 9.
--
-- See https://github.com/intersectmbo/cardano-ledger/issues/2902
-- and https://github.com/intersectmbo/cardano-ledger/blob/master/docs/adr/2022-12-05_006-cost-model-serialization.md
decodeCostModel :: Language -> Decoder s CostModel
decodeCostModel lang = do
ifDecoderVersionAtLeast
(natVersion @9)
decodeCostModelLenient
(decodeCostModelLegacy lang)
where
decodeCostModelLenient = do
checked <- mkCostModel lang <$> decCBOR
case checked of
Left e -> fail $ show e
Right cm -> pure cm
{-# INLINEABLE decodeCostModel #-}

decodeCostModelFailHard :: Language -> Decoder s CostModel
decodeCostModelFailHard lang = do
checked <- mkCostModel lang <$> decCBOR
case checked of
Left e -> fail $ show e
Right cm -> pure cm
{-# INLINEABLE decodeCostModelFailHard #-}
decodeCostModelFailHard = decodeCostModel
{-# DEPRECATED decodeCostModelFailHard "In favor of `decodeCostModel`" #-}

getEvaluationContext :: CostModel -> P.EvaluationContext
getEvaluationContext (CostModel _ _ ec) = ec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import Cardano.Ledger.Crypto (Crypto)
import Cardano.Ledger.Hashes (ScriptHash)
import Cardano.Ledger.Plutus.CostModels (
CostModel,
decodeCostModelFailHard,
decodeCostModel,
encodeCostModel,
getEvaluationContext,
)
Expand Down Expand Up @@ -202,7 +202,7 @@ instance Crypto c => FromCBOR (PlutusWithContext c) where
<> show scriptHash
pwcDatums <- decCBOR
pwcExUnits <- decCBOR
pwcCostModel <- decodeCostModelFailHard lang
pwcCostModel <- decodeCostModel lang
pure PlutusWithContext {..}

data PlutusDebugInfo c
Expand Down

0 comments on commit f9324de

Please sign in to comment.