Skip to content

Commit f6f9e89

Browse files
committed
Refactor mkPState' and add tests
1 parent 7e606db commit f6f9e89

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ module Cardano.Ledger.Api.State.Query (
7070
-- * @GetPoolState@
7171
queryPoolState,
7272
PState' (..),
73+
mkPState',
7374

7475
-- * For testing
7576
getNextEpochCommitteeMembers,
@@ -463,21 +464,25 @@ instance DecShareCBOR PState' where
463464
instance DecShareCBOR PState' => DecCBOR PState' where
464465
decCBOR = decNoShareCBOR
465466

467+
mkPState' ::
468+
(forall b. Map.Map (KeyHash 'StakePool) b -> Map.Map (KeyHash 'StakePool) b) ->
469+
PState era ->
470+
PState'
471+
mkPState' f ps =
472+
PState'
473+
{ psStakePoolParams = Map.mapWithKey stakePoolStateToPoolParams $ f $ psStakePools ps
474+
, psFutureStakePoolParams = Map.mapWithKey stakePoolStateToPoolParams $ f $ psFutureStakePools ps
475+
, psRetiring' = f $ psRetiring ps
476+
, psDeposits = Map.map (fromCompact . spsDeposit) $ f $ psStakePools ps
477+
}
478+
466479
-- | Query the PState'. This is slightly different from the internal
467480
-- representation used by Ledger and is intended to resemble how the internal
468481
-- representation used to be.
469482
queryPoolState ::
470483
EraCertState era => NewEpochState era -> Maybe (Set (KeyHash 'StakePool)) -> PState'
471484
queryPoolState nes mPoolKeys =
472485
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-
}
481486
in case mPoolKeys of
482-
Nothing -> mkPState' id
483-
Just keys -> mkPState' (`Map.restrictKeys` keys)
487+
Nothing -> mkPState' id pstate
488+
Just keys -> mkPState' (`Map.restrictKeys` keys) pstate

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ import Cardano.Ledger.Api.State.Query (
1616
HotCredAuthStatus (..),
1717
MemberStatus (..),
1818
NextEpochChange (..),
19+
PState',
1920
getNextEpochCommitteeMembers,
21+
mkPState',
2022
queryCommitteeMembersState,
23+
queryPoolState,
2124
queryStakePoolDelegsAndRewards,
2225
)
2326
import Cardano.Ledger.BaseTypes
@@ -54,12 +57,30 @@ import Test.Cardano.Ledger.Common
5457
import Test.Cardano.Ledger.Conway.Arbitrary ()
5558
import Test.Cardano.Ledger.Conway.Era (ShelleyEraTest)
5659
import Test.Cardano.Ledger.Core.Arbitrary (genValidUMapWithCreds)
60+
import Test.Cardano.Ledger.Core.Binary.RoundTrip (roundTripShareEraExpectation)
5761
import Test.Cardano.Ledger.Era (accountsFromUMap)
5862
import Test.Cardano.Ledger.Shelley.Arbitrary ()
5963
import Test.Cardano.Slotting.Numeric ()
6064

6165
spec :: Spec
6266
spec = do
67+
describe "API Types" $ do
68+
describe "Roundtrip" $ do
69+
prop "Shelley" $ roundTripShareEraExpectation @ShelleyEra @PState'
70+
prop "Allegra" $ roundTripShareEraExpectation @AllegraEra @PState'
71+
prop "Mary" $ roundTripShareEraExpectation @MaryEra @PState'
72+
prop "Alonzo" $ roundTripShareEraExpectation @AlonzoEra @PState'
73+
prop "Babbage" $ roundTripShareEraExpectation @BabbageEra @PState'
74+
prop "Conway" $ roundTripShareEraExpectation @ConwayEra @PState'
75+
prop "Dijkstra" $ roundTripShareEraExpectation @DijkstraEra @PState'
76+
describe "QuerySpec" $ do
77+
describe "queryPoolState" $ do
78+
queryPoolStateSpec @ShelleyEra
79+
queryPoolStateSpec @AllegraEra
80+
queryPoolStateSpec @MaryEra
81+
queryPoolStateSpec @AlonzoEra
82+
queryPoolStateSpec @BabbageEra
83+
queryPoolStateSpec @ConwayEra
6384
queryStakePoolDelegsAndRewardsSpec @ShelleyEra
6485
queryStakePoolDelegsAndRewardsSpec @AllegraEra
6586
queryStakePoolDelegsAndRewardsSpec @MaryEra
@@ -70,6 +91,12 @@ spec = do
7091
committeeMembersStateSpec @ConwayEra
7192
committeeMembersStateSpec @DijkstraEra
7293

94+
queryPoolStateSpec :: forall era. ShelleyEraTest era => Spec
95+
queryPoolStateSpec =
96+
prop ("mkPState' works as expected - " <> eraName @era) $ \(ps :: PState era) ->
97+
let nes = def & nesEsL . esLStateL . lsCertStateL . certPStateL .~ ps
98+
in queryPoolState nes Nothing `shouldBe` mkPState' id ps
99+
73100
queryStakePoolDelegsAndRewardsSpec :: forall era. ShelleyEraTest era => Spec
74101
queryStakePoolDelegsAndRewardsSpec =
75102
describe (eraName @era) $ do

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, PState', mkPState')
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 PState' where
13+
arbitrary = mkPState' id <$> 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)