Skip to content

Commit 780dae1

Browse files
committed
Optimise query leadership-schedule command
1 parent aed8e71 commit 780dae1

File tree

5 files changed

+54
-23
lines changed

5 files changed

+54
-23
lines changed

Diff for: cabal.project

+2-2
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,8 @@ source-repository-package
263263
source-repository-package
264264
type: git
265265
location: https://github.com/input-output-hk/ouroboros-network
266-
tag: c764553561bed8978d2c6753d1608dc65449617a
267-
--sha256: 0hdh7xdrvxw943r6qr0xr4kwszindh5mnsn1lww6qdnxnmn7wcsc
266+
tag: 9f549559bd6cb714486fc50a8dbf295278f6e9d1
267+
--sha256: 1c4cp0j6ry7n6ha5ix5cnmdl6n626am1mhm8c6q40iws8y4bxy05
268268
subdir:
269269
monoidal-synchronisation
270270
network-mux

Diff for: cardano-api/src/Cardano/Api/LedgerState.hs

+12-17
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ import Cardano.Api.Modes (CardanoMode, EpochSlots (..))
105105
import qualified Cardano.Api.Modes as Api
106106
import Cardano.Api.NetworkId (NetworkId (..), NetworkMagic (NetworkMagic))
107107
import Cardano.Api.ProtocolParameters
108-
import Cardano.Api.Query (CurrentEpochState (..), ProtocolState,
109-
SerialisedCurrentEpochState (..), decodeCurrentEpochState, decodeProtocolState)
108+
import Cardano.Api.Query (CurrentEpochState (..), PoolDistr (unPoolDistr), ProtocolState,
109+
SerialisedCurrentEpochState (..), SerialisedPoolDistr, decodeCurrentEpochState,
110+
decodePoolDistr, decodeProtocolState)
110111
import Cardano.Binary (DecoderError, FromCBOR)
111112
import qualified Cardano.Chain.Genesis
112113
import qualified Cardano.Chain.Update
@@ -1384,9 +1385,10 @@ nextEpochEligibleLeadershipSlots sbe sGen serCurrEpochState ptclState poolid (Vr
13841385
$ obtainDecodeEpochStateConstraints sbe
13851386
$ decodeCurrentEpochState serCurrEpochState
13861387

1387-
let markSnapshotPoolDistr :: Map (SL.KeyHash 'SL.StakePool Shelley.StandardCrypto) (SL.IndividualPoolStake Shelley.StandardCrypto)
1388-
markSnapshotPoolDistr = ShelleyAPI.unPoolDistr . ShelleyAPI.calculatePoolDistr . ShelleyAPI._pstakeMark
1389-
$ obtainIsStandardCrypto sbe $ ShelleyAPI.esSnapshots cEstate
1388+
let snapshot :: ShelleyAPI.SnapShot Shelley.StandardCrypto
1389+
snapshot = ShelleyAPI._pstakeMark $ obtainIsStandardCrypto sbe $ ShelleyAPI.esSnapshots cEstate
1390+
markSnapshotPoolDistr :: Map (SL.KeyHash 'SL.StakePool Shelley.StandardCrypto) (SL.IndividualPoolStake Shelley.StandardCrypto)
1391+
markSnapshotPoolDistr = ShelleyAPI.unPoolDistr . ShelleyAPI.calculatePoolDistr $ snapshot
13901392

13911393
let slotRangeOfInterest = Set.filter
13921394
(not . Ledger.isOverlaySlot firstSlotOfEpoch (getField @"_d" (toLedgerPParams sbe pParams)))
@@ -1510,10 +1512,10 @@ currentEpochEligibleLeadershipSlots :: forall era ledgerera. ()
15101512
-> ProtocolState era
15111513
-> PoolId
15121514
-> SigningKey VrfKey
1513-
-> SerialisedCurrentEpochState era
1515+
-> SerialisedPoolDistr era
15141516
-> EpochNo -- ^ Current EpochInfo
15151517
-> Either LeadershipError (Set SlotNo)
1516-
currentEpochEligibleLeadershipSlots sbe sGen eInfo pParams ptclState poolid (VrfSigningKey vrkSkey) serCurrEpochState currentEpoch = do
1518+
currentEpochEligibleLeadershipSlots sbe sGen eInfo pParams ptclState poolid (VrfSigningKey vrkSkey) serPoolDistr currentEpoch = do
15171519

15181520
chainDepState :: ChainDepState (Api.ConsensusProtocol era) <-
15191521
first LeaderErrDecodeProtocolStateFailure $ decodeProtocolState ptclState
@@ -1526,17 +1528,10 @@ currentEpochEligibleLeadershipSlots sbe sGen eInfo pParams ptclState poolid (Vrf
15261528
(firstSlotOfEpoch, lastSlotofEpoch) :: (SlotNo, SlotNo) <- first LeaderErrSlotRangeCalculationFailure
15271529
$ Slot.epochInfoRange eInfo currentEpoch
15281530

1529-
CurrentEpochState (cEstate :: ShelleyAPI.EpochState (ShelleyLedgerEra era)) <-
1530-
first LeaderErrDecodeProtocolEpochStateFailure
1531+
setSnapshotPoolDistr <-
1532+
first LeaderErrDecodeProtocolEpochStateFailure . fmap (SL.unPoolDistr . unPoolDistr)
15311533
$ obtainDecodeEpochStateConstraints sbe
1532-
$ decodeCurrentEpochState serCurrEpochState
1533-
1534-
-- We need the "set" stake distribution (distribution of the previous epoch)
1535-
-- in order to calculate the leadership schedule of the current epoch.
1536-
let setSnapshotPoolDistr :: Map (SL.KeyHash 'SL.StakePool Shelley.StandardCrypto) (SL.IndividualPoolStake Shelley.StandardCrypto)
1537-
setSnapshotPoolDistr = ShelleyAPI.unPoolDistr . ShelleyAPI.calculatePoolDistr
1538-
. ShelleyAPI._pstakeSet . obtainIsStandardCrypto sbe
1539-
$ ShelleyAPI.esSnapshots cEstate
1534+
$ decodePoolDistr serPoolDistr
15401535

15411536
let slotRangeOfInterest = Set.filter
15421537
(not . Ledger.isOverlaySlot firstSlotOfEpoch (getField @"_d" (toLedgerPParams sbe pParams)))

Diff for: cardano-api/src/Cardano/Api/Query.hs

+33
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ module Cardano.Api.Query (
4848
PoolState(..),
4949
decodePoolState,
5050

51+
SerialisedPoolDistr(..),
52+
PoolDistr(..),
53+
decodePoolDistr,
54+
5155
EraHistory(..),
5256
SystemStart(..),
5357

@@ -245,6 +249,10 @@ data QueryInShelleyBasedEra era result where
245249
:: Maybe (Set PoolId)
246250
-> QueryInShelleyBasedEra era (SerialisedPoolState era)
247251

252+
QueryPoolDistr
253+
:: Maybe (Set PoolId)
254+
-> QueryInShelleyBasedEra era (SerialisedPoolDistr era)
255+
248256
deriving instance Show (QueryInShelleyBasedEra era result)
249257

250258

@@ -403,6 +411,20 @@ decodePoolState
403411
-> Either DecoderError (PoolState era)
404412
decodePoolState (SerialisedPoolState (Serialised ls)) = PoolState <$> decodeFull ls
405413

414+
newtype SerialisedPoolDistr era
415+
= SerialisedPoolDistr (Serialised (Shelley.PoolDistr (Ledger.Crypto (ShelleyLedgerEra era))))
416+
417+
newtype PoolDistr era = PoolDistr
418+
{ unPoolDistr :: Shelley.PoolDistr (Ledger.Crypto (ShelleyLedgerEra era))
419+
}
420+
421+
decodePoolDistr
422+
:: forall era. ()
423+
=> FromCBOR (Shelley.PoolDistr (Ledger.Crypto (ShelleyLedgerEra era)))
424+
=> SerialisedPoolDistr era
425+
-> Either DecoderError (PoolDistr era)
426+
decodePoolDistr (SerialisedPoolDistr (Serialised ls)) = PoolDistr <$> decodeFull ls
427+
406428
toShelleyAddrSet :: CardanoEra era
407429
-> Set AddressAny
408430
-> Set (Shelley.Addr Consensus.StandardCrypto)
@@ -588,6 +610,12 @@ toConsensusQueryShelleyBased erainmode (QueryPoolState poolIds) =
588610
getPoolIds :: Set PoolId -> Set (Shelley.KeyHash Shelley.StakePool Consensus.StandardCrypto)
589611
getPoolIds = Set.map (\(StakePoolKeyHash kh) -> kh)
590612

613+
toConsensusQueryShelleyBased erainmode (QueryPoolDistr poolIds) =
614+
Some (consensusQueryInEraInMode erainmode (Consensus.GetCBOR (Consensus.GetPoolDistr (getPoolIds <$> poolIds))))
615+
where
616+
getPoolIds :: Set PoolId -> Set (Shelley.KeyHash Shelley.StakePool Consensus.StandardCrypto)
617+
getPoolIds = Set.map (\(StakePoolKeyHash kh) -> kh)
618+
591619
consensusQueryInEraInMode
592620
:: forall era mode erablock modeblock result result' xs.
593621
ConsensusBlockForEra era ~ erablock
@@ -823,6 +851,11 @@ fromConsensusQueryResultShelleyBased _ QueryPoolState{} q' r' =
823851
Consensus.GetCBOR Consensus.GetPoolState {} -> SerialisedPoolState r'
824852
_ -> fromConsensusQueryResultMismatch
825853

854+
fromConsensusQueryResultShelleyBased _ QueryPoolDistr{} q' r' =
855+
case q' of
856+
Consensus.GetCBOR Consensus.GetPoolDistr {} -> SerialisedPoolDistr r'
857+
_ -> fromConsensusQueryResultMismatch
858+
826859
-- | This should /only/ happen if we messed up the mapping in 'toConsensusQuery'
827860
-- and 'fromConsensusQueryResult' so they are inconsistent with each other.
828861
--

Diff for: cardano-api/src/Cardano/Api/Shelley.hs

+4-1
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ module Cardano.Api.Shelley
210210
PoolState(..),
211211
SerialisedPoolState(..),
212212
decodePoolState,
213+
PoolDistr(..),
214+
SerialisedPoolDistr(..),
215+
decodePoolDistr,
213216
UTxO(..),
214217
AcquireFailure(..),
215218
SystemStart(..),
@@ -235,8 +238,8 @@ import Cardano.Api.Address
235238
import Cardano.Api.Block
236239
import Cardano.Api.Certificate
237240
import Cardano.Api.Eras
238-
import Cardano.Api.IPC
239241
import Cardano.Api.InMode
242+
import Cardano.Api.IPC
240243
import Cardano.Api.KeysByron
241244
import Cardano.Api.KeysPraos
242245
import Cardano.Api.KeysShelley

Diff for: cardano-cli/src/Cardano/CLI/Shelley/Run/Query.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1181,9 +1181,9 @@ runQueryLeadershipSchedule (AnyConsensusModeParams cModeParams) network
11811181
schedule :: Set SlotNo
11821182
<- case whichSchedule of
11831183
CurrentEpoch -> do
1184-
let currentEpochStateQuery = QueryInEra eInMode $ QueryInShelleyBasedEra sbe QueryCurrentEpochState
1185-
currentEpochQuery = QueryInEra eInMode $ QueryInShelleyBasedEra sbe QueryEpoch
1186-
serCurrentEpochState <- executeQuery era cModeParams localNodeConnInfo currentEpochStateQuery
1184+
let currentEpochQuery = QueryInEra eInMode $ QueryInShelleyBasedEra sbe QueryEpoch
1185+
serCurrentEpochState <- executeQuery era cModeParams localNodeConnInfo $
1186+
QueryInEra eInMode $ QueryInShelleyBasedEra sbe (QueryPoolDistr (Just (Set.singleton poolid)))
11871187
curentEpoch <- executeQuery era cModeParams localNodeConnInfo currentEpochQuery
11881188
firstExceptT ShelleyQueryCmdLeaderShipError $ hoistEither
11891189
$ eligibleLeaderSlotsConstaints sbe

0 commit comments

Comments
 (0)