Skip to content

Commit de6e923

Browse files
committed
Change state of shelley DelegRule to CertState
1 parent de8b54b commit de6e923

File tree

8 files changed

+111
-92
lines changed

8 files changed

+111
-92
lines changed

eras/shelley/impl/CHANGELOG.md

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

33
## 1.17.1.0
44

5+
* Change state of `ShelleyDELEG` rule from `DState` to `CertState`
6+
* Add `EraCertState` constraint to `STS` instance for `ShelleyDELEG`
57
* Added `Generic` instance to `ShelleyTxOut`
68

79
### `testlib`

eras/shelley/impl/src/Cardano/Ledger/Shelley/Rules/Deleg.hs

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,15 @@ newtype ShelleyDelegEvent era = DelegNewEpoch EpochNo
131131
instance NFData (ShelleyDelegEvent era)
132132

133133
instance
134-
(EraPParams era, ShelleyEraAccounts era, ShelleyEraTxCert era, AtMostEra "Babbage" era) =>
134+
( EraCertState era
135+
, EraPParams era
136+
, ShelleyEraAccounts era
137+
, ShelleyEraTxCert era
138+
, AtMostEra "Babbage" era
139+
) =>
135140
STS (ShelleyDELEG era)
136141
where
137-
type State (ShelleyDELEG era) = DState era
142+
type State (ShelleyDELEG era) = CertState era
138143
type Signal (ShelleyDELEG era) = TxCert era
139144
type Environment (ShelleyDELEG era) = DelegEnv era
140145
type BaseM (ShelleyDELEG era) = ShelleyBase
@@ -242,17 +247,23 @@ instance
242247
k -> invalidKey k
243248

244249
delegationTransition ::
245-
(ShelleyEraAccounts era, ShelleyEraTxCert era, EraPParams era, AtMostEra "Babbage" era) =>
250+
( EraCertState era
251+
, ShelleyEraAccounts era
252+
, ShelleyEraTxCert era
253+
, EraPParams era
254+
, AtMostEra "Babbage" era
255+
) =>
246256
TransitionRule (ShelleyDELEG era)
247257
delegationTransition = do
248-
TRC (DelegEnv slot epochNo ptr chainAccountState pp, ds, c) <- judgmentContext
258+
TRC (DelegEnv slot epochNo ptr chainAccountState pp, certState, c) <- judgmentContext
249259
let pv = pp ^. ppProtocolVersionL
260+
ds = certState ^. certDStateL
250261
case c of
251262
RegTxCert cred -> do
252263
-- (hk ∉ dom (rewards ds))
253264
not (isAccountRegistered cred (ds ^. accountsL)) ?! StakeKeyAlreadyRegisteredDELEG cred
254265
let compactDeposit = compactCoinOrError (pp ^. ppKeyDepositL)
255-
pure (ds & accountsL %~ registerShelleyAccount cred ptr compactDeposit Nothing)
266+
pure $ certState & certDStateL . accountsL %~ registerShelleyAccount cred ptr compactDeposit Nothing
256267
UnRegTxCert cred -> do
257268
let !(mAccountState, !accounts) = unregisterShelleyAccount cred (ds ^. accountsL)
258269
checkStakeKeyHasZeroRewardBalance = do
@@ -263,13 +274,14 @@ delegationTransition = do
263274
-- (hk ∈ dom (rewards ds))
264275
isJust mAccountState ?! StakeKeyNotRegisteredDELEG cred
265276
failOnJust checkStakeKeyHasZeroRewardBalance StakeKeyNonZeroAccountBalanceDELEG
266-
pure $ ds & accountsL .~ accounts
277+
pure $ certState & certDStateL . accountsL .~ accounts
267278
DelegStakeTxCert cred stakePool -> do
268279
-- note that pattern match is used instead of cwitness and dpool, as in the spec
269280
-- (hk ∈ dom (rewards ds))
270281
isAccountRegistered cred (ds ^. accountsL) ?! StakeDelegationImpossibleDELEG cred
271282
pure $
272-
ds & accountsL %~ adjustAccountState (stakePoolDelegationAccountStateL ?~ stakePool) cred
283+
certState
284+
& certDStateL . accountsL %~ adjustAccountState (stakePoolDelegationAccountStateL ?~ stakePool) cred
273285
GenesisDelegTxCert gkh vkh vrf -> do
274286
sp <- liftSTS $ asks stabilityWindow
275287
-- note that pattern match is used instead of genesisDeleg, as in the spec
@@ -294,13 +306,12 @@ delegationTransition = do
294306
?! DuplicateGenesisVRFDELEG vrf
295307

296308
pure $
297-
ds
298-
{ dsFutureGenDelegs =
299-
eval (dsFutureGenDelegs ds singleton (FutureGenDeleg s' gkh) (GenDelegPair vkh vrf))
300-
}
309+
certState
310+
& certDStateL . dsFutureGenDelegsL
311+
.~ eval (dsFutureGenDelegs ds singleton (FutureGenDeleg s' gkh) (GenDelegPair vkh vrf))
301312
RegPoolTxCert _ -> do
302313
failBecause WrongCertificateTypeDELEG -- this always fails
303-
pure ds
314+
pure certState
304315
_ | Just (MIRCert targetPot mirTarget) <- getMirTxCert c -> do
305316
checkSlotNotTooLate slot epochNo
306317
case mirTarget of
@@ -327,46 +338,39 @@ delegationTransition = do
327338
else do
328339
all (>= mempty) credCoinMap ?! MIRNegativesNotCurrentlyAllowed
329340
pure (Map.union credCoinMap' instantaneousRewards, potAmount)
330-
updateReservesAndTreasury targetPot combinedMap available ds
341+
updateReservesAndTreasury targetPot combinedMap available certState
331342
SendToOppositePotMIR coin ->
332343
if hardforkAlonzoAllowMIRTransfer pv
333344
then do
334345
let available = availableAfterMIR targetPot chainAccountState (dsIRewards ds)
335346
coin >= mempty ?! MIRNegativeTransfer targetPot coin
336347
coin <= available ?! InsufficientForTransferDELEG targetPot (Mismatch coin available)
337-
338-
let ir = dsIRewards ds
339-
dr = deltaReserves ir
340-
dt = deltaTreasury ir
341348
case targetPot of
342349
ReservesMIR ->
343350
pure $
344-
ds
345-
{ dsIRewards =
346-
ir
347-
{ deltaReserves = dr <> invert (toDeltaCoin coin)
348-
, deltaTreasury = dt <> toDeltaCoin coin
349-
}
350-
}
351+
certState
352+
& certDStateL . dsIRewardsL . iRDeltaReservesL %~ (<> invert (toDeltaCoin coin))
353+
& certDStateL . dsIRewardsL . iRDeltaTreasuryL %~ (<> toDeltaCoin coin)
351354
TreasuryMIR ->
352355
pure $
353-
ds
354-
{ dsIRewards =
355-
ir
356-
{ deltaReserves = dr <> toDeltaCoin coin
357-
, deltaTreasury = dt <> invert (toDeltaCoin coin)
358-
}
359-
}
356+
certState
357+
& certDStateL . dsIRewardsL . iRDeltaReservesL %~ (<> toDeltaCoin coin)
358+
& certDStateL . dsIRewardsL . iRDeltaTreasuryL %~ (<> invert (toDeltaCoin coin))
360359
else do
361360
failBecause MIRTransferNotCurrentlyAllowed
362-
pure ds
361+
pure certState
363362
_ -> do
364363
-- The impossible case
365364
failBecause WrongCertificateTypeDELEG -- this always fails
366-
pure ds
365+
pure certState
367366

368367
checkSlotNotTooLate ::
369-
(ShelleyEraAccounts era, ShelleyEraTxCert era, EraPParams era, AtMostEra "Babbage" era) =>
368+
( EraCertState era
369+
, ShelleyEraAccounts era
370+
, ShelleyEraTxCert era
371+
, EraPParams era
372+
, AtMostEra "Babbage" era
373+
) =>
370374
SlotNo ->
371375
EpochNo ->
372376
Rule (ShelleyDELEG era) 'Transition ()
@@ -380,12 +384,13 @@ checkSlotNotTooLate slot curEpochNo = do
380384
slot < tooLate ?! MIRCertificateTooLateinEpochDELEG (Mismatch slot tooLate)
381385

382386
updateReservesAndTreasury ::
387+
EraCertState era =>
383388
MIRPot ->
384389
Map.Map (Credential 'Staking) Coin ->
385390
Coin ->
386-
DState era ->
387-
Rule (ShelleyDELEG era) 'Transition (DState era)
388-
updateReservesAndTreasury targetPot combinedMap available ds = do
391+
CertState era ->
392+
Rule (ShelleyDELEG era) 'Transition (CertState era)
393+
updateReservesAndTreasury targetPot combinedMap available certState = do
389394
let requiredForRewards = fold combinedMap
390395
requiredForRewards
391396
<= available
@@ -397,5 +402,5 @@ updateReservesAndTreasury targetPot combinedMap available ds = do
397402
}
398403
pure $
399404
case targetPot of
400-
ReservesMIR -> ds {dsIRewards = (dsIRewards ds) {iRReserves = combinedMap}}
401-
TreasuryMIR -> ds {dsIRewards = (dsIRewards ds) {iRTreasury = combinedMap}}
405+
ReservesMIR -> certState & certDStateL . dsIRewardsL . iRReservesL .~ combinedMap
406+
TreasuryMIR -> certState & certDStateL . dsIRewardsL . iRTreasuryL .~ combinedMap

eras/shelley/impl/src/Cardano/Ledger/Shelley/Rules/Delpl.hs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ instance
120120
, EraCertState era
121121
, Embed (EraRule "DELEG" era) (ShelleyDELPL era)
122122
, Environment (EraRule "DELEG" era) ~ DelegEnv era
123-
, State (EraRule "DELEG" era) ~ DState era
123+
, State (EraRule "DELEG" era) ~ CertState era
124124
, Embed (EraRule "POOL" era) (ShelleyDELPL era)
125125
, Environment (EraRule "POOL" era) ~ PoolEnv era
126126
, State (EraRule "POOL" era) ~ PState era
@@ -183,7 +183,7 @@ delplTransition ::
183183
forall era.
184184
( Embed (EraRule "DELEG" era) (ShelleyDELPL era)
185185
, Environment (EraRule "DELEG" era) ~ DelegEnv era
186-
, State (EraRule "DELEG" era) ~ DState era
186+
, State (EraRule "DELEG" era) ~ CertState era
187187
, State (EraRule "POOL" era) ~ PState era
188188
, Signal (EraRule "DELEG" era) ~ TxCert era
189189
, Embed (EraRule "POOL" era) (ShelleyDELPL era)
@@ -201,20 +201,14 @@ delplTransition = do
201201
trans @(EraRule "POOL" era) $ TRC (PoolEnv eNo pp, d ^. certPStateL, poolCert)
202202
pure $ d & certPStateL .~ ps
203203
ShelleyTxCertGenesisDeleg GenesisDelegCert {} -> do
204-
ds <-
205-
trans @(EraRule "DELEG" era) $
206-
TRC (DelegEnv slot eNo ptr chainAccountState pp, d ^. certDStateL, c)
207-
pure $ d & certDStateL .~ ds
204+
trans @(EraRule "DELEG" era) $
205+
TRC (DelegEnv slot eNo ptr chainAccountState pp, d, c)
208206
ShelleyTxCertDelegCert _ -> do
209-
ds <-
210-
trans @(EraRule "DELEG" era) $
211-
TRC (DelegEnv slot eNo ptr chainAccountState pp, d ^. certDStateL, c)
212-
pure $ d & certDStateL .~ ds
207+
trans @(EraRule "DELEG" era) $
208+
TRC (DelegEnv slot eNo ptr chainAccountState pp, d, c)
213209
ShelleyTxCertMir _ -> do
214-
ds <-
215-
trans @(EraRule "DELEG" era) $
216-
TRC (DelegEnv slot eNo ptr chainAccountState pp, d ^. certDStateL, c)
217-
pure $ d & certDStateL .~ ds
210+
trans @(EraRule "DELEG" era) $
211+
TRC (DelegEnv slot eNo ptr chainAccountState pp, d, c)
218212

219213
instance
220214
( Era era
@@ -230,6 +224,7 @@ instance
230224
instance
231225
( ShelleyEraAccounts era
232226
, ShelleyEraTxCert era
227+
, EraCertState era
233228
, EraPParams era
234229
, AtMostEra "Babbage" era
235230
, PredicateFailure (EraRule "DELEG" era) ~ ShelleyDelegPredFailure era

eras/shelley/test-suite/src/Test/Cardano/Ledger/Shelley/Rules/Deleg.hs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ tests =
8989

9090
-- | Check stake key registration
9191
keyRegistration ::
92-
(EraAccounts era, ShelleyEraTxCert era) =>
92+
(EraCertState era, ShelleyEraTxCert era) =>
9393
SourceSignalTarget (ShelleyDELEG era) ->
9494
Property
9595
keyRegistration
@@ -100,18 +100,18 @@ keyRegistration
100100
conjoin
101101
[ counterexample
102102
"a newly registered key should have a reward account"
103-
(isAccountRegistered cred (targetSt ^. accountsL))
103+
(isAccountRegistered cred (targetSt ^. certDStateL . accountsL))
104104
, counterexample
105105
"a newly registered key should have a reward account with 0 balance"
106-
( ((^. balanceAccountStateL) <$> lookupAccountState cred (targetSt ^. accountsL))
106+
( ((^. balanceAccountStateL) <$> lookupAccountState cred (targetSt ^. certDStateL . accountsL))
107107
=== Just mempty
108108
)
109109
]
110110
keyRegistration _ = property ()
111111

112112
-- | Check stake key de-registration
113113
keyDeRegistration ::
114-
(EraAccounts era, ShelleyEraTxCert era) =>
114+
(EraCertState era, ShelleyEraTxCert era) =>
115115
SourceSignalTarget (ShelleyDELEG era) ->
116116
Property
117117
keyDeRegistration
@@ -122,16 +122,16 @@ keyDeRegistration
122122
conjoin
123123
[ counterexample
124124
"a deregistered stake key should no longer be in the rewards mapping"
125-
(not (isAccountRegistered cred (targetSt ^. accountsL)))
125+
(not (isAccountRegistered cred (targetSt ^. certDStateL . accountsL)))
126126
, counterexample
127127
"a deregistered stake key should no longer be in the delegations mapping"
128-
(isNothing (lookupStakePoolDelegation cred (targetSt ^. accountsL)))
128+
(isNothing (lookupStakePoolDelegation cred (targetSt ^. certDStateL . accountsL)))
129129
]
130130
keyDeRegistration _ = property ()
131131

132132
-- | Check stake key delegation
133133
keyDelegation ::
134-
(EraAccounts era, ShelleyEraTxCert era) =>
134+
(EraCertState era, ShelleyEraTxCert era) =>
135135
SourceSignalTarget (ShelleyDELEG era) ->
136136
Property
137137
keyDelegation
@@ -142,21 +142,21 @@ keyDelegation
142142
conjoin
143143
[ counterexample
144144
"a delegated key should have a reward account"
145-
(isAccountRegistered stakeCred (targetSt ^. accountsL))
145+
(isAccountRegistered stakeCred (targetSt ^. certDStateL . accountsL))
146146
, counterexample
147147
"a registered stake credential should be delegated"
148-
(lookupStakePoolDelegation stakeCred (targetSt ^. accountsL) === Just poolId)
148+
(lookupStakePoolDelegation stakeCred (targetSt ^. certDStateL . accountsL) === Just poolId)
149149
]
150150
keyDelegation _ = property ()
151151

152152
-- | Check that the sum of balances does not change and that each element
153153
-- that is either removed or added has a zero balance.
154-
balancesSumInvariant :: EraAccounts era => SourceSignalTarget (ShelleyDELEG era) -> Property
154+
balancesSumInvariant :: EraCertState era => SourceSignalTarget (ShelleyDELEG era) -> Property
155155
balancesSumInvariant
156156
SourceSignalTarget {source, target} =
157157
let accountsBalances ds = Map.map (^. balanceAccountStateL) (ds ^. accountsL . accountsMapL)
158-
sourceBalances = accountsBalances source
159-
targetBalances = accountsBalances target
158+
sourceBalances = accountsBalances (source ^. certDStateL)
159+
targetBalances = accountsBalances (target ^. certDStateL)
160160
balancesSum = F.foldl' (<>) mempty
161161
in conjoin
162162
[ counterexample
@@ -171,7 +171,7 @@ balancesSumInvariant
171171
]
172172

173173
checkInstantaneousRewards ::
174-
(EraPParams era, ShelleyEraTxCert era, AtMostEra "Babbage" era) =>
174+
(EraPParams era, EraCertState era, ShelleyEraTxCert era, AtMostEra "Babbage" era) =>
175175
DelegEnv era ->
176176
SourceSignalTarget (ShelleyDELEG era) ->
177177
Property
@@ -183,36 +183,36 @@ checkInstantaneousRewards
183183
conjoin
184184
[ counterexample
185185
"a ReservesMIR certificate should add all entries to the `irwd` mapping"
186-
(Map.keysSet irwd `Set.isSubsetOf` Map.keysSet (iRReserves $ dsIRewards target))
186+
(Map.keysSet irwd `Set.isSubsetOf` Map.keysSet (iRReserves $ dsIRewards (target ^. certDStateL)))
187187
, counterexample
188188
"a ReservesMIR certificate should add the total value to the `irwd` map, overwriting any existing entries"
189189
( if hardforkAlonzoAllowMIRTransfer . view ppProtocolVersionL $ ppDE denv
190190
then -- In the Alonzo era, repeated fields are added
191-
fold (iRReserves $ dsIRewards source)
191+
fold (iRReserves $ dsIRewards (source ^. certDStateL))
192192
`addDeltaCoin` fold irwd
193-
== fold (iRReserves $ dsIRewards target)
193+
== fold (iRReserves $ dsIRewards (target ^. certDStateL))
194194
else -- Prior to the Alonzo era, repeated fields overridden
195-
fold (iRReserves (dsIRewards source) Map.\\ irwd)
195+
fold (iRReserves (dsIRewards (source ^. certDStateL)) Map.\\ irwd)
196196
`addDeltaCoin` fold irwd
197-
== fold (iRReserves $ dsIRewards target)
197+
== fold (iRReserves $ dsIRewards (target ^. certDStateL))
198198
)
199199
]
200200
MirTxCert (MIRCert TreasuryMIR (StakeAddressesMIR irwd)) ->
201201
conjoin
202202
[ counterexample
203203
"a TreasuryMIR certificate should add all entries to the `irwd` mapping"
204-
(Map.keysSet irwd `Set.isSubsetOf` Map.keysSet (iRTreasury $ dsIRewards target))
204+
(Map.keysSet irwd `Set.isSubsetOf` Map.keysSet (iRTreasury $ dsIRewards (target ^. certDStateL)))
205205
, counterexample
206206
"a TreasuryMIR certificate should add* the total value to the `irwd` map"
207207
( if hardforkAlonzoAllowMIRTransfer . view ppProtocolVersionL . ppDE $ denv
208208
then -- In the Alonzo era, repeated fields are added
209-
fold (iRTreasury $ dsIRewards source)
209+
fold (iRTreasury $ dsIRewards (source ^. certDStateL))
210210
`addDeltaCoin` fold irwd
211-
== fold (iRTreasury $ dsIRewards target)
211+
== fold (iRTreasury $ dsIRewards (target ^. certDStateL))
212212
else -- Prior to the Alonzo era, repeated fields overridden
213-
fold (iRTreasury (dsIRewards source) Map.\\ irwd)
213+
fold (iRTreasury (dsIRewards (source ^. certDStateL)) Map.\\ irwd)
214214
`addDeltaCoin` fold irwd
215-
== fold (iRTreasury $ dsIRewards target)
215+
== fold (iRTreasury $ dsIRewards (target ^. certDStateL))
216216
)
217217
]
218218
_ -> property ()

eras/shelley/test-suite/src/Test/Cardano/Ledger/Shelley/Rules/TestChain.hs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ delegTraceFromBlock ::
220220
delegTraceFromBlock chainSt block =
221221
( delegEnv
222222
, runShelleyBase $
223-
Trace.closure @(ShelleyDELEG era) delegEnv delegSt0 blockCerts
223+
Trace.closure @(ShelleyDELEG era) delegEnv (ledgerSt0 ^. lsCertStateL) blockCerts
224224
)
225225
where
226226
(_tickedChainSt, ledgerEnv, ledgerSt0, txs) = ledgerTraceBase chainSt block
@@ -231,8 +231,6 @@ delegTraceFromBlock chainSt block =
231231
dummyCertIx = minBound
232232
ptr = Ptr (SlotNo32 (fromIntegral slot64)) txIx dummyCertIx
233233
in DelegEnv slot (epochFromSlotNo slot) ptr reserves pp
234-
delegSt0 =
235-
ledgerSt0 ^. lsCertStateL . certDStateL
236234
delegCert (RegTxCert _) = True
237235
delegCert (UnRegTxCert _) = True
238236
delegCert (DelegStakeTxCert _ _) = True

0 commit comments

Comments
 (0)