Skip to content

Commit 2372b76

Browse files
committed
Some cleanup and complexity annotations.
1 parent 9ced91e commit 2372b76

11 files changed

Lines changed: 221 additions & 4 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{-# OPTIONS_GHC -Wno-deprecations #-}
22

3+
-- | Ledger state queries consolidated from @ouroboros-consensus@, @cardano-api@,
4+
-- @cardano-cli@, and @cardano-db-sync@.
5+
--
6+
-- Each query function includes @Source:@ comments pointing to the original
7+
-- call sites in these downstream packages. These are intended to guide
8+
-- downstream propagation of these queries. Once a downstream call site has been
9+
-- successfully migrated, the corresponding comment may be removed.
310
module Cardano.Ledger.Api.State.Query (
411
-- * @GetChainAccountState@
512
module Cardano.Ledger.Api.State.Query.Epoch,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import Cardano.Ledger.Shelley.PParams (ProposedPPUpdates, emptyPPPUpdates)
1919
--
2020
-- __Warning:__ This returns internal, era-parameterized state that is not
2121
-- covered by CBOR stability guarantees. Use only for debugging and tooling.
22+
--
23+
-- /O(1)/
2224
queryDebugEpochState :: NewEpochState era -> EpochState era
2325
queryDebugEpochState = nesEs
2426

@@ -28,12 +30,16 @@ queryDebugEpochState = nesEs
2830
--
2931
-- __Warning:__ This returns internal, era-parameterized state that is not
3032
-- covered by CBOR stability guarantees. Use only for debugging and tooling.
33+
--
34+
-- /O(1)/
3135
queryDebugNewEpochState :: NewEpochState era -> NewEpochState era
3236
queryDebugNewEpochState = id
3337

3438
-- | Query proposed protocol parameter updates.
3539
-- Source: ouroboros-consensus:ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Query.hs:420
3640
-- answerPureBlockQuery case for GetProposedPParamsUpdates
41+
--
42+
-- /O(1)/
3743
queryProposedPParamsUpdates :: NewEpochState era -> ProposedPPUpdates era
3844
queryProposedPParamsUpdates _ = emptyPPPUpdates
3945
{-# DEPRECATED

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import Lens.Micro ((^.))
1111
-- | Query the current epoch number.
1212
-- Source: ouroboros-consensus:ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Query.hs:413
1313
-- answerPureBlockQuery case for GetEpochNo
14+
--
15+
-- /O(1)/
1416
queryEpochNo :: NewEpochState era -> EpochNo
1517
queryEpochNo = nesEL
1618

@@ -19,6 +21,8 @@ queryEpochNo = nesEL
1921
-- answerPureBlockQuery case for GetAccountState
2022
-- Also: cardano-api:cardano-api/src/Cardano/Api/Query/Internal/Expr.hs:484
2123
-- queryAccountState cardano-api wrapper (CLI via Convenience.hs:171)
24+
--
25+
-- /O(1)/
2226
queryChainAccountState ::
2327
NewEpochState era ->
2428
ChainAccountState

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

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,8 @@ instance DecCBOR QueryResultDRepStates where
382382
-- queryGovState cardano-api wrapper
383383
-- Also: cardano-cli:cardano-cli/src/Cardano/CLI/EraBased/Query/Run.hs:1617
384384
-- CLI invocation
385+
--
386+
-- /O(1)/
385387
queryGovState :: NewEpochState era -> GovState era
386388
queryGovState nes = nes ^. nesEpochStateL . epochStateGovStateL
387389

@@ -392,12 +394,16 @@ queryGovState nes = nes ^. nesEpochStateL . epochStateGovStateL
392394
-- queryConstitution cardano-api wrapper
393395
-- Also: cardano-cli:cardano-cli/src/Cardano/CLI/EraBased/Query/Run.hs:1589
394396
-- CLI invocation
397+
--
398+
-- /O(1)/
395399
queryConstitution :: ConwayEraGov era => NewEpochState era -> QueryResultConstitution
396400
queryConstitution nes = toQueryResultConstitution $ queryGovState nes ^. constitutionGovStateL
397401

398402
-- | Query the constitution hash.
399403
--
400404
-- Convenience extraction — no corresponding standalone query in @ouroboros-consensus@.
405+
--
406+
-- /O(1)/
401407
queryConstitutionHash ::
402408
ConwayEraGov era =>
403409
NewEpochState era ->
@@ -414,6 +420,8 @@ queryConstitutionHash nes =
414420
-- CLI invocation
415421
--
416422
-- Empty 'Set' returns all proposals.
423+
--
424+
-- /O(g)/ where (g) is the proposals considered for ratification, Seq.filter.
417425
queryProposals ::
418426
ConwayEraGov era =>
419427
NewEpochState era ->
@@ -438,19 +446,28 @@ queryProposals nes gids
438446
-- queryRatifyState cardano-api wrapper
439447
-- Also: cardano-cli:cardano-cli/src/Cardano/CLI/EraBased/Query/Run.hs:1645
440448
-- CLI invocation
449+
--
450+
-- /O(P)/ where (P) is the DRep pulser completion cost, finishedPulserState.
441451
queryRatifyState :: ConwayEraGov era => NewEpochState era -> RatifyState era
442452
queryRatifyState = snd . finishedPulserState
443453

444454
-- | Query committee members. Whenever the system is in No Confidence mode this query will
445455
-- return no committee members.
446456
-- Source: ouroboros-consensus:ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Query.hs:471
447457
-- answerPureBlockQuery case for GetCommitteeMembersState
448-
-- Also: cardano-api:cardano-api/src/Cardano/Api/Query/Internal/Expr.hs:452
458+
-- Also: cardano-api:cardano-api/src/Cardano/Api/Query/Internal/Expr.hs:454
449459
-- queryCommitteeMembersState cardano-api wrapper
450460
-- Also: cardano-cli:cardano-cli/src/Cardano/CLI/EraBased/Query/Run.hs:1887
451461
-- CLI invocation
452462
--
453463
-- Empty 'Set' returns all matches (i.e. no filtering).
464+
--
465+
-- @
466+
-- O(P + m)
467+
-- @
468+
-- where,
469+
-- (P) is the DRep pulser completion cost, finishedPulserState
470+
-- (m) is the committee members (current + next + state), Map.union and Map.mapWithKey
454471
queryCommitteeMembersState ::
455472
forall era.
456473
(ConwayEraGov era, ConwayEraCertState era) =>
@@ -546,6 +563,8 @@ queryCommitteeMembersState coldCredsFilter hotCredsFilter statusFilter nes =
546563
}
547564

548565
-- | Get the committee members that will be in effect at the next epoch boundary.
566+
--
567+
-- /O(P)/ where (P) is the DRep pulser completion cost, finishedPulserState.
549568
getNextEpochCommitteeMembers ::
550569
ConwayEraGov era =>
551570
NewEpochState era ->
@@ -564,6 +583,13 @@ getNextEpochCommitteeMembers nes =
564583
-- CLI invocation
565584
--
566585
-- Empty 'Set' returns all DReps.
586+
--
587+
-- @
588+
-- O(d + k)
589+
-- @
590+
-- where,
591+
-- (d) is the total registered DReps, Map.restrictKeys and Map.map
592+
-- (k) is the requested DRep credential set, Map.restrictKeys
567593
queryDRepState ::
568594
ConwayEraCertState era =>
569595
NewEpochState era ->
@@ -590,6 +616,15 @@ queryDRepState nes creds =
590616
-- queryDRepDelegations cardano-api wrapper (not publicly exported)
591617
--
592618
-- Empty 'Set' returns all DReps.
619+
--
620+
-- @
621+
-- O(d + k) when all requested DReps are DRepCredentials
622+
-- O(c) when AlwaysAbstain or AlwaysNoConfidence are requested
623+
-- @
624+
-- where,
625+
-- (d) is the registered DReps, Map.restrictKeys
626+
-- (k) is the requested DRep set, Map.restrictKeys
627+
-- (c) is the total staking credentials, Map.foldlWithKey' (full accounts scan)
593628
queryDRepDelegations ::
594629
forall era.
595630
ConwayEraCertState era =>
@@ -637,6 +672,14 @@ queryDRepDelegations nes dreps =
637672
-- CLI invocation
638673
--
639674
-- Empty 'Set' returns all DReps.
675+
--
676+
-- @
677+
-- O(P + d + k)
678+
-- @
679+
-- where,
680+
-- (P) is the DRep pulser completion cost, finishedPulserState
681+
-- (d) is the total DReps in the distribution, Map.map
682+
-- (k) is the requested DRep set, Map.restrictKeys
640683
queryDRepStakeDistr ::
641684
ConwayEraGov era =>
642685
NewEpochState era ->
@@ -656,6 +699,15 @@ queryDRepStakeDistr nes creds
656699
-- No consensus BlockQuery constructor exists yet (GetRegisteredDRepStakeDistr was planned but not added).
657700
--
658701
-- Empty 'Set' returns all registered DReps.
702+
--
703+
-- @
704+
-- O(d + k + d * c_avg * log(c))
705+
-- @
706+
-- where,
707+
-- (d) is the registered DReps, Map.restrictKeys and Map.foldlWithKey'
708+
-- (k) is the requested DRep credential set, Map.restrictKeys
709+
-- (c_avg) is the average delegators per DRep, foldMap over drepDelegs
710+
-- (c) is the total staking credentials, Map.lookup in instantStake per delegator
659711
queryRegisteredDRepStakeDistr ::
660712
(ConwayEraGov era, ConwayEraCertState era) =>
661713
NewEpochState era ->
@@ -683,13 +735,20 @@ queryRegisteredDRepStakeDistr nes creds =
683735
-- | Query the DRep delegatee for each given staking credential.
684736
-- Source: ouroboros-consensus:ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Query.hs:457
685737
-- answerPureBlockQuery case for GetDRepStakeDistr (partial — delegatees extracted separately)
686-
-- Also: cardano-api:cardano-api/src/Cardano/Api/Query/Internal/Expr.hs:459
738+
-- Also: cardano-api:cardano-api/src/Cardano/Api/Query/Internal/Expr.hs:469
687739
-- queryStakeVoteDelegatees cardano-api wrapper
688740
-- Also: cardano-cli:cardano-cli/src/Cardano/CLI/EraBased/Query/Run.hs:1042
689741
-- CLI invocation
690742
--
691743
-- Returns the DRep each credential has delegated to. Credentials with no
692744
-- DRep delegation are omitted from the result. Empty 'Set' returns all.
745+
--
746+
-- @
747+
-- O(c + k)
748+
-- @
749+
-- where,
750+
-- (c) is the total staking credentials, Map.restrictKeys
751+
-- (k) is the requested credential set, Map.restrictKeys
693752
queryDRepDelegatees ::
694753
(EraCertState era, ConwayEraAccounts era) =>
695754
NewEpochState era ->
@@ -704,6 +763,8 @@ queryDRepDelegatees nes creds =
704763

705764
-- | Force the DRep pulser to completion and return the resulting snapshot and
706765
-- ratify state. Shared across governance query sub-modules.
766+
--
767+
-- /O(P)/ where (P) is the remaining DRep pulser work, amortised across the epoch.
707768
finishedPulserState ::
708769
ConwayEraGov era =>
709770
NewEpochState era ->

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import Lens.Micro ((^.))
1818
-- see if we are aware of any upcoming changes.
1919
-- Source: ouroboros-consensus:ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Query.hs:418
2020
-- answerPureBlockQuery case for GetCurrentPParams
21+
--
22+
-- /O(1)/
2123
queryCurrentPParams :: EraGov era => NewEpochState era -> PParams era
2224
queryCurrentPParams nes = nes ^. nesEpochStateL . epochStateGovStateL . curPParamsGovStateL
2325

@@ -30,6 +32,8 @@ queryCurrentPParams nes = nes ^. nesEpochStateL . epochStateGovStateL . curPPara
3032
-- queryFuturePParams cardano-api wrapper
3133
-- Also: cardano-cli:cardano-cli/src/Cardano/CLI/EraBased/Query/Run.hs:1673
3234
-- CLI invocation
35+
--
36+
-- /O(1)/
3337
queryFuturePParams :: EraGov era => NewEpochState era -> Maybe (PParams era)
3438
queryFuturePParams nes =
3539
case nes ^. nesEpochStateL . epochStateGovStateL . futurePParamsGovStateL of

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

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
{-# LANGUAGE ScopedTypeVariables #-}
66
{-# LANGUAGE StandaloneDeriving #-}
77
{-# LANGUAGE TypeApplications #-}
8-
{-# OPTIONS_GHC -Wno-deprecations #-}
98

109
module Cardano.Ledger.Api.State.Query.Pool (
1110
-- * Stable query result types
@@ -166,6 +165,13 @@ mkQueryPoolStateResult = toQueryResultPoolState
166165
-- CLI invocation
167166
--
168167
-- Empty 'Set' returns all pools.
168+
--
169+
-- @
170+
-- O(p + k)
171+
-- @
172+
-- where,
173+
-- (p) is the total registered stake pools, Map.restrictKeys and Map.mapWithKey
174+
-- (k) is the requested pool key set, Map.restrictKeys
169175
queryPoolState ::
170176
EraCertState era =>
171177
NewEpochState era ->
@@ -185,6 +191,13 @@ queryPoolState nes poolKeys network =
185191
-- queryStakePoolParameters cardano-api wrapper
186192
--
187193
-- Empty 'Set' returns all pools.
194+
--
195+
-- @
196+
-- O(p + k)
197+
-- @
198+
-- where,
199+
-- (p) is the total registered stake pools, Map.restrictKeys and Map.mapWithKey
200+
-- (k) is the requested pool key set, Map.restrictKeys
188201
queryStakePoolParams ::
189202
EraCertState era =>
190203
Network ->
@@ -218,6 +231,13 @@ queryPoolParameters = queryStakePoolParams
218231
-- queryStakePoolDefaultVote cardano-api wrapper
219232
-- Also: cardano-cli:cardano-cli/src/Cardano/CLI/EraBased/Query/Run.hs:2003
220233
-- CLI invocation
234+
--
235+
-- @
236+
-- O(log(p) + log(c))
237+
-- @
238+
-- where,
239+
-- (p) is the registered stake pools, Map.lookup
240+
-- (c) is the staking credentials, Map.lookup for reward account
221241
queryStakePoolDefaultVote ::
222242
(EraCertState era, ConwayEraAccounts era) =>
223243
NewEpochState era ->
@@ -237,6 +257,14 @@ queryStakePoolDefaultVote nes poolId =
237257
-- CLI invocation
238258
--
239259
-- Empty 'Set' returns all pools.
260+
--
261+
-- @
262+
-- O(P + p + k)
263+
-- @
264+
-- where,
265+
-- (P) is the DRep pulser completion cost, finishedPulserState
266+
-- (p) is the total pools in the distribution, Map.map
267+
-- (k) is the requested pool set, Map.restrictKeys
240268
querySPOStakeDistr ::
241269
ConwayEraGov era =>
242270
NewEpochState era ->
@@ -255,6 +283,8 @@ querySPOStakeDistr nes keys
255283
-- queryStakePools cardano-api wrapper (CLI via Convenience.hs:148)
256284
--
257285
-- Returns the key hashes of every pool in the current PState.
286+
--
287+
-- /O(p)/ where (p) is the registered stake pools, Map.keysSet.
258288
queryStakePools ::
259289
EraCertState era =>
260290
NewEpochState era ->
@@ -276,6 +306,13 @@ queryStakePools nes =
276306
-- memoized @nesPd@ (the __set__ snapshot computed at the last epoch boundary).
277307
--
278308
-- Needs @maxLovelaceSupply@ from genesis config.
309+
--
310+
-- @
311+
-- O(c + p)
312+
-- @
313+
-- where,
314+
-- (c) is the staking credentials, queryCurrentSnapshot
315+
-- (p) is the registered stake pools, calculatePoolDistr and Map.map
279316
queryStakePoolDistrByTotalSupply ::
280317
(EraStake era, EraCertState era) =>
281318
-- | maxLovelaceSupply from genesis config
@@ -325,6 +362,13 @@ queryStakePoolDistrByTotalSupply maxLovelaceSupply nes =
325362
-- This approach is more efficient: the expensive @calculatePoolDistr@
326363
-- computation happens once at the epoch boundary (ADR-7), and the query
327364
-- performs only an O(n+m) @Map.restrictKeys@.
365+
--
366+
-- @
367+
-- O(p + k)
368+
-- @
369+
-- where,
370+
-- (p) is the total pools in the memoized distribution, Map.restrictKeys
371+
-- (k) is the requested pool set, Map.restrictKeys
328372
queryStakePoolDistrFromSnapshot ::
329373
NewEpochState era ->
330374
Set (KeyHash StakePool) ->
@@ -346,6 +390,12 @@ queryStakePoolDistrFromSnapshot nes poolIds
346390
--
347391
-- Source: ouroboros-consensus:ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/PeerSelection.hs:32
348392
-- getPeers (LedgerSupportsPeerSelection instance)
393+
--
394+
-- @
395+
-- O(p * log(p))
396+
-- @
397+
-- where,
398+
-- (p) is the pools in the distribution, Map.mapMaybeWithKey with Map.lookup per pool
349399
queryStakePoolRelays ::
350400
EraCertState era =>
351401
NewEpochState era ->

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ instance DecCBOR QueryResultRewardInfoPools where
121121
--
122122
-- This uses a fresh snapshot derived from the current ledger state, not the
123123
-- stored mark\/set\/go snapshots.
124+
--
125+
-- @
126+
-- O(c + k * p)
127+
-- @
128+
-- where,
129+
-- (c) is the staking credentials, queryCurrentSnapshot
130+
-- (k) is the input credential\/coin set, iterate over
131+
-- (p) is the registered stake pools, VMap.mapWithKey per credential
124132
queryNonMyopicMemberRewards ::
125133
(EraGov era, EraStake era, EraCertState era) =>
126134
-- | maxLovelaceSupply from genesis config
@@ -172,6 +180,13 @@ queryNonMyopicMemberRewards maxLovelaceSupply ss = Map.fromSet nmmRewards
172180
--
173181
-- Uses a fresh snapshot from 'queryCurrentSnapshot' rather than the stored
174182
-- epoch-boundary snapshots, so the data reflects the most recent ledger state.
183+
--
184+
-- @
185+
-- O(c + p)
186+
-- @
187+
-- where,
188+
-- (c) is the staking credentials, queryCurrentSnapshot
189+
-- (p) is the registered stake pools, VMap.mapWithKey
175190
queryRewardInfoPools ::
176191
(EraGov era, EraStake era, EraCertState era) =>
177192
-- | maxLovelaceSupply from genesis config
@@ -220,6 +235,13 @@ queryRewardInfoPools maxLovelaceSupply nes =
220235
-- This function requires the full 'Globals' because it internally runs
221236
-- 'createRUpd' in a 'Reader' monad that needs access to 'epochInfo',
222237
-- 'activeSlotCoeff', 'securityParameter', and 'maxLovelaceSupply'.
238+
--
239+
-- @
240+
-- O(c * log(p))
241+
-- @
242+
-- where,
243+
-- (c) is the staking credentials, createRUpd reward computation
244+
-- (p) is the registered stake pools, reward calculation per credential
223245
queryRewardProvenance ::
224246
(EraGov era, EraCertState era) =>
225247
Globals ->

0 commit comments

Comments
 (0)