@@ -78,22 +78,21 @@ import Cardano.Ledger.DRep (DRep (..), DRepState (..))
78
78
import Cardano.Ledger.Hashes (GenDelegPair (.. ), GenDelegs (.. ))
79
79
import Cardano.Ledger.Slot (EpochNo (.. ), SlotNo (.. ))
80
80
import Cardano.Ledger.State.Account
81
- import Cardano.Ledger.State.StakePool (StakePoolState )
81
+ import Cardano.Ledger.State.StakePool (StakePoolState ( .. ), spsDepositL )
82
82
import Control.DeepSeq (NFData (.. ))
83
83
import Control.Monad.Trans
84
84
import Data.Aeson (ToJSON (.. ), object , (.=) )
85
85
import Data.Default (Default (def ))
86
86
import qualified Data.Foldable as F
87
87
import Data.Kind (Type )
88
+ import qualified Data.Map.Merge.Strict as Map
88
89
import Data.Map.Strict (Map )
89
90
import qualified Data.Map.Strict as Map
90
91
import qualified Data.Set as Set
91
92
import GHC.Generics (Generic )
92
- import Lens.Micro ( Lens' , lens , (^.) , _1 )
93
+ import Lens.Micro
93
94
import NoThunks.Class (NoThunks (.. ))
94
95
95
- -- ======================================
96
-
97
96
data FutureGenDeleg = FutureGenDeleg
98
97
{ fGenDelegSlot :: ! SlotNo
99
98
, fGenDelegGenKeyHash :: ! (KeyHash 'Genesis)
@@ -235,8 +234,6 @@ data PState era = PState
235
234
-- of the Shelley Ledger Specification for a sequence diagram.
236
235
, psRetiring :: ! (Map (KeyHash 'StakePool) EpochNo )
237
236
-- ^ A map of retiring stake pools to the epoch when they retire.
238
- , psDeposits :: ! (Map (KeyHash 'StakePool) (CompactForm Coin ))
239
- -- ^ A map of the deposits for each pool
240
237
}
241
238
deriving (Show , Eq , Generic )
242
239
deriving (ToJSON ) via KeyValuePairs (PState era )
@@ -246,17 +243,16 @@ instance NoThunks (PState era)
246
243
instance NFData (PState era )
247
244
248
245
instance Era era => EncCBOR (PState era ) where
249
- encCBOR (PState a b c d ) =
250
- encodeListLen 4 <> encCBOR a <> encCBOR b <> encCBOR c <> encCBOR d
246
+ encCBOR (PState a b c) =
247
+ encodeListLen 3 <> encCBOR a <> encCBOR b <> encCBOR c
251
248
252
249
instance DecShareCBOR (PState era ) where
253
250
type Share (PState era ) = Interns (KeyHash 'StakePool)
254
251
decSharePlusCBOR = decodeRecordNamedT " PState" (const 4 ) $ do
255
252
psStakePools <- decSharePlusLensCBOR (toMemptyLens _1 id )
256
253
psFutureStakePools <- decSharePlusLensCBOR (toMemptyLens _1 id )
257
254
psRetiring <- decSharePlusLensCBOR (toMemptyLens _1 id )
258
- psDeposits <- decSharePlusLensCBOR (toMemptyLens _1 id )
259
- pure PState {psStakePools, psFutureStakePools, psRetiring, psDeposits}
255
+ pure PState {psStakePools, psFutureStakePools, psRetiring}
260
256
261
257
instance (Era era , DecShareCBOR (PState era )) => DecCBOR (PState era ) where
262
258
decCBOR = decNoShareCBOR
@@ -266,7 +262,6 @@ instance ToKeyValuePairs (PState era) where
266
262
[ " stakePools" .= psStakePools
267
263
, " futureStakePools" .= psFutureStakePools
268
264
, " retiring" .= psRetiring
269
- , " deposits" .= psDeposits
270
265
]
271
266
272
267
data CommitteeAuthorization
@@ -396,35 +391,27 @@ instance Default (Accounts era) => Default (DState era) where
396
391
397
392
instance Default (PState era ) where
398
393
def =
399
- PState Map. empty Map. empty Map. empty Map. empty
400
-
401
- -- ==========================================================
402
- -- Functions that handle Deposits
403
-
404
- -- | One only pays a deposit on the initial pool registration. So return the
405
- -- the Deposits unchanged if the keyhash already exists. There are legal
406
- -- situations where a pool may be registered multiple times.
407
- payPoolDeposit ::
408
- EraPParams era =>
409
- KeyHash 'StakePool ->
410
- PParams era ->
411
- PState era ->
412
- PState era
413
- payPoolDeposit keyhash pp pstate = pstate {psDeposits = newpool}
394
+ PState Map. empty Map. empty Map. empty
395
+
396
+ -- | One only pays a deposit on the initial pool registration. So we return the
397
+ -- StakePoolState unchanged if the deposit is already non-zero.
398
+ --
399
+ -- NOTE: There are legal situations where a pool may be registered multiple times.
400
+ payPoolDeposit :: EraPParams era => KeyHash 'StakePool -> PParams era -> PState era -> PState era
401
+ payPoolDeposit keyhash pp pstate = pstate & psStakePoolsL %~ Map. adjust payDeposit keyhash
414
402
where
415
- pool = psDeposits pstate
416
403
! deposit = pp ^. ppPoolDepositCompactL
417
- newpool
418
- | Map. notMember keyhash pool = Map. insert keyhash deposit pool
419
- | otherwise = pool
404
+ payDeposit sps
405
+ | sps ^. spsDepositL <= mempty = sps & spsDepositL .~ deposit
406
+ | otherwise = sps
420
407
421
408
refundPoolDeposit :: KeyHash 'StakePool -> PState era -> (CompactForm Coin , PState era )
422
- refundPoolDeposit keyhash pstate = (coin, pstate {psDeposits = newpool})
423
- where
424
- pool = psDeposits pstate
425
- (coin, newpool) = case Map. lookup keyhash pool of
426
- Just c -> (c, Map. delete keyhash pool)
427
- Nothing -> ( mempty , pool )
409
+ refundPoolDeposit keyhash pstate =
410
+ ( -- The actual deposit or mempty
411
+ maybe mempty spsDeposit $ Map. lookup keyhash $ pstate ^. psStakePoolsL
412
+ , -- The PState, updated with the deposit set to mempty
413
+ pstate & psStakePoolsL %~ Map. adjust (spsDepositL .~ mempty ) keyhash
414
+ )
428
415
429
416
-- | A composite of all the Deposits the system is obligated to eventually pay back.
430
417
data Obligations = Obligations
@@ -462,12 +449,6 @@ instance Show Obligations where
462
449
, " Proposal deposits = " ++ show (oblProposal x)
463
450
]
464
451
465
- -- =======================================================
466
- -- Lenses for CertState and its subsidiary types
467
-
468
- -- ===================================
469
- -- DState
470
-
471
452
dsGenDelegsL :: Lens' (DState era ) GenDelegs
472
453
dsGenDelegsL = lens dsGenDelegs (\ ds u -> ds {dsGenDelegs = u})
473
454
@@ -478,9 +459,6 @@ dsFutureGenDelegsL ::
478
459
Lens' (DState era ) (Map FutureGenDeleg GenDelegPair )
479
460
dsFutureGenDelegsL = lens dsFutureGenDelegs (\ ds u -> ds {dsFutureGenDelegs = u})
480
461
481
- -- ===================================
482
- -- PState
483
-
484
462
psStakePoolsL :: Lens' (PState era ) (Map (KeyHash 'StakePool) StakePoolState )
485
463
psStakePoolsL = lens psStakePools (\ ds u -> ds {psStakePools = u})
486
464
@@ -494,4 +472,15 @@ psDepositsL :: Lens' (PState era) (Map (KeyHash 'StakePool) Coin)
494
472
psDepositsL = psDepositsCompactL . lens (fmap fromCompact) (\ _ -> fmap compactCoinOrError)
495
473
496
474
psDepositsCompactL :: Lens' (PState era ) (Map (KeyHash 'StakePool) (CompactForm Coin ))
497
- psDepositsCompactL = lens psDeposits (\ ds u -> ds {psDeposits = u})
475
+ psDepositsCompactL =
476
+ lens
477
+ (fmap spsDeposit . psStakePools)
478
+ ( \ pstate deposits ->
479
+ pstate
480
+ & psStakePoolsL
481
+ %~ Map. merge
482
+ Map. dropMissing
483
+ Map. preserveMissing
484
+ (Map. zipWithMatched (\ _key deposit sps -> sps {spsDeposit = deposit}))
485
+ deposits
486
+ )
0 commit comments