Skip to content

Commit

Permalink
Support parsing of PlutusV2 costmodel params in AlonzoGenesis
Browse files Browse the repository at this point in the history
which is needed for the future, non-legacy parsing of genesis and also
for current testing.
  • Loading branch information
teodanciu authored and JaredCorduan committed Oct 29, 2022
1 parent 4724c7b commit 82e5f8e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
1 change: 1 addition & 0 deletions eras/alonzo/impl/cardano-ledger-alonzo.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,6 @@ library
transformers,
utf8-string,
validation-selective,
vector
hs-source-dirs:
src
29 changes: 24 additions & 5 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Genesis.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import Cardano.Ledger.Coin (Coin)
import Cardano.Ledger.Core
import Cardano.Ledger.SafeHash (extractHash)
import Cardano.Ledger.Shelley.PParams (ShelleyPParams)
import Data.Aeson (FromJSON (..), Object, ToJSON (..), object, (.!=), (.:), (.:?), (.=))
import Data.Aeson (Array, FromJSON (..), Object, ToJSON (..), object, (.!=), (.:), (.:?), (.=))
import qualified Data.Aeson as Aeson
import Data.Aeson.Types (FromJSONKey (..), Parser, ToJSONKey (..), toJSONKeyText)
import Data.Coders
Expand All @@ -45,6 +45,7 @@ import qualified Data.Map.Strict as Map
import Data.Maybe (mapMaybe, maybeToList)
import Data.Scientific (fromRationalRepetendLimited)
import Data.Text (Text)
import Data.Vector as Vector (toList)
import Data.Word (Word64)
import GHC.Generics (Generic)
import NoThunks.Class (NoThunks)
Expand Down Expand Up @@ -211,21 +212,39 @@ instance ToJSONKey Language where
instance FromJSONKey Language where
fromJSONKey = Aeson.FromJSONKeyTextParser languageFromText

instance FromJSON CostModels where
parseJSON = Aeson.withObject "CostModels" $ \o -> do
v1CostModels <- legacyParseCostModels o
v2CostModels <- parseCostModels o
pure $ CostModels $ Map.fromList (v1CostModels ++ v2CostModels)

-- The costmodel parameters in Alonzo Genesis are represented as a map.
-- Plutus API does no longer require the map as a parameter to `mkEvaluationContext`, but the list of Integers representing the values of the map.
-- The expectation on this list of Integers is that they are sorted in the order given by the `ParamName` enum,
-- so even though we just have to pass the list to plutus, we still need to use the names of the parameters in order to sort the list.
-- In new versions, we want to represent the costmodel parameters directly as a list, so we can avoid this reordering, hence the name of this function.
legacyParseCostModels :: Object -> Parser CostModels
legacyParseCostModels :: Object -> Parser [(Language, CostModel)]
legacyParseCostModels o =
do
plutusV1 <- o .:? "PlutusV1"
cms <- traverse (validateCostModel PlutusV1 . cmParamValues) plutusV1
pure . CostModels . Map.fromList $ maybeToList cms
pure $ maybeToList cms
where
cmParamValues :: Map Text Integer -> [Integer]
cmParamValues cmMap = mapMaybe (`Map.lookup` cmMap) plutusV1ParamNames

parseCostModels :: Object -> Parser [(Language, CostModel)]
parseCostModels o =
do
plutusV2 <- o .:? "PlutusV2"
maybeCostModels <- traverse (Aeson.withArray "PlutusV2 values" parseCostModelsV2) plutusV2
pure $ maybeToList maybeCostModels
where
parseCostModelsV2 :: Array -> Parser ((Language, CostModel))
parseCostModelsV2 array = do
paramValues <- mapM parseJSON $ Vector.toList array
validateCostModel PlutusV2 paramValues

validateCostModel :: MonadFail m => Language -> [Integer] -> m (Language, CostModel)
validateCostModel lang cmps = case mkCostModel lang cmps of
Left err -> fail $ show err
Expand All @@ -234,7 +253,7 @@ validateCostModel lang cmps = case mkCostModel lang cmps of
instance FromJSON AlonzoGenesis where
parseJSON = Aeson.withObject "Alonzo Genesis" $ \o -> do
coinsPerUTxOWord <- o .: "lovelacePerUTxOWord"
costmdls <- o .: "costModels" >>= legacyParseCostModels
costmdls <- o .: "costModels"
prices <- o .: "executionPrices"
maxTxExUnits <- o .: "maxTxExUnits"
maxBlockExUnits <- o .: "maxBlockExUnits"
Expand Down Expand Up @@ -316,7 +335,7 @@ instance FromJSON (AlonzoPParams era) where
<*> obj .: "protocolVersion"
<*> obj .: "minPoolCost" .!= mempty
<*> obj .: "lovelacePerUTxOWord"
<*> (obj .: "costmdls" >>= legacyParseCostModels)
<*> obj .: "costmdls"
<*> obj .: "prices"
<*> obj .: "maxTxExUnits"
<*> obj .: "maxBlockExUnits"
Expand Down
5 changes: 2 additions & 3 deletions eras/alonzo/test-suite/golden/mainnet-alonzo-genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,5 @@
"verifySignature-memory-arguments": 1,
"cekLamCost-exBudgetMemory": 100,
"sliceByteString-cpu-arguments-intercept": 150000
}
}
}
},
"PlutusV2": [197209, 0, 1, 1, 396231, 621, 0, 1, 150000, 1000, 0, 1, 150000, 32, 2477736, 29175, 4, 29773, 100, 29773, 100, 29773, 100, 29773, 100, 29773, 100, 29773, 100, 100, 100, 29773, 100, 150000, 32, 150000, 32, 150000, 32, 150000, 1000, 0, 1, 150000, 32, 150000, 1000, 0, 8, 148000, 425507, 118, 0, 1, 1, 150000, 1000, 0, 8, 150000, 112536, 247, 1, 150000, 10000, 1, 136542, 1326, 1, 1000, 150000, 1000, 1, 150000, 32, 150000, 32, 150000, 32, 1, 1, 150000, 1, 150000, 4, 103599, 248, 1, 103599, 248, 1, 145276, 1366, 1, 179690, 497, 1, 150000, 32, 150000, 32, 150000, 32, 150000, 32, 150000, 32, 150000, 32, 148000, 425507, 118, 0, 1, 1, 61516, 11218, 0, 1, 150000, 32, 148000, 425507, 118, 0, 1, 1, 148000, 425507, 118, 0, 1, 1, 2477736, 29175, 4, 0, 82363, 4, 150000, 5000, 0, 1, 150000, 32, 197209, 0, 1, 1, 150000, 32, 150000, 32, 150000, 32, 150000, 32, 150000, 32, 150000, 32, 150000, 32, 3345831, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] } }
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ expectedGenesis =
AlonzoGenesis
{ coinsPerUTxOWord = Coin 34482,
prices = Prices (fromJust $ boundRational 0.0577) (fromJust $ boundRational 0.0000721),
costmdls = CostModels $ Map.fromList [(PlutusV1, expectedCostModel)],
costmdls = CostModels $ Map.fromList [(PlutusV1, expectedCostModel), (PlutusV2, expectedCostModelV2)],
maxTxExUnits = ExUnits 10000000 10000000000,
maxBlockExUnits = ExUnits 50000000 40000000000,
maxValSize = 5000,
Expand All @@ -317,6 +317,12 @@ expectedCostModel =
(error ("Error creating CostModel from known parameters" <> show expectedPParams))
(mkCostModel PlutusV1 expectedPParams)

expectedCostModelV2 :: CostModel
expectedCostModelV2 =
fromRight
(error ("Error creating CostModel from known PlutusV2 parameters" <> show expectedPParams))
(mkCostModel PlutusV2 (expectedPParams ++ (replicate 9 0)))

expectedPParams :: [Integer]
expectedPParams =
[ 197209,
Expand Down

0 comments on commit 82e5f8e

Please sign in to comment.