Skip to content

Commit

Permalink
Remove errors from CostModel deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
lehins committed May 7, 2024
1 parent 7c59e3a commit e173cf2
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,11 @@ instance
<*> arbitrary
<*> arbitrary

genEraLanguage :: forall era. AlonzoEraScript era => Gen Language
genEraLanguage = choose (minBound, eraMaxLanguage @era)

instance (AlonzoEraScript era, Script era ~ AlonzoScript era) => Arbitrary (AlonzoScript era) where
arbitrary = do
lang <- elements [minBound .. eraMaxLanguage @era]
genAlonzoScript lang
arbitrary = genEraLanguage @era >>= genAlonzoScript

genAlonzoScript ::
( AlonzoEraScript era
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ spec :: forall era. AlonzoEraPParams era => Spec
spec = do
describe "CBOR deserialization" $ do
validCostModelProp @era
invalidCostModelProp @era
underspecifiedCostModelProp @era
unknownCostModelProp @era
prop "applyPPUpdates" $ \valid validUpdate unknown unknownUpdate -> do
let
validExpected = mkCostModels (costModelsValid validUpdate <> costModelsValid valid)
unknownExpected = unknownUpdate <> unknown
original = mkCostModelsLenient (flattenCostModels valid <> unknown)
update = mkCostModelsLenient (flattenCostModels validUpdate <> unknownUpdate)
expected = mkCostModelsLenient (flattenCostModels validExpected <> unknownExpected)
pp =
emptyPParams & ppCostModelsL .~ original
original <- mkCostModelsLenient (flattenCostModels valid <> unknown)
update <- mkCostModelsLenient (flattenCostModels validUpdate <> unknownUpdate)
expected <- mkCostModelsLenient (flattenCostModels validExpected <> unknownExpected)
let
pp = emptyPParams & ppCostModelsL .~ original
ppUpdate =
emptyPParamsUpdate & ppuCostModelsL .~ SJust update
-- Starting with Conway we update CostModel on per-language basis, while before
Expand All @@ -62,26 +62,26 @@ validCostModelProp = do
where
genValidCostModelEnc lang = genCostModelEncForLanguage lang (costModelParamsCount lang)
validCm cms =
not (null (costModelsValid cms)) && null (costModelsErrors cms) && null (costModelsUnknown cms)
not (null (costModelsValid cms)) && null (costModelsUnknown cms)

invalidCostModelProp ::
underspecifiedCostModelProp ::
forall era.
AlonzoEraPParams era =>
Spec
invalidCostModelProp = do
prop "invalid CostModels fail within PParamsUpdate" $
underspecifiedCostModelProp = do
prop "CostModels with less than expected parameters within PParamsUpdate" $
\(lang :: Language) -> do
forAllShow (genInvalidCostModelEnc lang) (showEnc @era) $
\invalidCmEnc -> do
encodeAndCheckDecoded @era invalidCmEnc $
\shortCmEnc -> do
encodeAndCheckDecoded @era shortCmEnc $
\cmDecoded ppuDecoded -> do
-- pre-Conway we are failing when deserializing invalid costmodels
if eraProtVerHigh @era < natVersion @9
then expectDeserialiseFailure cmDecoded (Just "CostModels")
else do
-- post-Conway, we are collecting CostModels deserialization errors
cmRes <- expectRight cmDecoded
cmRes `shouldSatisfy` invalidCm
cmRes `shouldSatisfy` not . null . costModelsValid

-- in no era are we deserializing invalid costmodels within PParamsUpdate
expectDeserialiseFailure ppuDecoded Nothing
Expand All @@ -91,9 +91,6 @@ invalidCostModelProp = do
count <- choose (0, validCount - 1)
genCostModelEncForLanguage lang count

invalidCm cms =
null (costModelsValid cms) && not (null (costModelsErrors cms))

unknownCostModelProp ::
forall era.
AlonzoEraPParams era =>
Expand Down Expand Up @@ -122,7 +119,7 @@ unknownCostModelProp = do
NonNegative count <- arbitrary
genCostModelsEnc lang count
unknownCm cms =
null (costModelsValid cms) && null (costModelsErrors cms) && not (null (costModelsUnknown cms))
null (costModelsValid cms) && not (null (costModelsUnknown cms))

encodeAndCheckDecoded ::
forall era.
Expand Down
3 changes: 1 addition & 2 deletions eras/conway/impl/src/Cardano/Ledger/Conway/PParams.hs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ import Cardano.Ledger.HKD (
import Cardano.Ledger.Plutus.CostModels (
CostModel,
decodeCostModelFailHard,
decodeValidAndUnknownCostModels,
encodeCostModel,
mkCostModel,
mkCostModels,
Expand Down Expand Up @@ -1055,7 +1054,7 @@ updateField = \case
11 -> field (\x up -> up {cppTau = THKD (SJust x)}) From
16 -> field (\x up -> up {cppMinPoolCost = THKD (SJust x)}) From
17 -> field (\x up -> up {cppCoinsPerUTxOByte = THKD (SJust x)}) From
18 -> field (\x up -> up {cppCostModels = THKD (SJust x)}) $ D decodeValidAndUnknownCostModels
18 -> field (\x up -> up {cppCostModels = THKD (SJust x)}) From
19 -> field (\x up -> up {cppPrices = THKD (SJust x)}) From
20 -> field (\x up -> up {cppMaxTxExUnits = THKD (SJust x)}) From
21 -> field (\x up -> up {cppMaxBlockExUnits = THKD (SJust x)}) From
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 @@ -21,6 +21,7 @@
* `HKD StrictMaybe`
* Move `Metadatum` from `cardano-ledger-shelley` into a new module `Cardano.Ledger.Metadata`
* Add `mkBasicTxAuxData` and `metadataTxAuxDataL` to `EraTxAuxData` type class.
* Add `Random`, `Uniform` and `UniformRange` instances for `Language`

### `testlib`

Expand Down
2 changes: 2 additions & 0 deletions libs/cardano-ledger-core/cardano-ledger-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ library
partial-order,
plutus-ledger-api,
prettyprinter,
random,
quiet,
serialise,
scientific,
Expand Down Expand Up @@ -181,6 +182,7 @@ library testlib
containers,
data-default-class,
deepseq,
FailT,
generic-random,
genvalidity,
hspec,
Expand Down

0 comments on commit e173cf2

Please sign in to comment.