@@ -54,6 +54,7 @@ import Cardano.Ledger.Conway.TxCert (
5454 Delegatee (DelegStake , DelegStakeVote , DelegVote ),
5555 )
5656import Cardano.Ledger.Credential (Credential )
57+ import Cardano.Ledger.DRep
5758import Control.DeepSeq (NFData )
5859import Control.Monad (forM_ , guard , unless )
5960import Control.State.Transition (
@@ -66,6 +67,7 @@ import Control.State.Transition (
6667 State ,
6768 TRC (TRC ),
6869 TransitionRule ,
70+ failBecause ,
6971 failOnJust ,
7072 judgmentContext ,
7173 transitionRules ,
@@ -200,10 +202,6 @@ conwayDelegTransition = do
200202 else IncorrectDepositDELEG deposit
201203 checkStakeKeyNotRegistered stakeCred =
202204 not (isAccountRegistered stakeCred accounts) ?! StakeKeyRegisteredDELEG stakeCred
203- checkStakeKeyIsRegistered stakeCred = do
204- let mAccountState = lookupAccountState stakeCred accounts
205- isJust mAccountState ?! StakeKeyNotRegisteredDELEG stakeCred
206- pure $ mAccountState >>= accountStateDelegatee
207205 checkStakeDelegateeRegistered =
208206 let checkPoolRegistered targetPool =
209207 targetPool `Map.member` pools ?! DelegateeStakePoolNotRegisteredDELEG targetPool
@@ -229,7 +227,6 @@ conwayDelegTransition = do
229227 %~ registerConwayAccount stakeCred ppKeyDepositCompact Nothing
230228 ConwayUnRegCert stakeCred sMayRefund -> do
231229 let (mAccountState, newAccounts) = unregisterConwayAccount stakeCred accounts
232- mCurDelegatee = mAccountState >>= accountStateDelegatee
233230 checkInvalidRefund = do
234231 SJust suppliedRefund <- Just sMayRefund
235232 -- we don't want to report invalid refund when stake credential is not registered:
@@ -252,16 +249,23 @@ conwayDelegTransition = do
252249 guard (balanceCompact /= mempty )
253250 Just $ fromCompact balanceCompact
254251 failOnJust checkInvalidRefund id
255- isJust mAccountState ?! StakeKeyNotRegisteredDELEG stakeCred
256252 failOnJust checkStakeKeyHasZeroRewardBalance StakeKeyHasNonZeroRewardAccountBalanceDELEG
257- pure $
258- processDRepUnDelegation stakeCred mCurDelegatee $
259- certState & certDStateL . accountsL .~ newAccounts
253+ case mAccountState of
254+ Nothing -> do
255+ failBecause $ StakeKeyNotRegisteredDELEG stakeCred
256+ pure certState
257+ Just accountState ->
258+ pure $
259+ certState
260+ & certDStateL . accountsL .~ newAccounts
261+ & certVStateL %~ unDelegReDelegDRep stakeCred accountState Nothing
262+ & certPStateL %~ unDelegReDelegStakePool stakeCred accountState Nothing
260263 ConwayDelegCert stakeCred delegatee -> do
261- mCurDelegatee <- checkStakeKeyIsRegistered stakeCred
264+ let mAccountState = lookupAccountState stakeCred accounts
265+ isJust mAccountState ?! StakeKeyNotRegisteredDELEG stakeCred
262266 checkStakeDelegateeRegistered delegatee
263267 pure $
264- processDelegationInternal (pvMajor pv < natVersion @ 10 ) stakeCred mCurDelegatee delegatee certState
268+ processDelegationInternal (pvMajor pv < natVersion @ 10 ) stakeCred mAccountState delegatee certState
265269 ConwayRegDelegCert stakeCred delegatee deposit -> do
266270 checkDepositAgainstPParams deposit
267271 checkStakeKeyNotRegistered stakeCred
@@ -284,9 +288,8 @@ processDelegation ::
284288 CertState era
285289processDelegation stakeCred newDelegatee ! certState = certState'
286290 where
287- ! certState' = processDelegationInternal False stakeCred mCurDelegatee newDelegatee certState
291+ ! certState' = processDelegationInternal False stakeCred mAccountState newDelegatee certState
288292 mAccountState = Map. lookup stakeCred (certState ^. certDStateL . accountsL . accountsMapL)
289- mCurDelegatee = mAccountState >>= accountStateDelegatee
290293
291294-- | Same as `processDelegation`, except it expects the current delegation supplied as an
292295-- argument, because in ledger rules we already have it readily available.
@@ -296,13 +299,13 @@ processDelegationInternal ::
296299 Bool ->
297300 -- | Delegator
298301 Credential 'Staking ->
299- -- | Current delegatee for the above stake credential that needs to be cleaned up.
300- Maybe Delegatee ->
302+ -- | Account state for the above stake credential
303+ Maybe ( AccountState era ) ->
301304 -- | New delegatee
302305 Delegatee ->
303306 CertState era ->
304307 CertState era
305- processDelegationInternal preserveIncorrectDelegation stakeCred mCurDelegatee newDelegatee =
308+ processDelegationInternal preserveIncorrectDelegation stakeCred mAccountState newDelegatee =
306309 case newDelegatee of
307310 DelegStake sPool -> delegStake sPool
308311 DelegVote dRep -> delegVote dRep
@@ -312,37 +315,18 @@ processDelegationInternal preserveIncorrectDelegation stakeCred mCurDelegatee ne
312315 cState
313316 & certDStateL . accountsL
314317 %~ adjustAccountState (stakePoolDelegationAccountStateL ?~ stakePool) stakeCred
318+ & maybe
319+ (certPStateL . psStakePoolsL %~ Map. adjust (spsDelegatorsL %~ Set. insert stakeCred) stakePool)
320+ (\ accountState -> certPStateL %~ unDelegReDelegStakePool stakeCred accountState (Just stakePool))
321+ mAccountState
315322 delegVote dRep cState =
316- let cState' =
317- processDRepUnDelegation stakeCred mCurDelegatee cState
318- & certDStateL . accountsL
319- %~ adjustAccountState (dRepDelegationAccountStateL ?~ dRep) stakeCred
320- dReps
321- | preserveIncorrectDelegation = cState ^. certVStateL . vsDRepsL
322- | otherwise = cState' ^. certVStateL . vsDRepsL
323- in case dRep of
324- DRepCredential targetDRep
325- | Just dRepState <- Map. lookup targetDRep dReps ->
326- let dRepState' = dRepState {drepDelegs = Set. insert stakeCred (drepDelegs dRepState)}
327- in cState' & certVStateL . vsDRepsL .~ Map. insert targetDRep dRepState' dReps
328- _ -> cState'
329-
330- processDRepUnDelegation ::
331- ConwayEraCertState era =>
332- Credential 'Staking ->
333- Maybe Delegatee ->
334- CertState era ->
335- CertState era
336- processDRepUnDelegation _ Nothing cState = cState
337- processDRepUnDelegation stakeCred (Just delegatee) cState =
338- case delegatee of
339- DelegStake _ -> cState
340- DelegVote dRep -> cState & certVStateL .~ unDelegVote (cState ^. certVStateL) dRep
341- DelegStakeVote _sPool dRep -> cState & certVStateL .~ unDelegVote (cState ^. certVStateL) dRep
342- where
343- unDelegVote vState = \ case
323+ cState
324+ & certDStateL . accountsL %~ adjustAccountState (dRepDelegationAccountStateL ?~ dRep) stakeCred
325+ & maybe
326+ (certVStateL %~ insertDRepDeleg dRep)
327+ (\ accountState -> certVStateL %~ unDelegReDelegDRep stakeCred accountState (Just dRep))
328+ (guard (not preserveIncorrectDelegation) >> mAccountState)
329+ insertDRepDeleg dRep = case dRep of
344330 DRepCredential dRepCred ->
345- let removeDelegation dRepState =
346- dRepState {drepDelegs = Set. delete stakeCred (drepDelegs dRepState)}
347- in vState & vsDRepsL %~ Map. adjust removeDelegation dRepCred
348- _ -> vState
331+ vsDRepsL %~ Map. adjust (drepDelegsL %~ Set. insert stakeCred) dRepCred
332+ _ -> id
0 commit comments