Skip to content

Commit 0a8de81

Browse files
committed
Update StakePoolState delegs in shelley
1 parent de6e923 commit 0a8de81

File tree

2 files changed

+49
-7
lines changed
  • eras/shelley
    • impl/src/Cardano/Ledger/Shelley/Rules
    • test-suite/test/Test/Cardano/Ledger/Shelley/Examples

2 files changed

+49
-7
lines changed

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

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,17 +271,33 @@ delegationTransition = do
271271
let accountBalance = accountState ^. balanceAccountStateL
272272
guard (accountBalance /= mempty)
273273
Just $ fromCompact accountBalance
274-
-- (hk ∈ dom (rewards ds))
275-
isJust mAccountState ?! StakeKeyNotRegisteredDELEG cred
276274
failOnJust checkStakeKeyHasZeroRewardBalance StakeKeyNonZeroAccountBalanceDELEG
277-
pure $ certState & certDStateL . accountsL .~ accounts
275+
-- (hk ∈ dom (rewards ds))
276+
case mAccountState of
277+
Nothing -> do
278+
failBecause $ StakeKeyNotRegisteredDELEG cred
279+
pure certState
280+
Just accountState ->
281+
pure $
282+
certState
283+
& certDStateL . accountsL .~ accounts
284+
& certPStateL
285+
%~ unDelegStakePool cred (accountState ^. stakePoolDelegationAccountStateL) Nothing
278286
DelegStakeTxCert cred stakePool -> do
279287
-- note that pattern match is used instead of cwitness and dpool, as in the spec
280288
-- (hk ∈ dom (rewards ds))
281-
isAccountRegistered cred (ds ^. accountsL) ?! StakeDelegationImpossibleDELEG cred
282-
pure $
283-
certState
284-
& certDStateL . accountsL %~ adjustAccountState (stakePoolDelegationAccountStateL ?~ stakePool) cred
289+
case lookupAccountState cred (ds ^. accountsL) of
290+
Nothing -> do
291+
failBecause $ StakeDelegationImpossibleDELEG cred
292+
pure certState
293+
Just currentAccount ->
294+
pure $
295+
certState
296+
& certDStateL . accountsL %~ adjustAccountState (stakePoolDelegationAccountStateL ?~ stakePool) cred
297+
& certPStateL %~ \ps ->
298+
ps
299+
& unDelegStakePool cred (currentAccount ^. stakePoolDelegationAccountStateL) (Just stakePool)
300+
& psStakePoolsL %~ Map.adjust (spsDelegsL %~ Set.insert cred) stakePool
285301
GenesisDelegTxCert gkh vkh vrf -> do
286302
sp <- liftSTS $ asks stabilityWindow
287303
-- note that pattern match is used instead of genesisDeleg, as in the spec
@@ -404,3 +420,19 @@ updateReservesAndTreasury targetPot combinedMap available certState = do
404420
case targetPot of
405421
ReservesMIR -> certState & certDStateL . dsIRewardsL . iRReservesL .~ combinedMap
406422
TreasuryMIR -> certState & certDStateL . dsIRewardsL . iRTreasuryL .~ combinedMap
423+
424+
unDelegStakePool ::
425+
Credential 'Staking ->
426+
Maybe (KeyHash 'StakePool) ->
427+
Maybe (KeyHash 'StakePool) ->
428+
PState era ->
429+
PState era
430+
unDelegStakePool stakeCred mCurStakePool mNewPool =
431+
maybe
432+
id
433+
(\oldPool -> psStakePoolsL %~ Map.adjust (spsDelegsL %~ Set.delete stakeCred) oldPool)
434+
(mCurStakePool >>= stakePoolToUnDeleg)
435+
where
436+
stakePoolToUnDeleg oldPool
437+
| Just oldPool /= mNewPool = Just oldPool
438+
| otherwise = Nothing

eras/shelley/test-suite/test/Test/Cardano/Ledger/Shelley/Examples/Combinators.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ import Data.Foldable (fold)
9595
import Data.Map.Strict (Map)
9696
import qualified Data.Map.Strict as Map
9797
import Data.Maybe (fromJust, maybeToList)
98+
import qualified Data.Set as Set
9899
import Data.Word (Word64)
99100
import GHC.Stack (HasCallStack)
100101
import Lens.Micro
@@ -211,12 +212,19 @@ deregStakeCred cred cs = cs {chainNes = nes}
211212
chainNes cs
212213
& nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL .~ accounts'
213214
& nesEsL . esLStateL . lsUTxOStateL . utxosDepositedL %~ (<-> refund)
215+
& nesEsL . esLStateL . lsCertStateL . certPStateL %~ adjustPState
214216
accounts =
215217
chainNes cs
216218
^. nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL
217219
(mAccountState, accounts') =
218220
unregisterShelleyAccount cred accounts
219221
refund = fromCompact (fromJust mAccountState ^. depositAccountStateL)
222+
currentDeleg = Map.lookup cred (accounts ^. accountsMapL) >>= (^. stakePoolDelegationAccountStateL)
223+
adjustPState =
224+
maybe
225+
id
226+
(\oldPool -> psStakePoolsL %~ Map.adjust (spsDelegsL %~ Set.delete cred) oldPool)
227+
currentDeleg
220228

221229
-- | = New Delegation
222230
--
@@ -234,6 +242,8 @@ delegation cred poolId cs = cs {chainNes = nes}
234242
chainNes cs
235243
& nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL . accountsMapL
236244
%~ Map.adjust (stakePoolDelegationAccountStateL .~ Just poolId) cred
245+
& nesEsL . esLStateL . lsCertStateL . certPStateL . psStakePoolsL
246+
%~ Map.adjust (spsDelegsL %~ Set.insert cred) poolId
237247

238248
-- | Register a stake pool.
239249
regPool ::

0 commit comments

Comments
 (0)