Skip to content

Commit c3d0900

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

File tree

3 files changed

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

3 files changed

+23
-13
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ poolReapTransition = do
208208
a {casTreasury = casTreasury a <+> fromCompact unclaimed}
209209
( cs
210210
& certDStateL . accountsL
211-
%~ removeStakePoolDelegations retired . addToBalanceAccounts refunds
211+
%~ removeStakePoolDelegations (traceShowId (allDelegsToClear cs retired))
212+
. addToBalanceAccounts refunds
212213
& certPStateL . psStakePoolsL %~ (`Map.withoutKeys` retired)
213214
& certPStateL . psRetiringL %~ (`Map.withoutKeys` retired)
214215
& certPStateL . psVRFKeyHashesL
@@ -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 & 9 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.
@@ -245,11 +244,13 @@ drainAccounts (Withdrawals withdrawalsMap) accounts =
245244

246245
-- | Remove delegations for the supplied Stake
247246
removeStakePoolDelegations ::
248-
EraAccounts era => Set (KeyHash 'StakePool) -> Accounts era -> Accounts era
247+
EraAccounts era => Set (Credential 'Staking) -> Accounts era -> Accounts era
249248
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
249+
accounts
250+
& accountsMapL
251+
%~ ( \accountsMap ->
252+
foldr
253+
(Map.adjust (stakePoolDelegationAccountStateL .~ Nothing))
254+
accountsMap
255+
stakeDelegationsToRemove
256+
)

0 commit comments

Comments
 (0)