Skip to content

Commit e14a6cd

Browse files
committed
[wip] - removePoolsDelegations
1 parent 4196b28 commit e14a6cd

File tree

3 files changed

+24
-19
lines changed
  • eras/shelley
  • libs/cardano-ledger-core/src/Cardano/Ledger/State

3 files changed

+24
-19
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,12 @@ poolReapTransition = do
208208
a {casTreasury = casTreasury a <+> fromCompact unclaimed}
209209
( cs
210210
& certDStateL . accountsL
211-
%~ removeStakePoolDelegations retired . addToBalanceAccounts refunds
211+
%~ removeStakePoolDelegations (allDelegsToClear cs retired)
212+
. addToBalanceAccounts refunds
212213
& certPStateL . psStakePoolsL %~ (`Map.withoutKeys` retired)
213214
& certPStateL . psRetiringL %~ (`Map.withoutKeys` retired)
214215
& certPStateL . psVRFKeyHashesL
215-
%~ ( removeVRFKeyHashOccurrences (retiredVRFKeyHashes)
216+
%~ ( removeVRFKeyHashOccurrences retiredVRFKeyHashes
216217
. (`Map.withoutKeys` danglingVRFKeyHashes)
217218
)
218219
)
@@ -226,6 +227,11 @@ poolReapTransition = do
226227
-- Removes the key from the map if the value drops to 0
227228
Map.update (mapNonZero (\n -> n - 1))
228229

230+
allDelegsToClear cState pools =
231+
foldMap spsDelegs $
232+
Map.elems $
233+
Map.restrictKeys (cState ^. certPStateL . psStakePoolsL) pools
234+
229235
renderPoolReapViolation ::
230236
( EraGov era
231237
, State t ~ ShelleyPoolreapState era

eras/shelley/test-suite/test/Test/Cardano/Ledger/Shelley/Examples/Combinators.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ import Cardano.Slotting.Slot (EpochNo, WithOrigin (..))
9494
import Data.Foldable (fold)
9595
import Data.Map.Strict (Map)
9696
import qualified Data.Map.Strict as Map
97-
import Data.Maybe (fromJust)
98-
import qualified Data.Set as Set
97+
import Data.Maybe (fromJust, maybeToList)
9998
import Data.Word (Word64)
10099
import GHC.Stack (HasCallStack)
101100
import Lens.Micro
@@ -369,7 +368,11 @@ reapPool pool cs = cs {chainNes = nes'}
369368
in ( accounts & accountsMapL %~ Map.insert poolAccountCred accountState'
370369
, mempty
371370
)
372-
ds' = ds {dsAccounts = removeStakePoolDelegations (Set.singleton poolId) accounts'}
371+
delegsToClear =
372+
foldMap spsDelegs $
373+
maybeToList $
374+
Map.lookup poolId (dps ^. certPStateL . psStakePoolsL)
375+
ds' = ds {dsAccounts = removeStakePoolDelegations delegsToClear accounts'}
373376
chainAccountState = esChainAccountState es
374377
chainAccountState' = chainAccountState {casTreasury = casTreasury chainAccountState <+> fromCompact unclaimed}
375378
utxoSt = lsUTxOState ls

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import qualified Data.Map.Merge.Strict as Map
4545
import Data.Map.Strict (Map)
4646
import qualified Data.Map.Strict as Map
4747
import Data.Set (Set)
48-
import qualified Data.Set as Set
4948
import Lens.Micro
5049
import NoThunks.Class (NoThunks)
5150

@@ -164,7 +163,7 @@ isAccountRegistered cred accounts = Map.member cred (accounts ^. accountsMapL)
164163
adjustAccountState ::
165164
EraAccounts era =>
166165
(AccountState era -> AccountState era) -> Credential 'Staking -> Accounts era -> Accounts era
167-
adjustAccountState cred f = accountsMapL %~ Map.adjust cred f
166+
adjustAccountState f cred = accountsMapL %~ Map.adjust f cred
168167

169168
-- | In case when account state is registered and it is delegated to a stake pool this function
170169
-- will return that delegation.
@@ -238,18 +237,15 @@ drainAccounts (Withdrawals withdrawalsMap) accounts =
238237
accountsMap
239238
withdrawalsMap
240239

241-
-- TODO: This is an expensive operation, since it iterates over the whole accountsMap. We need to
242-
-- start keeping track of all delegations to the stake pool in its state, then we would be able to
243-
-- switch from `Set (KeyHash 'StakePool)` to `Map (KeyHash 'StakePool) (Set (Credential Staking))`
244-
-- and drastically speed up this operation.
245-
246240
-- | Remove delegations for the supplied Stake
247241
removeStakePoolDelegations ::
248-
EraAccounts era => Set (KeyHash 'StakePool) -> Accounts era -> Accounts era
242+
EraAccounts era => Set (Credential 'Staking) -> Accounts era -> Accounts era
249243
removeStakePoolDelegations stakeDelegationsToRemove accounts =
250-
accounts & accountsMapL %~ Map.map clearAccountStateDelegation
251-
where
252-
clearAccountStateDelegation =
253-
stakePoolDelegationAccountStateL %~ \case
254-
Just poolId | poolId `Set.member` stakeDelegationsToRemove -> Nothing
255-
delegation -> delegation
244+
accounts
245+
& accountsMapL
246+
%~ ( \accountsMap ->
247+
foldr
248+
(Map.adjust (stakePoolDelegationAccountStateL .~ Nothing))
249+
accountsMap
250+
stakeDelegationsToRemove
251+
)

0 commit comments

Comments
 (0)