@@ -67,6 +67,7 @@ import Control.State.Transition (
6767 State ,
6868 TRC (TRC ),
6969 TransitionRule ,
70+ failBecause ,
7071 failOnJust ,
7172 judgmentContext ,
7273 transitionRules ,
@@ -204,7 +205,7 @@ conwayDelegTransition = do
204205 checkStakeKeyIsRegistered stakeCred = do
205206 let mAccountState = lookupAccountState stakeCred accounts
206207 isJust mAccountState ?! StakeKeyNotRegisteredDELEG stakeCred
207- pure $ mAccountState >>= accountStateDelegatee
208+ pure mAccountState
208209 checkStakeDelegateeRegistered =
209210 let checkPoolRegistered targetPool =
210211 targetPool `Map.member` pools ?! DelegateeStakePoolNotRegisteredDELEG targetPool
@@ -253,18 +254,23 @@ conwayDelegTransition = do
253254 guard (balanceCompact /= mempty )
254255 Just $ fromCompact balanceCompact
255256 failOnJust checkInvalidRefund id
256- isJust mAccountState ?! StakeKeyNotRegisteredDELEG stakeCred
257257 failOnJust checkStakeKeyHasZeroRewardBalance StakeKeyHasNonZeroRewardAccountBalanceDELEG
258- pure $
259- certState
260- & certDStateL . accountsL .~ newAccounts
261- & certVStateL %~ unDelegDRep stakeCred mCurDelegatee
262- & certPStateL %~ unDelegStakePool stakeCred mCurDelegatee Nothing
258+ case mAccountState of
259+ Nothing ->
260+ do
261+ failBecause $ StakeKeyNotRegisteredDELEG stakeCred
262+ pure certState
263+ Just accountState ->
264+ pure $
265+ certState
266+ & certDStateL . accountsL .~ newAccounts
267+ & certVStateL %~ unDelegDRep stakeCred mCurDelegatee
268+ & certPStateL %~ unDelegReDelegStakePool stakeCred accountState Nothing
263269 ConwayDelegCert stakeCred delegatee -> do
264- mCurDelegatee <- checkStakeKeyIsRegistered stakeCred
270+ mAccountState <- checkStakeKeyIsRegistered stakeCred
265271 checkStakeDelegateeRegistered delegatee
266272 pure $
267- processDelegationInternal (pvMajor pv < natVersion @ 10 ) stakeCred mCurDelegatee delegatee certState
273+ processDelegationInternal (pvMajor pv < natVersion @ 10 ) stakeCred mAccountState delegatee certState
268274 ConwayRegDelegCert stakeCred delegatee deposit -> do
269275 checkDepositAgainstPParams deposit
270276 checkStakeKeyNotRegistered stakeCred
@@ -287,9 +293,8 @@ processDelegation ::
287293 CertState era
288294processDelegation stakeCred newDelegatee ! certState = certState'
289295 where
290- ! certState' = processDelegationInternal False stakeCred mCurDelegatee newDelegatee certState
296+ ! certState' = processDelegationInternal False stakeCred mAccountState newDelegatee certState
291297 mAccountState = Map. lookup stakeCred (certState ^. certDStateL . accountsL . accountsMapL)
292- mCurDelegatee = mAccountState >>= accountStateDelegatee
293298
294299-- | Same as `processDelegation`, except it expects the current delegation supplied as an
295300-- argument, because in ledger rules we already have it readily available.
@@ -299,23 +304,27 @@ processDelegationInternal ::
299304 Bool ->
300305 -- | Delegator
301306 Credential 'Staking ->
302- -- | Current delegatee for the above stake credential that needs to be cleaned up.
303- Maybe Delegatee ->
307+ -- | Account state for the above stake credential
308+ Maybe ( AccountState era ) ->
304309 -- | New delegatee
305310 Delegatee ->
306311 CertState era ->
307312 CertState era
308- processDelegationInternal preserveIncorrectDelegation stakeCred mCurDelegatee newDelegatee =
313+ processDelegationInternal preserveIncorrectDelegation stakeCred mAccountState newDelegatee =
309314 case newDelegatee of
310315 DelegStake sPool -> delegStake sPool
311316 DelegVote dRep -> delegVote dRep
312317 DelegStakeVote sPool dRep -> delegVote dRep . delegStake sPool
313318 where
319+ mCurDelegatee = mAccountState >>= accountStateDelegatee
314320 delegStake stakePool cState =
315321 cState
316322 & certDStateL . accountsL
317323 %~ adjustAccountState (stakePoolDelegationAccountStateL ?~ stakePool) stakeCred
318- & certPStateL %~ adjustPState stakePool
324+ & maybe
325+ (certPStateL . psStakePoolsL %~ Map. adjust (spsDelegatorsL %~ Set. insert stakeCred) stakePool)
326+ (\ accountState -> certPStateL %~ unDelegReDelegStakePool stakeCred accountState (Just stakePool))
327+ mAccountState
319328 delegVote dRep cState =
320329 let cState' =
321330 cState
@@ -331,26 +340,6 @@ processDelegationInternal preserveIncorrectDelegation stakeCred mCurDelegatee ne
331340 let dRepState' = dRepState {drepDelegs = Set. insert stakeCred (drepDelegs dRepState)}
332341 in cState' & certVStateL . vsDRepsL .~ Map. insert targetDRep dRepState' dReps
333342 _ -> cState'
334- adjustPState newPool =
335- (psStakePoolsL %~ Map. adjust (spsDelegatorsL %~ Set. insert stakeCred) newPool)
336- . unDelegStakePool stakeCred mCurDelegatee (Just newPool)
337-
338- unDelegStakePool ::
339- Credential 'Staking ->
340- Maybe Delegatee ->
341- Maybe (KeyHash 'StakePool) ->
342- PState era ->
343- PState era
344- unDelegStakePool stakeCred mCurDelegatee mNewPool =
345- maybe
346- id
347- (\ oldPool -> psStakePoolsL %~ Map. adjust (spsDelegatorsL %~ Set. delete stakeCred) oldPool)
348- (mCurDelegatee >>= stakePoolToUnDeleg)
349- where
350- stakePoolToUnDeleg = \ case
351- DelegStake oldPool | Just oldPool /= mNewPool -> Just oldPool
352- DelegStakeVote oldPool _ | Just oldPool /= mNewPool -> Just oldPool
353- _ -> Nothing
354343
355344unDelegDRep ::
356345 Credential 'Staking ->
0 commit comments