Skip to content

Consolidate ledger-state queries#5690

Draft
aniketd wants to merge 15 commits intomasterfrom
aniketd/lsq
Draft

Consolidate ledger-state queries#5690
aniketd wants to merge 15 commits intomasterfrom
aniketd/lsq

Conversation

@aniketd
Copy link
Copy Markdown
Contributor

@aniketd aniketd commented Mar 30, 2026

Description

Reorganise Ledger.Api.State.Query into domain-specific sub-modules, and re-export everything.

  • Consolidate ledger state queries from consensus, api, and cli.
  • Add stable QueryResult* types to decouple downstream from ledger-internal representation.
  • Wherever it made sense: filter ~ empty set -> return all.
  • Contains breaking changes, some tests.
  • CBOR codecs are preserved.

Closes #5659 and #5660

Checklist

  • Commits in meaningful sequence and with useful messages.
  • Tests added or updated when needed.
  • CHANGELOG.md files updated for packages with externally visible changes.
    NOTE: New section is never added with the code changes. (See RELEASING.md).
  • Versions updated in .cabal and CHANGELOG.md files when necessary, according to the
    versioning process.
  • Version bounds in .cabal files updated when necessary.
    NOTE: If bounds change in a cabal file, that package itself must have a version increase. (See RELEASING.md).
  • Code formatted (use scripts/fourmolize.sh).
  • Cabal files formatted (use scripts/cabal-format.sh).
  • CDDL files are up to date (use scripts/gen-cddl.sh)
  • hie.yaml updated (use scripts/gen-hie.sh).
  • Self-reviewed the diff.

@aniketd aniketd force-pushed the aniketd/lsq branch 5 times, most recently from c9d1cb9 to 38aa10e Compare March 31, 2026 15:32
@aniketd aniketd changed the title wip Consolidate ledger-state queries Mar 31, 2026
@aniketd aniketd marked this pull request as ready for review March 31, 2026 15:35
@aniketd aniketd requested a review from a team as a code owner March 31, 2026 15:35
@aniketd aniketd requested a review from Copilot March 31, 2026 19:54
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR restructures Cardano.Ledger.Api.State.Query into domain-focused submodules, consolidating ledger-state query helpers previously scattered across downstream packages, and introduces stable QueryResult* types to reduce coupling to ledger-internal representations.

Changes:

  • Split Cardano.Ledger.Api.State.Query into 9 submodules (Governance/Pool/Snapshot/StakeDelegation/Reward/UTxO/PParams/Epoch/Debug) and re-export them from Query.
  • Add/rename stable QueryResult* result types (with deprecated aliases/shims) and update tests/arbitrary instances accordingly.
  • Deprecate overlapping wallet API functions in Cardano.Ledger.Shelley.API.Wallet, bump cardano-ledger-api to 1.14.0.0, and update changelogs.

Reviewed changes

Copilot reviewed 19 out of 19 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
libs/cardano-ledger-api/testlib/Test/Cardano/Ledger/Api/Arbitrary.hs Adds Arbitrary instances for new QueryResult* types and imports wallet reward types.
libs/cardano-ledger-api/test/Test/Cardano/Ledger/Api/State/QuerySpec.hs Updates roundtrip tests to new types and adds extraction-function property tests.
libs/cardano-ledger-api/test/Test/Cardano/Ledger/Api/State/Imp/QuerySpec.hs Updates Imp tests to use QueryResult* governance query results.
libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query/UTxO.hs New UTxO query helpers (full/by-address/by-txin).
libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query/StakeDelegation.hs New stake delegation queries + stable result type for delegs/rewards.
libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query/Snapshot.hs New stake snapshot query + stable snapshot result types and deprecated aliases.
libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query/Reward.hs New reward-related queries + stable result type for reward-info pools.
libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query/PParams.hs New protocol parameter query helpers.
libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query/Pool.hs New pool queries + stable pool-state result type and deprecated aliases.
libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query/Governance.hs New governance queries + stable constitution/committee/drep result types and conversions.
libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query/Epoch.hs New epoch/account-state query helpers.
libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query/Debug.hs New debug query helpers + deprecated PParams updates query.
libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query/CommitteeMembersState.hs Removes now-redundant standalone committee-members-state module.
libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query.hs Becomes a thin re-export module for the new submodules.
libs/cardano-ledger-api/CHANGELOG.md Documents the breaking reorg and new query/result types.
libs/cardano-ledger-api/cardano-ledger-api.cabal Bumps version, registers new modules, adds dependencies.
eras/shelley/impl/src/Cardano/Ledger/Shelley/API/Wallet.hs Adds deprecations pointing to new query modules; exports currentSnapshot; adds Ord for reward types.
eras/shelley/impl/CHANGELOG.md Notes wallet API deprecations in favor of consolidated queries.
.claude/skills/update-changelogs/SKILL.md Clarifies changelog process rule: top entry is unreleased; don’t add a new section.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query/Snapshot.hs Outdated
Comment thread libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query/StakeDelegation.hs Outdated
Comment thread libs/cardano-ledger-api/CHANGELOG.md Outdated
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 19 out of 19 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query/UTxO.hs
Comment thread libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query/Governance.hs Outdated
Comment thread libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query.hs
Comment thread libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query/Debug.hs Outdated
aniketd added 7 commits April 6, 2026 19:50
Add haddock to mark usage sites across downstream dependencies.
Add usage-site sources in haddock.
Add stable types prefixed QueryResult*. Add CBOR round-trip tests.
Add stable QueryResult* types for some of them.
Add stable QueryResult* types for Snapshot and Snapshots.
Also simpify querySnapshots to remove the Maybe Set parameter to just a
Set, where an emtpy Set means return ALL pools.
Move queryStakePoolDelegsAndRewards into Query.StakeDelegation submodule.
Add QueryResultDelegsAndRewards stable type replacing the tuple. Now,
passing an emtpy set means the consumer is asking for all credentials.
Rename QueryPoolStateResult to QueryResultPoolState, and
queryPoolParameters to queryStakePoolParams. Simplify queryPoolState to
return all pool states when given an empty set.

Query is now a thin-wrapper re-exporting sub-modules.
aniketd added 6 commits April 6, 2026 19:51
These queries are from consensus, cardano-api, cardano-cli, and
Shelley.API.Wallet.
Some debug/deprecated queries.
New sub-module for reward-related queries (non-myopic member rewards,
per-pool reward info, reward provenance). Adds Ord instances
to RewardInfoPool and RewardParams in Wallet.hs to support the
QueryResultRewardInfoPools stable type.
Api.State.Query sub-modules are the place for all ledger-state queries.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 19 out of 19 changed files in this pull request and generated 6 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread libs/ledger-state/bench/Performance.hs Outdated
Comment thread libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query/Governance.hs Outdated
Comment thread libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query/Pool.hs Outdated
Comment thread libs/cardano-ledger-api/CHANGELOG.md Outdated
Comment thread libs/cardano-ledger-api/CHANGELOG.md Outdated
Comment thread libs/cardano-ledger-api/CHANGELOG.md Outdated
import Cardano.Ledger.Shelley.PParams (ProposedPPUpdates, emptyPPPUpdates)

-- | Query the full 'EpochState' for debugging.
-- Source: ouroboros-consensus:ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Query.hs:424
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These comments are guaranteed to get out of date very soon. I wonder if it would make sense to just name the module (e.g. Ouroboros.Consensus.Shelley.Ledger.Query)

Are they pointing at current usage or future usage?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that it will be quite difficult for the release engineer to figure out where to use the new queries added here, as some of them are completely new, whose bodies are currently in-line in certain downstream modules. Furthermore, some queries may have been changed or in a way, optimised, and therefore will not match syntactic searches to find those places. I have made this comment in the re-exporter "mother module" Query, as such.

I know this is weird, and unconventional, and a form of ephemeral communication between team-members by leaving messages in haddock, instead of using slack or issue comments here. But because it lives alongside the actual queries and the integration process is expected to delete them, I thought it is the safest and most clear - in-band, same-channel - form of communication that would be hard to get wrong or miss.

Let me know what you think. 🙂

Copy link
Copy Markdown
Contributor

@f-f f-f Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think migrations for breaking changes of this kind are best documented in a migration guide, rather than in the source. The source is by definition a formal spec and represents current intent, but guidance for how the downstream code needs to change is (IMHO) probably best given in a more informal guidance document "look, this functionality you were using is no more, so this is how you achieve circa the same" (e.g. this is how we do migration documents for PureScript, which is what I am thinking about here).

If you'd rather keep this guidance in the source (so it's in-band, which I also understand) then it should be spelled out in the deprecation notices, so that these warnings are notified to the user in the right places in their source.

Comment thread libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query/Debug.hs Outdated
import Cardano.Ledger.Slot (EpochNo)

-- | Query the current epoch number.
-- Source: ouroboros-consensus:ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Query.hs:413
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Links to mutable source code make no sense, since they will get outdated very quickly.
If you want to add links to the source, add them in PR review as a comment, since that is the actual audience

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I know this is totally weird, so I answered @f-f 's comment also 🙂 . I may be completely off the optimal solution to this. Let me know what you think. #5690 (comment)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I add a table or something to this PR that the release engineer can then track for the work? Or would it be better to open another tracking issue with all the references?

Comment thread libs/cardano-ledger-api/CHANGELOG.md Outdated
## 1.14.0.0

*
* Refactor pool related queries into `Query.Pool` and re-export from `Query`.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not visible to the outside world, as such it should not be even mentioned in the changelog

Since the query consolidation breaks downstream anyway (new types,
renamed functions, changed return types), keeping deprecated aliases
adds noise without value. Remove every deprecation across the Query
sub-modules and Shelley.API.Wallet for a completely fresh API.

Query sub-modules:
- Remove type aliases: CommitteeMemberState, CommitteeMembersState,
  QueryPoolStateResult, StakeSnapshot, StakeSnapshots
- Remove function aliases: mkQueryPoolStateResult, queryPoolParameters
- Remove queryProposedPParamsUpdates (always returned empty)
- Un-deprecate queryRewardProvenance (serves a distinct purpose)
- Remove {-# OPTIONS_GHC -Wno-deprecations #-} from Query.hs and
  Governance.hs

Shelley.API.Wallet:
- Remove 11 query functions: getUTxO, getFilteredUTxO, getUTxOSubset,
  getPools, getStakePools, poolsByTotalStakeFraction, getTotalStake,
  getNonMyopicMemberRewards, currentSnapshot, getRewardInfoPools,
  getRewardProvenance
- Clean up unused imports and language extensions

Add "Replaces @oldname@" haddock annotations to all 10 replacement
query functions to guide downstream migration. Update both CHANGELOGs
with explicit per-function replacement mappings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Transfer all logic for ledger state queries from Consensus

4 participants