@@ -17,6 +17,7 @@ module Cardano.Ledger.Conway.State.VState (
1717 vsNumDormantEpochsL ,
1818 vsActualDRepExpiry ,
1919 lookupDepositVState ,
20+ unDelegReDelegDRep ,
2021) where
2122
2223import Cardano.Ledger.BaseTypes (KeyValuePairs (.. ), ToKeyValuePairs (.. ), binOpEpochNo )
@@ -33,17 +34,21 @@ import Cardano.Ledger.Binary (
3334import Cardano.Ledger.Binary.Coders (Decode (.. ), Encode (.. ), decode , encode , (!>) , (<!) )
3435import Cardano.Ledger.Coin (Coin (.. ))
3536import Cardano.Ledger.Compactible (Compactible (.. ))
37+ import Cardano.Ledger.Conway.State.Account
3638import Cardano.Ledger.Core
3739import Cardano.Ledger.Credential (Credential (.. ))
40+ import Cardano.Ledger.DRep (drepDelegsL )
3841import Cardano.Ledger.Shelley.State
3942import Cardano.Ledger.Slot (EpochNo (.. ))
4043import Control.DeepSeq (NFData (.. ))
4144import Data.Aeson (ToJSON (.. ), (.=) )
4245import Data.Default (Default (def ))
4346import Data.Map.Strict (Map )
4447import qualified Data.Map.Strict as Map
48+ import Data.Maybe (fromMaybe )
49+ import qualified Data.Set as Set
4550import GHC.Generics (Generic )
46- import Lens.Micro ( Lens' , lens , (^.) )
51+ import Lens.Micro
4752import NoThunks.Class (NoThunks (.. ))
4853
4954-- | The state that tracks the voting entities (DReps and Constitutional Committee
@@ -109,6 +114,34 @@ instance ToKeyValuePairs (VState era) where
109114 , " numDormantEpochs" .= vsNumDormantEpochs
110115 ]
111116
117+ -- | Reverses DRep delegation.
118+ -- To be called when a stake credential is unregistered or its delegation target changes.
119+ -- If the new delegation matches the previous one, this is a noop.
120+ unDelegReDelegDRep ::
121+ ConwayEraAccounts era =>
122+ Credential 'Staking ->
123+ -- | Account that is losing its current delegation and/or acquiring a new one
124+ AccountState era ->
125+ -- | Potential new delegation. In case when stake credential unregisters this must be `Nothing`.
126+ Maybe DRep ->
127+ VState era ->
128+ VState era
129+ unDelegReDelegDRep stakeCred accountState mNewDRep =
130+ fromMaybe (vsDRepsL %~ addNewDelegation) $ do
131+ dRep@ (DRepCredential dRepCred) <- accountState ^. dRepDelegationAccountStateL
132+ pure $
133+ -- There is no need to update set of delegations if delegation is unchanged
134+ if Just dRep == mNewDRep
135+ then id
136+ else
137+ vsDRepsL %~ addNewDelegation . Map. adjust (drepDelegsL %~ Set. delete stakeCred) dRepCred
138+ where
139+ addNewDelegation =
140+ case mNewDRep of
141+ Just (DRepCredential dRepCred) ->
142+ Map. adjust (drepDelegsL %~ Set. insert stakeCred) dRepCred
143+ _ -> id
144+
112145vsDRepsL :: Lens' (VState era ) (Map (Credential 'DRepRole) DRepState )
113146vsDRepsL = lens vsDReps (\ vs u -> vs {vsDReps = u})
114147
0 commit comments