Skip to content

Commit ab839f1

Browse files
committed
Disable Alonzo Genesis cost models validation, since it is done by ledger now
1 parent cf3825b commit ab839f1

File tree

11 files changed

+80
-1062
lines changed

11 files changed

+80
-1062
lines changed

cardano-api/cardano-api.cabal

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ library
154154
iproute,
155155
memory,
156156
microlens,
157-
microlens-aeson,
158157
mono-traversable,
159158
mtl,
160159
network,
@@ -312,18 +311,18 @@ library gen
312311

313312
build-depends:
314313
FailT,
315-
QuickCheck < 2.16,
314+
QuickCheck <2.16,
316315
aeson >=1.5.6.0,
317316
base16-bytestring,
318317
bytestring,
319318
cardano-api,
320319
cardano-binary >=1.6 && <1.8,
321-
cardano-ledger-byron,
322320
cardano-crypto-class ^>=2.2.1,
323321
cardano-crypto-test ^>=1.6,
324322
cardano-crypto-wrapper,
325323
cardano-ledger-alonzo >=1.8.1,
326324
cardano-ledger-babbage,
325+
cardano-ledger-byron,
327326
cardano-ledger-conway,
328327
cardano-ledger-core >=1.14,
329328
cardano-ledger-dijkstra >=0.1,
@@ -350,7 +349,7 @@ test-suite cardano-api-test
350349
type: exitcode-stdio-1.0
351350
build-depends:
352351
FailT,
353-
QuickCheck < 2.16,
352+
QuickCheck <2.16,
354353
aeson >=1.5.6.0,
355354
base16-bytestring,
356355
bytestring,
@@ -382,7 +381,6 @@ test-suite cardano-api-test
382381
ouroboros-consensus,
383382
ouroboros-consensus-cardano,
384383
ouroboros-consensus-protocol,
385-
plutus-ledger-api,
386384
tasty,
387385
tasty-hedgehog,
388386
tasty-quickcheck,

cardano-api/gen/Test/Gen/Cardano/Api/Orphans.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import Cardano.Ledger.HKD (HKD, NoUpdate (..))
3636
import Cardano.Ledger.Keys (VRFVerKeyHash (..))
3737
import Cardano.Ledger.Mary.Value qualified as ConcreteValue
3838
import Cardano.Ledger.Mary.Value qualified as Ledger
39+
import Cardano.Ledger.Plutus.CostModels qualified as L
3940
import Cardano.Ledger.Plutus.CostModels qualified as Ledger
4041
import Cardano.Ledger.Plutus.Language qualified as L
4142
import Cardano.Ledger.Plutus.Language qualified as Ledger
@@ -742,7 +743,7 @@ instance Arbitrary Alonzo.CostModels where
742743

743744
genValidCostModel :: Ledger.Language -> Gen Ledger.CostModel
744745
genValidCostModel lang = do
745-
newParamValues <- vectorOf (costModelParamsCountLegacy lang) arbitrary
746+
newParamValues <- vectorOf (L.costModelInitParamCount lang) arbitrary
746747
either (\err -> error $ "Corrupt cost model: " ++ show err) pure $
747748
Ledger.mkCostModel lang newParamValues
748749

@@ -778,12 +779,12 @@ genCostModelValues lang = do
778779
Positive sub <- arbitrary
779780
(,) lang'
780781
<$> oneof
781-
[ listAtLeast (costModelParamsCountLegacy lang)
782+
[ listAtLeast (L.costModelInitParamCount lang)
782783
, take (tooFew sub) <$> arbitrary
783784
]
784785
where
785786
lang' = fromIntegral (fromEnum lang)
786-
tooFew sub = costModelParamsCountLegacy lang - sub
787+
tooFew sub = L.costModelInitParamCount lang - sub
787788
listAtLeast :: Int -> Gen [Int64]
788789
listAtLeast x = do
789790
NonNegative y <- arbitrary

cardano-api/src/Cardano/Api/Experimental/Tx/Internal/Certificate.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import Cardano.Api.Address qualified as Api
2323
import Cardano.Api.Certificate.Internal qualified as Api
2424
import Cardano.Api.Era.Internal.Core (DijkstraEra)
2525
import Cardano.Api.Era.Internal.Eon.Convert
26-
import Cardano.Api.Era.Internal.Eon.ConwayEraOnwards
2726
import Cardano.Api.Era.Internal.Eon.ShelleyBasedEra
2827
import Cardano.Api.Era.Internal.Eon.ShelleyToBabbageEra qualified as Api
2928
import Cardano.Api.Experimental.Era

cardano-api/src/Cardano/Api/Genesis.hs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module Cardano.Api.Genesis
22
( ShelleyGenesis (..)
33
, shelleyGenesisDefaults
44
, alonzoGenesisDefaults
5-
, decodeAlonzoGenesis
65
, conwayGenesisDefaults
76

87
-- ** Configuration
@@ -29,9 +28,6 @@ module Cardano.Api.Genesis
2928
-- * Utilities
3029
, unsafeBoundedRational
3130
, fromShelleyGenesis
32-
33-
-- * Testing only
34-
, costModelParamsCountLegacy
3531
)
3632
where
3733

cardano-api/src/Cardano/Api/Genesis/Internal.hs

Lines changed: 9 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
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
)
4237
where
4338

44-
import Cardano.Api.Era.Internal.Core
45-
( CardanoEra
46-
, forEraMaybeEon
47-
, monoidForEraInEon
48-
)
49-
import Cardano.Api.Era.Internal.Eon.ConwayEraOnwards
5039
import Cardano.Api.IO
51-
import Cardano.Api.Monad.Error
52-
( ExceptT
53-
, MonadError (throwError)
54-
, MonadTransError
55-
, liftEither
56-
, modifyError
57-
)
5840

5941
import Cardano.Chain.Genesis qualified
6042
import Cardano.Crypto.Hash.Blake2b qualified
@@ -64,6 +46,7 @@ import Cardano.Ledger.Alonzo.Scripts (ExUnits (..), Prices (..))
6446
import Cardano.Ledger.Api (CoinPerWord (..))
6547
import Cardano.Ledger.BaseTypes as Ledger
6648
import Cardano.Ledger.Coin (Coin (..))
49+
import Cardano.Ledger.Coin qualified as L
6750
import Cardano.Ledger.Conway.Genesis (ConwayGenesis (..))
6851
import Cardano.Ledger.Conway.PParams
6952
( DRepVotingThresholds (..)
@@ -85,33 +68,25 @@ import PlutusCore.Evaluation.Machine.CostModelInterface
8568
import PlutusCore.Evaluation.Machine.ExBudgetingDefaults
8669
import PlutusCore.Evaluation.Machine.MachineParameters
8770
import PlutusLedgerApi.Common (IsParamName, readParamName)
88-
import PlutusLedgerApi.V2 qualified as V2
8971
import PlutusLedgerApi.V3 qualified as V3
9072

9173
import Control.Monad
9274
import Control.Monad.Trans.Fail.String (errorFail)
93-
import Data.Aeson qualified as A
9475
import Data.ByteString (ByteString)
95-
import Data.ByteString.Lazy qualified as LBS
9676
import Data.Default.Class qualified as DefaultClass
9777
import Data.Functor.Identity
9878
import Data.Int (Int64)
99-
import Data.List (sortOn)
10079
import Data.ListMap qualified as ListMap
101-
import Data.Map (Map)
10280
import Data.Map.Strict qualified as M
10381
import Data.Map.Strict qualified as Map
10482
import Data.Maybe
10583
import Data.Ratio
106-
import Data.Set qualified as S
10784
import Data.Text (Text)
10885
import Data.Time qualified as Time
10986
import Data.Typeable
110-
import Data.Vector qualified as V
11187
import GHC.Exts (IsList (..))
11288
import GHC.Stack (HasCallStack)
11389
import Lens.Micro
114-
import Lens.Micro.Aeson qualified as AL
11590

11691
import Barbies (bmap)
11792
import 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
342319
n %! 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
453323
alonzoGenesisDefaults
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.
851703
unsafeBoundedRational
@@ -856,15 +708,3 @@ unsafeBoundedRational
856708
unsafeBoundedRational 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

Comments
 (0)