Skip to content

Commit

Permalink
Plutus cost model size: add tests of validation
Browse files Browse the repository at this point in the history
  • Loading branch information
smelc committed Jan 14, 2025
1 parent a1a40fa commit 82c6d28
Show file tree
Hide file tree
Showing 14 changed files with 2,670 additions and 0 deletions.
1 change: 1 addition & 0 deletions cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ module Cardano.Api
, ExecutionUnits (..)
, ExecutionUnitPrices (..)
, CostModel (..)
, CostModels (..)
, toAlonzoCostModel
, fromAlonzoCostModel
, toAlonzoCostModels
Expand Down
59 changes: 59 additions & 0 deletions cardano-api/test/cardano-api-test/Test/Cardano/Api/Genesis.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Test.Cardano.Api.Genesis
)
where

import qualified Cardano.Api as Api (CostModels (..))
import Cardano.Api.Eon.ShelleyBasedEra
import Cardano.Api.Eras
import Cardano.Api.Genesis
Expand All @@ -23,6 +24,7 @@ import qualified PlutusLedgerApi.V2 as V2
import qualified Codec.CBOR.Decoding as CBOR
import qualified Codec.CBOR.Encoding as CBOR
import qualified Codec.CBOR.Write as CBOR
import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Lazy as LBS
import Data.Either
import Data.Int (Int64)
Expand Down Expand Up @@ -177,8 +179,62 @@ encodeCborInEraCostModels aeo = CBOR.toLazyByteString . toEraCbor'
toEraCbor' :: L.CostModels -> CBOR.Encoding
toEraCbor' = alonzoEraOnwardsConstraints aeo $ L.toEraCBOR @(ShelleyLedgerEra era)

prop_plutus_costmodel_sizes :: Property
prop_plutus_costmodel_sizes = H.propertyOnce $ do
-- PV1 tests
-- Exact expected number of parameters
testWorks "./test/cardano-api-test/files/input/genesis/pv1-array-166.json" PlutusScriptV1 166
testWorks "./test/cardano-api-test/files/input/genesis/pv1-map-166.json" PlutusScriptV1 166
-- TODO This file loads fine, whereas it shouldn't
-- _testFails "./test/cardano-api-test/files/input/genesis/pv1-array-165.json"
_testFails "./test/cardano-api-test/files/input/genesis/pv1-map-165.json"

-- PV2 tests
-- Babbage has 175 PV2 parameters:
testWorks "./test/cardano-api-test/files/input/genesis/pv2-array-175.json" PlutusScriptV2 175
-- Conway has 185 PV2 parameters:
testWorks "./test/cardano-api-test/files/input/genesis/pv2-array-185.json" PlutusScriptV2 185
-- TODO This file loads fine, whereas it shouldn't
-- _testFails "./test/cardano-api-test/files/input/genesis/pv2-array-174.json"
_testFails "./test/cardano-api-test/files/input/genesis/pv2-map-174.json"

-- PV3 tests
testWorks "./test/cardano-api-test/files/input/genesis/pv3-array-297.json" PlutusScriptV3 297
testWorks "./test/cardano-api-test/files/input/genesis/pv3-map-297.json" PlutusScriptV3 297
_testFails "./test/cardano-api-test/files/input/genesis/pv3-map-296.json"
where
-- TODO This file loads fine, whereas it shouldn't
-- _testFails "./test/cardano-api-test/files/input/genesis/pv3-array-296.json"

testWorks filepath whichPlutusVersion expectedSize = do
genesisBs <- H.lbsReadFile filepath
let decoded :: Either String Api.CostModels = Aeson.eitherDecode genesisBs
case decoded of
Left err -> do
H.annotateShow err
H.assert False
Right (Api.CostModels cms) -> do
case M.lookup (AnyPlutusScriptVersion whichPlutusVersion) cms of
Nothing -> do
H.note_ $ show whichPlutusVersion <> " cost model not found in " <> filepath
H.assert False
Just (CostModel model) -> do
length model H.=== expectedSize
pure ()
_testFails filepath = do
genesisBs <- H.lbsReadFile filepath
let decoded :: Either String Api.CostModels = Aeson.eitherDecode genesisBs
case decoded of
Left _err -> do
pure ()
Right _ -> do
H.note_ $ "Decoding of " <> filepath <> " succeeded, whereas it was expected to fail!"
H.assert False

-- * List all test cases

-- Execute me with:
-- @cabal test cardano-api-test --test-options '-p "/Test.Cardano.Api.Genesis/"'@
tests :: TestTree
tests =
testGroup
Expand Down Expand Up @@ -210,4 +266,7 @@ tests =
, testProperty
"Make sure that last 10 PlutusV2 cost model parameters are the ones we expect"
prop_verify_plutus_v2_costmodel
, testProperty
"Make sure that Plutus cost model sizes are validated correctly"
prop_plutus_costmodel_sizes
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
{
"PlutusV1": [
812,
1,
1,
1000,
571,
0,
1,
1000,
24177,
4,
1,
1000,
32,
117366,
10475,
4,
23000,
100,
23000,
100,
23000,
100,
23000,
100,
23000,
100,
23000,
100,
100,
100,
23000,
100,
19537,
32,
175354,
32,
46417,
4,
221973,
511,
0,
1,
89141,
32,
497525,
14068,
4,
2,
196500,
453240,
220,
0,
1,
1,
1000,
28662,
4,
2,
245000,
216773,
62,
1,
1060367,
12586,
1,
208512,
421,
1,
187000,
1000,
52998,
1,
80436,
32,
43249,
32,
1000,
32,
80556,
1,
57667,
4,
1000,
10,
197145,
156,
1,
197145,
156,
1,
204924,
473,
1,
208896,
511,
1,
52467,
32,
64832,
32,
65493,
32,
22558,
32,
16563,
32,
76511,
32,
196500,
453240,
220,
0,
1,
1,
69522,
11687,
0,
1,
60091,
32,
196500,
453240,
220,
0,
1,
1,
196500,
453240,
220,
0,
1,
1,
806990,
30482,
4,
1927926,
82523,
4,
265318,
0,
4,
0,
85931,
32,
205665,
812,
1,
1,
41182,
32,
212342,
32,
31220,
32,
32696,
32,
43357,
32,
32247,
32,
38314,
32,
9462713,
1021,
10
]
}
Loading

0 comments on commit 82c6d28

Please sign in to comment.