Skip to content

Commit 3c908ab

Browse files
committed
Added DijkstraPParams
1 parent b07ea85 commit 3c908ab

File tree

19 files changed

+673
-208
lines changed

19 files changed

+673
-208
lines changed

eras/conway/impl/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
## 1.20.0.0
44

5+
* Removed:
6+
* `maxRefScriptSizePerTx`
7+
* `maxRefScriptSizePerBlock`
8+
* `refScriptCostMultiplier`
9+
* `refScriptCostStride`
10+
* Added:
11+
* `ppMaxRefScriptSizePerTxG`
12+
* `ppMaxRefScriptSizePerBlockG`
13+
* `ppRefScriptCostMultiplierG`
14+
* `ppRefScriptCostStrideG`
515
* Add `AlonzoEraTx` constraint to `STS` instance for `ConwayBBODY`
616
* Add `totalRefScriptSizeInBlock`
717
* Move some hard-fork triggers and export them from `Cardano.Ledger.Conway` module.

eras/conway/impl/src/Cardano/Ledger/Conway/PParams.hs

Lines changed: 50 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE AllowAmbiguousTypes #-}
22
{-# LANGUAGE DataKinds #-}
3+
{-# LANGUAGE DefaultSignatures #-}
34
{-# LANGUAGE DeriveGeneric #-}
45
{-# LANGUAGE DerivingVia #-}
56
{-# LANGUAGE FlexibleContexts #-}
@@ -9,13 +10,15 @@
910
{-# LANGUAGE LambdaCase #-}
1011
{-# LANGUAGE MultiParamTypeClasses #-}
1112
{-# LANGUAGE NamedFieldPuns #-}
13+
{-# LANGUAGE NumericUnderscores #-}
1214
{-# LANGUAGE OverloadedStrings #-}
1315
{-# LANGUAGE RankNTypes #-}
1416
{-# LANGUAGE RecordWildCards #-}
1517
{-# LANGUAGE ScopedTypeVariables #-}
1618
{-# LANGUAGE StandaloneDeriving #-}
1719
{-# LANGUAGE TypeApplications #-}
1820
{-# LANGUAGE TypeFamilies #-}
21+
{-# LANGUAGE TypeOperators #-}
1922
{-# LANGUAGE UndecidableInstances #-}
2023
{-# LANGUAGE UndecidableSuperClasses #-}
2124
{-# OPTIONS_GHC -Wno-orphans #-}
@@ -77,14 +80,13 @@ module Cardano.Ledger.Conway.PParams (
7780
DRepGroup (..),
7881
PPGroups (..),
7982
StakePoolGroup (..),
80-
conwayModifiedPPGroups,
8183
pvtHardForkInitiationL,
8284
pvtMotionNoConfidenceL,
83-
conwayApplyPPUpdates,
8485
emptyConwayPParams,
8586
emptyConwayPParamsUpdate,
8687
asNaturalHKD,
8788
asBoundedIntegralHKD,
89+
ppGroup,
8890
) where
8991

9092
import Cardano.Ledger.Alonzo.PParams
@@ -99,6 +101,7 @@ import Cardano.Ledger.Babbage (BabbageEra)
99101
import Cardano.Ledger.Babbage.Core
100102
import Cardano.Ledger.Babbage.PParams
101103
import Cardano.Ledger.BaseTypes (
104+
BoundedRational (..),
102105
EpochInterval (..),
103106
NonNegativeInterval,
104107
ProtVer (ProtVer),
@@ -141,22 +144,31 @@ import Data.Foldable (foldlM)
141144
import Data.Functor.Identity (Identity)
142145
import qualified Data.IntMap as IntMap
143146
import qualified Data.Map.Strict as Map
144-
import Data.Maybe (mapMaybe)
147+
import Data.Maybe (fromJust, mapMaybe)
145148
import Data.Maybe.Strict (StrictMaybe (..))
146149
import Data.Proxy
147150
import Data.Set (Set)
148151
import qualified Data.Set as Set
149152
import Data.Typeable
150153
import Data.Word (Word16, Word32)
151-
import GHC.Generics (Generic)
154+
import GHC.Generics (Generic (..), K1 (..), M1 (..), (:*:) (..))
152155
import GHC.Stack (HasCallStack)
153-
import Lens.Micro
156+
import Lens.Micro (Lens', SimpleGetter, lens, set, (^.))
157+
import qualified Lens.Micro as L
154158
import NoThunks.Class (NoThunks (..))
155159
import Numeric.Natural (Natural)
156160
import qualified PlutusLedgerApi.Common as P (Data (..))
157161

158162
class BabbageEraPParams era => ConwayEraPParams era where
159163
modifiedPPGroups :: PParamsUpdate era -> Set PPGroups
164+
default modifiedPPGroups ::
165+
forall a.
166+
( Generic (PParamsHKD StrictMaybe era)
167+
, CollectModifiedPPGroups (Rep (PParamsHKD StrictMaybe era) a)
168+
) =>
169+
PParamsUpdate era ->
170+
Set PPGroups
171+
modifiedPPGroups (PParamsUpdate ppu) = collectModifiedPPGroups $ from @_ @a ppu
160172
ppuWellFormed :: ProtVer -> PParamsUpdate era -> Bool
161173

162174
hkdPoolVotingThresholdsL :: HKDFunctor f => Lens' (PParamsHKD f era) (HKD f PoolVotingThresholds)
@@ -169,6 +181,10 @@ class BabbageEraPParams era => ConwayEraPParams era where
169181
hkdDRepActivityL :: HKDFunctor f => Lens' (PParamsHKD f era) (HKD f EpochInterval)
170182
hkdMinFeeRefScriptCostPerByteL ::
171183
HKDFunctor f => Lens' (PParamsHKD f era) (HKD f NonNegativeInterval)
184+
ppMaxRefScriptSizePerTxG :: SimpleGetter (PParams era) Word32
185+
ppMaxRefScriptSizePerBlockG :: SimpleGetter (PParams era) Word32
186+
ppRefScriptCostMultiplierG :: SimpleGetter (PParams era) NonNegativeInterval
187+
ppRefScriptCostStrideG :: SimpleGetter (PParams era) Word32
172188

173189
instance ConwayEraPParams era => ToPlutusData (PParamsUpdate era) where
174190
toPlutusData ppu = P.Map $ mapMaybe ppToData (eraPParams @era)
@@ -798,7 +814,7 @@ instance EraPParams ConwayEra where
798814
hkdMinPoolCostL = lens (unTHKD . cppMinPoolCost) $ \pp x -> pp {cppMinPoolCost = THKD x}
799815
ppProtocolVersionL = ppLensHKD . lens cppProtocolVersion (\pp x -> pp {cppProtocolVersion = x})
800816

801-
ppDG = to (const minBound)
817+
ppDG = L.to (const minBound)
802818
ppuProtocolVersionL = notSupportedInThisEraL
803819
hkdDL = notSupportedInThisEraL
804820
hkdExtraEntropyL = notSupportedInThisEraL
@@ -871,7 +887,6 @@ instance BabbageEraPParams ConwayEra where
871887
lens (unTHKD . cppCoinsPerUTxOByte) $ \pp x -> pp {cppCoinsPerUTxOByte = THKD x}
872888

873889
instance ConwayEraPParams ConwayEra where
874-
modifiedPPGroups (PParamsUpdate ppu) = conwayModifiedPPGroups ppu
875890
ppuWellFormed pv ppu =
876891
and
877892
[ -- Numbers
@@ -918,6 +933,10 @@ instance ConwayEraPParams ConwayEra where
918933
lens (unTHKD . cppDRepActivity) $ \pp x -> pp {cppDRepActivity = THKD x}
919934
hkdMinFeeRefScriptCostPerByteL =
920935
lens (unTHKD . cppMinFeeRefScriptCostPerByte) $ \pp x -> pp {cppMinFeeRefScriptCostPerByte = THKD x}
936+
ppMaxRefScriptSizePerTxG = L.to . const $ 200 * 1024
937+
ppMaxRefScriptSizePerBlockG = L.to . const $ 1024 * 1024
938+
ppRefScriptCostMultiplierG = L.to . const . fromJust $ boundRational 1.2
939+
ppRefScriptCostStrideG = L.to $ const 25_600
921940

922941
-- | Returns a basic "empty" `PParams` structure with all zero values.
923942
emptyConwayPParams :: forall era. Era era => ConwayPParams Identity era
@@ -1173,73 +1192,30 @@ conwayApplyPPUpdates pp ppu =
11731192
THKD SNothing -> cppGet pp
11741193
THKD (SJust ppNewValue) -> THKD ppNewValue
11751194

1176-
conwayModifiedPPGroups :: ConwayPParams StrictMaybe era -> Set PPGroups
1177-
conwayModifiedPPGroups
1178-
( ConwayPParams
1179-
p01
1180-
p02
1181-
p03
1182-
p04
1183-
p05
1184-
p06
1185-
p07
1186-
p08
1187-
p09
1188-
p10
1189-
p11
1190-
p12
1191-
_protocolVersion
1192-
p14
1193-
p15
1194-
p16
1195-
p17
1196-
p18
1197-
p19
1198-
p20
1199-
p21
1200-
p22
1201-
p23
1202-
p24
1203-
p25
1204-
p26
1205-
p27
1206-
p28
1207-
p29
1208-
p30
1209-
p31
1210-
) =
1211-
mconcat
1212-
[ ppGroup p01
1213-
, ppGroup p02
1214-
, ppGroup p03
1215-
, ppGroup p04
1216-
, ppGroup p05
1217-
, ppGroup p06
1218-
, ppGroup p07
1219-
, ppGroup p08
1220-
, ppGroup p09
1221-
, ppGroup p10
1222-
, ppGroup p11
1223-
, ppGroup p12
1224-
, ppGroup p14
1225-
, ppGroup p15
1226-
, ppGroup p16
1227-
, ppGroup p17
1228-
, ppGroup p18
1229-
, ppGroup p19
1230-
, ppGroup p20
1231-
, ppGroup p21
1232-
, ppGroup p22
1233-
, ppGroup p23
1234-
, ppGroup p24
1235-
, ppGroup p25
1236-
, ppGroup p26
1237-
, ppGroup p27
1238-
, ppGroup p28
1239-
, ppGroup p29
1240-
, ppGroup p30
1241-
, ppGroup p31
1242-
]
1195+
class CollectModifiedPPGroups x where
1196+
collectModifiedPPGroups :: x -> Set PPGroups
1197+
1198+
instance
1199+
( CollectModifiedPPGroups (x u)
1200+
, CollectModifiedPPGroups (y u)
1201+
) =>
1202+
CollectModifiedPPGroups ((x :*: y) u)
1203+
where
1204+
collectModifiedPPGroups (x :*: y) = collectModifiedPPGroups x <> collectModifiedPPGroups y
1205+
1206+
instance
1207+
( ToDRepGroup g
1208+
, ToStakePoolGroup h
1209+
) =>
1210+
CollectModifiedPPGroups (K1 i (THKD ('PPGroups g h) StrictMaybe a) p)
1211+
where
1212+
collectModifiedPPGroups (K1 x) = ppGroup x
1213+
1214+
instance CollectModifiedPPGroups (K1 i (NoUpdate a) p) where
1215+
collectModifiedPPGroups _ = mempty
1216+
1217+
instance CollectModifiedPPGroups (a u) => CollectModifiedPPGroups (M1 i c a u) where
1218+
collectModifiedPPGroups (M1 x) = collectModifiedPPGroups x
12431219

12441220
-- | Care should be taken to not apply this function to signed values, otherwise it will result in
12451221
-- an `ArithmeticUnderflow` exception for negative numbers.

eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Bbody.hs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@
1919
module Cardano.Ledger.Conway.Rules.Bbody (
2020
ConwayBBODY,
2121
ConwayBbodyPredFailure (..),
22-
maxRefScriptSizePerBlock,
2322
alonzoToConwayBbodyPredFailure,
2423
shelleyToConwayBbodyPredFailure,
2524
totalRefScriptSizeInBlock,
2625
) where
2726

2827
import Cardano.Ledger.Allegra.Rules (AllegraUtxoPredFailure)
29-
import Cardano.Ledger.Alonzo.PParams (AlonzoEraPParams)
3028
import Cardano.Ledger.Alonzo.Rules (
3129
AlonzoBbodyEvent (..),
3230
AlonzoBbodyPredFailure (ShelleyInAlonzoBbodyPredFailure),
@@ -56,6 +54,7 @@ import Cardano.Ledger.Binary (DecCBOR (..), EncCBOR (..))
5654
import Cardano.Ledger.Binary.Coders (Decode (..), Encode (..), decode, encode, (!>), (<!))
5755
import Cardano.Ledger.Block (Block (..))
5856
import Cardano.Ledger.Conway.Era (ConwayBBODY, ConwayEra)
57+
import Cardano.Ledger.Conway.PParams (ConwayEraPParams (..))
5958
import Cardano.Ledger.Conway.Rules.Cert (ConwayCertPredFailure)
6059
import Cardano.Ledger.Conway.Rules.Certs (ConwayCertsPredFailure)
6160
import Cardano.Ledger.Conway.Rules.Deleg (ConwayDelegPredFailure)
@@ -97,16 +96,11 @@ import Data.Monoid (Sum (getSum))
9796
import qualified Data.Monoid as Monoid (Sum (..))
9897
import Data.Sequence (Seq)
9998
import Data.Sequence.Strict (StrictSeq (..))
99+
import Data.Word (Word32)
100100
import GHC.Generics (Generic)
101101
import Lens.Micro ((^.))
102102
import NoThunks.Class (NoThunks (..))
103103

104-
-- | In the next era this will become a proper protocol parameter.
105-
-- For now this is a hard coded limit on the total number of bytes of all reference scripts
106-
-- combined from all transactions within a block.
107-
maxRefScriptSizePerBlock :: Int
108-
maxRefScriptSizePerBlock = 1024 * 1024 -- 1MiB
109-
110104
data ConwayBbodyPredFailure era
111105
= WrongBlockBodySizeBBODY (Mismatch 'RelEQ Int)
112106
| InvalidBodyHashBBODY (Mismatch 'RelEQ (Hash HASH EraIndependentBlockBody))
@@ -252,7 +246,7 @@ instance
252246
, AlonzoEraTxWits era
253247
, TxSeq era ~ AlonzoTxSeq era
254248
, EraSegWits era
255-
, AlonzoEraPParams era
249+
, ConwayEraPParams era
256250
, InjectRuleFailure "BBODY" AlonzoBbodyPredFailure era
257251
, InjectRuleFailure "BBODY" ConwayBbodyPredFailure era
258252
, EraRule "BBODY" era ~ ConwayBBODY era
@@ -287,6 +281,7 @@ conwayBbodyTransition ::
287281
, InjectRuleFailure "BBODY" ConwayBbodyPredFailure era
288282
, AlonzoEraTx era
289283
, BabbageEraTxBody era
284+
, ConwayEraPParams era
290285
) =>
291286
TransitionRule (EraRule "BBODY" era)
292287
conwayBbodyTransition = do
@@ -300,6 +295,7 @@ conwayBbodyTransition = do
300295
let utxo = utxosUtxo (lsUTxOState ls)
301296
txs = txSeqTxns txsSeq
302297
totalRefScriptSize = totalRefScriptSizeInBlock (pp ^. ppProtocolVersionL) txs utxo
298+
maxRefScriptSizePerBlock = fromIntegral @Word32 @Int $ pp ^. ppMaxRefScriptSizePerBlockG
303299
totalRefScriptSize
304300
<= maxRefScriptSizePerBlock
305301
?! injectFailure

eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Ledger.hs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ module Cardano.Ledger.Conway.Rules.Ledger (
1818
ConwayLEDGER,
1919
ConwayLedgerPredFailure (..),
2020
ConwayLedgerEvent (..),
21-
maxRefScriptSizePerTx,
2221
) where
2322

2423
import Cardano.Ledger.Address (RewardAccount (..))
@@ -67,6 +66,7 @@ import Cardano.Ledger.Conway.Governance (
6766
proposalsGovStateL,
6867
proposalsWithPurpose,
6968
)
69+
import Cardano.Ledger.Conway.PParams (ConwayEraPParams (..))
7070
import Cardano.Ledger.Conway.Rules.Cert (CertEnv, ConwayCertEvent (..), ConwayCertPredFailure (..))
7171
import Cardano.Ledger.Conway.Rules.Certs (
7272
CertsEnv (CertsEnv),
@@ -127,6 +127,7 @@ import Data.Sequence (Seq)
127127
import qualified Data.Sequence.Strict as StrictSeq
128128
import qualified Data.Set as Set
129129
import Data.Text (Text)
130+
import Data.Word (Word32)
130131
import GHC.Generics (Generic (..))
131132
import Lens.Micro as L
132133
import NoThunks.Class (NoThunks (..))
@@ -141,12 +142,6 @@ data ConwayLedgerPredFailure era
141142
| ConwayMempoolFailure Text
142143
deriving (Generic)
143144

144-
-- | In the next era this will become a proper protocol parameter. For now this is a hard
145-
-- coded limit on the total number of bytes of reference scripts that a transaction can
146-
-- use.
147-
maxRefScriptSizePerTx :: Int
148-
maxRefScriptSizePerTx = 200 * 1024 -- 200KiB
149-
150145
type instance EraRuleFailure "LEDGER" ConwayEra = ConwayLedgerPredFailure ConwayEra
151146

152147
type instance EraRuleEvent "LEDGER" ConwayEra = ConwayLedgerEvent ConwayEra
@@ -384,7 +379,9 @@ ledgerTransition = do
384379
}
385380
)
386381

387-
let totalRefScriptSize = txNonDistinctRefScriptsSize (utxoState ^. utxoL) tx
382+
let
383+
totalRefScriptSize = txNonDistinctRefScriptsSize (utxoState ^. utxoL) tx
384+
maxRefScriptSizePerTx = fromIntegral @Word32 @Int $ pp ^. ppMaxRefScriptSizePerTxG
388385
totalRefScriptSize
389386
<= maxRefScriptSizePerTx
390387
?! ConwayTxRefScriptsSizeTooBig

0 commit comments

Comments
 (0)