Skip to content

Commit 3f12258

Browse files
authored
Merge pull request #5242 from IntersectMBO/lehins/slight-improvements-to-genesis
Adds `NFData` instances for `Genesis` types
2 parents 693218d + e64628f commit 3f12258

File tree

13 files changed

+78
-54
lines changed

13 files changed

+78
-54
lines changed

eras/alonzo/impl/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 1.14.0.0
44

5+
* Add `NFData` instance for `AlonzoGenesis`
56
* Add `reqSignerHashesTxBodyG`
67
* Add `mkScriptIntegrity`
78
* Changed the type of `hashScriptIntegrity`

eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Genesis.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import Cardano.Ledger.Binary.Coders (
5050
)
5151
import Cardano.Ledger.Core
5252
import Cardano.Ledger.Genesis (EraGenesis (..))
53+
import Control.DeepSeq (NFData)
5354
import Data.Aeson (FromJSON (..), ToJSON (..), (.:), (.=))
5455
import qualified Data.Aeson as Aeson
5556
import Data.Functor.Identity (Identity)
@@ -62,7 +63,7 @@ newtype AlonzoGenesis = AlonzoGenesisWrapper
6263
{ unAlonzoGenesisWrapper :: UpgradeAlonzoPParams Identity
6364
}
6465
deriving stock (Eq, Generic)
65-
deriving newtype (Show, NoThunks)
66+
deriving newtype (Show, NoThunks, NFData)
6667
deriving (ToJSON) via KeyValuePairs AlonzoGenesis
6768

6869
pattern AlonzoGenesis ::

eras/alonzo/impl/src/Cardano/Ledger/Alonzo/PParams.hs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{-# LANGUAGE AllowAmbiguousTypes #-}
22
{-# LANGUAGE DataKinds #-}
3-
{-# LANGUAGE DefaultSignatures #-}
43
{-# LANGUAGE DeriveAnyClass #-}
54
{-# LANGUAGE DeriveGeneric #-}
65
{-# LANGUAGE DerivingStrategies #-}
@@ -9,7 +8,6 @@
98
{-# LANGUAGE GADTs #-}
109
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
1110
{-# LANGUAGE InstanceSigs #-}
12-
{-# LANGUAGE LambdaCase #-}
1311
{-# LANGUAGE MultiParamTypeClasses #-}
1412
{-# LANGUAGE NamedFieldPuns #-}
1513
{-# LANGUAGE OverloadedStrings #-}
@@ -18,7 +16,6 @@
1816
{-# LANGUAGE StandaloneDeriving #-}
1917
{-# LANGUAGE TypeApplications #-}
2018
{-# LANGUAGE TypeFamilies #-}
21-
{-# LANGUAGE TypeOperators #-}
2219
{-# LANGUAGE UndecidableInstances #-}
2320
{-# LANGUAGE UndecidableSuperClasses #-}
2421
{-# OPTIONS_GHC -Wno-orphans #-}

eras/conway/impl/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 1.20.0.0
44

5+
* Add `NFData` for `ConwayGenesis`
56
* Deprecate `PoolParams` in favor of `StakePoolState`. #5196
67
* Update `DRepPulser` and `RatifyEnv` to use `StakePoolState` instead of `PoolParams`.
78
* Better predicate failures for incorrect deposits and refunds.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import Cardano.Ledger.Core
3232
import Cardano.Ledger.Credential (Credential)
3333
import Cardano.Ledger.DRep (DRepState)
3434
import Cardano.Ledger.Genesis (EraGenesis (..))
35+
import Control.DeepSeq (NFData)
3536
import Data.Aeson (
3637
FromJSON (..),
3738
KeyValue (..),
@@ -66,6 +67,8 @@ instance EraGenesis ConwayEra where
6667

6768
instance NoThunks ConwayGenesis
6869

70+
instance NFData ConwayGenesis
71+
6972
-- | Genesis are always encoded with the version of era they are defined in.
7073
instance FromCBOR ConwayGenesis where
7174
fromCBOR =

eras/dijkstra/src/Cardano/Ledger/Dijkstra/Genesis.hs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE DeriveGeneric #-}
22
{-# LANGUAGE DerivingVia #-}
3+
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
34
{-# LANGUAGE RecordWildCards #-}
45
{-# LANGUAGE TypeApplications #-}
56
{-# LANGUAGE TypeFamilies #-}
@@ -9,7 +10,7 @@ module Cardano.Ledger.Dijkstra.Genesis (
910
DijkstraGenesis (..),
1011
) where
1112

12-
import Cardano.Ledger.BaseTypes (KeyValuePairs (..), ToKeyValuePairs (..))
13+
import Cardano.Ledger.BaseTypes (ToKeyValuePairs (..))
1314
import Cardano.Ledger.Binary (
1415
DecCBOR (..),
1516
EncCBOR (..),
@@ -21,35 +22,20 @@ import Cardano.Ledger.Core
2122
import Cardano.Ledger.Dijkstra.Era (DijkstraEra)
2223
import Cardano.Ledger.Dijkstra.PParams (UpgradeDijkstraPParams)
2324
import Cardano.Ledger.Genesis (EraGenesis (..))
24-
import Data.Aeson (FromJSON (..), ToJSON, Value (..), withObject)
25+
import Control.DeepSeq (NFData)
26+
import Data.Aeson (FromJSON (..), ToJSON)
2527
import Data.Functor.Identity (Identity)
2628
import GHC.Generics
2729
import NoThunks.Class (NoThunks)
2830

29-
-- TODO: Currently it is just a placeholder for all the new protocol parameters that will be added
30-
-- in the Dijkstra era
31-
data DijkstraGenesis = DijkstraGenesis
32-
{ dgUpgradePParams :: !(UpgradeDijkstraPParams Identity DijkstraEra)
31+
newtype DijkstraGenesis = DijkstraGenesis
32+
{ dgUpgradePParams :: UpgradeDijkstraPParams Identity DijkstraEra
3333
}
34-
deriving (Eq, Show, Generic)
35-
deriving (ToJSON) via KeyValuePairs DijkstraGenesis
36-
37-
instance FromJSON DijkstraGenesis where
38-
parseJSON = withObject "DijkstraGenesis" $ \obj -> do
39-
dgUpgradePParams <- parseJSON (Object obj)
40-
pure DijkstraGenesis {..}
41-
42-
instance NoThunks DijkstraGenesis
34+
deriving (Eq, Show, Generic, NoThunks, ToJSON, FromJSON, ToKeyValuePairs, NFData)
4335

4436
instance EraGenesis DijkstraEra where
4537
type Genesis DijkstraEra = DijkstraGenesis
4638

47-
-- TODO: Implement this and use for ToJSON instance
48-
instance ToKeyValuePairs DijkstraGenesis where
49-
toKeyValuePairs dg@(DijkstraGenesis _) =
50-
let DijkstraGenesis {..} = dg
51-
in toKeyValuePairs dgUpgradePParams
52-
5339
instance FromCBOR DijkstraGenesis where
5440
fromCBOR =
5541
eraDecoder @DijkstraEra $

eras/dijkstra/src/Cardano/Ledger/Dijkstra/PParams.hs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{-# LANGUAGE AllowAmbiguousTypes #-}
22
{-# LANGUAGE DataKinds #-}
33
{-# LANGUAGE DeriveGeneric #-}
4+
{-# LANGUAGE DerivingVia #-}
45
{-# LANGUAGE FlexibleInstances #-}
56
{-# LANGUAGE InstanceSigs #-}
67
{-# LANGUAGE OverloadedStrings #-}
@@ -32,6 +33,7 @@ import Cardano.Ledger.Alonzo.PParams
3233
import Cardano.Ledger.Babbage.PParams
3334
import Cardano.Ledger.BaseTypes (
3435
EpochInterval (..),
36+
KeyValuePairs (..),
3537
NonNegativeInterval,
3638
NonZero,
3739
PositiveInterval,
@@ -59,7 +61,7 @@ import Cardano.Ledger.Plutus (
5961
import Cardano.Ledger.Shelley.PParams
6062
import Cardano.Ledger.Val (Val (..))
6163
import Control.DeepSeq (NFData)
62-
import Data.Aeson (FromJSON, Key, ToJSON (..), withObject, (.:), (.=))
64+
import Data.Aeson (FromJSON, ToJSON (..), withObject, (.:), (.=))
6365
import qualified Data.Aeson as Aeson
6466
import Data.Data (Proxy (..))
6567
import Data.Default (Default (..))
@@ -240,12 +242,27 @@ deriving instance Eq (UpgradeDijkstraPParams Identity era)
240242
deriving instance Show (UpgradeDijkstraPParams Identity era)
241243

242244
instance FromJSON (UpgradeDijkstraPParams Identity era) where
243-
parseJSON = withObject "UpgradeDijkstraPParams" $ \o ->
244-
UpgradeDijkstraPParams
245-
<$> o .: "maxRefScriptSizePerBlock"
246-
<*> o .: "maxRefScriptSizePerTx"
247-
<*> o .: "refScriptCostStride"
248-
<*> o .: "refScriptCostMultiplier"
245+
parseJSON = withObject "UpgradeDijkstraPParams" $ \o -> do
246+
udppMaxRefScriptSizePerBlock <- o .: "maxRefScriptSizePerBlock"
247+
udppMaxRefScriptSizePerTx <- o .: "maxRefScriptSizePerTx"
248+
udppRefScriptCostStride <- o .: "refScriptCostStride"
249+
udppRefScriptCostMultiplier <- o .: "refScriptCostMultiplier"
250+
pure UpgradeDijkstraPParams {..}
251+
252+
instance ToKeyValuePairs (UpgradeDijkstraPParams Identity era) where
253+
toKeyValuePairs udpp =
254+
[ "maxRefScriptSizePerBlock" .= udppMaxRefScriptSizePerBlock udpp
255+
, "maxRefScriptSizePerTx" .= udppMaxRefScriptSizePerTx udpp
256+
, "refScriptCostStride" .= udppRefScriptCostStride udpp
257+
, "refScriptCostMultiplier" .= udppRefScriptCostMultiplier udpp
258+
]
259+
260+
deriving via
261+
KeyValuePairs (UpgradeDijkstraPParams Identity era)
262+
instance
263+
ToJSON (UpgradeDijkstraPParams Identity era)
264+
265+
instance NFData (UpgradeDijkstraPParams Identity era)
249266

250267
instance NoThunks (UpgradeDijkstraPParams Identity era)
251268

@@ -267,17 +284,6 @@ instance Era era => EncCBOR (UpgradeDijkstraPParams Identity era) where
267284
!> To udppRefScriptCostStride
268285
!> To udppRefScriptCostMultiplier
269286

270-
upgradeDijkstraPParamsHKDPairs :: UpgradeDijkstraPParams Identity era -> [(Key, Aeson.Value)]
271-
upgradeDijkstraPParamsHKDPairs UpgradeDijkstraPParams {..} =
272-
[ ("maxRefScriptSizePerBlock", toJSON udppMaxRefScriptSizePerBlock)
273-
, ("maxRefScriptSizePerTx", toJSON udppMaxRefScriptSizePerTx)
274-
, ("refScriptCostStride", toJSON udppRefScriptCostStride)
275-
, ("refScriptCostMultiplier", toJSON udppRefScriptCostMultiplier)
276-
]
277-
278-
instance ToKeyValuePairs (UpgradeDijkstraPParams Identity era) where
279-
toKeyValuePairs upp = uncurry (.=) <$> upgradeDijkstraPParamsHKDPairs upp
280-
281287
emptyDijkstraUpgradePParamsUpdate :: UpgradeDijkstraPParams StrictMaybe era
282288
emptyDijkstraUpgradePParamsUpdate = UpgradeDijkstraPParams SNothing SNothing SNothing SNothing
283289

eras/shelley/impl/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 1.17.0.0
44

5+
* Add `NFData` for `NominalDiffTimeMicro`, `ShelleyGenesisStaking` and `ShelleyGenesis`
56
* Deprecate `PoolParams` in favor of `StakePoolState`. #5196
67
* Deprecate the API `getPoolParameters` in favor of `getStakePools`.
78
* Deprecate the lens `epochStatePoolParamsL` in favor of `epochStateStakePoolsL`.

eras/shelley/impl/src/Cardano/Ledger/Shelley/Genesis.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ import Cardano.Ledger.TxIn (TxId (..), TxIn (..))
8787
import qualified Cardano.Ledger.Val as Val
8888
import Cardano.Slotting.EpochInfo (EpochInfo)
8989
import Cardano.Slotting.Time (SystemStart (SystemStart))
90+
import Control.DeepSeq (NFData)
9091
import Control.Monad.Identity (Identity)
9192
import Data.Aeson (FromJSON (..), ToJSON (..), object, (.!=), (.:), (.:?), (.=))
9293
import qualified Data.Aeson as Aeson
@@ -137,6 +138,8 @@ data ShelleyGenesisStaking = ShelleyGenesisStaking
137138
deriving stock (Eq, Show, Generic)
138139
deriving (ToJSON) via KeyValuePairs ShelleyGenesisStaking
139140

141+
instance NFData ShelleyGenesisStaking
142+
140143
instance NoThunks ShelleyGenesisStaking
141144

142145
instance Semigroup ShelleyGenesisStaking where
@@ -166,7 +169,7 @@ emptyGenesisStaking = mempty
166169
newtype NominalDiffTimeMicro = NominalDiffTimeMicro Micro
167170
deriving (Show, Eq, Generic)
168171
deriving anyclass (NoThunks)
169-
deriving newtype (Ord, Num, Fractional, Real, ToJSON, FromJSON, EncCBOR, DecCBOR)
172+
deriving newtype (Ord, Num, Fractional, Real, ToJSON, FromJSON, EncCBOR, DecCBOR, NFData)
170173

171174
-- | There is no loss of resolution in this conversion
172175
microToPico :: Micro -> Pico
@@ -230,6 +233,8 @@ data ShelleyGenesis = ShelleyGenesis
230233
deriving stock (Generic, Show, Eq)
231234
deriving (ToJSON) via KeyValuePairs ShelleyGenesis
232235

236+
instance NFData ShelleyGenesis
237+
233238
sgInitialFundsL :: Lens' ShelleyGenesis (LM.ListMap Addr Coin)
234239
sgInitialFundsL = lens sgInitialFunds (\sg x -> sg {sgInitialFunds = x})
235240

eras/shelley/impl/src/Cardano/Ledger/Shelley/Transition.hs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import Cardano.Ledger.BaseTypes
5454
import Cardano.Ledger.Coin
5555
import Cardano.Ledger.Core
5656
import Cardano.Ledger.Credential
57-
import Cardano.Ledger.Genesis (EraGenesis, NoGenesis (..))
57+
import Cardano.Ledger.Genesis
5858
import Cardano.Ledger.Keys
5959
import Cardano.Ledger.Shelley.Era
6060
import Cardano.Ledger.Shelley.Genesis
@@ -169,15 +169,17 @@ class
169169

170170
toTransitionConfigKeyValuePairs ::
171171
KeyValue e a =>
172-
TransitionConfig era -> [a]
172+
TransitionConfig era ->
173+
[a]
173174
default toTransitionConfigKeyValuePairs ::
174175
( EraTransition (PreviousEra era)
175176
, ToKeyValuePairs (TranslationContext era)
176177
, ToKeyValuePairs (TransitionConfig (PreviousEra era))
177178
, Typeable (TranslationContext era)
178179
, KeyValue e a
179180
) =>
180-
TransitionConfig era -> [a]
181+
TransitionConfig era ->
182+
[a]
181183
toTransitionConfigKeyValuePairs config =
182184
toKeyValuePairs (config ^. tcPreviousEraConfigL) ++ translationContextPairs
183185
where
@@ -194,15 +196,12 @@ class
194196
, FromJSON (TranslationContext era)
195197
, FromJSON (TransitionConfig (PreviousEra era))
196198
) =>
197-
Aeson.Value -> Parser (TransitionConfig era)
199+
Aeson.Value ->
200+
Parser (TransitionConfig era)
198201
parseTransitionConfigJSON = withObject (eraName @era <> "TransitionConfig") $ \o -> do
199202
prevTransitionConfig :: TransitionConfig (PreviousEra era) <- parseJSON (Aeson.Object o)
200-
case eqT :: Maybe (TranslationContext era :~: NoGenesis era) of
201-
Nothing -> do
202-
translationContext :: TranslationContext era <- o .: eraNameKey @era
203-
pure $ mkTransitionConfig translationContext prevTransitionConfig
204-
Just Refl ->
205-
pure $ mkTransitionConfig NoGenesis prevTransitionConfig
203+
genesis <- mkGenesisWith @era (o .: eraNameKey @era)
204+
pure $ mkTransitionConfig genesis prevTransitionConfig
206205

207206
eraNameKey :: forall era. Era era => Key
208207
eraNameKey = fromString (map toLower (eraName @era))

0 commit comments

Comments
 (0)