Skip to content

Commit c476239

Browse files
committed
Integrate the move of the GetStakeSnapshots query to Ledger
1 parent 3e3fa59 commit c476239

2 files changed

Lines changed: 37 additions & 24 deletions

File tree

cardano-api/src/Cardano/Api/Internal/Orphans/Serialisation.hs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import Cardano.Ledger.Alonzo.Rules qualified as Alonzo
5050
import Cardano.Ledger.Alonzo.Rules qualified as L
5151
import Cardano.Ledger.Alonzo.Tx qualified as L
5252
import Cardano.Ledger.Api qualified as L
53+
import Cardano.Ledger.Api.State.Query qualified as Ledger
5354
import Cardano.Ledger.Babbage.PParams qualified as Ledger
5455
import Cardano.Ledger.Babbage.Rules qualified as Babbage
5556
import Cardano.Ledger.Babbage.Rules qualified as L
@@ -313,18 +314,18 @@ instance Pretty L.AssetName where
313314
-- Orphan instances involved in the JSON output of the API queries.
314315
-- We will remove/replace these as we provide more API wrapper types
315316

316-
instance ToJSON Consensus.StakeSnapshots where
317+
instance ToJSON Ledger.StakeSnapshots where
317318
toJSON = object . stakeSnapshotsToPair
318319
toEncoding = pairs . mconcat . stakeSnapshotsToPair
319320

320321
stakeSnapshotsToPair
321-
:: Aeson.KeyValue e a => Consensus.StakeSnapshots -> [a]
322+
:: Aeson.KeyValue e a => Ledger.StakeSnapshots -> [a]
322323
stakeSnapshotsToPair
323-
Consensus.StakeSnapshots
324-
{ Consensus.ssStakeSnapshots
325-
, Consensus.ssMarkTotal
326-
, Consensus.ssSetTotal
327-
, Consensus.ssGoTotal
324+
Ledger.StakeSnapshots
325+
{ Ledger.ssStakeSnapshots
326+
, Ledger.ssMarkTotal
327+
, Ledger.ssSetTotal
328+
, Ledger.ssGoTotal
328329
} =
329330
[ "pools" .= ssStakeSnapshots
330331
, "total"
@@ -335,16 +336,16 @@ stakeSnapshotsToPair
335336
]
336337
]
337338

338-
instance ToJSON Consensus.StakeSnapshot where
339+
instance ToJSON Ledger.StakeSnapshot where
339340
toJSON = object . stakeSnapshotToPair
340341
toEncoding = pairs . mconcat . stakeSnapshotToPair
341342

342-
stakeSnapshotToPair :: Aeson.KeyValue e a => Consensus.StakeSnapshot -> [a]
343+
stakeSnapshotToPair :: Aeson.KeyValue e a => Ledger.StakeSnapshot -> [a]
343344
stakeSnapshotToPair
344-
Consensus.StakeSnapshot
345-
{ Consensus.ssMarkPool
346-
, Consensus.ssSetPool
347-
, Consensus.ssGoPool
345+
Ledger.StakeSnapshot
346+
{ Ledger.ssMarkPool
347+
, Ledger.ssSetPool
348+
, Ledger.ssGoPool
348349
} =
349350
[ "stakeMark" .= ssMarkPool
350351
, "stakeSet" .= ssSetPool

cardano-api/src/Cardano/Api/Query/Internal/Type/QueryInMode.hs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{-# LANGUAGE FlexibleContexts #-}
44
{-# LANGUAGE FlexibleInstances #-}
55
{-# LANGUAGE GADTs #-}
6+
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
67
{-# LANGUAGE InstanceSigs #-}
78
{-# LANGUAGE LambdaCase #-}
89
{-# LANGUAGE QuantifiedConstraints #-}
@@ -87,13 +88,11 @@ import Cardano.Api.Serialise.TextEnvelope.Internal
8788
import Cardano.Api.Tx.Internal.Body
8889
import Cardano.Api.UTxO (UTxO (..))
8990

90-
import Ouroboros.Consensus.Shelley.Ledger.SupportsProtocol ()
91-
import Ouroboros.Consensus.Cardano.CanHardFork ()
92-
import Ouroboros.Consensus.HardFork.Combinator.Ledger ()
9391
import Cardano.Binary qualified as CBOR
9492
import Cardano.Chain.Update.Validation.Interface qualified as Byron.Update
9593
import Cardano.Ledger.Api qualified as L
9694
import Cardano.Ledger.Api.State.Query qualified as L
95+
import Cardano.Ledger.Api.State.Query qualified as Ledger
9796
import Cardano.Ledger.Binary
9897
import Cardano.Ledger.Binary.Plain qualified as Plain
9998
import Cardano.Ledger.Coin qualified as L
@@ -108,15 +107,18 @@ import Ouroboros.Consensus.BlockchainTime.WallClock.Types (RelativeTime, SlotLen
108107
import Ouroboros.Consensus.Byron.Ledger qualified as Consensus
109108
import Ouroboros.Consensus.Cardano.Block (LedgerState (..), StandardCrypto)
110109
import Ouroboros.Consensus.Cardano.Block qualified as Consensus
110+
import Ouroboros.Consensus.Cardano.CanHardFork ()
111111
import Ouroboros.Consensus.HardFork.Combinator qualified as Consensus
112112
import Ouroboros.Consensus.HardFork.Combinator.AcrossEras (EraMismatch)
113113
import Ouroboros.Consensus.HardFork.Combinator.AcrossEras qualified as Consensus
114+
import Ouroboros.Consensus.HardFork.Combinator.Ledger ()
114115
import Ouroboros.Consensus.HardFork.History qualified as Consensus
115116
import Ouroboros.Consensus.HardFork.History qualified as History
116117
import Ouroboros.Consensus.HardFork.History.Qry qualified as Qry
117118
import Ouroboros.Consensus.Ledger.Query qualified as Consensus
118119
import Ouroboros.Consensus.Protocol.Abstract qualified as Consensus
119120
import Ouroboros.Consensus.Shelley.Ledger qualified as Consensus
121+
import Ouroboros.Consensus.Shelley.Ledger.SupportsProtocol ()
120122
import Ouroboros.Network.Block (Serialised (..))
121123
import Ouroboros.Network.PeerSelection.LedgerPeers.Type qualified as Diffusion
122124
import Ouroboros.Network.Protocol.LocalStateQuery.Client (Some (..))
@@ -422,15 +424,19 @@ decodePoolDistribution sbe (Serialised ls) =
422424

423425
newtype SerialisedStakeSnapshots era
424426
= SerialisedStakeSnapshots
425-
(Serialised Consensus.StakeSnapshots)
427+
(Serialised Ledger.StakeSnapshots)
426428

427-
newtype StakeSnapshot era = StakeSnapshot Consensus.StakeSnapshots
429+
newtype StakeSnapshot era = StakeSnapshot Ledger.StakeSnapshots
430+
deriving newtype (EncCBOR, DecCBOR)
428431

429432
decodeStakeSnapshot
430433
:: forall era
431-
. SerialisedStakeSnapshots era
434+
. ShelleyBasedEra era
435+
-> SerialisedStakeSnapshots era
432436
-> Either DecoderError (StakeSnapshot era)
433-
decodeStakeSnapshot (SerialisedStakeSnapshots (Serialised ls)) = StakeSnapshot <$> Plain.decodeFull ls
437+
decodeStakeSnapshot sbe (SerialisedStakeSnapshots (Serialised ls)) =
438+
shelleyBasedEraConstraints sbe $
439+
decodeFull (Core.eraProtVerHigh @(ShelleyLedgerEra era)) ls
434440

435441
decodeLedgerPeerSnapshot
436442
:: Consensus.ShelleyNodeToClientVersion
@@ -613,11 +619,17 @@ toConsensusQueryShelleyBased sbe = \case
613619
(Consensus.GetCBOR (Consensus.GetPoolState (Set.map unStakePoolKeyHash <$> poolIds)))
614620
)
615621
QueryStakeSnapshot mPoolIds ->
616-
Some
617-
( consensusQueryInEraInMode
618-
era
619-
(Consensus.GetCBOR (Consensus.GetStakeSnapshots (fmap (Set.map unStakePoolKeyHash) mPoolIds)))
622+
caseShelleyToBabbageOrConwayEraOnwards
623+
(const $ error "toConsensusQueryShelleyBased: QueryStakeSnapshot is only available in the Conway era")
624+
( const $
625+
( Some
626+
( consensusQueryInEraInMode
627+
era
628+
(Consensus.GetCBOR (Consensus.GetStakeSnapshots (fmap (Set.map unStakePoolKeyHash) mPoolIds)))
629+
)
630+
)
620631
)
632+
sbe
621633
QueryPoolDistribution poolIds ->
622634
Some
623635
(consensusQueryInEraInMode era (Consensus.GetCBOR (Consensus.GetPoolDistr2 (getPoolIds <$> poolIds))))

0 commit comments

Comments
 (0)