Skip to content

Commit b05ba36

Browse files
authored
Add a function registerInState to EraTransition (#4023)
* Add a new function `injectIntoTestState` to `EraTransition`, which allows to inject transition configuration data in the given `NewEpochState`. This change allows the Ledger to define this logic in this component without needing to do it in the Consensus layer. In this way, we improve the separation of concerns. * Do no register the initial funds in `createInitialState`. This allows the `ThreadNet` tests in Consensus to use `injectIntoTestState` after creating the initial state. Add a comment on `createInitialState` stating that no funds or delegates are registered by this function. * Remove registration functions used by Consensus. Functions `registerInitialFunds`, `registerInitialStaking`, `registerInitialDReps`, and `registerDelegs` were used by the Consensus code, which now relies on `injectIntoTestState`. * Update the changelogs.
1 parent 1ea568e commit b05ba36

File tree

10 files changed

+52
-15
lines changed

10 files changed

+52
-15
lines changed

eras/allegra/impl/src/Cardano/Ledger/Allegra/Transition.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ instance Crypto c => EraTransition (AllegraEra c) where
2222

2323
mkTransitionConfig () = AllegraTransitionConfig
2424

25+
injectIntoTestState = registerInitialFundsThenStaking
26+
2527
tcPreviousEraConfigL =
2628
lens atcShelleyTransitionConfig (\atc pc -> atc {atcShelleyTransitionConfig = pc})
2729

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ instance Crypto c => EraTransition (AlonzoEra c) where
4141

4242
mkTransitionConfig = AlonzoTransitionConfig
4343

44+
injectIntoTestState = registerInitialFundsThenStaking
45+
4446
tcPreviousEraConfigL =
4547
lens atcMaryTransitionConfig (\atc pc -> atc {atcMaryTransitionConfig = pc})
4648

eras/babbage/impl/src/Cardano/Ledger/Babbage/Transition.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ instance Crypto c => EraTransition (BabbageEra c) where
2323

2424
mkTransitionConfig () = BabbageTransitionConfig
2525

26+
injectIntoTestState = registerInitialFundsThenStaking
27+
2628
tcPreviousEraConfigL =
2729
lens btcAlonzoTransitionConfig (\btc pc -> btc {btcAlonzoTransitionConfig = pc})
2830

eras/conway/impl/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* Add `hoistGovRelation` and `withGovActionParent`
2323
* Add `TreeMaybe`, `toGovRelationTree` and `toGovRelationTreeEither`
2424
* Remove `proposalsAreConsistent`
25+
* Remove `registerDelegs` and `registerInitialDReps`
2526

2627
### `testlib`
2728

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ module Cardano.Ledger.Conway.Transition (
1212
ConwayEraTransition (..),
1313
TransitionConfig (..),
1414
toConwayTransitionConfigPairs,
15-
registerDelegs,
16-
registerInitialDReps,
1715
) where
1816

1917
import Cardano.Ledger.Alonzo.Transition (toAlonzoTransitionConfigPairs)
@@ -72,6 +70,15 @@ class EraTransition era => ConwayEraTransition era where
7270

7371
tcConwayGenesisL :: Lens' (TransitionConfig era) (ConwayGenesis (EraCrypto era))
7472

73+
registerDRepsThenDelegs ::
74+
ConwayEraTransition era =>
75+
TransitionConfig era ->
76+
NewEpochState era ->
77+
NewEpochState era
78+
registerDRepsThenDelegs cfg =
79+
-- NOTE: The order of registration does not matter.
80+
registerDelegs cfg . registerInitialDReps cfg
81+
7582
instance Crypto c => EraTransition (ConwayEra c) where
7683
data TransitionConfig (ConwayEra c) = ConwayTransitionConfig
7784
{ ctcConwayGenesis :: !(ConwayGenesis c)
@@ -81,6 +88,10 @@ instance Crypto c => EraTransition (ConwayEra c) where
8188

8289
mkTransitionConfig = ConwayTransitionConfig
8390

91+
injectIntoTestState cfg =
92+
registerDRepsThenDelegs cfg
93+
. registerInitialFundsThenStaking cfg
94+
8495
tcPreviousEraConfigL =
8596
lens ctcBabbageTransitionConfig (\ctc pc -> ctc {ctcBabbageTransitionConfig = pc})
8697

eras/mary/impl/src/Cardano/Ledger/Mary/Transition.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ instance Crypto c => EraTransition (MaryEra c) where
2323

2424
mkTransitionConfig () = MaryTransitionConfig
2525

26+
injectIntoTestState = registerInitialFundsThenStaking
27+
2628
tcPreviousEraConfigL =
2729
lens mtcAllegraTransitionConfig (\mtc pc -> mtc {mtcAllegraTransitionConfig = pc})
2830

eras/shelley/impl/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* Rename `RewardAccount` fields `getRwdNetwork` and `getRwdCred` to `raNetwork` and `raCredential` respectively
99
* Deprecate `prAcnt` in favor of `prAccountState`
1010
* Deprecate `RewardAcnt` in favor of `RewardAccount`
11+
* Remove `registerInitialFunds` and `registerInitialStaking`
12+
* Add `registerInitialFundsThenStaking`
1113

1214
### `testlib`
1315

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ module Cardano.Ledger.Shelley.Transition (
2424
tcInitialStakingL,
2525
mkShelleyTransitionConfig,
2626
createInitialState,
27-
registerInitialFunds,
28-
registerInitialStaking,
27+
registerInitialFundsThenStaking,
2928
toShelleyTransitionConfigPairs,
3029
protectMainnet,
3130
protectMainnetLens,
@@ -85,6 +84,15 @@ class
8584
TransitionConfig (PreviousEra era) ->
8685
TransitionConfig era
8786

87+
injectIntoTestState ::
88+
-- | Extract data from the given transition configuration and store it in the given state.
89+
--
90+
-- /Warning/ - Should only be used in testing and benchmarking. Will result in an error
91+
-- when 'NetworkId' is set to 'Mainnet'.
92+
TransitionConfig era ->
93+
NewEpochState era ->
94+
NewEpochState era
95+
8896
-- | In case when a previous era is available, we should always be able to access
8997
-- `TransitionConfig` for the previous era, from within the current era's
9098
-- `TransitionConfig`
@@ -130,6 +138,16 @@ class
130138
tcNetworkIDG :: EraTransition era => SimpleGetter (TransitionConfig era) Network
131139
tcNetworkIDG = tcShelleyGenesisL . to sgNetworkId
132140

141+
registerInitialFundsThenStaking ::
142+
EraTransition era =>
143+
TransitionConfig era ->
144+
NewEpochState era ->
145+
NewEpochState era
146+
registerInitialFundsThenStaking cfg =
147+
-- We must first register the initial funds, because the stake
148+
-- information depends on it.
149+
registerInitialStaking cfg . registerInitialFunds cfg
150+
133151
instance Crypto c => EraTransition (ShelleyEra c) where
134152
newtype TransitionConfig (ShelleyEra c) = ShelleyTransitionConfig
135153
{ stcShelleyGenesis :: ShelleyGenesis c
@@ -139,6 +157,8 @@ instance Crypto c => EraTransition (ShelleyEra c) where
139157
mkTransitionConfig =
140158
error "Impossible: There is no EraTransition instance for ByronEra"
141159

160+
injectIntoTestState = registerInitialFundsThenStaking
161+
142162
tcPreviousEraConfigL = notSupportedInThisEraL
143163

144164
tcTranslationContextL =
@@ -228,6 +248,8 @@ toShelleyTransitionConfigPairs stc@(ShelleyTransitionConfig _) =
228248
--
229249
-- /Warning/ - Should only be used in testing and benchmarking. Will result in an error
230250
-- when NetworkId is set to Mainnet
251+
--
252+
-- This function does not register any initial funds or delegates.
231253
createInitialState ::
232254
forall era.
233255
(EraTransition era, HasCallStack) =>
@@ -278,7 +300,7 @@ createInitialState tc =
278300
initialEpochNo :: EpochNo
279301
initialEpochNo = 0
280302
initialUtxo :: UTxO era
281-
initialUtxo = genesisUTxO sg
303+
initialUtxo = mempty
282304
reserves :: Coin
283305
reserves = word64ToCoin (sgMaxLovelaceSupply sg) <-> coinBalance initialUtxo
284306

libs/cardano-ledger-api/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
* Deprecate `deserialiseRewardAcnt` in favor of `deserialiseRewardAccount`
1212
* Deprecate `serialiseRewardAcnt` in favor of `serialiseRewardAccount`
1313
* Deprecate `RewardAcnt` in favor of `RewardAccount`
14+
* Remove `registerInitialFunds`, `registerInitialStaking`, `registerInitialDReps`, and `registerDelegs` in favor of `injectIntoTestState`.
15+
* Change the semantics of `createInitialState` to not register any initial funds or delegates.
1416

1517
## 1.8.0.0
1618

libs/cardano-ledger-api/src/Cardano/Ledger/Api/Transition.hs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,19 @@ module Cardano.Ledger.Api.Transition (
2121
tcInitialFundsL,
2222
tcInitialStakingL,
2323
createInitialState,
24-
registerInitialFunds,
25-
registerInitialStaking,
26-
registerInitialDReps,
27-
registerDelegs,
24+
injectIntoTestState,
2825
) where
2926

3027
import Cardano.Ledger.Alonzo.Genesis (AlonzoGenesis (..))
3128
import Cardano.Ledger.Api.Era (LatestKnownEra)
3229
import Cardano.Ledger.Conway.Genesis (ConwayGenesis (..))
33-
import Cardano.Ledger.Conway.Transition (
34-
registerDelegs,
35-
registerInitialDReps,
36-
)
3730
import Cardano.Ledger.Crypto
3831
import Cardano.Ledger.Shelley.Genesis (ShelleyGenesis (..))
3932
import Cardano.Ledger.Shelley.Transition (
4033
EraTransition (..),
4134
TransitionConfig,
4235
createInitialState,
4336
mkShelleyTransitionConfig,
44-
registerInitialFunds,
45-
registerInitialStaking,
4637
tcInitialFundsL,
4738
tcInitialStakingL,
4839
)

0 commit comments

Comments
 (0)