33{-# LANGUAGE FlexibleContexts #-}
44{-# LANGUAGE GADTs #-}
55{-# LANGUAGE GeneralisedNewtypeDeriving #-}
6- {-# LANGUAGE LambdaCase #-}
76{-# LANGUAGE ScopedTypeVariables #-}
87{-# LANGUAGE TupleSections #-}
98{-# LANGUAGE TypeApplications #-}
@@ -13,7 +12,6 @@ module Cardano.Api.Genesis.Internal
1312 ( ShelleyGenesis (.. )
1413 , shelleyGenesisDefaults
1514 , alonzoGenesisDefaults
16- , decodeAlonzoGenesis
1715 , conwayGenesisDefaults
1816
1917 -- ** Configuration
@@ -35,26 +33,10 @@ module Cardano.Api.Genesis.Internal
3533
3634 -- * Utilities
3735 , unsafeBoundedRational
38-
39- -- * Testing only
40- , costModelParamsCountLegacy
4136 )
4237where
4338
44- import Cardano.Api.Era.Internal.Core
45- ( CardanoEra
46- , forEraMaybeEon
47- , monoidForEraInEon
48- )
49- import Cardano.Api.Era.Internal.Eon.ConwayEraOnwards
5039import Cardano.Api.IO
51- import Cardano.Api.Monad.Error
52- ( ExceptT
53- , MonadError (throwError )
54- , MonadTransError
55- , liftEither
56- , modifyError
57- )
5840
5941import Cardano.Chain.Genesis qualified
6042import Cardano.Crypto.Hash.Blake2b qualified
@@ -64,6 +46,7 @@ import Cardano.Ledger.Alonzo.Scripts (ExUnits (..), Prices (..))
6446import Cardano.Ledger.Api (CoinPerWord (.. ))
6547import Cardano.Ledger.BaseTypes as Ledger
6648import Cardano.Ledger.Coin (Coin (.. ))
49+ import Cardano.Ledger.Coin qualified as L
6750import Cardano.Ledger.Conway.Genesis (ConwayGenesis (.. ))
6851import Cardano.Ledger.Conway.PParams
6952 ( DRepVotingThresholds (.. )
@@ -85,33 +68,25 @@ import PlutusCore.Evaluation.Machine.CostModelInterface
8568import PlutusCore.Evaluation.Machine.ExBudgetingDefaults
8669import PlutusCore.Evaluation.Machine.MachineParameters
8770import PlutusLedgerApi.Common (IsParamName , readParamName )
88- import PlutusLedgerApi.V2 qualified as V2
8971import PlutusLedgerApi.V3 qualified as V3
9072
9173import Control.Monad
9274import Control.Monad.Trans.Fail.String (errorFail )
93- import Data.Aeson qualified as A
9475import Data.ByteString (ByteString )
95- import Data.ByteString.Lazy qualified as LBS
9676import Data.Default.Class qualified as DefaultClass
9777import Data.Functor.Identity
9878import Data.Int (Int64 )
99- import Data.List (sortOn )
10079import Data.ListMap qualified as ListMap
101- import Data.Map (Map )
10280import Data.Map.Strict qualified as M
10381import Data.Map.Strict qualified as Map
10482import Data.Maybe
10583import Data.Ratio
106- import Data.Set qualified as S
10784import Data.Text (Text )
10885import Data.Time qualified as Time
10986import Data.Typeable
110- import Data.Vector qualified as V
11187import GHC.Exts (IsList (.. ))
11288import GHC.Stack (HasCallStack )
11389import Lens.Micro
114- import Lens.Micro.Aeson qualified as AL
11590
11691import Barbies (bmap )
11792import UntypedPlutusCore.Evaluation.Machine.Cek.CekMachineCosts
@@ -198,7 +173,7 @@ shelleyGenesisDefaults =
198173 -- pot = tx_fees + ρ * remaining_reserves
199174 & ppRhoL .~ unsafeBR (1 % 10 ) -- How much of reserves goes into pot
200175 & ppTauL .~ unsafeBR (1 % 10 ) -- τ * remaining_reserves is sent to treasury every epoch
201- & ppKeyDepositL .~ 400000 -- require a non-zero deposit when registering keys
176+ & ppKeyDepositL .~ L. Coin 400000 -- require a non-zero deposit when registering keys
202177 , -- genesis keys and initial funds
203178 sgGenDelegs = M. empty
204179 , sgStaking = emptyGenesisStaking
@@ -280,9 +255,11 @@ conwayGenesisDefaults =
280255
281256 costModelParamsForTesting :: HasCallStack => [(V3. ParamName , Int64 )]
282257 costModelParamsForTesting =
283- Map. toList $
284- fromJust $
285- extractCostModelParamsLedgerOrder mCostModel
258+ -- all geneses should contain only the number of cost model params equal to the initial number
259+ take (L. costModelInitParamCount PlutusV3 )
260+ . Map. toList
261+ . fromJust
262+ $ extractCostModelParamsLedgerOrder mCostModel
286263
287264 mCostModel :: MCostModel
288265 mCostModel =
@@ -341,119 +318,11 @@ type MBuiltinCostModel = BuiltinCostModelBase MCostingFun
341318(%!) :: forall r . (HasCallStack , Typeable r , BoundedRational r ) => Integer -> Integer -> r
342319n %! d = unsafeBoundedRational $ n Data.Ratio. % d
343320
344- -- | Decode Alonzo genesis in an optionally era sensitive way.
345- --
346- -- Because the Plutus V2 cost model has changed between Babbage and Conway era, we need to know the era if we
347- -- want to decde Alonzo Genesis with a cost model baked in. If the V2 cost model is present in genesis, you
348- -- need to provide an era witness.
349- --
350- -- When an era witness is provided, for Plutus V2 model the function additionally:
351- -- 1. Does extra cost model parameters name validation: Checks for mandatory 175 parameters if provided in
352- -- a map form.
353- -- 2. If >= Conway: adds defaults for new 10 parameters, if they were not provided (maxBound)
354- -- 3. Removes extra parameters above the max count: Babbage - 175, Conway - 185.
355- decodeAlonzoGenesis
356- :: forall era t m
357- . MonadTransError String t m
358- => Maybe (CardanoEra era )
359- -- ^ An optional era witness in which we're reading the genesis
360- -> LBS. ByteString
361- -- ^ Genesis JSON
362- -> t m AlonzoGenesis
363- decodeAlonzoGenesis Nothing genesisBs =
364- modifyError (" Cannot decode Alonzo genesis: " <> ) $
365- liftEither $
366- A. eitherDecode genesisBs
367- decodeAlonzoGenesis (Just era) genesisBs = modifyError (" Cannot decode era-sensitive Alonzo genesis: " <> ) $ do
368- genesisValue :: A. Value <- liftEither $ A. eitherDecode genesisBs
369- -- Making a fixup of a costmodel is easier before JSON deserialization. This also saves us from building
370- -- plutus' EvaluationContext one more time after cost model update.
371- genesisValue' <-
372- (AL. key " costModels" . AL. key " PlutusV2" . AL. _Value) setCostModelDefaultValues genesisValue
373- fromJsonE genesisValue'
374- where
375- setCostModelDefaultValues :: A. Value -> ExceptT String m A. Value
376- setCostModelDefaultValues = \ case
377- obj@ (A. Object _) -> do
378- -- decode cost model into a map first
379- costModel :: Map V2. ParamName Int64 <-
380- modifyError (" Decoding cost model object: " <> ) $ fromJsonE obj
381-
382- let costModelWithDefaults =
383- sortOn fst
384- . toList
385- $ M. union costModel optionalCostModelDefaultValues
386-
387- -- check that we have all required params
388- unless (allCostModelParams == (fst <$> costModelWithDefaults)) $ do
389- let allCostModelParamsSet = fromList allCostModelParams
390- providedCostModelParamsSet = fromList $ fst <$> costModelWithDefaults
391- missingParameters = toList $ S. difference allCostModelParamsSet providedCostModelParamsSet
392- throwError $
393- unlines
394- [ " Missing V2 Plutus cost model parameters: "
395- , show missingParameters
396- , " Number of missing parameters: " <> show (length missingParameters)
397- ]
398- -- We have already have required params, we already added optional ones (which are trimmed later
399- -- if required). Continue processing further in array representation.
400- setCostModelDefaultValues . A. toJSON $ map snd costModelWithDefaults
401- A. Array vec
402- -- here we rely on an assumption that params are in correct order, so that we can take only the
403- -- required ones for an era
404- | V. length vec < costModelExpectedCount ->
405- pure . A. Array . V. take costModelExpectedCount $
406- vec <> (A. toJSON . snd <$> optionalCostModelDefaultValues)
407- | V. length vec > costModelExpectedCount -> pure . A. Array $ V. take costModelExpectedCount vec
408- other -> pure other
409-
410- -- Plutus V2 params expected count depending on an era
411- costModelExpectedCount :: Int
412- costModelExpectedCount
413- -- use all available parameters >= conway
414- | isConwayOnwards = length allCostModelParams
415- -- use only required params in < conway
416- | otherwise = costModelParamsCountLegacy L. PlutusV2 -- Babbage
417-
418- -- A list-like of tuples (param name, value) with default maxBound value
419- optionalCostModelDefaultValues :: (Item l ~ (V2. ParamName , Int64 ), IsList l ) => l
420- optionalCostModelDefaultValues = fromList $ map (,maxBound ) optionalV2costModelParams
421-
422- allCostModelParams :: [V2. ParamName ]
423- allCostModelParams = [minBound .. maxBound ]
424-
425- -- The new V2 cost model params introduced in Conway
426- optionalV2costModelParams :: [V2. ParamName ]
427- optionalV2costModelParams =
428- [ V2. IntegerToByteString'cpu'arguments'c0
429- , V2. IntegerToByteString'cpu'arguments'c1
430- , V2. IntegerToByteString'cpu'arguments'c2
431- , V2. IntegerToByteString'memory'arguments'intercept
432- , V2. IntegerToByteString'memory'arguments'slope
433- , V2. ByteStringToInteger'cpu'arguments'c0
434- , V2. ByteStringToInteger'cpu'arguments'c1
435- , V2. ByteStringToInteger'cpu'arguments'c2
436- , V2. ByteStringToInteger'memory'arguments'intercept
437- , V2. ByteStringToInteger'memory'arguments'slope
438- ]
439-
440- fromJsonE :: A. FromJSON a => A. Value -> ExceptT String m a
441- fromJsonE v =
442- case A. fromJSON v of
443- A. Success a -> pure a
444- A. Error e -> throwError e
445-
446- isConwayOnwards = isJust $ forEraMaybeEon @ ConwayEraOnwards era
447-
448321-- | Some reasonable starting defaults for constructing a 'AlonzoGenesis'.
449322-- Based on https://github.com/IntersectMBO/cardano-node/blob/master/cardano-testnet/src/Testnet/Defaults.hs
450- -- The era determines Plutus V2 cost model parameters:
451- -- * Conway: 185
452- -- * <= Babbage: 175
453323alonzoGenesisDefaults
454- :: CardanoEra era
455- -> AlonzoGenesis
456- alonzoGenesisDefaults era =
324+ :: AlonzoGenesis
325+ alonzoGenesisDefaults =
457326 AlonzoGenesis
458327 { agPrices =
459328 Prices
@@ -829,23 +698,6 @@ alonzoGenesisDefaults era =
829698 , 32947
830699 , 10
831700 ]
832- <> defaultV2CostModelNewConwayParams
833-
834- -- New Conway cost model parameters
835- defaultV2CostModelNewConwayParams =
836- monoidForEraInEon @ ConwayEraOnwards era $
837- const
838- [ 1292075
839- , 24469
840- , 74
841- , 0
842- , 1
843- , 936157
844- , 49601
845- , 237
846- , 0
847- , 1
848- ]
849701
850702-- | Convert Rational to a bounded rational. Throw an exception when the rational is out of bounds.
851703unsafeBoundedRational
@@ -856,15 +708,3 @@ unsafeBoundedRational
856708unsafeBoundedRational x = fromMaybe (error errMessage) $ boundRational x
857709 where
858710 errMessage = show (typeRep (Proxy @ r )) <> " is out of bounds: " <> show x
859-
860- -- Only use this function in the generation of an Alonzo genesis file
861- -- The number of parameters for PlutusV3 reflects that of the Babbage
862- -- era cost model before the intra era hardfork.
863- -- Pre intra-era hardfork the V3 cost model has 231 parameters
864- -- Post intra-era hardfork the V3 cost model has 251 parameters
865- -- TODO: This needs to be parameterized by the protocol version.
866- costModelParamsCountLegacy :: Language -> Int
867- costModelParamsCountLegacy PlutusV1 = 166
868- costModelParamsCountLegacy PlutusV2 = 175
869- costModelParamsCountLegacy PlutusV3 = 231
870- costModelParamsCountLegacy PlutusV4 = 251
0 commit comments