Skip to content

Commit a24e06c

Browse files
authored
Move pool deposits into StakePoolState (#5234)
* Move pool deposits from PState into StakePoolState. * Add `spsDeposit` field to `StakePoolState` * Remove `psDeposits` field from `PState` data constructor * Update `mkStakePoolState` to take deposit parameter as first argument * Replace `psDepositsL` and `psDepositsCompactL` lenses with `psDepositsG` and `psDepositsCompactG` getters * Remove `payPoolDeposit` and `refundPoolDeposit` functions * Update `EncCBOR`/`DecCBOR` instances for `PState` to handle new structure * Add lenses for `StakePoolState` fields * Update shelley to use StakePoolState deposit. Also, * Use `mempty` deposits for genesis stake pools per specification. * Remove `epochStatePoolParamsL` lens. * Add prUTxOStateL, prChainAccountStateL, prCertStateL lenses to PoolReap exports. * Update shelley tests. * Update ledger-test and conformance tests. * Update test examples to handle deposits correctly. - Replace newPool/reregPool by a singular regPool. - Remove addPoolDeposits. - Update test examples to use this new way of handling pool deposits. - Update ledger state golden test.
1 parent 898b0be commit a24e06c

File tree

34 files changed

+262
-283
lines changed

34 files changed

+262
-283
lines changed

eras/shelley/impl/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## 1.17.0.0
44

5+
* Refactor pool deposits to use `StakePoolState`. #5234
6+
* Update `Pool` rule to store deposits in individual `StakePoolState` records
7+
* Add and export `prUTxOStateL`, `prChainAccountStateL`, and `prCertStateL` lenses for `ShelleyPoolreapState`
8+
* Update genesis stake pool registration to use `mempty` deposits for initial pools per specification
9+
* Remove `epochStatePoolParamsL` lens.
510
* Add `hardforkConwayDisallowDuplicatedVRFKeys`
611
* Add `VRFKeyHashAlreadyRegistered` to `ShelleyPoolPredFailure` type
712
* Add `NFData` for `NominalDiffTimeMicro`, `ShelleyGenesisStaking` and `ShelleyGenesis`

eras/shelley/impl/src/Cardano/Ledger/Shelley/LedgerState.hs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ module Cardano.Ledger.Shelley.LedgerState (
4242
produced,
4343

4444
-- * DelegationState
45-
payPoolDeposit,
46-
refundPoolDeposit,
4745
totalObligation,
4846
allObligations,
4947

@@ -102,7 +100,6 @@ module Cardano.Ledger.Shelley.LedgerState (
102100
utxosDonationL,
103101
epochStateGovStateL,
104102
epochStateStakeDistrL,
105-
epochStatePoolParamsL,
106103
epochStateStakePoolsL,
107104
epochStateDonationL,
108105
newEpochStateGovStateL,
@@ -115,7 +112,7 @@ module Cardano.Ledger.Shelley.LedgerState (
115112
psStakePoolsL,
116113
psFutureStakePoolsL,
117114
psRetiringL,
118-
psDepositsL,
115+
psDepositsG,
119116
psVRFKeyHashesL,
120117

121118
-- * Lenses from SnapShot(s)

eras/shelley/impl/src/Cardano/Ledger/Shelley/LedgerState/Types.hs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import Control.Monad.State.Strict (evalStateT)
5555
import Control.Monad.Trans (MonadTrans (lift))
5656
import Data.Aeson (ToJSON (..), (.=))
5757
import Data.Default (Default, def)
58-
import Data.Map.Strict (Map, mapWithKey)
58+
import Data.Map.Strict (Map)
5959
import Data.VMap (VB, VMap, VP)
6060
import GHC.Generics (Generic)
6161
import Lens.Micro
@@ -698,12 +698,6 @@ epochStateStakePoolsL ::
698698
EraCertState era => Lens' (EpochState era) (Map (KeyHash 'StakePool) StakePoolState)
699699
epochStateStakePoolsL = esLStateL . lsCertStateL . certPStateL . psStakePoolsL
700700

701-
epochStatePoolParamsL ::
702-
EraCertState era => Lens' (EpochState era) (Map (KeyHash 'StakePool) PoolParams)
703-
epochStatePoolParamsL =
704-
epochStateStakePoolsL . lens (mapWithKey stakePoolStateToPoolParams) (const $ fmap mkStakePoolState)
705-
{-# DEPRECATED epochStatePoolParamsL "In favor of `epochStateStakePoolsL`" #-}
706-
707701
epochStateStakeDistrL ::
708702
Lens' (EpochState era) (VMap VB VP (Credential 'Staking) (CompactForm Coin))
709703
epochStateStakeDistrL = esSnapshotsL . ssStakeMarkL . ssStakeDistrL

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,9 @@ poolDelegationTransition = do
259259
| otherwise = id
260260
tellEvent $ RegisterPool ppId
261261
pure $
262-
payPoolDeposit ppId pp $
263-
ps
264-
& psStakePoolsL %~ Map.insert ppId (mkStakePoolState poolParams)
265-
& psVRFKeyHashesL %~ updateVRFKeyHash
262+
ps
263+
& psStakePoolsL %~ Map.insert ppId (mkStakePoolState (pp ^. ppPoolDepositCompactL) poolParams)
264+
& psVRFKeyHashesL %~ updateVRFKeyHash
266265
-- re-register Pool
267266
Just stakePoolState -> do
268267
when (hardforkConwayDisallowDuplicatedVRFKeys pv) $ do
@@ -281,18 +280,21 @@ poolDelegationTransition = do
281280
| otherwise -> id
282281
| otherwise = id
283282
tellEvent $ ReregisterPool ppId
284-
-- hk is already registered, so we want to reregister it. That means adding it
285-
-- to the Future pool params (if it is not there already), and overriding the
286-
-- range with the new 'poolParam', if it is (using ⨃ ). We must also unretire
287-
-- it, if it has been scheduled for retirement. The deposit does not
288-
-- change. One pays the deposit just once. Only if it is fully retired
289-
-- (i.e. it's deposit has been refunded, and it has been removed from the
290-
-- registered pools). does it need to pay a new deposit (at the current deposit
291-
-- amount). But of course, if that has happened, we cannot be in this branch of
292-
-- the if statement.
283+
-- NOTE: The `ppId` is already registered, so we want to reregister
284+
-- it. That means adding it to the Future Stake Pools (if it is not
285+
-- there already), and overriding its range with the new 'poolParams',
286+
-- if it is.
287+
--
288+
-- We must also unretire it, if it has been scheduled for retirement.
289+
--
290+
-- The deposit does not change. One pays the deposit just once. Only
291+
-- if it is fully retired (i.e. it's deposit has been refunded, and it
292+
-- has been removed from the registered pools). does it need to pay a
293+
-- new deposit (at the current deposit amount). But of course, if that
294+
-- has happened, we cannot be in this branch of the case statement.
293295
pure $
294296
ps
295-
& psFutureStakePoolsL %~ Map.insert ppId (mkStakePoolState poolParams)
297+
& psFutureStakePoolsL %~ Map.insert ppId (mkStakePoolState (stakePoolState ^. spsDepositL) poolParams)
296298
& psRetiringL %~ Map.delete ppId
297299
& psVRFKeyHashesL %~ updateFutureVRFKeyHash
298300
RetirePool ppId e -> do

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ module Cardano.Ledger.Shelley.Rules.PoolReap (
1616
ShelleyPOOLREAP,
1717
ShelleyPoolreapEvent (..),
1818
ShelleyPoolreapState (..),
19+
prCertStateL,
20+
prChainAccountStateL,
21+
prUTxOStateL,
1922
PredicateFailure,
2023
ShelleyPoolreapPredFailure,
2124
) where
2225

23-
import Cardano.Ledger.Address (RewardAccount, raCredential)
26+
import Cardano.Ledger.Address
2427
import Cardano.Ledger.BaseTypes (ShelleyBase)
2528
import Cardano.Ledger.Coin (Coin, CompactForm)
2629
import Cardano.Ledger.Compactible (fromCompact)
@@ -65,6 +68,15 @@ data ShelleyPoolreapState era = PoolreapState
6568
deriving stock instance
6669
(Show (UTxOState era), Show (CertState era)) => Show (ShelleyPoolreapState era)
6770

71+
prUTxOStateL :: Lens' (ShelleyPoolreapState era) (UTxOState era)
72+
prUTxOStateL = lens prUTxOSt $ \sprs x -> sprs {prUTxOSt = x}
73+
74+
prChainAccountStateL :: Lens' (ShelleyPoolreapState era) ChainAccountState
75+
prChainAccountStateL = lens prChainAccountState $ \sprs x -> sprs {prChainAccountState = x}
76+
77+
prCertStateL :: Lens' (ShelleyPoolreapState era) (CertState era)
78+
prCertStateL = lens prCertState $ \sprs x -> sprs {prCertState = x}
79+
6880
data ShelleyPoolreapPredFailure era -- No predicate failures
6981
deriving (Show, Eq, Generic)
7082

@@ -155,38 +167,37 @@ poolReapTransition = do
155167
-- The set of pools retiring this epoch
156168
retired :: Set (KeyHash 'StakePool)
157169
retired = Set.fromDistinctAscList [k | (k, v) <- Map.toAscList (psRetiring ps), v == e]
158-
-- The Map of pools (retiring this epoch) to their deposits
159-
retiringDeposits, remainingDeposits :: Map.Map (KeyHash 'StakePool) (CompactForm Coin)
160-
(retiringDeposits, remainingDeposits) =
161-
Map.partitionWithKey (\k _ -> Set.member k retired) (psDeposits ps)
170+
-- The Map of pools retiring this epoch
171+
retiringPools :: Map.Map (KeyHash 'StakePool) StakePoolState
172+
retiringPools = Map.restrictKeys (psStakePools ps) retired
162173
-- collect all accounts for stake pools that will retire
163174
retiredStakePoolAccountsWithVRFs ::
164175
Map.Map (KeyHash 'StakePool) (RewardAccount, VRFVerKeyHash 'StakePoolVRF)
165176
retiredStakePoolAccountsWithVRFs =
166177
Map.map
167178
(\sps -> (spsRewardAccount sps, spsVrf sps))
168-
$ Map.restrictKeys (psStakePools ps) retired
179+
retiringPools
169180
retiredVRFs = foldMap (Set.singleton . snd) retiredStakePoolAccountsWithVRFs
170181
retiredStakePoolAccountsWithRefund ::
171182
Map.Map (KeyHash 'StakePool) (RewardAccount, CompactForm Coin)
172183
retiredStakePoolAccountsWithRefund =
173184
Map.intersectionWith
174-
(\(rewardAccount, _) coin -> (rewardAccount, coin))
185+
(\(rewardAccount, _) sps -> (rewardAccount, spsDeposit sps))
175186
retiredStakePoolAccountsWithVRFs
176-
retiringDeposits
187+
retiringPools
177188
-- collect all of the potential refunds
178189
accountRefunds :: Map.Map (Credential 'Staking) (CompactForm Coin)
179190
accountRefunds =
180191
Map.fromListWith (<>) $
181192
[(raCredential k, v) | (k, v) <- Map.elems retiredStakePoolAccountsWithRefund]
182193
accounts = ds ^. accountsL
183-
-- figure out whcich deposits can be refunded and which ones will be deposited into the treasury
184-
-- as unclaimed
194+
-- Deposits that can be refunded and those that are unclaimed (to be deposited into the treasury).
185195
refunds, unclaimedDeposits :: Map.Map (Credential 'Staking) (CompactForm Coin)
186196
(refunds, unclaimedDeposits) =
187197
Map.partitionWithKey
188198
(\stakeCred _ -> isAccountRegistered stakeCred accounts) -- (k ∈ dom (rewards ds))
189199
accountRefunds
200+
190201
refunded = fold refunds
191202
unclaimed = fold unclaimedDeposits
192203

@@ -216,7 +227,6 @@ poolReapTransition = do
216227
%~ removeStakePoolDelegations retired . addToBalanceAccounts refunds
217228
& certPStateL . psStakePoolsL %~ (`Map.withoutKeys` retired)
218229
& certPStateL . psRetiringL %~ (`Map.withoutKeys` retired)
219-
& certPStateL . psDepositsCompactL .~ remainingDeposits
220230
& certPStateL . psVRFKeyHashesL
221231
%~ ((`Set.difference` retiredVRFs) . (`Set.difference` danglingVrfKeyHashes))
222232
)

eras/shelley/impl/src/Cardano/Ledger/Shelley/State/CertState.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import Cardano.Ledger.Binary (
3030
encodeListLen,
3131
)
3232
import Cardano.Ledger.Coin (Coin (..))
33+
import Cardano.Ledger.Compactible (fromCompact)
3334
import Cardano.Ledger.Core
3435
import Cardano.Ledger.Credential (Credential (..))
3536
import Cardano.Ledger.Shelley.Era (ShelleyEra)
@@ -77,7 +78,8 @@ shelleyObligationCertState :: EraCertState era => CertState era -> Obligations
7778
shelleyObligationCertState certState =
7879
Obligations
7980
{ oblStake = sumDepositsAccounts (certState ^. certDStateL . accountsL)
80-
, oblPool = F.foldl' (<>) (Coin 0) (certState ^. certPStateL . psDepositsL)
81+
, oblPool =
82+
F.foldl' (<>) (Coin 0) (fromCompact . spsDeposit <$> certState ^. certPStateL . psStakePoolsL)
8183
, oblDRep = Coin 0
8284
, oblProposal = Coin 0
8385
}

eras/shelley/impl/src/Cardano/Ledger/Shelley/Transition.hs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,18 @@ createInitialState tc =
385385
reserves :: Coin
386386
reserves = word64ToCoin (sgMaxLovelaceSupply sg) <-> sumCoinUTxO initialUtxo
387387

388+
-- | From the haddock for `ShelleyGenesisStaking`:
389+
--
390+
-- > `ShelleyGenesisStaking` allows us to configure some initial stake pools and
391+
-- > delegation to them, in order to test Praos in a static configuration, without
392+
-- > requiring on-chain registration and delegation.
393+
--
394+
-- > For simplicity, pools defined in the genesis staking DO NOT PAY DEPOSITS FOR
395+
-- > THEIR REGISTRATION
396+
--
397+
-- Therefore, we use `mempty` in the convertion below.
398+
--
399+
-- QUESTION: @aniketd: Is the assumption that we can use mempty truly harmless?
388400
registerInitialStakePools ::
389401
forall era.
390402
EraCertState era =>
@@ -394,7 +406,7 @@ registerInitialStakePools ::
394406
registerInitialStakePools ShelleyGenesisStaking {sgsPools} nes =
395407
nes
396408
& nesEsL . esLStateL . lsCertStateL . certPStateL . psStakePoolsL
397-
.~ (mkStakePoolState <$> ListMap.toMap sgsPools)
409+
.~ (mkStakePoolState mempty <$> ListMap.toMap sgsPools)
398410

399411
-- | Register all staking credentials and apply delegations. Make sure StakePools that are bing
400412
-- delegated to are already registered, which can be done with `registerInitialStakePools`.

eras/shelley/impl/testlib/Test/Cardano/Ledger/Shelley/UnitTests/InstantStakeTest.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ instantStakeIncludesRewards = do
8585

8686
instantStake = addInstantStake utxo1 mempty
8787
poolparamMap = Map.fromList [(poolId1, pool1), (poolId2, pool2)]
88-
pState <- arbitraryLens psStakePoolsL $ mkStakePoolState <$> poolparamMap
88+
pState <- arbitraryLens psStakePoolsL $ mkStakePoolState mempty <$> poolparamMap
8989
let snapShot = snapShotFromInstantStake instantStake dState pState
9090
computedStakeDistr = VMap.toMap (unStake (ssStake snapShot))
9191

eras/shelley/test-suite/src/Test/Cardano/Ledger/Shelley/Rules/AdaPreservation.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ checkPreservation SourceSignalTarget {source, target, signal} count =
236236
oldCertState = lsCertState lsOld
237237
oldRetire = lsOld ^. lsCertStateL . certPStateL . psRetiringL
238238
newRetire = lsNew ^. lsCertStateL . certPStateL . psRetiringL
239-
oldPoolDeposit = lsOld ^. lsCertStateL . certPStateL . psDepositsL
240-
newPoolDeposit = lsNew ^. lsCertStateL . certPStateL . psDepositsL
239+
oldPoolDeposit = spsDeposit <$> lsOld ^. lsCertStateL . certPStateL . psStakePoolsL
240+
newPoolDeposit = spsDeposit <$> lsNew ^. lsCertStateL . certPStateL . psStakePoolsL
241241

242242
proposal = votedFuturePParams (sgsCurProposals . utxosGovState $ lsUTxOState lsOld) currPP 5
243243
obligationMsgs = case proposal of

eras/shelley/test-suite/src/Test/Cardano/Ledger/Shelley/Rules/Deposits.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,15 @@ depositInvariant SourceSignalTarget {source = chainSt} =
8080
pstate = certState ^. certPStateL
8181
allDeposits = utxosDeposited utxost
8282
keyDeposits = sumDepositsAccounts (dstate ^. accountsL)
83-
poolDeposits = foldMap fromCompact (psDeposits pstate)
83+
poolDeposits = foldMap (fromCompact . spsDeposit) (psStakePools pstate)
8484
in counterexample
8585
( ansiDocToString . Pretty.vsep $
8686
[ "Deposit invariant fails:"
8787
, Pretty.indent 2 . Pretty.vsep . map Pretty.pretty $
8888
[ "All deposits = " ++ show allDeposits
8989
, "Key deposits = "
9090
++ show ((^. depositAccountStateL) <$> (dstate ^. accountsL . accountsMapL))
91-
, "Pool deposits = " ++ synopsisCoinMap (Just (fromCompact <$> psDeposits pstate))
91+
, "Pool deposits = " ++ synopsisCoinMap (Just (fromCompact . spsDeposit <$> psStakePools pstate))
9292
]
9393
]
9494
)

0 commit comments

Comments
 (0)