33{-# LANGUAGE DeriveGeneric #-}
44{-# LANGUAGE DerivingVia #-}
55{-# LANGUAGE GeneralizedNewtypeDeriving #-}
6+ {-# LANGUAGE LambdaCase #-}
67{-# LANGUAGE NamedFieldPuns #-}
78{-# LANGUAGE OverloadedStrings #-}
89{-# LANGUAGE PatternSynonyms #-}
1213{-# LANGUAGE TypeOperators #-}
1314{-# LANGUAGE UndecidableSuperClasses #-}
1415{-# LANGUAGE ViewPatterns #-}
15- {-# OPTIONS_GHC -Wno-orphans #-}
16+ -- `unused-pattern-binds` warning is disabled to preserve safety of `RecordWildCards` "trick" while
17+ -- avoiding unnecessary pattern matching that is not zero cost with pattern synonyms.
18+ {-# OPTIONS_GHC -Wno-orphans -Wno-unused-pattern-binds #-}
1619
1720module Cardano.Ledger.Conway.State.Account (
1821 ConwayAccountState (
@@ -22,6 +25,10 @@ module Cardano.Ledger.Conway.State.Account (
2225 casStakePoolDelegation ,
2326 casDRepDelegation
2427 ),
28+ balanceConwayAccountStateL ,
29+ depositConwayAccountStateL ,
30+ stakePoolDelegationConwayAccountStateL ,
31+ dRepDelegationConwayAccountStateL ,
2532 ConwayAccounts (.. ),
2633 ConwayEraAccounts (.. ),
2734 accountStateDelegatee ,
@@ -83,17 +90,17 @@ data ConwayAccountState era
8390
8491viewConwayAccountState ::
8592 ConwayAccountState era ->
86- (CompactForm Coin , CompactForm Coin , StrictMaybe (KeyHash StakePool ), StrictMaybe DRep )
87- viewConwayAccountState (CASNoDelegation x y) = (x, y, SNothing , SNothing )
88- viewConwayAccountState (CASStakePool x y z) = (x, y, SJust z, SNothing )
89- viewConwayAccountState (CASDRep x y w) = (x, y, SNothing , SJust w)
90- viewConwayAccountState (CASStakePoolAndDRep x y z w) = (x, y, SJust z, SJust w)
93+ (CompactForm Coin , CompactForm Coin , Maybe (KeyHash StakePool ), Maybe DRep )
94+ viewConwayAccountState (CASNoDelegation x y) = (x, y, Nothing , Nothing )
95+ viewConwayAccountState (CASStakePool x y z) = (x, y, Just z, Nothing )
96+ viewConwayAccountState (CASDRep x y w) = (x, y, Nothing , Just w)
97+ viewConwayAccountState (CASStakePoolAndDRep x y z w) = (x, y, Just z, Just w)
9198
9299pattern ConwayAccountState ::
93100 CompactForm Coin ->
94101 CompactForm Coin ->
95- StrictMaybe (KeyHash StakePool ) ->
96- StrictMaybe DRep ->
102+ Maybe (KeyHash StakePool ) ->
103+ Maybe DRep ->
97104 ConwayAccountState era
98105pattern ConwayAccountState
99106 { casBalance
@@ -103,26 +110,28 @@ pattern ConwayAccountState
103110 } <-
104111 (viewConwayAccountState -> (casBalance, casDeposit, casStakePoolDelegation, casDRepDelegation))
105112 where
106- ConwayAccountState x y SNothing SNothing = CASNoDelegation x y
107- ConwayAccountState x y (SJust z) SNothing = CASStakePool x y z
108- ConwayAccountState x y SNothing ( SJust w) = CASDRep x y w
109- ConwayAccountState x y (SJust z) (SJust w) = CASStakePoolAndDRep x y z w
113+ ConwayAccountState x y Nothing Nothing = CASNoDelegation x y
114+ ConwayAccountState x y (Just z) Nothing = CASStakePool x y z
115+ ConwayAccountState x y Nothing ( Just w) = CASDRep x y w
116+ ConwayAccountState x y (Just z) (Just w) = CASStakePoolAndDRep x y z w
110117
111118{-# COMPLETE ConwayAccountState #-}
112119
120+ {-# INLINE ConwayAccountState #-}
121+
113122instance NoThunks (ConwayAccountState era )
114123
115124instance NFData (ConwayAccountState era ) where
116125 rnf = rwhnf
117126
118127instance EncCBOR (ConwayAccountState era ) where
119- encCBOR cas@ ( ConwayAccountState _ _ _ _) =
120- let ConwayAccountState { .. } = cas
128+ encCBOR cas@ ConwayAccountState { .. } =
129+ let ConwayAccountState _ _ _ _ = cas
121130 in encodeListLen 4
122131 <> encCBOR casBalance
123132 <> encCBOR casDeposit
124- <> encodeNullStrictMaybe encCBOR casStakePoolDelegation
125- <> encodeNullStrictMaybe encCBOR casDRepDelegation
133+ <> encodeNullMaybe encCBOR casStakePoolDelegation
134+ <> encodeNullMaybe encCBOR casDRepDelegation
126135
127136instance Typeable era => DecShareCBOR (ConwayAccountState era ) where
128137 type
@@ -133,12 +142,12 @@ instance Typeable era => DecShareCBOR (ConwayAccountState era) where
133142 ConwayAccountState
134143 <$> decCBOR
135144 <*> decCBOR
136- <*> decodeNullStrictMaybe (interns ks <$> decCBOR)
137- <*> decodeNullStrictMaybe (decShareCBOR cd)
145+ <*> decodeNullMaybe (interns ks <$> decCBOR)
146+ <*> decodeNullMaybe (decShareCBOR cd)
138147
139148instance ToKeyValuePairs (ConwayAccountState era ) where
140- toKeyValuePairs cas@ ( ConwayAccountState _ _ _ _) =
141- let ConwayAccountState { .. } = cas
149+ toKeyValuePairs cas@ ConwayAccountState { .. } =
150+ let ConwayAccountState _ _ _ _ = cas
142151 in [ " reward" .= casBalance -- deprecated
143152 , " balance" .= casBalance
144153 , " deposit" .= casDeposit
@@ -172,16 +181,103 @@ instance EraAccounts ConwayEra where
172181
173182 accountsMapL = lens caStates $ \ cas asMap -> cas {caStates = asMap}
174183
175- balanceAccountStateL = lens casBalance $ \ cas b -> cas {casBalance = b}
184+ balanceAccountStateL = balanceConwayAccountStateL
185+ {-# INLINE balanceAccountStateL #-}
176186
177- depositAccountStateL = lens casDeposit $ \ cas d -> cas {casDeposit = d}
187+ depositAccountStateL = depositConwayAccountStateL
188+ {-# INLINE depositAccountStateL #-}
178189
179- stakePoolDelegationAccountStateL =
180- lens (strictMaybeToMaybe . casStakePoolDelegation) $ \ cas d ->
181- cas {casStakePoolDelegation = maybeToStrictMaybe d}
190+ stakePoolDelegationAccountStateL = stakePoolDelegationConwayAccountStateL
191+ {-# INLINE stakePoolDelegationAccountStateL #-}
182192
183193 unregisterAccount = unregisterConwayAccount
184194
195+ -- /Note/ - Lenses below do not use pattern synonym in order to guarantee optimal performance
196+
197+ balanceConwayAccountStateL :: Lens' (ConwayAccountState era ) (CompactForm Coin )
198+ balanceConwayAccountStateL =
199+ lens
200+ ( \ case
201+ CASNoDelegation balance _ -> balance
202+ CASStakePool balance _ _ -> balance
203+ CASDRep balance _ _ -> balance
204+ CASStakePoolAndDRep balance _ _ _ -> balance
205+ )
206+ $ \ cas balance ->
207+ case cas of
208+ CASNoDelegation _ deposit -> CASNoDelegation balance deposit
209+ CASStakePool _ deposit stakePool -> CASStakePool balance deposit stakePool
210+ CASDRep _ deposit dRep -> CASDRep balance deposit dRep
211+ CASStakePoolAndDRep _ deposit stakePool dRep -> CASStakePoolAndDRep balance deposit stakePool dRep
212+ {-# INLINE balanceConwayAccountStateL #-}
213+
214+ depositConwayAccountStateL :: Lens' (ConwayAccountState era ) (CompactForm Coin )
215+ depositConwayAccountStateL =
216+ lens
217+ ( \ case
218+ CASNoDelegation _ deposit -> deposit
219+ CASStakePool _ deposit _ -> deposit
220+ CASDRep _ deposit _ -> deposit
221+ CASStakePoolAndDRep _ deposit _ _ -> deposit
222+ )
223+ $ \ cas deposit ->
224+ case cas of
225+ CASNoDelegation balance _ -> CASNoDelegation balance deposit
226+ CASStakePool balance _ stakePool -> CASStakePool balance deposit stakePool
227+ CASDRep balance _ dRep -> CASDRep balance deposit dRep
228+ CASStakePoolAndDRep balance _ stakePool dRep -> CASStakePoolAndDRep balance deposit stakePool dRep
229+ {-# INLINE depositConwayAccountStateL #-}
230+
231+ stakePoolDelegationConwayAccountStateL :: Lens' (ConwayAccountState era ) (Maybe (KeyHash StakePool ))
232+ stakePoolDelegationConwayAccountStateL =
233+ lens
234+ ( \ case
235+ CASNoDelegation _ _ -> Nothing
236+ CASStakePool _ _ stakePool -> Just stakePool
237+ CASDRep _ _ _ -> Nothing
238+ CASStakePoolAndDRep _ _ stakePool _ -> Just stakePool
239+ )
240+ $ \ cas mStakePool ->
241+ case cas of
242+ CASNoDelegation balance deposit
243+ | Just stakePool <- mStakePool -> CASStakePool balance deposit stakePool
244+ | otherwise -> CASNoDelegation balance deposit
245+ CASStakePool balance deposit _
246+ | Just stakePool <- mStakePool -> CASStakePool balance deposit stakePool
247+ | otherwise -> CASNoDelegation balance deposit
248+ CASDRep balance deposit dRep
249+ | Just stakePool <- mStakePool -> CASStakePoolAndDRep balance deposit stakePool dRep
250+ | otherwise -> CASDRep balance deposit dRep
251+ CASStakePoolAndDRep balance deposit _ dRep
252+ | Just stakePool <- mStakePool -> CASStakePoolAndDRep balance deposit stakePool dRep
253+ | otherwise -> CASDRep balance deposit dRep
254+ {-# INLINE stakePoolDelegationConwayAccountStateL #-}
255+
256+ dRepDelegationConwayAccountStateL :: Lens' (ConwayAccountState era ) (Maybe DRep )
257+ dRepDelegationConwayAccountStateL =
258+ lens
259+ ( \ case
260+ CASNoDelegation _ _ -> Nothing
261+ CASStakePool _ _ _ -> Nothing
262+ CASDRep _ _ dRep -> Just dRep
263+ CASStakePoolAndDRep _ _ _ dRep -> Just dRep
264+ )
265+ $ \ cas mDRep ->
266+ case cas of
267+ CASNoDelegation balance deposit
268+ | Just dRep <- mDRep -> CASDRep balance deposit dRep
269+ | otherwise -> CASNoDelegation balance deposit
270+ CASStakePool balance deposit stakePool
271+ | Just dRep <- mDRep -> CASStakePoolAndDRep balance deposit stakePool dRep
272+ | otherwise -> CASStakePool balance deposit stakePool
273+ CASDRep balance deposit _
274+ | Just dRep <- mDRep -> CASDRep balance deposit dRep
275+ | otherwise -> CASNoDelegation balance deposit
276+ CASStakePoolAndDRep balance deposit stakePool _
277+ | Just dRep <- mDRep -> CASStakePoolAndDRep balance deposit stakePool dRep
278+ | otherwise -> CASStakePool balance deposit stakePool
279+ {-# INLINE dRepDelegationConwayAccountStateL #-}
280+
185281class EraAccounts era => ConwayEraAccounts era where
186282 mkConwayAccountState :: CompactForm Coin -> AccountState era
187283 default mkConwayAccountState ::
@@ -192,16 +288,15 @@ class EraAccounts era => ConwayEraAccounts era where
192288 ConwayAccountState
193289 { casBalance = mempty
194290 , casDeposit = deposit
195- , casStakePoolDelegation = SNothing
196- , casDRepDelegation = SNothing
291+ , casStakePoolDelegation = Nothing
292+ , casDRepDelegation = Nothing
197293 }
198294
199295 dRepDelegationAccountStateL :: Lens' (AccountState era ) (Maybe DRep )
200296
201297instance ConwayEraAccounts ConwayEra where
202- dRepDelegationAccountStateL =
203- lens (strictMaybeToMaybe . casDRepDelegation) $ \ cas d ->
204- cas {casDRepDelegation = maybeToStrictMaybe d}
298+ dRepDelegationAccountStateL = dRepDelegationConwayAccountStateL
299+ {-# INLINE dRepDelegationAccountStateL #-}
205300
206301lookupDRepDelegation :: ConwayEraAccounts era => Credential Staking -> Accounts era -> Maybe DRep
207302lookupDRepDelegation cred accounts = do
0 commit comments