Skip to content

Commit af31396

Browse files
authored
Merge pull request #5264 from IntersectMBO/aniketd/pstate-query
Add queryPoolState state query
2 parents c3d4f5d + fde058f commit af31396

File tree

5 files changed

+88
-2
lines changed

5 files changed

+88
-2
lines changed

libs/cardano-ledger-api/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 1.12.0.0
44

5+
* Add `queryPoolState` and bring back `queryPoolParameters` state query.
56
* Add `queryDRepDelegations` state query
67
* Remove `filterStakePoolDelegsAndRewards` as unnecessary. Use `queryStakePoolDelegsAndRewards` instead
78
* Expose `binaryUpgradeTx`, `binaryUpgradeTxBody`, `binaryUpgradeTxWits`, `binaryUpgradeTxAuxData`, `upgradeTx`, `upgradeTxBody`, `upgradeTxWits`, `upgradeTxAuxData`

libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query.hs

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
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

712
module Cardano.Ledger.Api.State.Query (
813
-- * @GetFilteredDelegationsAndRewardAccounts@
@@ -58,10 +63,16 @@ module Cardano.Ledger.Api.State.Query (
5863
-- * @GetRatifyState@
5964
queryRatifyState,
6065

61-
-- * @GetStakePoolDefaultVote
66+
-- * @GetStakePoolDefaultVote@
6267
queryStakePoolDefaultVote,
6368
DefaultVote (..),
6469

70+
-- * @GetPoolState@
71+
queryPoolParameters,
72+
queryPoolState,
73+
QueryPoolStateResult (..),
74+
mkQueryPoolStateResult,
75+
6576
-- * For testing
6677
getNextEpochCommitteeMembers,
6778
) where
@@ -74,6 +85,7 @@ import Cardano.Ledger.Api.State.Query.CommitteeMembersState (
7485
NextEpochChange (..),
7586
)
7687
import Cardano.Ledger.BaseTypes (EpochNo, strictMaybeToMaybe)
88+
import Cardano.Ledger.Binary
7789
import Cardano.Ledger.Coin (Coin (..), CompactForm (..))
7890
import Cardano.Ledger.Compactible (fromCompact)
7991
import Cardano.Ledger.Conway.Governance (
@@ -426,3 +438,61 @@ queryStakePoolDefaultVote ::
426438
queryStakePoolDefaultVote nes poolId =
427439
defaultStakePoolVote poolId (nes ^. nesEsL . epochStateStakePoolsL) $
428440
nes ^. nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL
441+
442+
-- | Used only for the `queryPoolState` query. This resembles the older way of
443+
-- representing StakePoolState in Ledger.
444+
data QueryPoolStateResult = QueryPoolStateResult
445+
{ qpsrStakePoolParams :: !(Map (KeyHash 'StakePool) PoolParams)
446+
, qpsrFutureStakePoolParams :: !(Map (KeyHash 'StakePool) PoolParams)
447+
, qpsrRetiring :: !(Map (KeyHash 'StakePool) EpochNo)
448+
, qpsrDeposits :: !(Map (KeyHash 'StakePool) Coin)
449+
}
450+
deriving (Show, Eq)
451+
452+
instance EncCBOR QueryPoolStateResult where
453+
encCBOR (QueryPoolStateResult a b c d) =
454+
encodeListLen 4 <> encCBOR a <> encCBOR b <> encCBOR c <> encCBOR d
455+
456+
instance DecCBOR QueryPoolStateResult where
457+
decCBOR = decodeRecordNamed "QueryPoolStateResult" (const 4) $ do
458+
qpsrStakePoolParams <- decCBOR
459+
qpsrFutureStakePoolParams <- decCBOR
460+
qpsrRetiring <- decCBOR
461+
qpsrDeposits <- decCBOR
462+
pure
463+
QueryPoolStateResult {qpsrStakePoolParams, qpsrFutureStakePoolParams, qpsrRetiring, qpsrDeposits}
464+
465+
mkQueryPoolStateResult ::
466+
(forall x. Map.Map (KeyHash 'StakePool) x -> Map.Map (KeyHash 'StakePool) x) ->
467+
PState era ->
468+
QueryPoolStateResult
469+
mkQueryPoolStateResult f ps =
470+
QueryPoolStateResult
471+
{ qpsrStakePoolParams = Map.mapWithKey stakePoolStateToPoolParams restrictedStakePools
472+
, qpsrFutureStakePoolParams = Map.mapWithKey stakePoolStateToPoolParams restrictedStakePools
473+
, qpsrRetiring = f $ psRetiring ps
474+
, qpsrDeposits = Map.map (fromCompact . spsDeposit) restrictedStakePools
475+
}
476+
where
477+
restrictedStakePools = f $ psStakePools ps
478+
479+
-- | Query the QueryPoolStateResult. This is slightly different from the internal
480+
-- representation used by Ledger and is intended to resemble how the internal
481+
-- representation used to be.
482+
queryPoolState ::
483+
EraCertState era => NewEpochState era -> Maybe (Set (KeyHash 'StakePool)) -> QueryPoolStateResult
484+
queryPoolState nes mPoolKeys =
485+
let pstate = nes ^. nesEsL . esLStateL . lsCertStateL . certPStateL
486+
f :: forall x. Map.Map (KeyHash 'StakePool) x -> Map.Map (KeyHash 'StakePool) x
487+
f = case mPoolKeys of
488+
Nothing -> id
489+
Just keys -> (`Map.restrictKeys` keys)
490+
in mkQueryPoolStateResult f pstate
491+
492+
-- | Query the current PoolParams.
493+
queryPoolParameters ::
494+
EraCertState era =>
495+
NewEpochState era -> Set (KeyHash 'StakePool) -> Map (KeyHash 'StakePool) PoolParams
496+
queryPoolParameters nes poolKeys =
497+
let pools = nes ^. nesEsL . esLStateL . lsCertStateL . certPStateL . psStakePoolsL
498+
in Map.mapWithKey stakePoolStateToPoolParams $ Map.restrictKeys pools poolKeys

libs/cardano-ledger-api/test/Test/Cardano/Ledger/Api/State/QuerySpec.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import Cardano.Ledger.Api.State.Query (
1616
HotCredAuthStatus (..),
1717
MemberStatus (..),
1818
NextEpochChange (..),
19+
QueryPoolStateResult,
1920
getNextEpochCommitteeMembers,
2021
queryCommitteeMembersState,
2122
queryStakePoolDelegsAndRewards,
@@ -54,12 +55,22 @@ import Test.Cardano.Ledger.Common
5455
import Test.Cardano.Ledger.Conway.Arbitrary ()
5556
import Test.Cardano.Ledger.Conway.Era (ShelleyEraTest)
5657
import Test.Cardano.Ledger.Core.Arbitrary (genValidUMapWithCreds)
58+
import Test.Cardano.Ledger.Core.Binary.RoundTrip (roundTripEraExpectation)
5759
import Test.Cardano.Ledger.Era (accountsFromUMap)
5860
import Test.Cardano.Ledger.Shelley.Arbitrary ()
5961
import Test.Cardano.Slotting.Numeric ()
6062

6163
spec :: Spec
6264
spec = do
65+
describe "API Types" $ do
66+
describe "Roundtrip" $ do
67+
prop "Shelley" $ roundTripEraExpectation @ShelleyEra @QueryPoolStateResult
68+
prop "Allegra" $ roundTripEraExpectation @AllegraEra @QueryPoolStateResult
69+
prop "Mary" $ roundTripEraExpectation @MaryEra @QueryPoolStateResult
70+
prop "Alonzo" $ roundTripEraExpectation @AlonzoEra @QueryPoolStateResult
71+
prop "Babbage" $ roundTripEraExpectation @BabbageEra @QueryPoolStateResult
72+
prop "Conway" $ roundTripEraExpectation @ConwayEra @QueryPoolStateResult
73+
prop "Dijkstra" $ roundTripEraExpectation @DijkstraEra @QueryPoolStateResult
6374
queryStakePoolDelegsAndRewardsSpec @ShelleyEra
6475
queryStakePoolDelegsAndRewardsSpec @AllegraEra
6576
queryStakePoolDelegsAndRewardsSpec @MaryEra

libs/cardano-ledger-api/testlib/Test/Cardano/Ledger/Api/Arbitrary.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
module Test.Cardano.Ledger.Api.Arbitrary () where
44

5-
import Cardano.Ledger.Api.State.Query (MemberStatus)
5+
import Cardano.Ledger.Api.State.Query (MemberStatus, QueryPoolStateResult (..))
66
import Test.Cardano.Ledger.Common
77
import Test.Cardano.Ledger.Dijkstra.Arbitrary ()
88

99
instance Arbitrary MemberStatus where
1010
arbitrary = arbitraryBoundedEnum
11+
12+
instance Arbitrary QueryPoolStateResult where
13+
arbitrary = QueryPoolStateResult <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary

libs/cardano-ledger-api/testlib/Test/Cardano/Ledger/Api/Upgrade.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import Cardano.Ledger.Core
1717
import Cardano.Ledger.MemoBytes (EqRaw (eqRaw))
1818
import Data.Default (Default (def))
1919
import qualified Prettyprinter as Pretty
20+
import Test.Cardano.Ledger.Api.Arbitrary ()
2021
import Test.Cardano.Ledger.Binary.RoundTrip
2122
import Test.Cardano.Ledger.Common
2223
import Test.Cardano.Ledger.Core.Arbitrary ()

0 commit comments

Comments
 (0)