Skip to main content

Queries

Ouroboros-consensus implements a querying interface to ask information about the ledger state or about the shape of the blockchain. This interface is exposed via the LocalStateQuery mini-protocol (Section 3.13 in the Ouroboros Network Specification).

Queries can be categorized in 3 classes:

  • Top level queries:

    • GetSystemStart returns the start time of the system,
    • GetChainBlockNo returns the current tip block number,
    • GetChainPoint returns the current tip point (slot number and hash),
    • DebugLedgerConfig returns a debug representation of the ledger configuration in use,
    • BlockQuery which depends on the particular block type, concretized in BlockQuery (HardForkBlock xs) for the Hard Fork Block and in particular the Cardano block, and BlockQuery (ShelleyBlock proto era) for the shelley based eras.
  • Hard Fork era independent queries (BlockQuery (HardForkBlock xs)): can be answered at any point in the chain.

  • Single-era block queries, note these can only be interpreted when the tip of the chain is on the particular era, wrapped in QueryIfCurrent:

    • Byron queries (BlockQuery ByronBlock): which consists only of GetUpdateInterfaceState which returns the state of the blockchain. This query can only be answered while the node's tip is still in the Byron era.
    • Shelley queries (BlockQuery (ShelleyBlock proto era)): these queries give access to information on the Ledger state. They are discussed below as there are many of them.

Versioning

Versioning of queries is done in multiple layers, some as global versions, some as block-dependent versions. When a client connects to a cardano-node they negotiate the highest NodeToClientVersion they both know about and use that one for deciding which queries are available and how to interpret the results.

The top-level available queries depend on the NodeToClientVersion.

NodeToClientVersionQueryVersionNewly enabled top-level queries
NodeToClientV_16QueryVersion2BlockQuery, GetSystemStart, GetChainBlockNo, GetChainPoint
NodeToClientV_17QueryVersion2
NodeToClientV_18QueryVersion2
NodeToClientV_19QueryVersion2
NodeToClientV_20QueryVersion3DebugLedgerConfig
NodeToClientV_21QueryVersion3
NodeToClientV_22QueryVersion3
NodeToClientV_23QueryVersion3

Particular block-query versions are of type BlockNodeToNodeVersion blk, which is associated with the global NodeToClientVersion in supportedNodeToClientVersions. There exist associations for the Byron and Shelley blocks alone but those are in principle uninteresting for mainnet, and instead we focus on the Cardano version. All the current versions imply also HardForkSpecificNodeToClientVersion3 and ByronNodeToClientVersion1:

NodeToClientVersionBlockNodeToNodeVersion blkShelleyNodeToClientVersion
NodeToClientV_16CardanoNodeToClientVersion12ShelleyNodeToClientVersion8
NodeToClientV_17CardanoNodeToClientVersion13ShelleyNodeToClientVersion9
NodeToClientV_18CardanoNodeToClientVersion14ShelleyNodeToClientVersion10
NodeToClientV_19CardanoNodeToClientVersion15ShelleyNodeToClientVersion11
NodeToClientV_20CardanoNodeToClientVersion16ShelleyNodeToClientVersion12
NodeToClientV_21CardanoNodeToClientVersion17ShelleyNodeToClientVersion13
NodeToClientV_22CardanoNodeToClientVersion18ShelleyNodeToClientVersion14
NodeToClientV_23CardanoNodeToClientVersion19ShelleyNodeToClientVersion15

Codecs

Queries are sent over the LocalStateQuery mini-protocol (section 3.13 in the Network documentation). First the client has to request the acquisition of a point to run the queries on, and after confirmation from the server, the client can send msgQuery messages, finishing with a msgRelease signal.

Queries are encoded with queryEncodeNodeToClient and decoded with queryDecodeNodeToClient. Query results are encoded via the SerialiseResult class.

Query binary format is a CBOR encoding of tagged constructors, followed by the arguments for the query. Note the description of the binary format is representing almost CBOR diagnostics notation, so containers without underscores mean definite-length containers. The top level queries are encoded as:

QueryBinary format
BlockQuery query[0, <encode query>]
GetSystemStart[1]
GetChainBlockNo[2]
GetChainPoint[3]

The hard-fork era independent queries are encoded as:

QueryBinary format
QueryIfCurrent blockquery[0, <encode blockquery>]
QueryAnytime GetEraStart era[1, [0], <encode era>]
QueryHardFork GetInterpreter[2, [0]]
QueryHardFork GetCurrentEra[2, [1]]

The block query is an N-ary sum-like construct, so queries for each era are encoded in a tagged construct (note that we concretize the Byron query as there is only one):

Query in eraEncodingConstructor
Byron[0, 0]QZ GetUpdateInterfaceState
Shelley[1, <query>]QS (QZ query)
Allegra[2, <query>]QS (QS (QZ query))
.........

We will discuss the codecs of the Shelley eras' queries in the section below.

The encoding of results is a CBOR encoding of the value, done without any tagging or prefixing. As the client knows which query it sent, it can infer how to decode the result. Sometimes the encoding of the result changes depending on the particular NodeToClientVersion negotiated.

Shelley queries

The different parts of the Ledger State can be accessed via these Shelley queries. The interface is shared among all Shelley-based blocks, but some are only accessible when the tip of the chain is in Conway or a later era. The encoding of the queries consists of a (definite-length) list containing a tag and the serialization of the arguments.

TagQueryOnly active on Shelley NTC versionsArgumentsResult
0GetLedgerTipPoint (ShelleyBlock proto era)
1GetEpochNoEpochNo
2GetNonMyopicMemberRewardsSet (Either Coin (Credential Staking))NonMyopicMemberRewards
3GetCurrentPParamsPParams era
4GetProposedPParamsUpdates< v12ProposedPPUpdates era
5GetStakeDistribution< v13 *PoolDistr (ProtoCrypto proto)
6GetUTxOByAddressSet AddrUTxO era
7GetUTxOWholeUTxO era
8DebugEpochStateEpochState era
9GetCBORthe version of the internal queryBlockQuery (ShelleyBlock proto era) fp resultBlockQuery (ShelleyBlock proto era) fp (Serialised result)
10GetFilteredDelegationsAndRewardAccountsSet (Credential Staking)(Delegations, Map (Credential Staking) Coin)
11GetGenesisConfigCompactGenesis
12DebugNewEpochStateNewEpochState era
13DebugChainDepStateChainDepState proto
14GetRewardProvenanceRewardProvenance
15GetUTxOByTxInSet TxInUTxO era
16GetStakePoolsSet (KeyHash StakePool)
17GetStakePoolParamsSet (KeyHash StakePool)Map (KeyHash StakePool) PoolParams
18GetRewardInfoPools(RewardParams, Map (KeyHash StakePool) RewardInfoPool)
19GetPoolStateMaybe (Set (KeyHash StakePool))QueryPoolStateResult
20GetStakeSnapshotsMaybe (Set (KeyHash StakePool))StakeSnapshots
21GetPoolDistr< v13 *Maybe (Set (KeyHash StakePool))PoolDistr (ProtoCrypto proto)
22GetStakeDelegDepositsSet StakeCredentialMap StakeCredential Coin
23GetConstitutionConstitution era
24GetGovStateGovState era
25GetDRepStateSet (Credential DRepRole)Map (Credential DRepRole) DRepState
26GetDRepStakeDistrSet DRepMap DRep Coin
27GetCommitteeMembersStateSet (Credential ColdCommitteeRole), Set (Credential HotCommitteeRole), Set MemberStatusCommitteeMembersState
28GetFilteredVoteDelegateesSet (Credential Staking)VoteDelegatees
29GetAccountStateChainAccountState
30GetSPOStakeDistrSet (KeyHash StakePool)Map (KeyHash StakePool) Coin
31GetProposals>= v9 †Set GovActionIdSeq (GovActionState era)
32GetRatifyState>= v9 †RatifyState era
33GetFuturePParams>= v10Maybe (PParams era)
34GetBigLedgerPeerSnapshot>= v11LedgerPeerSnapshot
35GetStakePoolDefaultVote>= v12 †KeyHash StakePoolDefaultVote
36GetPoolDistr2>= v13 *Maybe (Set (KeyHash StakePool))PoolDistr
37GetStakeDistribution2>= v13 *PoolDistr
38GetMaxMajorProtocolVersion>= v13MaxMajorProtVer
39GetDRepDelegations>= v14Set DRep(Map DRep (Set (Credential Staking)))

*: The queries enabled only before version 13 used old types from the Ledger that were removed in recent versions. The queries enabled at version 13 use the new corresponding ledger types.

†: Even if an appropriate version is enabled, these queries can only be answered when the corresponding era is Conway or later, as they relate to governance concepts only present starting on Conway.

‡: The format of the result of these queries depend on the particular NodeToClientVersion negotiated. For now, do check encodeShelleyResult to see what exactly changes.