3
3
{-# LANGUAGE FlexibleContexts #-}
4
4
{-# LANGUAGE GADTs #-}
5
5
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
6
- {-# LANGUAGE LambdaCase #-}
7
6
{-# LANGUAGE ScopedTypeVariables #-}
8
7
{-# LANGUAGE TupleSections #-}
9
8
{-# LANGUAGE TypeApplications #-}
@@ -13,7 +12,6 @@ module Cardano.Api.Genesis.Internal
13
12
( ShelleyGenesis (.. )
14
13
, shelleyGenesisDefaults
15
14
, alonzoGenesisDefaults
16
- , decodeAlonzoGenesis
17
15
, conwayGenesisDefaults
18
16
19
17
-- ** Configuration
@@ -35,26 +33,10 @@ module Cardano.Api.Genesis.Internal
35
33
36
34
-- * Utilities
37
35
, unsafeBoundedRational
38
-
39
- -- * Testing only
40
- , costModelParamsCountLegacy
41
36
)
42
37
where
43
38
44
- import Cardano.Api.Era.Internal.Core
45
- ( CardanoEra
46
- , forEraMaybeEon
47
- , monoidForEraInEon
48
- )
49
- import Cardano.Api.Era.Internal.Eon.ConwayEraOnwards
50
39
import Cardano.Api.IO
51
- import Cardano.Api.Monad.Error
52
- ( ExceptT
53
- , MonadError (throwError )
54
- , MonadTransError
55
- , liftEither
56
- , modifyError
57
- )
58
40
59
41
import Cardano.Chain.Genesis qualified
60
42
import Cardano.Crypto.Hash.Blake2b qualified
@@ -64,6 +46,7 @@ import Cardano.Ledger.Alonzo.Scripts (ExUnits (..), Prices (..))
64
46
import Cardano.Ledger.Api (CoinPerWord (.. ))
65
47
import Cardano.Ledger.BaseTypes as Ledger
66
48
import Cardano.Ledger.Coin (Coin (.. ))
49
+ import Cardano.Ledger.Coin qualified as L
67
50
import Cardano.Ledger.Conway.Genesis (ConwayGenesis (.. ))
68
51
import Cardano.Ledger.Conway.PParams
69
52
( DRepVotingThresholds (.. )
@@ -85,33 +68,25 @@ import PlutusCore.Evaluation.Machine.CostModelInterface
85
68
import PlutusCore.Evaluation.Machine.ExBudgetingDefaults
86
69
import PlutusCore.Evaluation.Machine.MachineParameters
87
70
import PlutusLedgerApi.Common (IsParamName , readParamName )
88
- import PlutusLedgerApi.V2 qualified as V2
89
71
import PlutusLedgerApi.V3 qualified as V3
90
72
91
73
import Control.Monad
92
74
import Control.Monad.Trans.Fail.String (errorFail )
93
- import Data.Aeson qualified as A
94
75
import Data.ByteString (ByteString )
95
- import Data.ByteString.Lazy qualified as LBS
96
76
import Data.Default.Class qualified as DefaultClass
97
77
import Data.Functor.Identity
98
78
import Data.Int (Int64 )
99
- import Data.List (sortOn )
100
79
import Data.ListMap qualified as ListMap
101
- import Data.Map (Map )
102
80
import Data.Map.Strict qualified as M
103
81
import Data.Map.Strict qualified as Map
104
82
import Data.Maybe
105
83
import Data.Ratio
106
- import Data.Set qualified as S
107
84
import Data.Text (Text )
108
85
import Data.Time qualified as Time
109
86
import Data.Typeable
110
- import Data.Vector qualified as V
111
87
import GHC.Exts (IsList (.. ))
112
88
import GHC.Stack (HasCallStack )
113
89
import Lens.Micro
114
- import Lens.Micro.Aeson qualified as AL
115
90
116
91
import Barbies (bmap )
117
92
import UntypedPlutusCore.Evaluation.Machine.Cek.CekMachineCosts
@@ -198,7 +173,7 @@ shelleyGenesisDefaults =
198
173
-- pot = tx_fees + ρ * remaining_reserves
199
174
& ppRhoL .~ unsafeBR (1 % 10 ) -- How much of reserves goes into pot
200
175
& 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
202
177
, -- genesis keys and initial funds
203
178
sgGenDelegs = M. empty
204
179
, sgStaking = emptyGenesisStaking
@@ -280,9 +255,11 @@ conwayGenesisDefaults =
280
255
281
256
costModelParamsForTesting :: HasCallStack => [(V3. ParamName , Int64 )]
282
257
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
286
263
287
264
mCostModel :: MCostModel
288
265
mCostModel =
@@ -341,119 +318,11 @@ type MBuiltinCostModel = BuiltinCostModelBase MCostingFun
341
318
(%!) :: forall r . (HasCallStack , Typeable r , BoundedRational r ) => Integer -> Integer -> r
342
319
n %! d = unsafeBoundedRational $ n Data.Ratio. % d
343
320
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
-
448
321
-- | Some reasonable starting defaults for constructing a 'AlonzoGenesis'.
449
322
-- 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
453
323
alonzoGenesisDefaults
454
- :: CardanoEra era
455
- -> AlonzoGenesis
456
- alonzoGenesisDefaults era =
324
+ :: AlonzoGenesis
325
+ alonzoGenesisDefaults =
457
326
AlonzoGenesis
458
327
{ agPrices =
459
328
Prices
@@ -829,23 +698,6 @@ alonzoGenesisDefaults era =
829
698
, 32947
830
699
, 10
831
700
]
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
- ]
849
701
850
702
-- | Convert Rational to a bounded rational. Throw an exception when the rational is out of bounds.
851
703
unsafeBoundedRational
@@ -856,15 +708,3 @@ unsafeBoundedRational
856
708
unsafeBoundedRational x = fromMaybe (error errMessage) $ boundRational x
857
709
where
858
710
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