Skip to content

Commit c6148bc

Browse files
committed
Add code review suggestions
* Override cost models when injecting into test state (if extra config cost models are provided) * Preserve `AlonzoGenesis` JSON and binary representations but change the Haskell side representation
1 parent c9a7e1c commit c6148bc

File tree

8 files changed

+254
-406
lines changed

8 files changed

+254
-406
lines changed

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
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

1715
module 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
)
5051
import Cardano.Ledger.BaseTypes (KeyValuePairs (..), ToKeyValuePairs (..))
5152
import Cardano.Ledger.Binary (
@@ -59,20 +60,22 @@ import Cardano.Ledger.Binary (
5960
import Cardano.Ledger.Binary.Coders
6061
import Cardano.Ledger.Core
6162
import 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)
6465
import Control.DeepSeq (NFData)
6566
import Data.Aeson (FromJSON (..), ToJSON (..), (.:), (.:?), (.=))
6667
import qualified Data.Aeson as Aeson
6768
import Data.Functor.Identity (Identity)
69+
import qualified Data.List as List
70+
import qualified Data.Map.Strict as Map
6871
import GHC.Generics (Generic)
6972
import NoThunks.Class (NoThunks)
7073
import Numeric.Natural (Natural)
7174

7275
-- | All configuration that is necessary to bootstrap AlonzoEra from ShelleyGenesis
7376
data 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
128131
pattern AlonzoGenesis
129132
{ agCoinsPerUTxOWord
@@ -218,25 +221,34 @@ instance ToCBOR AlonzoGenesis where
218221
instance 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

231243
instance 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]]

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
{-# LANGUAGE DeriveGeneric #-}
22
{-# LANGUAGE FlexibleInstances #-}
3+
{-# LANGUAGE LambdaCase #-}
34
{-# LANGUAGE ScopedTypeVariables #-}
45
{-# LANGUAGE TypeFamilies #-}
56
{-# OPTIONS_GHC -Wno-orphans #-}
67

78
module Cardano.Ledger.Alonzo.Transition (
89
TransitionConfig (..),
10+
alonzoInjectCostModels,
911
) where
1012

13+
import Cardano.Ledger.Alonzo.Core (AlonzoEraPParams, ppCostModelsL)
1114
import Cardano.Ledger.Alonzo.Era
12-
import Cardano.Ledger.Alonzo.Genesis (AlonzoGenesis)
15+
import Cardano.Ledger.Alonzo.Genesis
1316
import Cardano.Ledger.Alonzo.Translation ()
1417
import Cardano.Ledger.Mary
1518
import Cardano.Ledger.Mary.Transition (TransitionConfig (MaryTransitionConfig))
19+
import Cardano.Ledger.Plutus.CostModels (CostModels, updateCostModels)
20+
import Cardano.Ledger.Shelley.LedgerState
1621
import Cardano.Ledger.Shelley.Transition
1722
import GHC.Generics
1823
import Lens.Micro
@@ -27,7 +32,8 @@ instance EraTransition AlonzoEra where
2732

2833
mkTransitionConfig = AlonzoTransitionConfig
2934

30-
injectIntoTestState = shelleyRegisterInitialFundsThenStaking
35+
injectIntoTestState cfg =
36+
shelleyRegisterInitialFundsThenStaking cfg . alonzoInjectCostModels cfg
3137

3238
tcPreviousEraConfigL =
3339
lens atcMaryTransitionConfig (\atc pc -> atc {atcMaryTransitionConfig = pc})
@@ -36,3 +42,20 @@ instance EraTransition AlonzoEra where
3642
lens atcAlonzoGenesis (\atc ag -> atc {atcAlonzoGenesis = ag})
3743

3844
instance NoThunks (TransitionConfig AlonzoEra)
45+
46+
alonzoInjectCostModels ::
47+
(EraTransition era, AlonzoEraPParams era) =>
48+
TransitionConfig AlonzoEra -> NewEpochState era -> NewEpochState era
49+
alonzoInjectCostModels cfg =
50+
case agExtraConfig $ cfg ^. tcTranslationContextL of
51+
Nothing -> id
52+
Just aec -> overrideCostModels (aecCostModels aec)
53+
54+
overrideCostModels ::
55+
(EraTransition era, AlonzoEraPParams era) =>
56+
Maybe CostModels ->
57+
NewEpochState era ->
58+
NewEpochState era
59+
overrideCostModels = \case
60+
Nothing -> id
61+
Just cms -> nesEsL . curPParamsEpochStateL . ppCostModelsL %~ updateCostModels cms

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module Test.Cardano.Ledger.Alonzo.Examples (
1515

1616
import Cardano.Ledger.Alonzo (AlonzoEra)
1717
import Cardano.Ledger.Alonzo.Core
18-
import Cardano.Ledger.Alonzo.Genesis (AlonzoExtraConfig (..), AlonzoGenesis (..))
18+
import Cardano.Ledger.Alonzo.Genesis (AlonzoGenesis (..))
1919
import Cardano.Ledger.Alonzo.Plutus.Context (EraPlutusTxInfo)
2020
import Cardano.Ledger.Alonzo.Scripts (
2121
AlonzoPlutusPurpose (..),
@@ -217,5 +217,5 @@ exampleAlonzoGenesis =
217217
, agMaxValSize = 1234
218218
, agCollateralPercentage = 20
219219
, agMaxCollateralInputs = 30
220-
, agExtraConfig = AlonzoExtraConfig Nothing
220+
, agExtraConfig = Nothing
221221
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ module Test.Cardano.Ledger.Alonzo.ImpTest (
4747
import Cardano.Ledger.Address (Addr (..))
4848
import Cardano.Ledger.Alonzo (AlonzoEra)
4949
import Cardano.Ledger.Alonzo.Core
50-
import Cardano.Ledger.Alonzo.Genesis (AlonzoExtraConfig (..), AlonzoGenesis (..))
50+
import Cardano.Ledger.Alonzo.Genesis (AlonzoGenesis (..))
5151
import Cardano.Ledger.Alonzo.Plutus.Context (ContextError)
5252
import Cardano.Ledger.Alonzo.Plutus.Evaluate (
5353
collectPlutusScriptsWithContext,
@@ -432,7 +432,7 @@ instance ShelleyEraImp AlonzoEra where
432432
, agMaxValSize = 5000
433433
, agCollateralPercentage = 150
434434
, agMaxCollateralInputs = 3
435-
, agExtraConfig = AlonzoExtraConfig Nothing
435+
, agExtraConfig = Nothing
436436
}
437437

438438
impSatisfyNativeScript = impAllegraSatisfyNativeScript

0 commit comments

Comments
 (0)