# Query Lifecycle

This document describes the lifecycle of a query in a SDK application, from the user interface to application stores and back. The query will be referred to as Query.

# Pre-requisite Readings

# Query Creation

A query is a request for information made by end-users of applications through an interface and processed by a full-node. Users can query information about the network, the application itself, and application state directly from the application's stores or modules. Note that queries are different from transactions (view the lifecycle here), particularly in that they do not require consensus to be processed (as they do not trigger state-transitions); they can be fully handled by one full-node.

For the purpose of explaining the query lifecycle, let's say Query is requesting a list of delegations made by a certain delegator address in the application called app. As to be expected, the staking module handles this query. But first, there are a few ways Query can be created by users.

# CLI

The main interface for an application is the command-line interface. Users connect to a full-node and run the CLI directly from their machines - the CLI interacts directly with the full-node. To create Query from their terminal, users type the following command:

Copy appcli query staking delegations <delegatorAddress>

This query command was defined by the staking module developer and added to the list of subcommands by the application developer when creating the CLI. The code for this particular command is the following:

Copy // GetCmdQueryDelegations implements the command to query all the delegations // made from one delegator. func GetCmdQueryDelegations(queryRoute string, cdc *codec.Codec) *cobra.Command { return &cobra.Command{ Use: "delegations [delegator-addr]", Short: "Query all delegations made by one delegator", Long: strings.TrimSpace( fmt.Sprintf(`Query delegations for an individual delegator on all validators. Example: $ %s query staking delegations cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p `, version.ClientName, ), ), Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { cliCtx := context.NewCLIContext().WithCodec(cdc) delAddr, err := sdk.AccAddressFromBech32(args[0]) if err != nil { return err } bz, err := cdc.MarshalJSON(types.NewQueryDelegatorParams(delAddr)) if err != nil { return err } route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryDelegatorDelegations) res, _, err := cliCtx.QueryWithData(route, bz) if err != nil { return err } var resp types.DelegationResponses if err := cdc.UnmarshalJSON(res, &resp); err != nil { return err } return cliCtx.PrintOutput(resp) }, } }

Note that the general format is as follows:

Copy appcli query [moduleName] [command] <arguments> --flag <flagArg>

To provide values such as --node (the full-node the CLI connects to), the user can use the config command to set themn or provide them as flags.

The CLI understands a specific set of commands, defined in a hierarchical structure by the application developer: from the root command (appcli), the type of command (query), the module that contains the command (staking), and command itself (delegations). Thus, the CLI knows exactly which module handles this command and directly passes the call there.

# REST

Another interface through which users can make queries is through HTTP Requests to a REST server. The REST server contains, among other things, a Context and mux router. The request looks like this:

Copy GET http://localhost:{PORT}/staking/delegators/{delegatorAddr}/delegations

To provide values such as --node (the full-node the CLI connects to) that are required by baseReq, the user must configure their local REST server with the values or provide them in the request body.

The router automatically routes the Query HTTP request to the staking module delegatorDelegationsHandlerFn() function.

Copy // HTTP request handler to query a delegator delegations func delegatorDelegationsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return queryDelegator(cliCtx, fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryDelegatorDelegations)) }

Since this function is defined within the module and thus has no inherent knowledge of the application Query belongs to, it takes in the application codec and Context as parameters.

To summarize, when users interact with the interfaces, they create a CLI command or HTTP request. Query now exists in one of these two forms, but needs to be transformed into an object understood by a full-node.

# Query Preparation

The interactions from the users' perspective are a bit different, but the underlying functions are almost identical because they are implementations of the same command defined by the module developer. This step of processing happens within the CLI or REST server and heavily involves a Context.

# Context

The first thing that is created in the execution of a CLI command is a Context, while the REST Server directly provides a Context for the REST Request handler. A Context is an immutable object that stores all the data needed to process a request on the user side. In particular, a Context stores the following:

  • Codec: The encoder/decoder used by the application, used to marshal the parameters and query before making the Tendermint RPC request and unmarshal the returned response into a JSON object.
  • Account Decoder: The account decoder from the auth module, which translates []bytes into accounts.
  • RPC Client: The Tendermint RPC Client, or node, to which the request will be relayed to.
  • Keybase: A Key Manager used to sign transactions and handle other operations with keys.
  • Output Writer: A Writer used to output the response.
  • Configurations: The flags configured by the user for this command, including --height, specifying the height of the blockchain to query and --indent, which indicates to add an indent to the JSON response.

The Context also contains various functions such as Query() which retrieves the RPC Client and makes an ABCI call to relay a query to a full-node.

Copy // CLIContext implements a typical CLI context created in SDK modules for // transaction handling and queries. type CLIContext struct { FromAddress sdk.AccAddress Client rpcclient.Client ChainID string Keybase cryptokeys.Keybase Input io.Reader Output io.Writer OutputFormat string Height int64 HomeDir string NodeURI string From string BroadcastMode string Verifier tmlite.Verifier FromName string Codec *codec.Codec TrustNode bool UseLedger bool Simulate bool GenerateOnly bool Indent bool SkipConfirm bool }

The Context's primary role is to store data used during interactions with the end-user and provide methods to interact with this data - it is used before and after the query is processed by the full-node. Specifically, in handling Query, the Context is utilized to encode the query parameters, retrieve the full-node, and write the output. Prior to being relayed to a full-node, the query needs to be encoded into a []byte form, as full-nodes are application-agnostic and do not understand specific types. The full-node (RPC Client) itself is retrieved using the Context, which knows which node the user CLI is connected to. The query is relayed to this full-node to be processed. Finally, the Context contains a Writer to write output when the response is returned. These steps are further described in later sections.

# Arguments and Route Creation

At this point in the lifecycle, the user has created a CLI command or HTTP Request with all of the data they wish to include in their Query. A Context exists to assist in the rest of the Query's journey. Now, the next step is to parse the command or request, extract the arguments, create a queryRoute, and encode everything. These steps all happen on the user side within the interface they are interacting with.

# Encoding

In this case, Query contains an address delegatorAddress as its only argument. However, the request can only contain []bytes, as it will be relayed to a consensus engine (e.g. Tendermint Core) of a full-node that has no inherent knowledge of the application types. Thus, the codec of Context is used to marshal the address.

Here is what the code looks like for the CLI command:

Copy delAddr, err := sdk.AccAddressFromBech32(args[0]) bz, err := cdc.MarshalJSON(types.NewQueryDelegatorParams(delAddr))

Here is what the code looks like for the HTTP Request:

Copy vars := mux.Vars(r) bech32delegator := vars["delegatorAddr"] delegatorAddr, err := sdk.AccAddressFromBech32(bech32delegator) cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) if !ok { return } params := types.NewQueryDelegatorParams(delegatorAddr)

# Query Route Creation

Important to note is that there will never be a "query" object created for Query; the SDK actually takes a simpler approach. Instead of an object, all the full-node needs to process a query is its route which specifies exactly which module to route the query to and the name of this query type. The route will be passed to the application baseapp, then module, then querier, and each will understand the route and pass it to the appropriate next step. baseapp will understand this query to be a custom query in the module staking, and the staking module querier supports the type QueryDelegatorDelegations. Thus, the route will be "custom/staking/delegatorDelegations".

Here is what the code looks like:

Copy route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryDelegatorDelegations)

Now, Query exists as a set of encoded arguments and a route to a specific module and its query type. It is ready to be relayed to a full-node.

# ABCI Query Call

The Context has a Query() function used to retrieve the pre-configured node and relay a query to it; the function takes the query route and arguments as parameters. It first retrieves the RPC Client (called the node) configured by the user to relay this query to, and creates the ABCIQueryOptions (parameters formatted for the ABCI call). The node is then used to make the ABCI call, ABCIQueryWithOptions().

Here is what the code looks like:

Copy // query performs a query to a Tendermint node with the provided store name // and path. It returns the result and height of the query upon success // or an error if the query fails. In addition, it will verify the returned // proof if TrustNode is disabled. If proof verification fails or the query // height is invalid, an error will be returned. func (ctx CLIContext) query(path string, key cmn.HexBytes) (res []byte, height int64, err error) { node, err := ctx.GetNode() if err != nil { return res, height, err } opts := rpcclient.ABCIQueryOptions{ Height: ctx.Height, Prove: !ctx.TrustNode, } result, err := node.ABCIQueryWithOptions(path, key, opts) if err != nil { return res, height, err } resp := result.Response if !resp.IsOK() { return res, resp.Height, errors.New(resp.Log) } // data from trusted node or subspace query doesn't need verification if ctx.TrustNode || !isQueryStoreWithProof(path) { return resp.Value, resp.Height, nil } err = ctx.verifyProof(path, resp) if err != nil { return res, resp.Height, err } return resp.Value, resp.Height, nil }

# RPC

With a call to ABCIQueryWithOptions(), Query is received by a full-node which will then process the request. Note that, while the RPC is made to the consensus engine (e.g. Tendermint Core) of a full-node, queries are not part of consensus and will not be broadcasted to the rest of the network, as they do not require anything the network needs to agree upon.

Read more about ABCI Clients and Tendermint RPC in the Tendermint documentation here.

# Application Query Handling

When a query is received by the full-node after it has been relayed from the underlying consensus engine, it is now being handled within an environment that understands application-specific types and has a copy of the state. baseapp implements the ABCI Query() function and handles four different types of queries: app, store, p2p, and custom. The queryRoute is parsed such that the first string must be one of the four options, then the rest of the path is parsed within the subroutines handling each type of query. The first three types (app, store, p2p) are purely application-level and thus directly handled by baseapp or the stores, but the custom query type requires baseapp to route the query to a module's query service.

Since Query is a custom query type from the staking module, baseapp first parses the path, then uses the QueryRouter to retrieve the corresponding querier, and routes the query to the module. The querier is responsible for recognizing this query, retrieving the appropriate values from the application's stores, and returning a response. Read more about query services here.

Once a result is received from the querier, baseapp begins the process of returning a response to the user.

# Response

Since Query() is an ABCI function, baseapp returns the response as an abci.ResponseQuery type. The Context Query() routine receives the response and.

Copy // verifyProof perform response proof verification. func (ctx CLIContext) verifyProof(queryPath string, resp abci.ResponseQuery) error { if ctx.Verifier == nil { return fmt.Errorf("missing valid certifier to verify data from distrusted node") } // the AppHash for height H is in header H+1 commit, err := ctx.Verify(resp.Height + 1) if err != nil { return err } // TODO: Instead of reconstructing, stash on CLIContext field? prt := rootmulti.DefaultProofRuntime() // TODO: Better convention for path? storeName, err := parseQueryStorePath(queryPath) if err != nil { return err } kp := merkle.KeyPath{} kp = kp.AppendKey([]byte(storeName), merkle.KeyEncodingURL) kp = kp.AppendKey(resp.Key, merkle.KeyEncodingURL) if resp.Value == nil { err = prt.VerifyAbsence(resp.Proof, commit.Header.AppHash, kp.String()) if err != nil { return errors.Wrap(err, "failed to prove merkle proof") } return nil } err = prt.VerifyValue(resp.Proof, commit.Header.AppHash, kp.String(), resp.Value) if err != nil { return errors.Wrap(err, "failed to prove merkle proof") } return nil }

# CLI Response

The application codec is used to unmarshal the response to a JSON and the Context prints the output to the command line, applying any configurations such as --indent.

Copy func GetCmdQueryDelegations(queryRoute string, cdc *codec.Codec) *cobra.Command { return &cobra.Command{ Use: "delegations [delegator-addr]", Short: "Query all delegations made by one delegator", Long: strings.TrimSpace( fmt.Sprintf(`Query delegations for an individual delegator on all validators. Example: $ %s query staking delegations cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p `, version.ClientName, ), ), Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { cliCtx := context.NewCLIContext().WithCodec(cdc) delAddr, err := sdk.AccAddressFromBech32(args[0]) if err != nil { return err } bz, err := cdc.MarshalJSON(types.NewQueryDelegatorParams(delAddr)) if err != nil { return err } route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryDelegatorDelegations) res, _, err := cliCtx.QueryWithData(route, bz) if err != nil { return err } var resp types.DelegationResponses if err := cdc.UnmarshalJSON(res, &resp); err != nil { return err } return cliCtx.PrintOutput(resp) }, } }

# REST Response

The REST server uses the Context to format the response properly, then uses the HTTP package to write the appropriate response or error.

Copy func queryValidator(cliCtx context.CLIContext, endpoint string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) bech32validatorAddr := vars["validatorAddr"] validatorAddr, err := sdk.ValAddressFromBech32(bech32validatorAddr) if err != nil { rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) return } cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) if !ok { return } params := types.NewQueryValidatorParams(validatorAddr) bz, err := cliCtx.Codec.MarshalJSON(params) if err != nil { rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) return } res, height, err := cliCtx.QueryWithData(endpoint, bz) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } cliCtx = cliCtx.WithHeight(height) rest.PostProcessResponse(w, cliCtx, res) } }

# Next

Read about how to build a Command-Line Interface, or a REST Interface