Skip to content

Commit c9cd2e7

Browse files
authored
Merge pull request #5398 from IntersectMBO/td/intern-reverse-delegations-creds
Intern stake credentials in reverse delegations
2 parents c095c48 + b5eb27b commit c9cd2e7

File tree

6 files changed

+39
-8
lines changed

6 files changed

+39
-8
lines changed

eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Deleg.hs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ import Control.State.Transition (
7575
)
7676
import Data.Map (Map)
7777
import qualified Data.Map.Strict as Map
78-
import Data.Maybe (isJust)
7978
import Data.Set as Set
8079
import Data.Void (Void)
8180
import GHC.Generics (Generic)
@@ -261,11 +260,19 @@ conwayDelegTransition = do
261260
& certVStateL %~ unDelegReDelegDRep stakeCred accountState Nothing
262261
& certPStateL %~ unDelegReDelegStakePool stakeCred accountState Nothing
263262
ConwayDelegCert stakeCred delegatee -> do
264-
let mAccountState = lookupAccountState stakeCred accounts
265-
isJust mAccountState ?! StakeKeyNotRegisteredDELEG stakeCred
266263
checkStakeDelegateeRegistered delegatee
267-
pure $
268-
processDelegationInternal (pvMajor pv < natVersion @10) stakeCred mAccountState delegatee certState
264+
case lookupAccountStateIntern stakeCred accounts of
265+
Nothing -> do
266+
failBecause $ StakeKeyNotRegisteredDELEG stakeCred
267+
pure certState
268+
Just (internedCred, accountState) -> do
269+
pure $
270+
processDelegationInternal
271+
(pvMajor pv < natVersion @10)
272+
internedCred
273+
(Just accountState)
274+
delegatee
275+
certState
269276
ConwayRegDelegCert stakeCred delegatee deposit -> do
270277
checkDepositAgainstPParams deposit
271278
checkStakeKeyNotRegistered stakeCred

eras/shelley/impl/src/Cardano/Ledger/Shelley/Rules/Deleg.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,15 +286,15 @@ delegationTransition = do
286286
DelegStakeTxCert cred stakePool -> do
287287
-- note that pattern match is used instead of cwitness and dpool, as in the spec
288288
-- (hk ∈ dom (rewards ds))
289-
case lookupAccountState cred (ds ^. accountsL) of
289+
case lookupAccountStateIntern cred (ds ^. accountsL) of
290290
Nothing -> do
291291
failBecause $ StakeDelegationImpossibleDELEG cred
292292
pure certState
293-
Just accountState ->
293+
Just (internedCred, accountState) ->
294294
pure $
295295
certState
296296
& certDStateL . accountsL %~ adjustAccountState (stakePoolDelegationAccountStateL ?~ stakePool) cred
297-
& certPStateL %~ unDelegReDelegStakePool cred accountState (Just stakePool)
297+
& certPStateL %~ unDelegReDelegStakePool internedCred accountState (Just stakePool)
298298
GenesisDelegTxCert gkh vkh vrf -> do
299299
sp <- liftSTS $ asks stabilityWindow
300300
-- note that pattern match is used instead of genesisDeleg, as in the spec

libs/cardano-data/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 1.3.0.0
44

5+
* Add `lookupInternMap`
56
* Replace `okeyL` with `toOKey`
67

78
## 1.2.4.1

libs/cardano-data/src/Data/MapExtras.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module Data.MapExtras (
2828
extractKeysSmallSet,
2929
fromKeys,
3030
fromElems,
31+
lookupInternMap,
3132
) where
3233

3334
import Data.Foldable (toList)
@@ -270,3 +271,16 @@ fromElems f vs =
270271
-- a nice optimization for already sorted keys and with list fusion there should be no overhead
271272
Map.fromList [(f v, v) | v <- toList vs]
272273
{-# INLINE fromElems #-}
274+
275+
-- | Look up a key in a map and return the interned key together with its value, if present.
276+
-- The returned key is exactly the one stored in the map.
277+
-- Useful for maximizing sharing by avoiding duplicate-but-equal keys.
278+
lookupInternMap :: Ord k => k -> Map k v -> Maybe (k, v)
279+
lookupInternMap k = go
280+
where
281+
go Tip = Nothing
282+
go (Bin _ kx v l r) =
283+
case compare k kx of
284+
LT -> go l
285+
GT -> go r
286+
EQ -> Just (kx, v)

libs/cardano-ledger-core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 1.19.0.0
44

5+
* Add `lookupAccountStateIntern` to `State.Account` module
56
* Add `HasOKey` instance for `TxId (Tx l era)`
67
* Remove `Generic` instance from `BoundedRatio` type
78
* Remove deprecated function `addrPtrNormalize`

libs/cardano-ledger-core/src/Cardano/Ledger/State/Account.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module Cardano.Ledger.State.Account (
1515
CanSetAccounts (..),
1616
EraAccounts (..),
1717
lookupAccountState,
18+
lookupAccountStateIntern,
1819
updateLookupAccountState,
1920
isAccountRegistered,
2021
adjustAccountState,
@@ -44,6 +45,7 @@ import Data.Kind (Type)
4445
import qualified Data.Map.Merge.Strict as Map
4546
import Data.Map.Strict (Map)
4647
import qualified Data.Map.Strict as Map
48+
import Data.MapExtras (lookupInternMap)
4749
import Data.Set (Set)
4850
import Lens.Micro
4951
import NoThunks.Class (NoThunks)
@@ -145,6 +147,12 @@ lookupAccountState ::
145147
EraAccounts era => Credential 'Staking -> Accounts era -> Maybe (AccountState era)
146148
lookupAccountState cred accounts = Map.lookup cred (accounts ^. accountsMapL)
147149

150+
lookupAccountStateIntern ::
151+
EraAccounts era =>
152+
Credential 'Staking -> Accounts era -> Maybe (Credential 'Staking, AccountState era)
153+
lookupAccountStateIntern cred accounts =
154+
lookupInternMap cred (accounts ^. accountsMapL)
155+
148156
-- | Update account state. Returns Nothing if the value is not present and modified value otherwise
149157
updateLookupAccountState ::
150158
EraAccounts era =>

0 commit comments

Comments
 (0)