44{-# LANGUAGE FlexibleInstances #-}
55{-# LANGUAGE GeneralizedNewtypeDeriving #-}
66{-# LANGUAGE LambdaCase #-}
7- {-# LANGUAGE NamedFieldPuns #-}
87{-# LANGUAGE OverloadedStrings #-}
98{-# LANGUAGE PatternSynonyms #-}
109{-# LANGUAGE RecordWildCards #-}
1110{-# LANGUAGE TypeApplications #-}
1211{-# LANGUAGE TypeFamilies #-}
1312{-# LANGUAGE UndecidableInstances #-}
14- {-# LANGUAGE ViewPatterns #-}
1513{-# OPTIONS_GHC -Wno-orphans #-}
1614
1715module Cardano.Ledger.Alonzo.Genesis (
@@ -43,9 +41,12 @@ import Cardano.Ledger.Alonzo.Scripts (
4341 CostModels ,
4442 ExUnits (.. ),
4543 Prices (.. ),
44+ costModelsValid ,
4645 decodeCostModel ,
4746 decodeCostModelsLenient ,
4847 encodeCostModel ,
48+ flattenCostModels ,
49+ mkCostModels ,
4950 )
5051import Cardano.Ledger.BaseTypes (KeyValuePairs (.. ), ToKeyValuePairs (.. ))
5152import Cardano.Ledger.Binary (
@@ -59,20 +60,22 @@ import Cardano.Ledger.Binary (
5960import Cardano.Ledger.Binary.Coders
6061import Cardano.Ledger.Core
6162import Cardano.Ledger.Genesis (EraGenesis (.. ))
62- import Cardano.Ledger.Plutus.CostModels ( parseCostModelAsArray , parseCostModels )
63- import Cardano.Ledger.Plutus.Language ( Language ( .. ) )
63+ import Cardano.Ledger.Plutus ( Language ( PlutusV1 ) )
64+ import Cardano.Ledger.Plutus.CostModels ( parseCostModels )
6465import Control.DeepSeq (NFData )
6566import Data.Aeson (FromJSON (.. ), ToJSON (.. ), (.:) , (.:?) , (.=) )
6667import qualified Data.Aeson as Aeson
6768import Data.Functor.Identity (Identity )
69+ import qualified Data.List as List
70+ import qualified Data.Map.Strict as Map
6871import GHC.Generics (Generic )
6972import NoThunks.Class (NoThunks )
7073import Numeric.Natural (Natural )
7174
7275-- | All configuration that is necessary to bootstrap AlonzoEra from ShelleyGenesis
7376data AlonzoGenesis = AlonzoGenesisWrapper
7477 { unAlonzoGenesisWrapper :: UpgradeAlonzoPParams Identity
75- , extraConfig :: AlonzoExtraConfig
78+ , extraConfig :: Maybe AlonzoExtraConfig
7679 }
7780 deriving stock (Eq , Show , Generic )
7881 deriving (ToJSON ) via KeyValuePairs AlonzoGenesis
@@ -123,7 +126,7 @@ pattern AlonzoGenesis ::
123126 Natural ->
124127 Natural ->
125128 Natural ->
126- AlonzoExtraConfig ->
129+ Maybe AlonzoExtraConfig ->
127130 AlonzoGenesis
128131pattern AlonzoGenesis
129132 { agCoinsPerUTxOWord
@@ -218,25 +221,34 @@ instance ToCBOR AlonzoGenesis where
218221instance FromJSON AlonzoGenesis where
219222 parseJSON = Aeson. withObject " Alonzo Genesis" $ \ o -> do
220223 agCoinsPerUTxOWord <- o .: " lovelacePerUTxOWord"
221- agPlutusV1CostModel <- parseCostModelAsArray False PlutusV1 =<< o .: " plutusV1CostModel "
224+ cms <- parseCostModels False =<< o .: " costModels "
222225 agPrices <- o .: " executionPrices"
223226 agMaxTxExUnits <- o .: " maxTxExUnits"
224227 agMaxBlockExUnits <- o .: " maxBlockExUnits"
225228 agMaxValSize <- o .: " maxValueSize"
226229 agCollateralPercentage <- o .: " collateralPercentage"
227230 agMaxCollateralInputs <- o .: " maxCollateralInputs"
228- agExtraConfig <- o .: " extraConfig"
231+ agExtraConfig <- o .:? " extraConfig"
232+ agPlutusV1CostModel <-
233+ case Map. toList (costModelsValid cms) of
234+ [] -> fail " Expected \" PlutusV1\" cost model to be supplied"
235+ [(PlutusV1 , pv1CostModel)] -> pure pv1CostModel
236+ _ ->
237+ fail $
238+ " Only PlutusV1 CostModel is allowed in the AlonzoGenesis, but "
239+ <> List. intercalate " , " (map show . Map. keys $ flattenCostModels cms)
240+ <> " were supplied. Use \" extraConfig\" if you need to inject other cost models for testing."
229241 return AlonzoGenesis {.. }
230242
231243instance ToKeyValuePairs AlonzoGenesis where
232244 toKeyValuePairs ag =
233245 [ " lovelacePerUTxOWord" .= agCoinsPerUTxOWord ag
234- , " plutusV1CostModel " .= agPlutusV1CostModel ag
246+ , " costModels " .= mkCostModels ( Map. singleton PlutusV1 $ agPlutusV1CostModel ag)
235247 , " executionPrices" .= agPrices ag
236248 , " maxTxExUnits" .= agMaxTxExUnits ag
237249 , " maxBlockExUnits" .= agMaxBlockExUnits ag
238250 , " maxValueSize" .= agMaxValSize ag
239251 , " collateralPercentage" .= agCollateralPercentage ag
240252 , " maxCollateralInputs" .= agMaxCollateralInputs ag
241- , " extraConfig" .= agExtraConfig ag
242253 ]
254+ ++ [" extraConfig" .= extraConfig | Just extraConfig <- [agExtraConfig ag]]
0 commit comments