11{-# LANGUAGE DataKinds #-}
2+ {-# LANGUAGE FlexibleContexts #-}
23{-# LANGUAGE GADTs #-}
4+ {-# LANGUAGE NamedFieldPuns #-}
35{-# LANGUAGE OverloadedStrings #-}
6+ {-# LANGUAGE RankNTypes #-}
47{-# LANGUAGE RecordWildCards #-}
58{-# LANGUAGE ScopedTypeVariables #-}
9+ {-# LANGUAGE TypeFamilies #-}
10+ {-# LANGUAGE UndecidableInstances #-}
611
712module Cardano.Ledger.Api.State.Query (
813 -- * @GetFilteredDelegationsAndRewardAccounts@
@@ -58,10 +63,14 @@ module Cardano.Ledger.Api.State.Query (
5863 -- * @GetRatifyState@
5964 queryRatifyState ,
6065
61- -- * @GetStakePoolDefaultVote
66+ -- * @GetStakePoolDefaultVote@
6267 queryStakePoolDefaultVote ,
6368 DefaultVote (.. ),
6469
70+ -- * @GetPoolState@
71+ queryPoolState ,
72+ PState' (.. ),
73+
6574 -- * For testing
6675 getNextEpochCommitteeMembers ,
6776) where
@@ -74,6 +83,7 @@ import Cardano.Ledger.Api.State.Query.CommitteeMembersState (
7483 NextEpochChange (.. ),
7584 )
7685import Cardano.Ledger.BaseTypes (EpochNo , strictMaybeToMaybe )
86+ import Cardano.Ledger.Binary
7787import Cardano.Ledger.Coin (Coin (.. ), CompactForm (.. ))
7888import Cardano.Ledger.Compactible (fromCompact )
7989import Cardano.Ledger.Conway.Governance (
@@ -426,3 +436,48 @@ queryStakePoolDefaultVote ::
426436queryStakePoolDefaultVote nes poolId =
427437 defaultStakePoolVote poolId (nes ^. nesEsL . epochStateStakePoolsL) $
428438 nes ^. nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL
439+
440+ -- | Used only for the `queryPoolState` query. This resembles the older way of
441+ -- representing StakePoolState in Ledger.
442+ data PState' = PState'
443+ { psStakePoolParams :: ! (Map (KeyHash 'StakePool) PoolParams )
444+ , psFutureStakePoolParams :: ! (Map (KeyHash 'StakePool) PoolParams )
445+ , psRetiring' :: ! (Map (KeyHash 'StakePool) EpochNo )
446+ , psDeposits :: ! (Map (KeyHash 'StakePool) Coin )
447+ }
448+ deriving (Show , Eq )
449+
450+ instance EncCBOR PState' where
451+ encCBOR (PState' a b c d) =
452+ encodeListLen 4 <> encCBOR a <> encCBOR b <> encCBOR c <> encCBOR d
453+
454+ instance DecShareCBOR PState' where
455+ type Share PState' = Interns (KeyHash 'StakePool)
456+ decSharePlusCBOR = decodeRecordNamedT " PState'" (const 4 ) $ do
457+ psStakePoolParams <- decSharePlusLensCBOR (toMemptyLens _1 id )
458+ psFutureStakePoolParams <- decSharePlusLensCBOR (toMemptyLens _1 id )
459+ psRetiring' <- decSharePlusLensCBOR (toMemptyLens _1 id )
460+ psDeposits <- decSharePlusLensCBOR (toMemptyLens _1 id )
461+ pure PState' {psStakePoolParams, psFutureStakePoolParams, psRetiring', psDeposits}
462+
463+ instance DecShareCBOR PState' => DecCBOR PState' where
464+ decCBOR = decNoShareCBOR
465+
466+ -- | Query the PState'. This is slightly different from the internal
467+ -- representation used by Ledger and is intended to resemble how the internal
468+ -- representation used to be.
469+ queryPoolState ::
470+ EraCertState era => NewEpochState era -> Maybe (Set (KeyHash 'StakePool)) -> PState'
471+ queryPoolState nes mPoolKeys =
472+ let pstate = nes ^. nesEsL . esLStateL . lsCertStateL . certPStateL
473+ mkPState' :: (forall b . Map. Map (KeyHash 'StakePool) b -> Map. Map (KeyHash 'StakePool) b ) -> PState'
474+ mkPState' f =
475+ PState'
476+ { psStakePoolParams = Map. mapWithKey stakePoolStateToPoolParams $ f $ psStakePools pstate
477+ , psFutureStakePoolParams = Map. mapWithKey stakePoolStateToPoolParams $ f $ psFutureStakePools pstate
478+ , psRetiring' = f $ psRetiring pstate
479+ , psDeposits = Map. map (fromCompact . spsDeposit) $ f $ psStakePools pstate
480+ }
481+ in case mPoolKeys of
482+ Nothing -> mkPState' id
483+ Just keys -> mkPState' (`Map.restrictKeys` keys)
0 commit comments