@@ -8,6 +8,7 @@ module Test.Cardano.Api.Genesis
88 )
99where
1010
11+ import qualified Cardano.Api as Api (CostModels (.. ))
1112import Cardano.Api.Eon.ShelleyBasedEra
1213import Cardano.Api.Eras
1314import Cardano.Api.Genesis
@@ -23,6 +24,7 @@ import qualified PlutusLedgerApi.V2 as V2
2324import qualified Codec.CBOR.Decoding as CBOR
2425import qualified Codec.CBOR.Encoding as CBOR
2526import qualified Codec.CBOR.Write as CBOR
27+ import qualified Data.Aeson as Aeson
2628import qualified Data.ByteString.Lazy as LBS
2729import Data.Either
2830import Data.Int (Int64 )
@@ -177,8 +179,62 @@ encodeCborInEraCostModels aeo = CBOR.toLazyByteString . toEraCbor'
177179 toEraCbor' :: L. CostModels -> CBOR. Encoding
178180 toEraCbor' = alonzoEraOnwardsConstraints aeo $ L. toEraCBOR @ (ShelleyLedgerEra era )
179181
182+ prop_plutus_costmodel_sizes :: Property
183+ prop_plutus_costmodel_sizes = H. propertyOnce $ do
184+ -- PV1 tests
185+ -- Exact expected number of parameters
186+ testWorks " ./test/cardano-api-test/files/input/genesis/pv1-array-166.json" PlutusScriptV1 166
187+ testWorks " ./test/cardano-api-test/files/input/genesis/pv1-map-166.json" PlutusScriptV1 166
188+ -- TODO This file loads fine, whereas it shouldn't
189+ -- _testFails "./test/cardano-api-test/files/input/genesis/pv1-array-165.json"
190+ _testFails " ./test/cardano-api-test/files/input/genesis/pv1-map-165.json"
191+
192+ -- PV2 tests
193+ -- Babbage has 175 PV2 parameters:
194+ testWorks " ./test/cardano-api-test/files/input/genesis/pv2-array-175.json" PlutusScriptV2 175
195+ -- Conway has 185 PV2 parameters:
196+ testWorks " ./test/cardano-api-test/files/input/genesis/pv2-array-185.json" PlutusScriptV2 185
197+ -- TODO This file loads fine, whereas it shouldn't
198+ -- _testFails "./test/cardano-api-test/files/input/genesis/pv2-array-174.json"
199+ _testFails " ./test/cardano-api-test/files/input/genesis/pv2-map-174.json"
200+
201+ -- PV3 tests
202+ testWorks " ./test/cardano-api-test/files/input/genesis/pv3-array-297.json" PlutusScriptV3 297
203+ testWorks " ./test/cardano-api-test/files/input/genesis/pv3-map-297.json" PlutusScriptV3 297
204+ _testFails " ./test/cardano-api-test/files/input/genesis/pv3-map-296.json"
205+ where
206+ -- TODO This file loads fine, whereas it shouldn't
207+ -- _testFails "./test/cardano-api-test/files/input/genesis/pv3-array-296.json"
208+
209+ testWorks filepath whichPlutusVersion expectedSize = do
210+ genesisBs <- H. lbsReadFile filepath
211+ let decoded :: Either String Api. CostModels = Aeson. eitherDecode genesisBs
212+ case decoded of
213+ Left err -> do
214+ H. annotateShow err
215+ H. assert False
216+ Right (Api. CostModels cms) -> do
217+ case M. lookup (AnyPlutusScriptVersion whichPlutusVersion) cms of
218+ Nothing -> do
219+ H. note_ $ show whichPlutusVersion <> " cost model not found in " <> filepath
220+ H. assert False
221+ Just (CostModel model) -> do
222+ length model H. === expectedSize
223+ pure ()
224+ _testFails filepath = do
225+ genesisBs <- H. lbsReadFile filepath
226+ let decoded :: Either String Api. CostModels = Aeson. eitherDecode genesisBs
227+ case decoded of
228+ Left _err -> do
229+ pure ()
230+ Right _ -> do
231+ H. note_ $ " Decoding of " <> filepath <> " succeeded, whereas it was expected to fail!"
232+ H. assert False
233+
180234-- * List all test cases
181235
236+ -- Execute me with:
237+ -- @cabal test cardano-api-test --test-options '-p "/Test.Cardano.Api.Genesis/"'@
182238tests :: TestTree
183239tests =
184240 testGroup
@@ -210,4 +266,7 @@ tests =
210266 , testProperty
211267 " Make sure that last 10 PlutusV2 cost model parameters are the ones we expect"
212268 prop_verify_plutus_v2_costmodel
269+ , testProperty
270+ " Make sure that Plutus cost model sizes are validated correctly"
271+ prop_plutus_costmodel_sizes
213272 ]
0 commit comments