Skip to content

Commit 91bd434

Browse files
committed
WIP: agCostModels -> agPlutusV1CostModel
1 parent 44c1c1e commit 91bd434

File tree

9 files changed

+462
-258
lines changed

9 files changed

+462
-258
lines changed

eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Genesis.hs

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module Cardano.Ledger.Alonzo.Genesis (
2121
extraConfig,
2222
AlonzoGenesis,
2323
agCoinsPerUTxOWord,
24-
agCostModels,
24+
agPlutusV1CostModel,
2525
agPrices,
2626
agMaxTxExUnits,
2727
agMaxBlockExUnits,
@@ -38,25 +38,28 @@ import Cardano.Ledger.Alonzo.PParams (
3838
CoinPerWord,
3939
UpgradeAlonzoPParams (..),
4040
)
41-
import Cardano.Ledger.Alonzo.Scripts (CostModels, ExUnits (..), Prices (..))
41+
import Cardano.Ledger.Alonzo.Scripts (
42+
CostModel,
43+
CostModels,
44+
ExUnits (..),
45+
Prices (..),
46+
decodeCostModel,
47+
decodeCostModelsLenient,
48+
encodeCostModel,
49+
)
4250
import Cardano.Ledger.BaseTypes (KeyValuePairs (..), ToKeyValuePairs (..))
4351
import Cardano.Ledger.Binary (
44-
DecCBOR,
45-
EncCBOR,
52+
DecCBOR (..),
53+
EncCBOR (..),
4654
FromCBOR (..),
4755
ToCBOR (..),
56+
decodeNullMaybe,
57+
encodeNullMaybe,
4858
)
49-
import Cardano.Ledger.Binary.Coders (
50-
Decode (From, RecD),
51-
Encode (Rec, To),
52-
decode,
53-
encode,
54-
(!>),
55-
(<!),
56-
)
59+
import Cardano.Ledger.Binary.Coders
5760
import Cardano.Ledger.Core
5861
import Cardano.Ledger.Genesis (EraGenesis (..))
59-
import Cardano.Ledger.Plutus.CostModels (parseCostModels)
62+
import Cardano.Ledger.Plutus.CostModels (parseCostModelAsArray, parseCostModels)
6063
import Cardano.Ledger.Plutus.Language (Language (..))
6164
import Control.DeepSeq (NFData)
6265
import Data.Aeson (FromJSON (..), ToJSON (..), (.:), (.:?), (.=))
@@ -82,7 +85,25 @@ newtype AlonzoExtraConfig = AlonzoExtraConfig
8285
{ aecCostModels :: Maybe CostModels
8386
}
8487
deriving (Eq)
85-
deriving newtype (EncCBOR, DecCBOR, NFData, NoThunks, Show)
88+
deriving newtype (NFData, NoThunks, Show)
89+
90+
instance DecCBOR AlonzoExtraConfig
91+
92+
instance EncCBOR AlonzoExtraConfig
93+
94+
instance FromCBOR AlonzoExtraConfig where
95+
fromCBOR =
96+
eraDecoder @AlonzoEra $
97+
decode $
98+
RecD AlonzoExtraConfig
99+
<! D (decodeNullMaybe decodeCostModelsLenient)
100+
101+
instance ToCBOR AlonzoExtraConfig where
102+
toCBOR x@(AlonzoExtraConfig _) =
103+
let AlonzoExtraConfig {..} = x
104+
in toEraCBOR @AlonzoEra . encode $
105+
Rec AlonzoExtraConfig
106+
!> E (encodeNullMaybe encCBOR) aecCostModels
86107

87108
instance FromJSON AlonzoExtraConfig where
88109
parseJSON = Aeson.withObject "Extra Config" $ \o ->
@@ -95,7 +116,7 @@ instance ToJSON AlonzoExtraConfig where
95116

96117
pattern AlonzoGenesis ::
97118
CoinPerWord ->
98-
CostModels ->
119+
CostModel ->
99120
Prices ->
100121
ExUnits ->
101122
ExUnits ->
@@ -106,7 +127,7 @@ pattern AlonzoGenesis ::
106127
AlonzoGenesis
107128
pattern AlonzoGenesis
108129
{ agCoinsPerUTxOWord
109-
, agCostModels
130+
, agPlutusV1CostModel
110131
, agPrices
111132
, agMaxTxExUnits
112133
, agMaxBlockExUnits
@@ -119,7 +140,7 @@ pattern AlonzoGenesis
119140
{ unAlonzoGenesisWrapper =
120141
UpgradeAlonzoPParams
121142
{ uappCoinsPerUTxOWord = agCoinsPerUTxOWord
122-
, uappCostModels = agCostModels
143+
, uappPlutusV1CostModel = agPlutusV1CostModel
123144
, uappPrices = agPrices
124145
, uappMaxTxExUnits = agMaxTxExUnits
125146
, uappMaxBlockExUnits = agMaxBlockExUnits
@@ -143,7 +164,7 @@ pattern AlonzoGenesis
143164
AlonzoGenesisWrapper
144165
( UpgradeAlonzoPParams
145166
{ uappCoinsPerUTxOWord = coinsPerUTxOWord_
146-
, uappCostModels = costModels_
167+
, uappPlutusV1CostModel = costModels_
147168
, uappPrices = prices_
148169
, uappMaxTxExUnits = maxTxExUnits_
149170
, uappMaxBlockExUnits = maxBlockExUnits_
@@ -170,7 +191,7 @@ instance FromCBOR AlonzoGenesis where
170191
decode $
171192
RecD AlonzoGenesis
172193
<! From
173-
<! From
194+
<! D (decodeCostModel PlutusV1)
174195
<! From
175196
<! From
176197
<! From
@@ -180,33 +201,24 @@ instance FromCBOR AlonzoGenesis where
180201
<! From
181202

182203
instance ToCBOR AlonzoGenesis where
183-
toCBOR
184-
AlonzoGenesis
185-
{ agCoinsPerUTxOWord
186-
, agCostModels
187-
, agPrices
188-
, agMaxTxExUnits
189-
, agMaxBlockExUnits
190-
, agMaxValSize
191-
, agCollateralPercentage
192-
, agMaxCollateralInputs
193-
} =
194-
toEraCBOR @AlonzoEra
195-
. encode
196-
$ Rec AlonzoGenesis
197-
!> To agCoinsPerUTxOWord
198-
!> To agCostModels
199-
!> To agPrices
200-
!> To agMaxTxExUnits
201-
!> To agMaxBlockExUnits
202-
!> To agMaxValSize
203-
!> To agCollateralPercentage
204-
!> To agMaxCollateralInputs
204+
toCBOR x@(AlonzoGenesis _ _ _ _ _ _ _ _ _) =
205+
let AlonzoGenesis {..} = x
206+
in toEraCBOR @AlonzoEra . encode $
207+
Rec AlonzoGenesis
208+
!> To agCoinsPerUTxOWord
209+
!> E encodeCostModel agPlutusV1CostModel
210+
!> To agPrices
211+
!> To agMaxTxExUnits
212+
!> To agMaxBlockExUnits
213+
!> To agMaxValSize
214+
!> To agCollateralPercentage
215+
!> To agMaxCollateralInputs
216+
!> To agExtraConfig
205217

206218
instance FromJSON AlonzoGenesis where
207219
parseJSON = Aeson.withObject "Alonzo Genesis" $ \o -> do
208220
agCoinsPerUTxOWord <- o .: "lovelacePerUTxOWord"
209-
agCostModels <- parseCostModels False [PlutusV1] =<< o .: "costModels"
221+
agPlutusV1CostModel <- parseCostModelAsArray False PlutusV1 =<< o .: "plutusV1CostModel"
210222
agPrices <- o .: "executionPrices"
211223
agMaxTxExUnits <- o .: "maxTxExUnits"
212224
agMaxBlockExUnits <- o .: "maxBlockExUnits"
@@ -219,7 +231,7 @@ instance FromJSON AlonzoGenesis where
219231
instance ToKeyValuePairs AlonzoGenesis where
220232
toKeyValuePairs ag =
221233
[ "lovelacePerUTxOWord" .= agCoinsPerUTxOWord ag
222-
, "costModels" .= agCostModels ag
234+
, "plutusV1CostModel" .= agPlutusV1CostModel ag
223235
, "executionPrices" .= agPrices ag
224236
, "maxTxExUnits" .= agMaxTxExUnits ag
225237
, "maxBlockExUnits" .= agMaxBlockExUnits ag

eras/alonzo/impl/src/Cardano/Ledger/Alonzo/PParams.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ import Cardano.Ledger.Plutus.CostModels (
9797
emptyCostModels,
9898
getCostModelLanguage,
9999
getCostModelParams,
100+
mkCostModels,
100101
)
101102
import Cardano.Ledger.Plutus.ExUnits (
102103
ExUnits (..),
@@ -408,7 +409,7 @@ instance Ord OrdExUnits where
408409
-- | Parameters that were added in Alonzo
409410
data UpgradeAlonzoPParams f = UpgradeAlonzoPParams
410411
{ uappCoinsPerUTxOWord :: !(HKD f CoinPerWord)
411-
, uappCostModels :: !(HKD f CostModels)
412+
, uappPlutusV1CostModel :: !(HKD f CostModel)
412413
, uappPrices :: !(HKD f Prices)
413414
, uappMaxTxExUnits :: !(HKD f ExUnits)
414415
, uappMaxBlockExUnits :: !(HKD f ExUnits)
@@ -442,7 +443,7 @@ instance Default (UpgradeAlonzoPParams StrictMaybe) where
442443
def =
443444
UpgradeAlonzoPParams
444445
{ uappCoinsPerUTxOWord = SNothing
445-
, uappCostModels = SNothing
446+
, uappPlutusV1CostModel = SNothing
446447
, uappPrices = SNothing
447448
, uappMaxTxExUnits = SNothing
448449
, uappMaxBlockExUnits = SNothing
@@ -611,7 +612,7 @@ upgradeAlonzoPParams UpgradeAlonzoPParams {..} ShelleyPParams {..} =
611612
, appMinPoolCost = sppMinPoolCost
612613
, -- new in alonzo
613614
appCoinsPerUTxOWord = uappCoinsPerUTxOWord
614-
, appCostModels = uappCostModels
615+
, appCostModels = hkdMap (Proxy @f) (mkCostModels . Map.singleton PlutusV1) uappPlutusV1CostModel
615616
, appPrices = uappPrices
616617
, appMaxTxExUnits = hkdMap (Proxy @f) OrdExUnits uappMaxTxExUnits
617618
, appMaxBlockExUnits = hkdMap (Proxy @f) OrdExUnits uappMaxBlockExUnits

eras/alonzo/impl/testlib/Test/Cardano/Ledger/Alonzo/Arbitrary.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ instance Arbitrary AlonzoGenesis where
425425
arbitrary =
426426
AlonzoGenesis
427427
<$> arbitrary
428-
<*> genValidCostModels [PlutusV1, PlutusV2]
428+
<*> genValidCostModel PlutusV1
429429
<*> arbitrary
430430
<*> arbitrary
431431
<*> arbitrary

eras/alonzo/impl/testlib/Test/Cardano/Ledger/Alonzo/Examples.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import Cardano.Ledger.BaseTypes (StrictMaybe (..))
3030
import Cardano.Ledger.Coin (Coin (..))
3131
import Cardano.Ledger.Keys (asWitness)
3232
import Cardano.Ledger.Mary.Value (MaryValue (..))
33-
import Cardano.Ledger.Plutus.CostModels (mkCostModels)
3433
import Cardano.Ledger.Plutus.Data (Data (..), hashData)
3534
import Cardano.Ledger.Plutus.Language (Language (..))
3635
import Cardano.Ledger.Shelley.API (
@@ -211,7 +210,7 @@ exampleAlonzoGenesis :: AlonzoGenesis
211210
exampleAlonzoGenesis =
212211
AlonzoGenesis
213212
{ agCoinsPerUTxOWord = CoinPerWord $ Coin 1
214-
, agCostModels = mkCostModels (Map.fromList [(PlutusV1, zeroTestingCostModelV1)])
213+
, agPlutusV1CostModel = zeroTestingCostModelV1
215214
, agPrices = Prices (unsafeBoundRational 90) (unsafeBoundRational 91)
216215
, agMaxTxExUnits = ExUnits 123 123
217216
, agMaxBlockExUnits = ExUnits 223 223

eras/alonzo/impl/testlib/Test/Cardano/Ledger/Alonzo/ImpTest.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ import Test.Cardano.Ledger.Mary.ImpTest
112112
import Test.Cardano.Ledger.Plutus (
113113
PlutusArgs (..),
114114
ScriptTestContext (..),
115-
testingCostModels,
115+
testingCostModel,
116116
)
117117
import Test.Cardano.Ledger.Plutus.Examples
118118
import Test.Cardano.Ledger.Plutus.Guardrail (guardrailScript)
@@ -413,7 +413,7 @@ instance ShelleyEraImp AlonzoEra where
413413
pure
414414
AlonzoGenesis
415415
{ agCoinsPerUTxOWord = CoinPerWord (Coin 34_482)
416-
, agCostModels = testingCostModels [PlutusV1]
416+
, agPlutusV1CostModel = testingCostModel PlutusV1
417417
, agPrices =
418418
Prices
419419
{ prMem = 577 %! 10_000

0 commit comments

Comments
 (0)