@@ -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
0 commit comments