Skip to content

Commit 56c2ae8

Browse files
committed
Simplify CardanoHardForkTriggers
see the PR and issue description for details
1 parent c1a681d commit 56c2ae8

File tree

10 files changed

+165
-247
lines changed

10 files changed

+165
-247
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
### Breaking
2+
3+
- Changed `CardanoHardTriggers` to contain `CardanoHardForkTrigger`s which are a
4+
simpler version of the previous `TriggerHardForkAt`. In particular, this will
5+
affect call sites of `protocolInfoCardano`.
6+
7+
Migration notes:
8+
9+
- Change `TriggerHardForkAtEpoch` to `CardanoTriggerHardForkAtEpoch`.
10+
- Change `TriggerHardForkAtVersion` to `CardanoTriggerHardForkAtDefaultVersion`.
11+
12+
This constructor does not take a version argument, but rather defaults to
13+
the corresponding first ledger protocol version. We are not aware of any
14+
use case that requires a different value, but if there is, it is still
15+
possible to manually modify the returned `LedgerConfig`s of
16+
`protocolInfoCardano` directly.

ouroboros-consensus-cardano/src/ouroboros-consensus-cardano/Ouroboros/Consensus/Cardano.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Ouroboros.Consensus.Cardano (
88
, ProtocolCardano
99
, ProtocolShelley
1010
-- * Abstract over the various protocols
11+
, CardanoHardForkTrigger (..)
1112
, CardanoHardForkTriggers (..)
1213
, module X
1314
) where

ouroboros-consensus-cardano/src/ouroboros-consensus-cardano/Ouroboros/Consensus/Cardano/Node.hs

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE CPP #-}
22
{-# LANGUAGE DataKinds #-}
3+
{-# LANGUAGE DerivingStrategies #-}
34
{-# LANGUAGE FlexibleContexts #-}
45
{-# LANGUAGE FlexibleInstances #-}
56
{-# LANGUAGE LambdaCase #-}
@@ -33,6 +34,7 @@
3334

3435
module Ouroboros.Consensus.Cardano.Node (
3536
CardanoHardForkConstraints
37+
, CardanoHardForkTrigger (..)
3638
, CardanoHardForkTriggers (.., CardanoHardForkTriggers', triggerHardForkShelley, triggerHardForkAllegra, triggerHardForkMary, triggerHardForkAlonzo, triggerHardForkBabbage, triggerHardForkConway)
3739
, CardanoProtocolParams (..)
3840
, MaxMajorProtVer (..)
@@ -504,17 +506,42 @@ instance CardanoHardForkConstraints c
504506
ProtocolInfo
505507
-------------------------------------------------------------------------------}
506508

509+
-- | When to trigger a hard fork to a Cardano era.
510+
data CardanoHardForkTrigger blk =
511+
-- | Trigger the hard fork when the ledger protocol version is updated to
512+
-- the default for that era (@'L.eraProtVerLow' \@('ShelleyBlockLedgerEra'
513+
-- blk)@). Also see 'TriggerHardForkAtVersion'.
514+
CardanoTriggerHardForkAtDefaultVersion
515+
|
516+
-- | Trigger the hard fork at the given epoch. For testing only. Also see
517+
-- 'TriggerHardForkAtEpoch'.
518+
CardanoTriggerHardForkAtEpoch EpochNo
519+
deriving stock (Show)
520+
521+
toTriggerHardFork ::
522+
forall blk. L.Era (ShelleyBlockLedgerEra blk)
523+
=> CardanoHardForkTrigger blk
524+
-> TriggerHardFork
525+
toTriggerHardFork = \case
526+
CardanoTriggerHardForkAtDefaultVersion ->
527+
TriggerHardForkAtVersion $
528+
SL.getVersion (L.eraProtVerLow @(ShelleyBlockLedgerEra blk))
529+
CardanoTriggerHardForkAtEpoch epochNo ->
530+
TriggerHardForkAtEpoch epochNo
531+
507532
newtype CardanoHardForkTriggers = CardanoHardForkTriggers {
508-
getCardanoHardForkTriggers :: NP (K TriggerHardFork) (CardanoShelleyEras StandardCrypto)
533+
getCardanoHardForkTriggers ::
534+
NP CardanoHardForkTrigger (CardanoShelleyEras StandardCrypto)
509535
}
510536

511537
pattern CardanoHardForkTriggers' ::
512-
TriggerHardFork
513-
-> TriggerHardFork
514-
-> TriggerHardFork
515-
-> TriggerHardFork
516-
-> TriggerHardFork
517-
-> TriggerHardFork
538+
(c ~ StandardCrypto)
539+
=> CardanoHardForkTrigger (ShelleyBlock (TPraos c) (ShelleyEra c))
540+
-> CardanoHardForkTrigger (ShelleyBlock (TPraos c) (AllegraEra c))
541+
-> CardanoHardForkTrigger (ShelleyBlock (TPraos c) (MaryEra c))
542+
-> CardanoHardForkTrigger (ShelleyBlock (TPraos c) (AlonzoEra c))
543+
-> CardanoHardForkTrigger (ShelleyBlock (Praos c) (BabbageEra c))
544+
-> CardanoHardForkTrigger (ShelleyBlock (Praos c) (ConwayEra c))
518545
-> CardanoHardForkTriggers
519546
pattern CardanoHardForkTriggers' {
520547
triggerHardForkShelley
@@ -525,12 +552,12 @@ pattern CardanoHardForkTriggers' {
525552
, triggerHardForkConway
526553
} =
527554
CardanoHardForkTriggers
528-
( K triggerHardForkShelley
529-
:* K triggerHardForkAllegra
530-
:* K triggerHardForkMary
531-
:* K triggerHardForkAlonzo
532-
:* K triggerHardForkBabbage
533-
:* K triggerHardForkConway
555+
( triggerHardForkShelley
556+
:* triggerHardForkAllegra
557+
:* triggerHardForkMary
558+
:* triggerHardForkAlonzo
559+
:* triggerHardForkBabbage
560+
:* triggerHardForkConway
534561
:* Nil
535562
)
536563
{-# COMPLETE CardanoHardForkTriggers' #-}
@@ -674,7 +701,7 @@ protocolInfoCardano paramsCardano
674701
partialLedgerConfigByron :: PartialLedgerConfig ByronBlock
675702
partialLedgerConfigByron = ByronPartialLedgerConfig {
676703
byronLedgerConfig = ledgerConfigByron
677-
, byronTriggerHardFork = triggerHardForkShelley
704+
, byronTriggerHardFork = toTriggerHardFork triggerHardForkShelley
678705
}
679706

680707
kByron :: SecurityParam
@@ -728,7 +755,7 @@ protocolInfoCardano paramsCardano
728755
mkPartialLedgerConfigShelley
729756
transitionConfigShelley
730757
maxMajorProtVer
731-
triggerHardForkAllegra
758+
(toTriggerHardFork triggerHardForkAllegra)
732759

733760
kShelley :: SecurityParam
734761
kShelley = SecurityParam $ sgSecurityParam genesisShelley
@@ -751,7 +778,7 @@ protocolInfoCardano paramsCardano
751778
mkPartialLedgerConfigShelley
752779
transitionConfigAllegra
753780
maxMajorProtVer
754-
triggerHardForkMary
781+
(toTriggerHardFork triggerHardForkMary)
755782

756783
-- Mary
757784

@@ -771,7 +798,7 @@ protocolInfoCardano paramsCardano
771798
mkPartialLedgerConfigShelley
772799
transitionConfigMary
773800
maxMajorProtVer
774-
triggerHardForkAlonzo
801+
(toTriggerHardFork triggerHardForkAlonzo)
775802

776803
-- Alonzo
777804

@@ -791,7 +818,7 @@ protocolInfoCardano paramsCardano
791818
mkPartialLedgerConfigShelley
792819
transitionConfigAlonzo
793820
maxMajorProtVer
794-
triggerHardForkBabbage
821+
(toTriggerHardFork triggerHardForkBabbage)
795822

796823
-- Babbage
797824

@@ -821,7 +848,7 @@ protocolInfoCardano paramsCardano
821848
mkPartialLedgerConfigShelley
822849
transitionConfigBabbage
823850
maxMajorProtVer
824-
triggerHardForkConway
851+
(toTriggerHardFork triggerHardForkConway)
825852

826853
-- Conway
827854

ouroboros-consensus-cardano/src/unstable-cardano-testlib/Test/Consensus/Cardano/ProtocolInfo.hs

Lines changed: 29 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{-# LANGUAGE DataKinds #-}
22
{-# LANGUAGE FlexibleContexts #-}
33
{-# LANGUAGE GADTs #-}
4-
{-# LANGUAGE NamedFieldPuns #-}
54
{-# LANGUAGE ScopedTypeVariables #-}
65
{-# LANGUAGE TypeApplications #-}
76
{-# LANGUAGE TypeOperators #-}
@@ -14,9 +13,8 @@ module Test.Consensus.Cardano.ProtocolInfo (
1413
, ShelleySlotLengthInSeconds (..)
1514
-- ** Hard-fork specification
1615
, Era (..)
17-
, HardForkSpec (..)
1816
, hardForkInto
19-
, stayInByron
17+
, hardForkOnDefaultProtocolVersions
2018
-- * ProtocolInfo elaboration
2119
, mkSimpleTestProtocolInfo
2220
, mkTestProtocolInfo
@@ -31,6 +29,7 @@ import qualified Cardano.Ledger.BaseTypes as SL
3129
import qualified Cardano.Protocol.TPraos.OCert as SL
3230
import qualified Cardano.Slotting.Time as Time
3331
import Data.Proxy (Proxy (..))
32+
import Data.SOP.Strict
3433
import Data.Word (Word64)
3534
import Ouroboros.Consensus.Block.Forging (BlockForging)
3635
import Ouroboros.Consensus.BlockchainTime (SlotLength)
@@ -39,9 +38,8 @@ import Ouroboros.Consensus.Byron.Node (ByronLeaderCredentials,
3938
byronPbftSignatureThreshold, byronSoftwareVersion)
4039
import Ouroboros.Consensus.Cardano.Block (CardanoBlock)
4140
import Ouroboros.Consensus.Cardano.Node (CardanoHardForkConstraints,
42-
CardanoHardForkTriggers (..), CardanoProtocolParams (..),
43-
TriggerHardFork (TriggerHardForkAtEpoch, TriggerHardForkNotDuringThisExecution),
44-
protocolInfoCardano)
41+
CardanoHardForkTrigger (..), CardanoHardForkTriggers (..),
42+
CardanoProtocolParams (..), protocolInfoCardano)
4543
import Ouroboros.Consensus.Config (emptyCheckpointsMap)
4644
import Ouroboros.Consensus.Config.SecurityParam (SecurityParam (..))
4745
import Ouroboros.Consensus.Node.ProtocolInfo (NumCoreNodes (..),
@@ -77,19 +75,6 @@ instance ToSlotLength ByronSlotLengthInSeconds where
7775
instance ToSlotLength ShelleySlotLengthInSeconds where
7876
toSlotLength (ShelleySlotLengthInSeconds n) = Time.slotLengthFromSec $ fromIntegral n
7977

80-
-- | This data structure is used to specify if and when hardforks should take
81-
-- place, and the version used at each era. See 'stayInByron' and 'hardForkInto'
82-
-- for examples.
83-
data HardForkSpec =
84-
HardForkSpec {
85-
shelleyHardForkSpec :: TriggerHardFork
86-
, allegraHardForkSpec :: TriggerHardFork
87-
, maryHardForkSpec :: TriggerHardFork
88-
, alonzoHardForkSpec :: TriggerHardFork
89-
, babbageHardForkSpec :: TriggerHardFork
90-
, conwayHardForkSpec :: TriggerHardFork
91-
}
92-
9378
data Era = Byron
9479
| Shelley
9580
| Allegra
@@ -99,52 +84,37 @@ data Era = Byron
9984
| Conway
10085
deriving (Show, Eq, Ord, Enum)
10186

102-
selectEra :: Era -> HardForkSpec -> TriggerHardFork
103-
selectEra Byron _ = error "Byron is the first era, therefore there is no hard fork spec."
104-
selectEra Shelley HardForkSpec { shelleyHardForkSpec } = shelleyHardForkSpec
105-
selectEra Allegra HardForkSpec { allegraHardForkSpec } = allegraHardForkSpec
106-
selectEra Mary HardForkSpec { maryHardForkSpec } = maryHardForkSpec
107-
selectEra Alonzo HardForkSpec { alonzoHardForkSpec } = alonzoHardForkSpec
108-
selectEra Babbage HardForkSpec { babbageHardForkSpec } = babbageHardForkSpec
109-
selectEra Conway HardForkSpec { conwayHardForkSpec } = conwayHardForkSpec
110-
111-
stayInByron :: HardForkSpec
112-
stayInByron =
113-
HardForkSpec {
114-
shelleyHardForkSpec = TriggerHardForkNotDuringThisExecution
115-
, allegraHardForkSpec = TriggerHardForkNotDuringThisExecution
116-
, maryHardForkSpec = TriggerHardForkNotDuringThisExecution
117-
, alonzoHardForkSpec = TriggerHardForkNotDuringThisExecution
118-
, babbageHardForkSpec = TriggerHardForkNotDuringThisExecution
119-
, conwayHardForkSpec = TriggerHardForkNotDuringThisExecution
120-
}
121-
12287
protocolVersionZero :: SL.ProtVer
12388
protocolVersionZero = SL.ProtVer versionZero 0
12489
where
12590
versionZero :: SL.Version
12691
versionZero = SL.natVersion @0
12792

128-
hardForkInto :: Era -> HardForkSpec
129-
hardForkInto Byron = stayInByron
93+
hardForkOnDefaultProtocolVersions :: CardanoHardForkTriggers
94+
hardForkOnDefaultProtocolVersions =
95+
CardanoHardForkTriggers
96+
$ hpure CardanoTriggerHardForkAtDefaultVersion
97+
98+
hardForkInto :: Era -> CardanoHardForkTriggers
99+
hardForkInto Byron = hardForkOnDefaultProtocolVersions
130100
hardForkInto Shelley =
131-
stayInByron
132-
{ shelleyHardForkSpec = TriggerHardForkAtEpoch 0 }
101+
hardForkOnDefaultProtocolVersions
102+
{ triggerHardForkShelley = CardanoTriggerHardForkAtEpoch 0 }
133103
hardForkInto Allegra =
134104
(hardForkInto Shelley)
135-
{ allegraHardForkSpec = TriggerHardForkAtEpoch 0 }
105+
{ triggerHardForkAllegra = CardanoTriggerHardForkAtEpoch 0 }
136106
hardForkInto Mary =
137107
(hardForkInto Allegra)
138-
{ maryHardForkSpec = TriggerHardForkAtEpoch 0 }
108+
{ triggerHardForkMary = CardanoTriggerHardForkAtEpoch 0 }
139109
hardForkInto Alonzo =
140110
(hardForkInto Mary)
141-
{ alonzoHardForkSpec = TriggerHardForkAtEpoch 0 }
111+
{ triggerHardForkAlonzo = CardanoTriggerHardForkAtEpoch 0 }
142112
hardForkInto Babbage =
143113
(hardForkInto Alonzo)
144-
{ babbageHardForkSpec = TriggerHardForkAtEpoch 0 }
114+
{ triggerHardForkBabbage = CardanoTriggerHardForkAtEpoch 0 }
145115
hardForkInto Conway =
146116
(hardForkInto Babbage)
147-
{ conwayHardForkSpec = TriggerHardForkAtEpoch 0 }
117+
{ triggerHardForkConway = CardanoTriggerHardForkAtEpoch 0 }
148118

149119
{-------------------------------------------------------------------------------
150120
ProtocolInfo elaboration
@@ -167,9 +137,10 @@ hardForkInto Conway =
167137
-- If you want to tweak the resulting protocol info further see
168138
-- 'mkTestProtocolInfo'.
169139
--
170-
-- The resulting 'ProtocolInfo' contains a ledger state. The 'HardForkSpec'
171-
-- parameter will determine to which era this ledger state belongs. See
172-
-- 'HardForkSpec' for more details on how to specify a value of this type.
140+
-- The resulting 'ProtocolInfo' contains a ledger state. The
141+
-- 'CardanoHardForkTriggers' parameter will determine to which era this ledger
142+
-- state belongs. See 'hardForkInto' and 'hardForkOnDefaultProtocolVersions' for
143+
-- more details on how to specify a value of this type.
173144
--
174145
mkSimpleTestProtocolInfo ::
175146
forall c
@@ -180,15 +151,15 @@ mkSimpleTestProtocolInfo ::
180151
-> ByronSlotLengthInSeconds
181152
-> ShelleySlotLengthInSeconds
182153
-> SL.ProtVer
183-
-> HardForkSpec
154+
-> CardanoHardForkTriggers
184155
-> ProtocolInfo (CardanoBlock c)
185156
mkSimpleTestProtocolInfo
186157
decentralizationParam
187158
securityParam
188159
byronSlotLenghtInSeconds
189160
shelleySlotLengthInSeconds
190161
protocolVersion
191-
hardForkSpec
162+
hardForkTriggers
192163
= fst
193164
$ mkTestProtocolInfo @IO
194165
(CoreNodeId 0, coreNodeShelley)
@@ -199,7 +170,7 @@ mkSimpleTestProtocolInfo
199170
generatedSecretsByron
200171
(Just $ PBftSignatureThreshold 1)
201172
protocolVersion
202-
hardForkSpec
173+
hardForkTriggers
203174
where
204175
byronProtocolVersion =
205176
CC.Update.ProtocolVersion 0 0 0
@@ -258,8 +229,8 @@ mkTestProtocolInfo ::
258229
-> SL.ProtVer
259230
-- ^ See 'protocolInfoCardano' for the details of what is the
260231
-- relation between this version and any 'TriggerHardForkAtVersion'
261-
-- that __might__ appear in the 'HardForkSpec' parameter.
262-
-> HardForkSpec
232+
-- that __might__ appear in the 'CardanoHardForkTriggers' parameter.
233+
-> CardanoHardForkTriggers
263234
-- ^ Specification of the era to which the initial state should hard-fork to.
264235
-> (ProtocolInfo (CardanoBlock c), m [BlockForging m (CardanoBlock c)])
265236
mkTestProtocolInfo
@@ -271,7 +242,7 @@ mkTestProtocolInfo
271242
generatedSecretsByron
272243
aByronPbftSignatureThreshold
273244
protocolVersion
274-
hardForkSpec
245+
hardForkTriggers
275246
=
276247
protocolInfoCardano
277248
(CardanoProtocolParams
@@ -286,14 +257,7 @@ mkTestProtocolInfo
286257
shelleyBasedInitialNonce = initialNonce
287258
, shelleyBasedLeaderCredentials = [leaderCredentialsShelley]
288259
}
289-
CardanoHardForkTriggers' {
290-
triggerHardForkShelley = selectEra Shelley hardForkSpec
291-
, triggerHardForkAllegra = selectEra Allegra hardForkSpec
292-
, triggerHardForkMary = selectEra Mary hardForkSpec
293-
, triggerHardForkAlonzo = selectEra Alonzo hardForkSpec
294-
, triggerHardForkBabbage = selectEra Babbage hardForkSpec
295-
, triggerHardForkConway = selectEra Conway hardForkSpec
296-
}
260+
hardForkTriggers
297261
( L.mkLatestTransitionConfig
298262
shelleyGenesis
299263
-- These example genesis objects might need to become more

0 commit comments

Comments
 (0)