github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/docs/core/baseapp.md (about)

     1  <!--
     2  order: 1
     3  synopsis: This document describes `BaseApp`, the abstraction that implements the core functionalities of an SDK application.
     4  -->
     5  
     6  # Baseapp
     7  
     8  ## Pre-requisite Readings {hide}
     9  
    10  - [Anatomy of an SDK application](../basics/app-anatomy.md) {prereq}
    11  - [Lifecycle of an SDK transaction](../basics/tx-lifecycle.md) {prereq}
    12  
    13  ## Introduction
    14  
    15  `BaseApp` is a base type that implements the core of an SDK application, namely:
    16  
    17  - The [Application Blockchain Interface](#abci), for the state-machine to communicate with the
    18  underlying consensus engine (e.g. Tendermint).
    19  - A [Router](#routing), to route messages and queries to the appropriate module.
    20  - Different [states](#states), as the state-machine can have different volatile
    21  states updated based on the ABCI message received.
    22  
    23  The goal of `BaseApp` is to provide the fundamental layer of an SDK application
    24  that developers can easily extend to build their own custom application. Usually,
    25  developers will create a custom type for their application, like so:
    26  
    27  ```go
    28  type App struct {
    29    // reference to a BaseApp
    30    *bam.BaseApp
    31  
    32    // list of application store keys
    33  
    34    // list of application keepers
    35  
    36    // module manager
    37  }
    38  ```
    39  
    40  Extending the application with `BaseApp` gives the former access to all of `BaseApp`'s methods.
    41  This allows developers to compose their custom application with the modules they want, while not
    42  having to concern themselves with the hard work of implementing the ABCI, the routing and state
    43  management logic.
    44  
    45  ## Type Definition
    46  
    47  The `BaseApp` type holds many important parameters for any Cosmos SDK based application. 
    48  
    49  +++ https://github.com/cosmos/cosmos-sdk/blob/7d7821b9af132b0f6131640195326aa02b6751db/baseapp/baseapp.go#L54-L108
    50  
    51  Let us go through the most important components.
    52  
    53  > __Note__: Not all parameters are described, only the most important ones. Refer to the
    54  type definition for the full list.
    55  
    56  First, the important parameters that are initialized during the bootstrapping of the application:
    57  
    58  - [`CommitMultiStore`](./store.md#commitmultistore): This is the main store of the application,
    59  which holds the canonical state that is committed at the [end of each block](#commit). This store
    60  is **not** cached, meaning it is not used to update the application's volatile (un-committed) states.
    61  The `CommitMultiStore` is a multi-store, meaning a store of stores. Each module of the application
    62  uses one or multiple `KVStores` in the multi-store to persist their subset of the state.
    63  - Database: The `db` is used by the `CommitMultiStore` to handle data persistence.
    64  - [Router](#message-routing): The `router` facilitates the routing of `messages` to the appropriate
    65  module for it to be processed. Here a `message` refers to the transaction components that need to be
    66  processed by the application in order to update the state, and not to ABCI messages which implement
    67  the interface between the application and the underlying consensus engine.
    68  - [Query Router](#query-routing): The `query router` facilitates the routing of queries to the
    69  appropriate module for it to be processed. These `queries` are not ABCI messages themselves, but they
    70  are relayed to the application from the underlying consensus engine via the ABCI message [`Query`](#query).
    71  - [`TxDecoder`](https://godoc.org/github.com/cosmos/cosmos-sdk/types#TxDecoder): It is used to decode
    72  raw transaction bytes relayed by the underlying Tendermint engine.
    73  - `BaseKey`: This key is used to access the main store in the `CommitMultiStore`. The main store is
    74  used to persist data related to the core of the application, like consensus parameters.
    75  - [`AnteHandler`](#antehandler): This handler is used to handle signature verification, fee payment,
    76  and other pre-message execution checks when a transaction is received. It's executed during
    77  [`CheckTx/RecheckTx`](#checktx) and [`DeliverTx`](#delivertx).
    78  - [`InitChainer`](../basics/app-anatomy.md#initchainer),
    79  [`BeginBlocker` and `EndBlocker`](../basics/app-anatomy.md#beginblocker-and-endblocker): These are
    80  the functions executed when the application receives the `InitChain`, `BeginBlock` and `EndBlock`
    81  ABCI messages from the underlying Tendermint engine.
    82  
    83  Then, parameters used to define [volatile states](#volatile-states) (i.e. cached states):
    84  
    85  - `checkState`: This state is updated during [`CheckTx`](#checktx), and reset on [`Commit`](#commit).
    86  - `deliverState`: This state is updated during [`DeliverTx`](#delivertx), and set to `nil` on
    87  [`Commit`](#commit) and gets re-initialized on BeginBlock.
    88  
    89  Finally, a few more important parameterd:
    90  
    91  - `voteInfos`: This parameter carries the list of validators whose precommit is missing, either
    92  because they did not vote or because the proposer did not include their vote. This information is
    93  carried by the [Context](#context) and can be used by the application for various things like
    94  punishing absent validators.
    95  - `minGasPrices`: This parameter defines the minimum gas prices accepted by the node. This is a
    96  **local** parameter, meaning each full-node can set a different `minGasPrices`. It is used in the
    97  `AnteHandler` during [`CheckTx`](#checktx), mainly as a spam protection mechanism. The transaction
    98  enters the [mempool](https://tendermint.com/docs/tendermint-core/mempool.html#transaction-ordering)
    99  only if the gas prices of the transaction are greater than one of the minimum gas price in
   100  `minGasPrices` (e.g. if `minGasPrices == 1uatom,1photon`, the `gas-price` of the transaction must be
   101  greater than `1uatom` OR `1photon`).
   102  - `appVersion`: Version of the application. It is set in the
   103  [application's constructor function](../basics/app-anatomy.md#constructor-function).
   104  
   105  ## Constructor
   106  
   107  ```go
   108  func NewBaseApp(
   109    name string, logger log.Logger, db dbm.DB, txDecoder sdk.TxDecoder, options ...func(*BaseApp),
   110  ) *BaseApp {
   111  
   112    // ...
   113  }
   114  ```
   115  
   116  The `BaseApp` constructor function is pretty straightforward. The only thing worth noting is the
   117  possibility to provide additional [`options`](https://github.com/cosmos/cosmos-sdk/blob/7d7821b9af132b0f6131640195326aa02b6751db/baseapp/options.go)
   118  to the `BaseApp`, which will execute them in order. The `options` are generally `setter` functions
   119  for important parameters, like `SetPruning()` to set pruning options or `SetMinGasPrices()` to set
   120  the node's `min-gas-prices`.
   121  
   122  Naturally, developers can add additional `options` based on their application's needs.
   123  
   124  ## State Updates 
   125  
   126  The `BaseApp` maintains two primary volatile states and a root or main state. The main state
   127  is the canonical state of the application and the volatile states, `checkState` and `deliverState`,
   128  are used to handle state transitions in-between the main state made during [`Commit`](#commit).
   129  
   130  Internally, there is only a single `CommitMultiStore` which we refer to as the main or root state.
   131  From this root state, we derive two volatile state through a mechanism called cache-wrapping. The
   132  types can be illustrated as follows:
   133  
   134  ![Types](./baseapp_state_types.png)
   135  
   136  ### InitChain State Updates
   137  
   138  During `InitChain`, the two volatile states, `checkState` and `deliverState` are set by cache-wrapping
   139  the root `CommitMultiStore`. Any subsequent reads and writes happen on cached versions of the `CommitMultiStore`.
   140  
   141  ![InitChain](./baseapp_state-initchain.png)
   142  
   143  ### CheckTx State Updates
   144  
   145  During `CheckTx`, the `checkState`, which is based off of the last committed state from the root
   146  store, is used for any reads and writes. Here we only execute the `AnteHandler` and verify a router
   147  exists for every message in the transaction. Note, when we execute the `AnteHandler`, we cache-wrap
   148  the already cache-wrapped `checkState`. This has the side effect that if the `AnteHandler` fails,
   149  the state transitions won't be reflected in the `checkState` -- i.e. `checkState` is only updated on
   150  success.
   151  
   152  ![CheckTx](./baseapp_state-checktx.png)
   153  
   154  ### BeginBlock State Updates
   155  
   156  During `BeginBlock`, the `deliverState` is set for use in subsequent `DeliverTx` ABCI messages. The
   157  `deliverState` is based off of the last committed state from the root store and is cache-wrapped.
   158  Note, the `deliverState` is set to `nil` on [`Commit`](#commit).
   159  
   160  ![BeginBlock](./baseapp_state-begin_block.png)
   161  
   162  ### DeliverTx State Updates
   163  
   164  The state flow for `DeliverTx` is nearly identical to `CheckTx` except state transitions occur on
   165  the `deliverState` and messages in a transaction are executed. Similarly to `CheckTx`, state transitions
   166  occur on a doubly cache-wrapped state -- `deliverState`. Successful message execution results in
   167  writes being committed to `deliverState`. Note, if message execution fails, state transitions from
   168  the AnteHandler are persisted.
   169  
   170  ![DeliverTx](./baseapp_state-deliver_tx.png)
   171  
   172  ### Commit State Updates
   173  
   174  During `Commit` all the state transitions that occurred in the `deliverState` are finally written to
   175  the root `CommitMultiStore` which in turn is committed to disk and results in a new application
   176  root hash. These state transitions are now considered final. Finally, the `checkState` is set to the
   177  newly committed state and `deliverState` is set to `nil` to be reset on `BeginBlock`.
   178  
   179  ![Commit](./baseapp_state-commit.png)
   180  
   181  ## Routing
   182  
   183  When messages and queries are received by the application, they must be routed to the appropriate module in order to be processed. Routing is done via `baseapp`, which holds a `router` for messages, and a `query router` for queries.
   184  
   185  ### Message Routing
   186  
   187  [`Message`s](#../building-modules/messages-and-queries.md#messages) need to be routed after they are extracted from transactions, which are sent from the underlying Tendermint engine via the [`CheckTx`](#checktx) and [`DeliverTx`](#delivertx) ABCI messages. To do so, `baseapp` holds a `router` which maps `paths` (`string`) to the appropriate module [`handler`](../building-modules/handler.md) using the `.Route(ctx sdk.Context, path string)` function. Usually, the `path` is the name of the module.
   188  
   189  The [default router included in baseapp](https://github.com/cosmos/cosmos-sdk/blob/master/baseapp/router.go) is stateless.  However, some applications may want to make use of more stateful routing mechanisms such as allowing governance to disable certain routes or point them to new modules for upgrade purposes.  For this reason, the `sdk.Context` is also passed into the `Route` function of the [Router interface](https://github.com/cosmos/cosmos-sdk/blob/master/types/router.go#L12).  For a stateless router that doesn't want to make use of this, can just ignore the ctx.
   190  
   191  The application's `router` is initilalized with all the routes using the application's [module manager](../building-modules/module-manager.md#manager), which itself is initialized with all the application's modules in the application's [constructor](../basics/app-anatomy.md#app-constructor). 
   192  
   193  ### Query Routing
   194  
   195  Similar to `message`s, [`queries`](../building-modules/messages-and-queries.md#queries) need to be routed to the appropriate module's [querier](../building-modules/querier.md). To do so, `baseapp` holds a `query router`, which maps module names to module `querier`s. The `queryRouter` is called during the initial stages of `query` processing, which is done via the [`Query` ABCI message](#query). 
   196  
   197  Just like the `router`, the `query router` is initilalized with all the query routes using the application's [module manager](../building-modules/module-manager.md), which itself is initialized with all the application's modules in the application's [constructor](../basics/app-anatomy.md#app-constructor).
   198  
   199  ## Main ABCI Messages
   200  
   201  The [Application-Blockchain Interface](https://tendermint.com/docs/spec/abci/) (ABCI) is a generic interface that connects a state-machine with a consensus engine to form a functional full-node. It can be wrapped in any language, and needs to be implemented by each application-specific blockchain built on top of an ABCI-compatible consensus engine like Tendermint. 
   202  
   203  The consensus engine handles two main tasks:
   204  
   205  - The networking logic, which mainly consists in gossiping block parts, transactions and consensus votes.
   206  - The consensus logic, which results in the deterministic ordering of transactions in the form of blocks. 
   207  
   208  It is **not** the role of the consensus engine to define the state or the validity of transactions. Generally, transactions are handled by the consensus engine in the form of `[]bytes`, and relayed to the application via the ABCI to be decoded and processed. At keys moments in the networking and consensus processes (e.g. beginning of a block, commit of a block, reception of an unconfirmed transaction, ...), the consensus engine emits ABCI messages for the state-machine to act on. 
   209  
   210  Developers building on top of the Cosmos SDK need not implement the ABCI themselves, as `baseapp` comes with a built-in implementation of the interface. Let us go through the main ABCI messages that `baseapp` implements: [`CheckTx`](#checktx) and [`DeliverTx`](#delivertx)
   211  
   212  ### CheckTx
   213  
   214  `CheckTx` is sent by the underlying consensus engine when a new unconfirmed (i.e. not yet included in a valid block)
   215  transaction is received by a full-node. The role of `CheckTx` is to guard the full-node's mempool
   216  (where unconfirmed transactions are stored until they are included in a block) from spam transactions.
   217  Unconfirmed transactions are relayed to peers only if they pass `CheckTx`.
   218  
   219  `CheckTx()` can perform both _stateful_ and _stateless_ checks, but developers should strive to
   220  make them lightweight. In the Cosmos SDK, after [decoding transactions](./encoding.md), `CheckTx()` is implemented
   221  to do the following checks:
   222  
   223  1. Extract the `message`s from the transaction.
   224  2. Perform _stateless_ checks by calling `ValidateBasic()` on each of the `messages`. This is done
   225     first, as _stateless_ checks are less computationally expensive than _stateful_ checks. If
   226     `ValidateBasic()` fail, `CheckTx` returns before running _stateful_ checks, which saves resources.
   227  3. Perform non-module related _stateful_ checks on the [account](../basics/accounts.md). This step is mainly about checking
   228     that the `message` signatures are valid, that enough fees are provided and that the sending account
   229     has enough funds to pay for said fees. Note that no precise [`gas`](../basics/gas-fees.md) counting occurs here,
   230     as `message`s are not processed. Usually, the [`AnteHandler`](../basics/gas-fees.md#antehandler) will check that the `gas` provided
   231     with the transaction is superior to a minimum reference gas amount based on the raw transaction size,
   232     in order to avoid spam with transactions that provide 0 gas.
   233  4. Ensure that a [`Route`](#message-routing) exists for each `message`, but do **not** actually
   234     process `message`s. `Message`s only need to be processed when the canonical state need to be updated,
   235     which happens during `DeliverTx`.
   236  
   237  Steps 2. and 3. are performed by the [`AnteHandler`](../basics/gas-fees.md#antehandler) in the [`RunTx()`](#runtx-antehandler-and-runmsgs)
   238  function, which `CheckTx()` calls with the `runTxModeCheck` mode. During each step of `CheckTx()`, a
   239  special [volatile state](#volatile-states) called `checkState` is updated. This state is used to keep
   240  track of the temporary changes triggered by the `CheckTx()` calls of each transaction without modifying
   241  the [main canonical state](#main-state) . For example, when a transaction goes through `CheckTx()`, the
   242  transaction's fees are deducted from the sender's account in `checkState`. If a second transaction is
   243  received from the same account before the first is processed, and the account has consumed all its
   244  funds in `checkState` during the first transaction, the second transaction will fail `CheckTx`() and
   245  be rejected. In any case, the sender's account will not actually pay the fees until the transaction
   246  is actually included in a block, because `checkState` never gets committed to the main state. The
   247  `checkState` is reset to the latest state of the main state each time a blocks gets [committed](#commit).
   248  
   249  `CheckTx` returns a response to the underlying consensus engine of type [`abci.ResponseCheckTx`](https://tendermint.com/docs/spec/abci/abci.html#messages).
   250  The response contains:
   251  
   252  - `Code (uint32)`: Response Code. `0` if successful. 
   253  - `Data ([]byte)`: Result bytes, if any.
   254  - `Log (string):` The output of the application's logger. May be non-deterministic.
   255  - `Info (string):` Additional information. May be non-deterministic.
   256  - `GasWanted (int64)`: Amount of gas requested for transaction. It is provided by users when they generate the transaction. 
   257  - `GasUsed (int64)`: Amount of gas consumed by transaction. During `CheckTx`, this value is computed by multiplying the standard cost of a transaction byte by the size of the raw transaction. Next is an example:
   258    +++ https://github.com/cosmos/cosmos-sdk/blob/7d7821b9af132b0f6131640195326aa02b6751db/x/auth/ante/basic.go#L104
   259  - `Events ([]cmn.KVPair)`: Key-Value tags for filtering and indexing transactions (eg. by account). See [`event`s](./events.md) for more.
   260  - `Codespace (string)`: Namespace for the Code.
   261  
   262  #### RecheckTx
   263  
   264  After `Commit`, `CheckTx` is run again on all transactions that remain in the node's local mempool
   265  after filtering those included in the block. To prevent the mempool from rechecking all transactions
   266  every time a block is committed, the configuration option `mempool.recheck=false` can be set. As of
   267  Tendermint v0.32.1, an additional `Type` parameter is made available to the `CheckTx` function that
   268  indicates whether an incoming transaction is new (`CheckTxType_New`), or a recheck (`CheckTxType_Recheck`).
   269  This allows certain checks like signature verification can be skipped during `CheckTxType_Recheck`.
   270  
   271  ### DeliverTx
   272  
   273  When the underlying consensus engine receives a block proposal, each transaction in the block needs to be processed by the application. To that end, the underlying consensus engine sends a `DeliverTx` message to the application for each transaction in a sequential order.
   274  
   275  Before the first transaction of a given block is processed, a [volatile state](#volatile-states) called `deliverState` is intialized during [`BeginBlock`](#beginblock). This state is updated each time a transaction is processed via `DeliverTx`, and committed to the [main state](#main-state) when the block is [committed](#commit), after what is is set to `nil`. 
   276  
   277  `DeliverTx` performs the **exact same steps as `CheckTx`**, with a little caveat at step 3 and the addition of a fifth step:
   278  
   279  1. The `AnteHandler` does **not** check that the transaction's `gas-prices` is sufficient. That is because the `min-gas-prices` value `gas-prices` is checked against is local to the node, and therefore what is enough for one full-node might not be for another. This means that the proposer can potentially include transactions for free, although they are not incentivised to do so, as they earn a bonus on the total fee of the block they propose.
   280  2. For each `message` in the transaction, route to the appropriate module's [`handler`](../building-modules/handler.md). Additional _stateful_ checks are performed, and the cache-wrapped multistore held in `deliverState`'s `context` is updated by the module's `keeper`. If the `handler` returns successfully, the cache-wrapped multistore held in `context` is written to `deliverState` `CacheMultiStore`.
   281  
   282  During step 5., each read/write to the store increases the value of `GasConsumed`. You can find the default cost of each operation:
   283  
   284  +++ https://github.com/cosmos/cosmos-sdk/blob/7d7821b9af132b0f6131640195326aa02b6751db/store/types/gas.go#L142-L150
   285  
   286  At any point, if `GasConsumed > GasWanted`, the function returns with `Code != 0` and `DeliverTx` fails. 
   287  
   288  `DeliverTx` returns a response to the underlying consensus engine of type [`abci.ResponseDeliverTx`](https://tendermint.com/docs/spec/abci/abci.html#delivertx). The response contains:
   289  
   290  - `Code (uint32)`: Response Code. `0` if successful. 
   291  - `Data ([]byte)`: Result bytes, if any.
   292  - `Log (string):` The output of the application's logger. May be non-deterministic.
   293  - `Info (string):` Additional information. May be non-deterministic.
   294  - `GasWanted (int64)`: Amount of gas requested for transaction. It is provided by users when they generate the transaction. 
   295  - `GasUsed (int64)`: Amount of gas consumed by transaction. During `DeliverTx`, this value is computed by multiplying the standard cost of a transaction byte by the size of the raw transaction, and by adding gas each time a read/write to the store occurs. 
   296  - `Events ([]cmn.KVPair)`: Key-Value tags for filtering and indexing transactions (eg. by account). See [`event`s](./events.md) for more.
   297  - `Codespace (string)`: Namespace for the Code.
   298  
   299  ## RunTx, AnteHandler and RunMsgs
   300  
   301  ### RunTx
   302  
   303  `RunTx` is called from `CheckTx`/`DeliverTx` to handle the transaction, with `runTxModeCheck` or `runTxModeDeliver` as parameter to differentiate between the two modes of execution. Note that when `RunTx` receives a transaction, it has already been decoded. 
   304  
   305  The first thing `RunTx` does upon being called is to retrieve the `context`'s `CacheMultiStore` by calling the `getContextForTx()` function with the appropriate mode (either `runTxModeCheck` or `runTxModeDeliver`). This `CacheMultiStore` is a cached version of the main store instantiated during `BeginBlock` for `DeliverTx` and during the `Commit` of the previous block for `CheckTx`.  After that, two `defer func()` are called for [`gas`](../basics/gas-fees.md) management. They are executed when `runTx` returns and make sure `gas` is actually consumed, and will throw errors, if any.
   306  
   307  After that, `RunTx()` calls `ValidateBasic()` on each `message`in the `Tx`, which runs preliminary _stateless_ validity checks. If any `message` fails to pass `ValidateBasic()`, `RunTx()` returns with an error.
   308  
   309  Then, the [`anteHandler`](#antehandler) of the application is run (if it exists). In preparation of this step, both the `checkState`/`deliverState`'s `context` and `context`'s `CacheMultiStore` are cached-wrapped using the `cacheTxContext()` function. 
   310  
   311  +++ https://github.com/cosmos/cosmos-sdk/blob/7d7821b9af132b0f6131640195326aa02b6751db/baseapp/baseapp.go#L587
   312  
   313  This allows `RunTx` not to commit the changes made to the state during the execution of `anteHandler` if it ends up failing. It also prevents the module implementing the `anteHandler` from writing to state, which is an important part of the [object-capabilities](./ocap.md) of the Cosmos SDK. 
   314  
   315  Finally, the [`RunMsgs()`](#runmsgs) function is called to process the `messages`s in the `Tx`. In preparation of this step, just like with the `anteHandler`, both the `checkState`/`deliverState`'s `context` and `context`'s `CacheMultiStore` are cached-wrapped using the `cacheTxContext()` function.
   316  
   317  ### AnteHandler
   318  
   319  The `AnteHandler` is a special handler that implements the `anteHandler` interface and is used to authenticate the transaction before the transaction's internal messages are processed.
   320  
   321  +++ https://github.com/cosmos/cosmos-sdk/blob/7d7821b9af132b0f6131640195326aa02b6751db/types/handler.go#L8
   322  
   323  The `AnteHandler` is theoretically optional, but still a very important component of public blockchain networks. It serves 3 primary purposes:
   324  
   325  - Be a primary line of defense against spam and second line of defense (the first one being the mempool) against transaction replay with fees deduction and [`sequence`](./transactions.md#transaction-generation) checking. 
   326  - Perform preliminary *stateful* validity checks like ensuring signatures are valid or that the sender has enough funds to pay for fees. 
   327  - Play a role in the incentivisation of stakeholders via the collection of transaction fees. 
   328  
   329  `baseapp` holds an `anteHandler` as paraemter, which is initialized in the [application's constructor](../basics/app-anatomy.md#application-constructor). The most widely used `anteHandler` today is that of the [`auth` module](https://github.com/cosmos/cosmos-sdk/blob/master/x/auth/ante/ante.go).
   330  
   331  Click [here](../basics/gas-fees.md#antehandler) for more on the `anteHandler`. 
   332  
   333  ### RunMsgs
   334  
   335  `RunMsgs` is called from `RunTx` with `runTxModeCheck` as parameter to check the existence of a route for each message the transaction, and with `runTxModeDeliver` to actually process the `message`s. 
   336  
   337  First, it retreives the `message`'s `route` using the `Msg.Route()` method. Then, using the application's [`router`](#routing) and the `route`, it checks for the existence of a `handler`. At this point, if `mode == runTxModeCheck`, `RunMsgs` returns. If instead `mode == runTxModeDeliver`, the [`handler`](../building-modules/handler.md) function for the message is executed, before `RunMsgs` returns. 
   338  
   339  ## Other ABCI Messages
   340  
   341  ### InitChain
   342  
   343  The [`InitChain` ABCI message](https://tendermint.com/docs/app-dev/abci-spec.html#initchain) is sent from the underlying Tendermint engine when the chain is first started. It is mainly used to **initialize** parameters and state like:
   344  
   345  - [Consensus Parameters](https://tendermint.com/docs/spec/abci/apps.html#consensus-parameters) via `setConsensusParams`. 
   346  - [`checkState` and `deliverState`](#volatile-states) via `setCheckState` and `setDeliverState`.
   347  - The [block gas meter](../basics/gas-fees.md#block-gas-meter), with infinite gas to process genesis transactions. 
   348  
   349  Finally, the `InitChain(req abci.RequestInitChain)` method of `baseapp` calls the [`initChainer()`](../basics/app-anatomy.md#initchainer) of the application in order to initialize the main state of the application from the `genesis file` and, if defined, call the [`InitGenesis`](../building-modules/genesis.md#initgenesis) function of each of the application's modules.
   350  
   351  ### BeginBlock
   352  
   353  The [`BeginBlock` ABCI message](#https://tendermint.com/docs/app-dev/abci-spec.html#beginblock) is sent from the underlying Tendermint engine when a block proposal created by the correct proposer is received, before [`DeliverTx`](#delivertx) is run for each transaction in the block. It allows developers to have logic be executed at the beginning of each block. In the Cosmos SDK, the `BeginBlock(req abci.RequestBeginBlock)` method does the following:
   354  
   355  - Initialize [`deliverState`](#volatile-states) with the latest header using the `req abci.RequestBeginBlock` passed as parameter via the `setDeliverState` function. 
   356    +++ https://github.com/cosmos/cosmos-sdk/blob/7d7821b9af132b0f6131640195326aa02b6751db/baseapp/baseapp.go#L387-L397
   357  This function also resets the [main gas meter](../basics/gas-fees.md#main-gas-meter).
   358  - Initialize the [block gas meter](../basics/gas-fees.md#block-gas-meter) with the `maxGas` limit. The `gas` consumed within the block cannot go above `maxGas`. This parameter is defined in the application's consensus parameters. 
   359  - Run the application's [`beginBlocker()`](../basics/app-anatomy.md#beginblocker-and-endblock), which mainly runs the [`BeginBlocker()`](../building-modules/beginblock-endblock.md#beginblock) method of each of the application's modules.
   360  - Set the [`VoteInfos`](https://tendermint.com/docs/app-dev/abci-spec.html#voteinfo) of the application, i.e. the list of validators whose *precommit* for the previous block was included by the proposer of the current block. This information is carried into the [`Context`](./context.md) so that it can be used during `DeliverTx` and `EndBlock`.
   361  
   362  ### EndBlock
   363  
   364  The [`EndBlock` ABCI message](#https://tendermint.com/docs/app-dev/abci-spec.html#endblock) is sent from the underlying Tendermint engine after [`DeliverTx`](#delivertx) as been run for each transaction in the block. It allows developers to have logic be executed at the end of each block. In the Cosmos SDK, the bulk `EndBlock(req abci.RequestEndBlock)` method is to run the application's [`EndBlocker()`](../basics/app-anatomy.md#beginblocker-and-endblock), which mainly runs the [`EndBlocker()`](../building-modules/beginblock-endblock.md#beginblock) method of each of the application's modules. 
   365  
   366  ### Commit
   367  
   368  The [`Commit` ABCI message](https://tendermint.com/docs/app-dev/abci-spec.html#commit) is sent from the underlying Tendermint engine after the full-node has received *precommits* from 2/3+ of validators (weighted by voting power). On the `baseapp` end, the `Commit(res abci.ResponseCommit)` function is implemented to commit all the valid state transitions that occured during `BeginBlock`, `DeliverTx` and `EndBlock` and to reset state for the next block. 
   369  
   370  To commit state-transitions, the `Commit` function calls the `Write()` function on `deliverState.ms`, where `deliverState.ms` is a cached multistore of the main store `app.cms`. Then, the `Commit` function sets `checkState` to the latest header (obtbained from `deliverState.ctx.BlockHeader`) and `deliverState` to `nil`.
   371  
   372  Finally, `Commit` returns the hash of the commitment of `app.cms` back to the underlying consensus engine. This hash is used as a reference in the header of the next block. 
   373  
   374  ### Info
   375  
   376  The [`Info` ABCI message](https://tendermint.com/docs/app-dev/abci-spec.html#info) is a simple query from the underlying consensus engine, notably used to sync the latter with the application during a handshake that happens on startup. When called, the `Info(res abci.ResponseInfo)` function from `baseapp` will return the application's name, version and the hash of the last commit of `app.cms`. 
   377  
   378  ### Query 
   379  
   380  The [`Query` ABCI message](https://tendermint.com/docs/app-dev/abci-spec.html#query) is used to serve queries received from the underlying consensus engine, including queries received via RPC like Tendermint RPC. It is the main entrypoint to build interfaces with the application. The application must respect a few rules when implementing the `Query` method, which are outlined [here](https://tendermint.com/docs/app-dev/abci-spec.html#query). 
   381  
   382  Each `query` comes with a `path`, which contains multiple `string`s. By convention, the first element of the `path` (`path[0]`) contains the category of `query` (`app`, `p2p`, `store` or `custom`). The `baseapp` implementation of the `Query(req abci.RequestQuery)` method is a simple dispatcher serving these 4 main categories of queries:
   383  
   384  - Application-related queries like querying the application's version, which are served via the `handleQueryApp` method.
   385  - Direct queries to the multistore, which are served by the `handlerQueryStore` method. These direct queryeis are different from custom queries which go through `app.queryRouter`, and are mainly used by third-party service provider like block explorers. 
   386  - P2P queries, which are served via the `handleQueryP2P` method. These queries return either `app.addrPeerFilter` or `app.ipPeerFilter` that contain the list of peers filtered by address or IP respectively. These lists are first initialized via `options` in `baseapp`'s [constructor](#constructor).
   387  - Custom queries, which encompass most queries, are served via the `handleQueryCustom` method. The `handleQueryCustom` cache-wraps the multistore before using the `queryRoute` obtained from [`app.queryRouter`](#query-routing) to map the query to the appropriate module's `querier`.
   388  
   389  ## Next {hide}
   390  
   391  Learn more about [transactions](./transactions.md) {hide}