Skip to content

Commit 0ffbd5a

Browse files
authored
Merge pull request #4250 from input-output-hk/newhoggy/optimise-query-leadership-schedule-command
Optimise query leadership schedule command
2 parents 6c859e0 + 692e79d commit 0ffbd5a

File tree

5 files changed

+83
-55
lines changed

5 files changed

+83
-55
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 (..), PoolDistribution (unPoolDistr), ProtocolState,
109+
SerialisedCurrentEpochState (..), SerialisedPoolDistribution,
110+
decodeCurrentEpochState, decodePoolDistribution, decodeProtocolState)
110111
import Cardano.Api.Utils (textShow)
111112
import Cardano.Binary (DecoderError, FromCBOR)
112113
import qualified Cardano.Chain.Genesis
@@ -1385,9 +1386,10 @@ nextEpochEligibleLeadershipSlots sbe sGen serCurrEpochState ptclState poolid (Vr
13851386
$ obtainDecodeEpochStateConstraints sbe
13861387
$ decodeCurrentEpochState serCurrEpochState
13871388

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

13921394
let slotRangeOfInterest = Set.filter
13931395
(not . Ledger.isOverlaySlot firstSlotOfEpoch (getField @"_d" (toLedgerPParams sbe pParams)))
@@ -1515,10 +1517,10 @@ currentEpochEligibleLeadershipSlots :: forall era ledgerera. ()
15151517
-> ProtocolState era
15161518
-> PoolId
15171519
-> SigningKey VrfKey
1518-
-> SerialisedCurrentEpochState era
1520+
-> SerialisedPoolDistribution era
15191521
-> EpochNo -- ^ Current EpochInfo
15201522
-> Either LeadershipError (Set SlotNo)
1521-
currentEpochEligibleLeadershipSlots sbe sGen eInfo pParams ptclState poolid (VrfSigningKey vrkSkey) serCurrEpochState currentEpoch = do
1523+
currentEpochEligibleLeadershipSlots sbe sGen eInfo pParams ptclState poolid (VrfSigningKey vrkSkey) serPoolDistr currentEpoch = do
15221524

15231525
chainDepState :: ChainDepState (Api.ConsensusProtocol era) <-
15241526
first LeaderErrDecodeProtocolStateFailure $ decodeProtocolState ptclState
@@ -1531,17 +1533,10 @@ currentEpochEligibleLeadershipSlots sbe sGen eInfo pParams ptclState poolid (Vrf
15311533
(firstSlotOfEpoch, lastSlotofEpoch) :: (SlotNo, SlotNo) <- first LeaderErrSlotRangeCalculationFailure
15321534
$ Slot.epochInfoRange eInfo currentEpoch
15331535

1534-
CurrentEpochState (cEstate :: ShelleyAPI.EpochState (ShelleyLedgerEra era)) <-
1535-
first LeaderErrDecodeProtocolEpochStateFailure
1536+
setSnapshotPoolDistr <-
1537+
first LeaderErrDecodeProtocolEpochStateFailure . fmap (SL.unPoolDistr . unPoolDistr)
15361538
$ obtainDecodeEpochStateConstraints sbe
1537-
$ decodeCurrentEpochState serCurrEpochState
1538-
1539-
-- We need the "set" stake distribution (distribution of the previous epoch)
1540-
-- in order to calculate the leadership schedule of the current epoch.
1541-
let setSnapshotPoolDistr :: Map (SL.KeyHash 'SL.StakePool Shelley.StandardCrypto) (SL.IndividualPoolStake Shelley.StandardCrypto)
1542-
setSnapshotPoolDistr = ShelleyAPI.unPoolDistr . ShelleyAPI.calculatePoolDistr
1543-
. ShelleyAPI._pstakeSet . obtainIsStandardCrypto sbe
1544-
$ ShelleyAPI.esSnapshots cEstate
1539+
$ decodePoolDistribution serPoolDistr
15451540

15461541
let slotRangeOfInterest = Set.filter
15471542
(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+
SerialisedPoolDistribution(..),
52+
PoolDistribution(..),
53+
decodePoolDistribution,
54+
5155
EraHistory(..),
5256
SystemStart(..),
5357

@@ -246,6 +250,10 @@ data QueryInShelleyBasedEra era result where
246250
:: Maybe (Set PoolId)
247251
-> QueryInShelleyBasedEra era (SerialisedPoolState era)
248252

253+
QueryPoolDistribution
254+
:: Maybe (Set PoolId)
255+
-> QueryInShelleyBasedEra era (SerialisedPoolDistribution era)
256+
249257
deriving instance Show (QueryInShelleyBasedEra era result)
250258

251259

@@ -406,6 +414,20 @@ decodePoolState
406414
-> Either DecoderError (PoolState era)
407415
decodePoolState (SerialisedPoolState (Serialised ls)) = PoolState <$> decodeFull ls
408416

417+
newtype SerialisedPoolDistribution era
418+
= SerialisedPoolDistribution (Serialised (Shelley.PoolDistr (Ledger.Crypto (ShelleyLedgerEra era))))
419+
420+
newtype PoolDistribution era = PoolDistribution
421+
{ unPoolDistr :: Shelley.PoolDistr (Ledger.Crypto (ShelleyLedgerEra era))
422+
}
423+
424+
decodePoolDistribution
425+
:: forall era. ()
426+
=> FromCBOR (Shelley.PoolDistr (Ledger.Crypto (ShelleyLedgerEra era)))
427+
=> SerialisedPoolDistribution era
428+
-> Either DecoderError (PoolDistribution era)
429+
decodePoolDistribution (SerialisedPoolDistribution (Serialised ls)) = PoolDistribution <$> decodeFull ls
430+
409431
toShelleyAddrSet :: CardanoEra era
410432
-> Set AddressAny
411433
-> Set (Shelley.Addr Consensus.StandardCrypto)
@@ -591,6 +613,12 @@ toConsensusQueryShelleyBased erainmode (QueryPoolState poolIds) =
591613
getPoolIds :: Set PoolId -> Set (Shelley.KeyHash Shelley.StakePool Consensus.StandardCrypto)
592614
getPoolIds = Set.map (\(StakePoolKeyHash kh) -> kh)
593615

616+
toConsensusQueryShelleyBased erainmode (QueryPoolDistribution poolIds) =
617+
Some (consensusQueryInEraInMode erainmode (Consensus.GetCBOR (Consensus.GetPoolDistr (getPoolIds <$> poolIds))))
618+
where
619+
getPoolIds :: Set PoolId -> Set (Shelley.KeyHash Shelley.StakePool Consensus.StandardCrypto)
620+
getPoolIds = Set.map (\(StakePoolKeyHash kh) -> kh)
621+
594622
consensusQueryInEraInMode
595623
:: forall era mode erablock modeblock result result' xs.
596624
ConsensusBlockForEra era ~ erablock
@@ -826,6 +854,11 @@ fromConsensusQueryResultShelleyBased _ QueryPoolState{} q' r' =
826854
Consensus.GetCBOR Consensus.GetPoolState {} -> SerialisedPoolState r'
827855
_ -> fromConsensusQueryResultMismatch
828856

857+
fromConsensusQueryResultShelleyBased _ QueryPoolDistribution{} q' r' =
858+
case q' of
859+
Consensus.GetCBOR Consensus.GetPoolDistr {} -> SerialisedPoolDistribution r'
860+
_ -> fromConsensusQueryResultMismatch
861+
829862
-- | This should /only/ happen if we messed up the mapping in 'toConsensusQuery'
830863
-- and 'fromConsensusQueryResult' so they are inconsistent with each other.
831864
--

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+
PoolDistribution(..),
214+
SerialisedPoolDistribution(..),
215+
decodePoolDistribution,
213216
UTxO(..),
214217
AcquiringFailure(..),
215218
SystemStart(..),

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
module Cardano.CLI.Shelley.Orphans () where
1616

1717
import Cardano.Api.Orphans ()
18-
import Cardano.Ledger.AuxiliaryData (AuxiliaryDataHash (..))
18+
import qualified Cardano.Ledger.AuxiliaryData as Ledger
1919
import qualified Cardano.Ledger.Credential as Ledger
2020
import qualified Cardano.Ledger.Crypto as CC (Crypto)
2121
import qualified Cardano.Ledger.Mary.Value as Ledger.Mary
22-
import Cardano.Ledger.PoolDistr (PoolDistr (..))
22+
import qualified Cardano.Ledger.PoolDistr as Ledger
2323
import qualified Cardano.Ledger.Shelley.EpochBoundary as Ledger
2424
import qualified Cardano.Ledger.Shelley.PoolRank as Ledger
2525
import Cardano.Ledger.TxIn (TxId (..))
@@ -78,9 +78,9 @@ deriving newtype instance CC.Crypto crypto => ToJSON (TxId crypto)
7878
deriving newtype instance CC.Crypto crypto => ToJSON (ShelleyHash crypto)
7979
deriving newtype instance CC.Crypto crypto => ToJSON (HashHeader crypto)
8080

81-
deriving newtype instance ToJSON (AuxiliaryDataHash StandardCrypto)
81+
deriving newtype instance ToJSON (Ledger.AuxiliaryDataHash StandardCrypto)
8282
deriving newtype instance ToJSON Ledger.LogWeight
83-
deriving newtype instance ToJSON (PoolDistr StandardCrypto)
83+
deriving newtype instance ToJSON (Ledger.PoolDistr StandardCrypto)
8484

8585
deriving newtype instance ToJSON (Ledger.Stake StandardCrypto)
8686

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

+31-34
Original file line numberDiff line numberDiff line change
@@ -1247,40 +1247,37 @@ runQueryLeadershipSchedule (AnyConsensusModeParams cModeParams) network
12471247
ptclState <- executeQuery era cModeParams localNodeConnInfo ptclStateQuery
12481248
eraHistory <- firstExceptT ShelleyQueryCmdAcquireFailure . newExceptT $ queryNodeLocalState localNodeConnInfo Nothing eraHistoryQuery
12491249
let eInfo = toEpochInfo eraHistory
1250-
1251-
schedule :: Set SlotNo
1252-
<- case whichSchedule of
1253-
CurrentEpoch -> do
1254-
let currentEpochStateQuery = QueryInEra eInMode $ QueryInShelleyBasedEra sbe QueryCurrentEpochState
1255-
currentEpochQuery = QueryInEra eInMode $ QueryInShelleyBasedEra sbe QueryEpoch
1256-
serCurrentEpochState <- executeQuery era cModeParams localNodeConnInfo currentEpochStateQuery
1257-
curentEpoch <- executeQuery era cModeParams localNodeConnInfo currentEpochQuery
1258-
firstExceptT ShelleyQueryCmdLeaderShipError $ hoistEither
1259-
$ eligibleLeaderSlotsConstaints sbe
1260-
$ currentEpochEligibleLeadershipSlots
1261-
sbe
1262-
shelleyGenesis
1263-
eInfo
1264-
pparams
1265-
ptclState
1266-
poolid
1267-
vrkSkey
1268-
serCurrentEpochState
1269-
curentEpoch
1270-
1271-
NextEpoch -> do
1272-
let currentEpochStateQuery = QueryInEra eInMode $ QueryInShelleyBasedEra sbe QueryCurrentEpochState
1273-
currentEpochQuery = QueryInEra eInMode $ QueryInShelleyBasedEra sbe QueryEpoch
1274-
tip <- liftIO $ getLocalChainTip localNodeConnInfo
1275-
1276-
curentEpoch <- executeQuery era cModeParams localNodeConnInfo currentEpochQuery
1277-
serCurrentEpochState <- executeQuery era cModeParams localNodeConnInfo currentEpochStateQuery
1278-
1279-
firstExceptT ShelleyQueryCmdLeaderShipError $ hoistEither
1280-
$ eligibleLeaderSlotsConstaints sbe
1281-
$ nextEpochEligibleLeadershipSlots sbe shelleyGenesis
1282-
serCurrentEpochState ptclState poolid vrkSkey pparams
1283-
eInfo (tip, curentEpoch)
1250+
let currentEpochQuery = QueryInEra eInMode $ QueryInShelleyBasedEra sbe QueryEpoch
1251+
curentEpoch <- executeQuery era cModeParams localNodeConnInfo currentEpochQuery
1252+
1253+
schedule <- case whichSchedule of
1254+
CurrentEpoch -> do
1255+
serCurrentEpochState <- executeQuery era cModeParams localNodeConnInfo $
1256+
QueryInEra eInMode $ QueryInShelleyBasedEra sbe (QueryPoolDistribution (Just (Set.singleton poolid)))
1257+
firstExceptT ShelleyQueryCmdLeaderShipError $ hoistEither
1258+
$ eligibleLeaderSlotsConstaints sbe
1259+
$ currentEpochEligibleLeadershipSlots
1260+
sbe
1261+
shelleyGenesis
1262+
eInfo
1263+
pparams
1264+
ptclState
1265+
poolid
1266+
vrkSkey
1267+
serCurrentEpochState
1268+
curentEpoch
1269+
1270+
NextEpoch -> do
1271+
let currentEpochStateQuery = QueryInEra eInMode $ QueryInShelleyBasedEra sbe QueryCurrentEpochState
1272+
1273+
tip <- liftIO $ getLocalChainTip localNodeConnInfo
1274+
serCurrentEpochState <- executeQuery era cModeParams localNodeConnInfo currentEpochStateQuery
1275+
1276+
firstExceptT ShelleyQueryCmdLeaderShipError $ hoistEither
1277+
$ eligibleLeaderSlotsConstaints sbe
1278+
$ nextEpochEligibleLeadershipSlots sbe shelleyGenesis
1279+
serCurrentEpochState ptclState poolid vrkSkey pparams
1280+
eInfo (tip, curentEpoch)
12841281

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

0 commit comments

Comments
 (0)