Skip to content

Commit 82c6d28

Browse files
committed
Plutus cost model size: add tests of validation
1 parent a1a40fa commit 82c6d28

14 files changed

+2670
-0
lines changed

cardano-api/src/Cardano/Api.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@ module Cardano.Api
622622
, ExecutionUnits (..)
623623
, ExecutionUnitPrices (..)
624624
, CostModel (..)
625+
, CostModels (..)
625626
, toAlonzoCostModel
626627
, fromAlonzoCostModel
627628
, toAlonzoCostModels

cardano-api/test/cardano-api-test/Test/Cardano/Api/Genesis.hs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Test.Cardano.Api.Genesis
88
)
99
where
1010

11+
import qualified Cardano.Api as Api (CostModels (..))
1112
import Cardano.Api.Eon.ShelleyBasedEra
1213
import Cardano.Api.Eras
1314
import Cardano.Api.Genesis
@@ -23,6 +24,7 @@ import qualified PlutusLedgerApi.V2 as V2
2324
import qualified Codec.CBOR.Decoding as CBOR
2425
import qualified Codec.CBOR.Encoding as CBOR
2526
import qualified Codec.CBOR.Write as CBOR
27+
import qualified Data.Aeson as Aeson
2628
import qualified Data.ByteString.Lazy as LBS
2729
import Data.Either
2830
import 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/"'@
182238
tests :: TestTree
183239
tests =
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
]
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
{
2+
"PlutusV1": [
3+
812,
4+
1,
5+
1,
6+
1000,
7+
571,
8+
0,
9+
1,
10+
1000,
11+
24177,
12+
4,
13+
1,
14+
1000,
15+
32,
16+
117366,
17+
10475,
18+
4,
19+
23000,
20+
100,
21+
23000,
22+
100,
23+
23000,
24+
100,
25+
23000,
26+
100,
27+
23000,
28+
100,
29+
23000,
30+
100,
31+
100,
32+
100,
33+
23000,
34+
100,
35+
19537,
36+
32,
37+
175354,
38+
32,
39+
46417,
40+
4,
41+
221973,
42+
511,
43+
0,
44+
1,
45+
89141,
46+
32,
47+
497525,
48+
14068,
49+
4,
50+
2,
51+
196500,
52+
453240,
53+
220,
54+
0,
55+
1,
56+
1,
57+
1000,
58+
28662,
59+
4,
60+
2,
61+
245000,
62+
216773,
63+
62,
64+
1,
65+
1060367,
66+
12586,
67+
1,
68+
208512,
69+
421,
70+
1,
71+
187000,
72+
1000,
73+
52998,
74+
1,
75+
80436,
76+
32,
77+
43249,
78+
32,
79+
1000,
80+
32,
81+
80556,
82+
1,
83+
57667,
84+
4,
85+
1000,
86+
10,
87+
197145,
88+
156,
89+
1,
90+
197145,
91+
156,
92+
1,
93+
204924,
94+
473,
95+
1,
96+
208896,
97+
511,
98+
1,
99+
52467,
100+
32,
101+
64832,
102+
32,
103+
65493,
104+
32,
105+
22558,
106+
32,
107+
16563,
108+
32,
109+
76511,
110+
32,
111+
196500,
112+
453240,
113+
220,
114+
0,
115+
1,
116+
1,
117+
69522,
118+
11687,
119+
0,
120+
1,
121+
60091,
122+
32,
123+
196500,
124+
453240,
125+
220,
126+
0,
127+
1,
128+
1,
129+
196500,
130+
453240,
131+
220,
132+
0,
133+
1,
134+
1,
135+
806990,
136+
30482,
137+
4,
138+
1927926,
139+
82523,
140+
4,
141+
265318,
142+
0,
143+
4,
144+
0,
145+
85931,
146+
32,
147+
205665,
148+
812,
149+
1,
150+
1,
151+
41182,
152+
32,
153+
212342,
154+
32,
155+
31220,
156+
32,
157+
32696,
158+
32,
159+
43357,
160+
32,
161+
32247,
162+
32,
163+
38314,
164+
32,
165+
9462713,
166+
1021,
167+
10
168+
]
169+
}

0 commit comments

Comments
 (0)