Skip to content

Commit 38c7814

Browse files
authored
Merge pull request #5232 from IntersectMBO/td/activate-future-stakepools-in-poolreap
Activate future stake pools in `PoolReap`
2 parents d157bd5 + 2bedb77 commit 38c7814

File tree

5 files changed

+28
-46
lines changed

5 files changed

+28
-46
lines changed

eras/conway/impl/cardano-ledger-conway.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ library
113113
mtl,
114114
nothunks,
115115
plutus-ledger-api >=1.37,
116-
set-algebra,
117116
small-steps >=1.1.2,
118117
text,
119118
transformers,

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ import Cardano.Ledger.Slot (EpochNo)
9595
import Cardano.Ledger.Val (zero, (<->))
9696
import Control.DeepSeq (NFData)
9797
import Control.Monad (guard)
98-
import Control.SetAlgebra (eval, (⨃))
9998
import Control.State.Transition (
10099
Embed (..),
101100
STS (..),
@@ -297,20 +296,12 @@ epochTransition = do
297296
utxoState0 = lsUTxOState ledgerState0
298297
certState0 = ledgerState0 ^. lsCertStateL
299298
vState = certState0 ^. certVStateL
300-
pState0 = certState0 ^. certPStateL
301299
snapshots1 <-
302300
trans @(EraRule "SNAP" era) $ TRC (SnapEnv ledgerState0 curPParams, snapshots0, ())
303301

304-
-- Activate future StakePools
305-
let newStakePools = eval (psStakePools pState0 psFutureStakePools pState0)
306-
pState1 =
307-
pState0
308-
{ psStakePools = newStakePools
309-
, psFutureStakePools = Map.empty
310-
}
311302
PoolreapState utxoState1 chainAccountState1 certState1 <-
312303
trans @(EraRule "POOLREAP" era) $
313-
TRC ((), PoolreapState utxoState0 chainAccountState0 (certState0 & certPStateL .~ pState1), eNo)
304+
TRC ((), PoolreapState utxoState0 chainAccountState0 certState0, eNo)
314305

315306
let
316307
stakePoolDistr = ssStakeMarkPoolDistr snapshots1

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ import Cardano.Ledger.Shelley.Rules.Upec (ShelleyUPEC, ShelleyUpecPredFailure, U
6060
import Cardano.Ledger.Slot (EpochNo)
6161
import Cardano.Ledger.State
6262
import Control.DeepSeq (NFData)
63-
import Control.SetAlgebra (eval, (⨃))
6463
import Control.State.Transition (
6564
Embed (..),
6665
STS (..),
@@ -70,7 +69,6 @@ import Control.State.Transition (
7069
trans,
7170
)
7271
import Data.Default (Default)
73-
import qualified Data.Map.Strict as Map
7472
import Data.Void (Void)
7573
import GHC.Generics (Generic)
7674
import Lens.Micro
@@ -208,20 +206,12 @@ epochTransition = do
208206
let pp = es ^. curPParamsEpochStateL
209207
utxoSt = lsUTxOState ls
210208
certState = ls ^. lsCertStateL
211-
pstate = certState ^. certPStateL
212209
ss' <-
213210
trans @(EraRule "SNAP" era) $ TRC (SnapEnv ls pp, ss, ())
214211

215-
let PState stakePools futureStakePools _ _ = pstate
216-
ppp = eval (stakePools futureStakePools)
217-
pstate' =
218-
pstate
219-
{ psStakePools = ppp
220-
, psFutureStakePools = Map.empty
221-
}
222212
PoolreapState utxoSt' chainAccountState' adjustedCertState <-
223213
trans @(EraRule "POOLREAP" era) $
224-
TRC ((), PoolreapState utxoSt chainAccountState (certState & certPStateL .~ pstate'), e)
214+
TRC ((), PoolreapState utxoSt chainAccountState certState, e)
225215

226216
let ls' = ls {lsUTxOState = utxoSt', lsCertState = adjustedCertState}
227217

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,11 @@ import Cardano.Ledger.Shelley.Era (
5050
ShelleyPOOL,
5151
hardforkAlonzoValidatePoolRewardAccountNetID,
5252
)
53-
import Cardano.Ledger.Shelley.LedgerState (PState (..), payPoolDeposit)
5453
import qualified Cardano.Ledger.Shelley.SoftForks as SoftForks
55-
import Cardano.Ledger.State (PoolMetadata (..), PoolParams (..), mkStakePoolState)
54+
import Cardano.Ledger.State
5655
import Control.DeepSeq
5756
import Control.Monad (forM_, when)
5857
import Control.Monad.Trans.Reader (asks)
59-
import Control.SetAlgebra (dom, eval, setSingleton, singleton, (∈), (∉), (⋪), (⨃))
6058
import Control.State.Transition (
6159
STS (..),
6260
TRC (..),
@@ -68,9 +66,10 @@ import Control.State.Transition (
6866
)
6967
import qualified Data.ByteString as BS
7068
import Data.Kind (Type)
69+
import qualified Data.Map as Map
7170
import Data.Word (Word8)
7271
import GHC.Generics (Generic)
73-
import Lens.Micro ((^.))
72+
import Lens.Micro
7473
import NoThunks.Class (NoThunks (..))
7574

7675
data PoolEnv era
@@ -199,7 +198,7 @@ poolDelegationTransition ::
199198
poolDelegationTransition = do
200199
TRC
201200
( PoolEnv cEpoch pp
202-
, ps@PState {psStakePools, psFutureStakePools, psRetiring}
201+
, ps@PState {psStakePools}
203202
, poolCert
204203
) <-
205204
judgmentContext
@@ -234,13 +233,13 @@ poolDelegationTransition = do
234233
, mismatchExpected = minPoolCost
235234
}
236235

237-
if eval (ppId dom psStakePools)
236+
if not (Map.member ppId psStakePools)
238237
then do
239238
-- register new, Pool-Reg
240239
tellEvent $ RegisterPool ppId
241240
pure $
242241
payPoolDeposit ppId pp $
243-
ps {psStakePools = eval (psStakePools singleton ppId (mkStakePoolState poolParams))}
242+
ps & psStakePoolsL %~ Map.insert ppId (mkStakePoolState poolParams)
244243
else do
245244
tellEvent $ ReregisterPool ppId
246245
-- hk is already registered, so we want to reregister it. That means adding it
@@ -254,12 +253,10 @@ poolDelegationTransition = do
254253
-- the if statement.
255254
pure $
256255
ps
257-
{ psFutureStakePools =
258-
eval (psFutureStakePools singleton ppId (mkStakePoolState poolParams))
259-
, psRetiring = eval (setSingleton ppId psRetiring)
260-
}
261-
RetirePool hk e -> do
262-
eval (hk dom psStakePools) ?! StakePoolNotRegisteredOnKeyPOOL hk
256+
& psFutureStakePoolsL %~ Map.insert ppId (mkStakePoolState poolParams)
257+
& psRetiringL %~ Map.delete ppId
258+
RetirePool ppId e -> do
259+
Map.member ppId psStakePools ?! StakePoolNotRegisteredOnKeyPOOL ppId
263260
let maxEpoch = pp ^. ppEMaxL
264261
limitEpoch = addEpochInterval cEpoch maxEpoch
265262
(cEpoch < e && e <= limitEpoch)
@@ -273,4 +270,4 @@ poolDelegationTransition = do
273270
, mismatchExpected = limitEpoch
274271
}
275272
-- We just schedule it for retirement. When it is retired we refund the deposit (see POOLREAP)
276-
pure $ ps {psRetiring = eval (psRetiring singleton hk e)}
273+
pure $ ps & psRetiringL %~ Map.insert ppId e

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import Cardano.Ledger.Slot (EpochNo (..))
3737
import Cardano.Ledger.State
3838
import Cardano.Ledger.Val ((<+>), (<->))
3939
import Control.DeepSeq (NFData)
40-
import Control.SetAlgebra (dom, eval, setSingleton, (⋪), (▷), (◁))
4140
import Control.State.Transition (
4241
Assertion (..),
4342
AssertionViolation (..),
@@ -51,7 +50,7 @@ import Data.Default (Default, def)
5150
import Data.Foldable (fold)
5251
import qualified Data.Map.Strict as Map
5352
import Data.Set (Set)
54-
import qualified Data.Set as Set (member)
53+
import qualified Data.Set as Set
5554
import GHC.Generics (Generic)
5655
import Lens.Micro
5756
import NoThunks.Class (NoThunks (..))
@@ -126,21 +125,28 @@ instance
126125

127126
poolReapTransition :: forall era. EraCertState era => TransitionRule (ShelleyPOOLREAP era)
128127
poolReapTransition = do
129-
TRC (_, PoolreapState us a cs, e) <- judgmentContext
130-
128+
TRC (_, PoolreapState us a cs0, e) <- judgmentContext
131129
let
132-
ps = cs ^. certPStateL
130+
ps0 = cs0 ^. certPStateL
131+
-- activate future stakePools
132+
ps =
133+
ps0
134+
{ psStakePools = Map.union (ps0 ^. psFutureStakePoolsL) (ps0 ^. psStakePoolsL)
135+
, psFutureStakePools = Map.empty
136+
}
137+
cs = cs0 & certPStateL .~ ps
138+
133139
ds = cs ^. certDStateL
134140
-- The set of pools retiring this epoch
135141
retired :: Set (KeyHash 'StakePool)
136-
retired = eval (dom (psRetiring ps setSingleton e))
142+
retired = Set.fromDistinctAscList [k | (k, v) <- Map.toAscList (psRetiring ps), v == e]
137143
-- The Map of pools (retiring this epoch) to their deposits
138144
retiringDeposits, remainingDeposits :: Map.Map (KeyHash 'StakePool) (CompactForm Coin)
139145
(retiringDeposits, remainingDeposits) =
140146
Map.partitionWithKey (\k _ -> Set.member k retired) (psDeposits ps)
141147
-- collect all accounts for stake pools that will retire
142148
retiredStakePoolAccounts :: Map.Map (KeyHash 'StakePool) RewardAccount
143-
retiredStakePoolAccounts = Map.map spsRewardAccount $ eval (retired psStakePools ps)
149+
retiredStakePoolAccounts = Map.map spsRewardAccount $ Map.restrictKeys (psStakePools ps) retired
144150
retiredStakePoolAccountsWithRefund :: Map.Map (KeyHash 'StakePool) (RewardAccount, CompactForm Coin)
145151
retiredStakePoolAccountsWithRefund = Map.intersectionWith (,) retiredStakePoolAccounts retiringDeposits
146152
-- collect all of the potential refunds
@@ -183,9 +189,8 @@ poolReapTransition = do
183189
( cs
184190
& certDStateL . accountsL
185191
%~ removeStakePoolDelegations retired . addToBalanceAccounts refunds
186-
& certPStateL . psStakePoolsL %~ (eval . (retired ))
187-
& certPStateL . psFutureStakePoolsL %~ (eval . (retired ))
188-
& certPStateL . psRetiringL %~ (eval . (retired ))
192+
& certPStateL . psStakePoolsL %~ (`Map.withoutKeys` retired)
193+
& certPStateL . psRetiringL %~ (`Map.withoutKeys` retired)
189194
& certPStateL . psDepositsCompactL .~ remainingDeposits
190195
)
191196

0 commit comments

Comments
 (0)