Skip to content

Commit ae0dcf6

Browse files
committed
Optimise query leadership-schedule command
1 parent 13e7b47 commit ae0dcf6

File tree

4 files changed

+80
-50
lines changed

4 files changed

+80
-50
lines changed

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

+3
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(..),

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

+32-33
Original file line numberDiff line numberDiff line change
@@ -1178,39 +1178,38 @@ runQueryLeadershipSchedule (AnyConsensusModeParams cModeParams) network
11781178
eraHistory <- firstExceptT ShelleyQueryCmdAcquireFailure . newExceptT $ queryNodeLocalState localNodeConnInfo Nothing eraHistoryQuery
11791179
let eInfo = toEpochInfo eraHistory
11801180

1181-
schedule :: Set SlotNo
1182-
<- case whichSchedule of
1183-
CurrentEpoch -> do
1184-
let currentEpochStateQuery = QueryInEra eInMode $ QueryInShelleyBasedEra sbe QueryCurrentEpochState
1185-
currentEpochQuery = QueryInEra eInMode $ QueryInShelleyBasedEra sbe QueryEpoch
1186-
serCurrentEpochState <- executeQuery era cModeParams localNodeConnInfo currentEpochStateQuery
1187-
curentEpoch <- executeQuery era cModeParams localNodeConnInfo currentEpochQuery
1188-
firstExceptT ShelleyQueryCmdLeaderShipError $ hoistEither
1189-
$ eligibleLeaderSlotsConstaints sbe
1190-
$ currentEpochEligibleLeadershipSlots
1191-
sbe
1192-
shelleyGenesis
1193-
eInfo
1194-
pparams
1195-
ptclState
1196-
poolid
1197-
vrkSkey
1198-
serCurrentEpochState
1199-
curentEpoch
1200-
1201-
NextEpoch -> do
1202-
let currentEpochStateQuery = QueryInEra eInMode $ QueryInShelleyBasedEra sbe QueryCurrentEpochState
1203-
currentEpochQuery = QueryInEra eInMode $ QueryInShelleyBasedEra sbe QueryEpoch
1204-
tip <- liftIO $ getLocalChainTip localNodeConnInfo
1205-
1206-
curentEpoch <- executeQuery era cModeParams localNodeConnInfo currentEpochQuery
1207-
serCurrentEpochState <- executeQuery era cModeParams localNodeConnInfo currentEpochStateQuery
1208-
1209-
firstExceptT ShelleyQueryCmdLeaderShipError $ hoistEither
1210-
$ eligibleLeaderSlotsConstaints sbe
1211-
$ nextEpochEligibleLeadershipSlots sbe shelleyGenesis
1212-
serCurrentEpochState ptclState poolid vrkSkey pparams
1213-
eInfo (tip, curentEpoch)
1181+
schedule <- case whichSchedule of
1182+
CurrentEpoch -> do
1183+
let currentEpochQuery = QueryInEra eInMode $ QueryInShelleyBasedEra sbe QueryEpoch
1184+
serCurrentEpochState <- executeQuery era cModeParams localNodeConnInfo $
1185+
QueryInEra eInMode $ QueryInShelleyBasedEra sbe (QueryPoolDistr (Just (Set.singleton poolid)))
1186+
curentEpoch <- executeQuery era cModeParams localNodeConnInfo currentEpochQuery
1187+
firstExceptT ShelleyQueryCmdLeaderShipError $ hoistEither
1188+
$ eligibleLeaderSlotsConstaints sbe
1189+
$ currentEpochEligibleLeadershipSlots
1190+
sbe
1191+
shelleyGenesis
1192+
eInfo
1193+
pparams
1194+
ptclState
1195+
poolid
1196+
vrkSkey
1197+
serCurrentEpochState
1198+
curentEpoch
1199+
1200+
NextEpoch -> do
1201+
let currentEpochStateQuery = QueryInEra eInMode $ QueryInShelleyBasedEra sbe QueryCurrentEpochState
1202+
currentEpochQuery = QueryInEra eInMode $ QueryInShelleyBasedEra sbe QueryEpoch
1203+
tip <- liftIO $ getLocalChainTip localNodeConnInfo
1204+
1205+
curentEpoch <- executeQuery era cModeParams localNodeConnInfo currentEpochQuery
1206+
serCurrentEpochState <- executeQuery era cModeParams localNodeConnInfo currentEpochStateQuery
1207+
1208+
firstExceptT ShelleyQueryCmdLeaderShipError $ hoistEither
1209+
$ eligibleLeaderSlotsConstaints sbe
1210+
$ nextEpochEligibleLeadershipSlots sbe shelleyGenesis
1211+
serCurrentEpochState ptclState poolid vrkSkey pparams
1212+
eInfo (tip, curentEpoch)
12141213

12151214
case mJsonOutputFile of
12161215
Nothing -> liftIO $ printLeadershipScheduleAsText schedule eInfo (SystemStart $ sgSystemStart shelleyGenesis)

0 commit comments

Comments
 (0)