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

     1  <!--
     2  order: 3
     3  synopsis: The `context` is a data structure intended to be passed from function to function that carries information about the current state of the application. It holds a cached copy of the entire state as well as useful objects and information like `gasMeter`, `block height`, `consensus parameters` and more. 
     4  -->
     5  
     6  # Context
     7  
     8  ## Pre-requisites Readings {hide}
     9  
    10  - [Anatomy of an SDK Application](../basics/app-anatomy.md) {prereq}
    11  - [Lifecycle of a Transaction](../basics/tx-lifecycle.md) {prereq}
    12  
    13  ## Context Definition
    14  
    15  The SDK `Context` is a custom data structure that contains Go's stdlib [`context`](https://golang.org/pkg/context) as its base, and has many additional types within its definition that are specific to the Cosmos SDK. he `Context` is integral to transaction processing in that it allows modules to easily access their respective [store](./store.md#base-layer-kvstores) in the [`multistore`](./store.md#multistore) and retrieve transactional context such as the block header and gas meter.
    16  
    17  ```go
    18  type Context struct {
    19    ctx           context.Context
    20    ms            MultiStore
    21    header        abci.Header
    22    chainID       string
    23    txBytes       []byte
    24    logger        log.Logger
    25    voteInfo      []abci.VoteInfo
    26    gasMeter      GasMeter
    27    blockGasMeter GasMeter
    28    checkTx       bool
    29    minGasPrice   DecCoins
    30    consParams    *abci.ConsensusParams
    31    eventManager  *EventManager
    32  }
    33  ```
    34  
    35  - **Context:** The base type is a Go [Context](https://golang.org/pkg/context), which is explained further in the [Go Context Package](#go-context-package) section below. 
    36  - **Multistore:** Every application's `BaseApp` contains a [`CommitMultiStore`](./store.md#multistore) which is provided when a `Context` is created. Calling the `KVStore()` and `TransientStore()` methods allows modules to fetch their respective [`KVStore`](./store.md#base-layer-kvstores) using their unique `StoreKey`.
    37  - **ABCI Header:** The [header](https://tendermint.com/docs/spec/abci/abci.html#header) is an ABCI type. It carries important information about the state of the blockchain, such as block height and proposer of the current block.
    38  - **Chain ID:** The unique identification number of the blockchain a block pertains to.
    39  - **Transaction Bytes:** The `[]byte` representation of a transaction being processed using the context. Every transaction is processed by various parts of the SDK and consensus engine (e.g. Tendermint) throughout its [lifecycle](../basics/tx-lifecycle.md), some of which to not have any understanding of transaction types. Thus, transactions are marshaled into the generic `[]byte` type using some kind of [encoding format](./encoding.md) such as [Amino](./encoding.md).
    40  - **Logger:** A `logger` from the Tendermint libraries. Learn more about logs [here](https://tendermint.com/docs/tendermint-core/how-to-read-logs.html#how-to-read-logs). Modules call this method to create their own unique module-specific logger.
    41  - **VoteInfo:** A list of the ABCI type [`VoteInfo`](https://tendermint.com/docs/spec/abci/abci.html#voteinfo), which includes the name of a validator and a boolean indicating whether they have signed the block.
    42  - **Gas Meters:** Specifically, a [`gasMeter`](../basics/gas-fees.md#main-gas-meter) for the transaction currently being processed using the context and a [`blockGasMeter`](../basics/gas-fees.md#block-gas-meter) for the entire block it belongs to. Users specify how much in fees they wish to pay for the execution of their transaction; these gas meters keep track of how much [gas](../basics/gas-fees.md) has been used in the transaction or block so far. If the gas meter runs out, execution halts.
    43  - **CheckTx Mode:** A boolean value indicating whether a transaction should be processed in `CheckTx` or `DeliverTx` mode.
    44  - **Min Gas Price:** The minimum [gas](../basics/gas-fees.md) price a node is willing to take in order to include a transaction in its block. This price is a local value configured by each node individually, and should therefore **not be used in any functions used in sequences leading to state-transitions**. 
    45  - **Consensus Params:** The ABCI type [Consensus Parameters](https://tendermint.com/docs/spec/abci/apps.html#consensus-parameters), which specify certain limits for the blockchain, such as maximum gas for a block.
    46  - **Event Manager:** The event manager allows any caller with access to a `Context` to emit [`Events`](./events.md). Modules may define module specific
    47  `Events` by defining various `Types` and `Attributes` or use the common definitions found in `types/`. Clients can subscribe or query for these `Events`. These `Events` are collected throughout `DeliverTx`, `BeginBlock`, and `EndBlock` and are returned to Tendermint for indexing. For example:
    48  
    49  ```go
    50  ctx.EventManager().EmitEvent(sdk.NewEvent(
    51      sdk.EventTypeMessage,
    52      sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory)),
    53  )
    54  ```
    55  
    56  ## Go Context Package
    57  
    58  A basic `Context` is defined in the [Golang Context Package](https://golang.org/pkg/context). A `Context`
    59  is an immutable data structure that carries request-scoped data across APIs and processes. Contexts
    60  are also designed to enable concurrency and to be used in goroutines.
    61  
    62  Contexts are intended to be **immutable**; they should never be edited. Instead, the convention is
    63  to create a child context from its parent using a `With` function. For example:
    64  
    65  ``` go
    66  childCtx = parentCtx.WithBlockHeader(header)
    67  ```
    68  
    69  The [Golang Context Package](https://golang.org/pkg/context) documentation instructs developers to
    70  explicitly pass a context `ctx` as the first argument of a process.
    71  
    72  ## Cache Wrapping
    73  
    74  The `Context` contains a `MultiStore`, which allows for cache-wrapping functionality: a `CacheMultiStore`
    75  where each `KVStore` is is wrapped with an ephemeral cache. Processes are free to write changes to
    76  the `CacheMultiStore`, then write the changes back to the original state or disregard them if something
    77  goes wrong. The pattern of usage for a Context is as follows:
    78  
    79  1. A process receives a Context `ctx` from its parent process, which provides information needed to
    80     perform the process.
    81  2. The `ctx.ms` is **cache wrapped**, i.e. a cached copy of the [multistore](./store.md#multistore) is made so that the process can make changes to the state as it executes, without changing the original`ctx.ms`. This is useful to protect the underlying multistore in case the changes need to be reverted at some point in the execution. 
    82  3. The process may read and write from `ctx` as it is executing. It may call a subprocess and pass
    83  `ctx` to it as needed.
    84  4. When a subprocess returns, it checks if the result is a success or failure. If a failure, nothing
    85  needs to be done - the cache wrapped `ctx` is simply discarded. If successful, the changes made to
    86  the cache-wrapped `MultiStore` can be committed to the original `ctx.ms` via `Write()`.
    87  
    88  For example, here is a snippet from the [`runTx`](./baseapp.md#runtx-and-runmsgs) function in
    89  [`baseapp`](./baseapp.md):
    90  
    91  ```go
    92  runMsgCtx, msCache := app.cacheTxContext(ctx, txBytes)
    93  result = app.runMsgs(runMsgCtx, msgs, mode)
    94  result.GasWanted = gasWanted
    95  
    96  if mode != runTxModeDeliver {
    97    return result
    98  }
    99  
   100  if result.IsOK() {
   101    msCache.Write()
   102  }
   103  ```
   104  
   105  Here is the process:
   106  
   107  1. Prior to calling `runMsgs` on the message(s) in the transaction, it uses `app.cacheTxContext()`
   108  to cache-wrap the context and multistore.
   109  2. The cache-wrapped context, `runMsgCtx`, is used in `runMsgs` to return a result.
   110  3. If the process is running in [`checkTxMode`](./baseapp.md#checktx), there is no need to write the
   111  changes - the result is returned immediately.
   112  4. If the process is running in [`deliverTxMode`](./baseapp.md#delivertx) and the result indicates
   113  a successful run over all the messages, the cached multistore is written back to the original.
   114  
   115  ## Next {hide}
   116  
   117  Learn about the [node client](./node.md) {hide}