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

     1  <!--
     2  order: 2
     3  synopsis: "This document describes the lifecycle of a transaction from creation to committed state changes. Transaction definition is described in a [different doc](../core/transactions.md). The transaction will be referred to as `Tx`."
     4  -->
     5  
     6  # Transaction Lifecycle
     7  
     8  ## Pre-requisite Readings {hide}
     9  
    10  - [Anatomy of an SDK Application](./app-anatomy.md) {prereq}
    11  
    12  ## Creation
    13  
    14  ### Transaction Creation
    15  
    16  One of the main application interfaces is the command-line interface. The transaction `Tx` can be created by the user inputting a command in the following format from the [command-line](../interfaces/cli.md), providing the type of transaction in `[command]`, arguments in `[args]`, and configurations such as gas prices in `[flags]`:
    17  
    18  ```bash
    19  [appname] tx [command] [args] [flags]
    20  ```
    21  
    22  This command will automatically **create** the transaction, **sign** it using the account's private key, and **broadcast** it to the specified peer node.
    23  
    24  There are several required and optional flags for transaction creation. The `--from` flag specifies which [account](./accounts.md) the transaction is originating from. For example, if the transaction is sending coins, the funds will be drawn from the specified `from` address.
    25  
    26  #### Gas and Fees
    27  
    28  Additionally, there are several [flags](../interfaces/cli.md) users can use to indicate how much they are willing to pay in [fees](./gas-fees.md):
    29  
    30  * `--gas` refers to how much [gas](./gas-fees.md), which represents computational resources, `Tx` consumes. Gas is dependent on the transaction and is not precisely calculated until execution, but can be estimated by providing `auto` as the value for `--gas`.
    31  * `--gas-adjustment` (optional) can be used to scale `gas` up in order to avoid underestimating. For example, users can specify their gas adjustment as 1.5 to use 1.5 times the estimated gas.
    32  * `--gas-prices` specifies how much the user is willing pay per unit of gas, which can be one or multiple denominations of tokens. For example, `--gas-prices=0.025uatom, 0.025upho` means the user is willing to pay 0.025uatom AND 0.025upho per unit of gas.
    33  * `--fees` specifies how much in fees the user is willing to pay in total.
    34  
    35  The ultimate value of the fees paid is equal to the gas multiplied by the gas prices. In other words, `fees = ceil(gas * gasPrices)`. Thus, since fees can be calculated using gas prices and vice versa, the users specify only one of the two.
    36  
    37  Later, validators decide whether or not to include the transaction in their block by comparing the given or calculated `gas-prices` to their local `min-gas-prices`. `Tx` will be rejected if its `gas-prices` is not high enough, so users are incentivized to pay more.
    38  
    39  #### CLI Example
    40  
    41  Users of application `app` can enter the following command into their CLI to generate a transaction to send 1000uatom from a `senderAddress` to a `recipientAddress`. It specifies how much gas they are willing to pay: an automatic estimate scaled up by 1.5 times, with a gas price of 0.025uatom per unit gas.
    42  
    43  ```bash
    44  appcli tx send <recipientAddress> 1000uatom --from <senderAddress> --gas auto --gas-adjustment 1.5 --gas-prices 0.025uatom
    45  ```
    46  
    47  #### Other Transaction Creation Methods
    48  
    49  The command-line is an easy way to interact with an application, but `Tx` can also be created using a [REST interface](../interfaces/rest.md) or some other entrypoint defined by the application developer. From the user's perspective, the interaction depends on the web interface or wallet they are using (e.g. creating `Tx` using [Lunie.io](https://lunie.io/#/) and signing it with a Ledger Nano S).
    50  
    51  ## Addition to Mempool
    52  
    53  Each full-node (running Tendermint) that receives a `Tx` sends an [ABCI message](https://tendermint.com/docs/spec/abci/abci.html#messages),
    54  `CheckTx`, to the application layer to check for validity, and receives an `abci.ResponseCheckTx`. If the `Tx` passes the checks, it is held in the nodes'
    55  [**Mempool**](https://tendermint.com/docs/tendermint-core/mempool.html#mempool), an in-memory pool of transactions unique to each node) pending inclusion in a block - honest nodes will discard `Tx` if it is found to be invalid. Prior to consensus, nodes continuously check incoming transactions and gossip them to their peers.
    56  
    57  ### Types of Checks
    58  
    59  The full-nodes perform stateless, then stateful checks on `Tx` during `CheckTx`, with the goal to
    60  identify and reject an invalid transaction as early on as possible to avoid wasted computation.
    61  
    62  ***Stateless*** checks do not require nodes to access state - light clients or offline nodes can do
    63  them - and are thus less computationally expensive. Stateless checks include making sure addresses
    64  are not empty, enforcing nonnegative numbers, and other logic specified in the definitions.
    65  
    66  ***Stateful*** checks validate transactions and messages based on a committed state. Examples
    67  include checking that the relevant values exist and are able to be transacted with, the address
    68  has sufficient funds, and the sender is authorized or has the correct ownership to transact.
    69  At any given moment, full-nodes typically have [multiple versions](../core/baseapp.md#volatile-states)
    70  of the application's internal state for different purposes. For example, nodes will execute state
    71  changes while in the process of verifying transactions, but still need a copy of the last committed
    72  state in order to answer queries - they should not respond using state with uncommitted changes.
    73  
    74  In order to verify a `Tx`, full-nodes call `CheckTx`, which includes both _stateless_ and _stateful_
    75  checks. Further validation happens later in the [`DeliverTx`](#delivertx) stage. `CheckTx` goes
    76  through several steps, beginning with decoding `Tx`.
    77  
    78  ### Decoding
    79  
    80  When `Tx` is received by the application from the underlying consensus engine (e.g. Tendermint), it is still in its [encoded](../core/encoding.md) `[]byte` form and needs to be unmarshaled in order to be processed. Then, the [`runTx`](../core/baseapp.md#runtx-and-runmsgs) function is called to run in `runTxModeCheck` mode, meaning the function will run all checks but exit before executing messages and writing state changes.
    81  
    82  ### ValidateBasic
    83  
    84  [`Message`s](../core/transactions.md#messages) are extracted from `Tx` and `ValidateBasic`, a method of the `Msg` interface implemented by the module developer, is run for each one. It should include basic **stateless** sanity checks. For example, if the message is to send coins from one address to another, `ValidateBasic` likely checks for nonempty addresses and a nonnegative coin amount, but does not require knowledge of state such as account balance of an address.
    85  
    86  ### AnteHandler
    87  
    88  The [`AnteHandler`](../basics/gas-fees.md#antehandler), which is technically optional but should be defined for each application, is run. A deep copy of the internal state, `checkState`, is made and the defined `AnteHandler` performs limited checks specified for the transaction type. Using a copy allows the handler to do stateful checks for `Tx` without modifying the last committed state, and revert back to the original if the execution fails.
    89  
    90  For example, the [`auth`](https://github.com/cosmos/cosmos-sdk/tree/master/x/auth/spec) module `AnteHandler` checks and increments sequence numbers, checks signatures and account numbers, and deducts fees from the first signer of the transaction - all state changes are made using the `checkState`.
    91  
    92  ### Gas
    93  
    94  The [`Context`](../core/context.md), which keeps a `GasMeter` that will track how much gas has been used during the execution of `Tx`, is initialized. The user-provided amount of gas for `Tx` is known as `GasWanted`. If `GasConsumed`, the amount of gas consumed so during execution, ever exceeds `GasWanted`, the execution will stop and the changes made to the cacehd copy of the state won't be committed. Otherwise, `CheckTx` sets `GasUsed` equal to `GasConsumed` and returns it in the result. After calculating the gas and fee values, validator-nodes check that the user-specified `gas-prices` is less than their locally defined `min-gas-prices`.
    95  
    96  ### Discard or Addition to Mempool
    97  
    98  If at any point during `CheckTx` the `Tx` fails, it is discarded and the transaction lifecycle ends
    99  there. Otherwise, if it passes `CheckTx` successfully, the default protocol is to relay it to peer
   100  nodes and add it to the Mempool so that the `Tx` becomes a candidate to be included in the next block.
   101  
   102  The **mempool** serves the purpose of keeping track of transactions seen by all full-nodes.
   103  Full-nodes keep a **mempool cache** of the last `mempool.cache_size` transactions they have seen, as a first line of
   104  defense to prevent replay attacks. Ideally, `mempool.cache_size` is large enough to encompass all
   105  of the transactions in the full mempool. If the the mempool cache is too small to keep track of all
   106  the transactions, `CheckTx` is responsible for identifying and rejecting replayed transactions.
   107  
   108  Currently existing preventative measures include fees and a `sequence` (nonce) counter to distinguish
   109  replayed transactions from identical but valid ones. If an attacker tries to spam nodes with many
   110  copies of a `Tx`, full-nodes keeping a mempool cache will reject identical copies instead of running
   111  `CheckTx` on all of them. Even if the copies have incremented `sequence` numbers, attackers are
   112  disincentivized by the need to pay fees.
   113  
   114  Validator nodes keep a mempool to prevent replay attacks, just as full-nodes do, but also use it as
   115  a pool of unconfirmed transactions in preparation of block inclusion. Note that even if a `Tx`
   116  passes all checks at this stage, it is still possible to be found invalid later on, because
   117  `CheckTx` does not fully validate the transaction (i.e. it does not actually execute the messages).
   118  
   119  ## Inclusion in a Block
   120  
   121  Consensus, the process through which validator nodes come to agreement on which transactions to
   122  accept, happens in **rounds**. Each round begins with a proposer creating a block of the most
   123  recent transactions and ends with **validators**, special full-nodes with voting power responsible
   124  for consensus, agreeing to accept the block or go with a `nil` block instead. Validator nodes
   125  execute the consensus algorithm, such as [Tendermint BFT](https://tendermint.com/docs/spec/consensus/consensus.html#terms),
   126  confirming the transactions using ABCI requests to the application, in order to come to this agreement.
   127  
   128  The first step of consensus is the **block proposal**. One proposer amongst the validators is chosen
   129  by the consensus algorithm to create and propose a block - in order for a `Tx` to be included, it
   130  must be in this proposer's mempool.
   131  
   132  ## State Changes
   133  
   134  The next step of consensus is to execute the transactions to fully validate them. All full-nodes
   135  that receive a block proposal from the correct proposer execute the transactions by calling the ABCI functions
   136  [`BeginBlock`](./app-anatomy.md#beginblocker-and-endblocker), `DeliverTx` for each transaction,
   137  and [`EndBlock`](./app-anatomy.md#beginblocker-and-endblocker). While each full-node runs everything
   138  locally, this process yields a single, unambiguous result, since the messages' state transitions are deterministic and transactions are
   139  explicitly ordered in the block proposal.
   140  
   141  ```
   142  		-----------------------		
   143  		|Receive Block Proposal|
   144  		-----------------------		
   145  		          |		
   146  			  v
   147  		-----------------------		
   148  		| BeginBlock	      |        
   149  		-----------------------		
   150  		          |		
   151  			  v			
   152  		-----------------------		    
   153  		| DeliverTx(tx0)      |  
   154  		| DeliverTx(tx1)      |   	  
   155  		| DeliverTx(tx2)      |  
   156  		| DeliverTx(tx3)      |  
   157  		|	.	      |  
   158  		|	.	      |
   159  		|	.	      |
   160  		-----------------------		
   161  		          |			
   162  			  v			
   163  		-----------------------
   164  		| EndBlock	      |         
   165  		-----------------------
   166  		          |			
   167  			  v			
   168  		-----------------------
   169  		| Consensus	      |         
   170  		-----------------------
   171  		          |			
   172  			  v			
   173  		-----------------------
   174  		| Commit	      |         
   175  		-----------------------
   176  ```
   177  
   178  ### DeliverTx
   179  
   180  The `DeliverTx` ABCI function defined in [`baseapp`](../core/baseapp.md) does the bulk of the
   181  state transitions: it is run for each transaction in the block in sequential order as committed
   182  to during consensus. Under the hood, `DeliverTx` is almost identical to `CheckTx` but calls the
   183  [`runTx`](../core/baseapp.md#runtx) function in deliver mode instead of check mode.
   184  Instead of using their `checkState`, full-nodes use `deliverState`:
   185  
   186  * **Decoding:** Since `DeliverTx` is an ABCI call, `Tx` is received in the encoded `[]byte` form.
   187  Nodes first unmarshal the transaction, then call `runTx` in `runTxModeDeliver`, which is very
   188  similar to `CheckTx` but also executes and writes state changes.
   189  
   190  * **Checks:** Full-nodes call `validateBasicMsgs` and the `AnteHandler` again. This second check
   191  happens because they may not have seen the same transactions during the addition to Mempool stage\
   192  and a malicious proposer may have included invalid ones. One difference here is that the
   193  `AnteHandler` will not compare `gas-prices` to the node's `min-gas-prices` since that value is local
   194  to each node - differing values across nodes would yield nondeterministic results.
   195  
   196  * **Route and Handler:** While `CheckTx` would have exited, `DeliverTx` continues to run
   197  [`runMsgs`](../core/baseapp.md#runtx-and-runmsgs) to fully execute each `Msg` within the transaction.
   198  Since the transaction may have messages from different modules, `baseapp` needs to know which module
   199  to find the appropriate Handler. Thus, the `route` function is called via the [module manager](../building-modules/module-manager.md) to
   200  retrieve the route name and find the [`Handler`](../building-modules/handler.md) within the module.
   201  
   202  * **Handler:** The `handler`, a step up from `AnteHandler`, is responsible for executing each
   203  message in the `Tx` and causes state transitions to persist in `deliverTxState`. It is defined
   204  within a `Msg`'s module and writes to the appropriate stores within the module.
   205  
   206  * **Gas:** While a `Tx` is being delivered, a `GasMeter` is used to keep track of how much
   207  gas is being used; if execution completes, `GasUsed` is set and returned in the
   208  `abci.ResponseDeliverTx`. If execution halts because `BlockGasMeter` or `GasMeter` has run out or something else goes
   209  wrong, a deferred function at the end appropriately errors or panics.
   210  
   211  If there are any failed state changes resulting from a `Tx` being invalid or `GasMeter` running out,
   212  the transaction processing terminates and any state changes are reverted. Invalid transactions in a
   213  block proposal cause validator nodes to reject the block and vote for a `nil` block instead. If a
   214  `Tx` is delivered successfully, any leftover gas is returned to the user and the transaction is
   215  validated.
   216  
   217  ### Commit
   218  
   219  The final step is for nodes to commit the block and state changes. Validator nodes
   220  perform the previous step of executing state transitions in order to validate the transactions,
   221  then sign the block to confirm it. Full nodes that are not validators do not
   222  participate in consensus - i.e. they cannot vote - but listen for votes to understand whether or
   223  not they should commit the state changes. 
   224  
   225  When they receive enough validator votes (2/3+ *precommits* weighted by voting power), full nodes commit to a new block to be added to the blockchain and
   226  finalize the state transitions in the application layer. A new state root is generated to serve as
   227  a merkle proof for the state transitions. Applications use the [`Commit`](../core/baseapp.md#commit)
   228  ABCI method inherited from [Baseapp](../core/baseapp.md); it syncs all the state transitions by
   229  writing the `deliverState` into the application's internal state. As soon as the state changes are
   230  committed, `checkState`  start afresh from the most recently committed state and `deliverState`
   231  resets to `nil` in order to be consistent and reflect the changes.
   232  
   233  Note that not all blocks have the same number of transactions and it is possible for consensus to
   234  result in a `nil` block or one with none at all. In a public blockchain network, it is also possible
   235  for validators to be **byzantine**, or malicious, which may prevent a `Tx` from being committed in
   236  the blockchain. Possible malicious behaviors include the proposer deciding to censor a `Tx` by
   237  excluding it from the block or a validator voting against the block.
   238  
   239  At this point, the transaction lifecycle of a `Tx` is over: nodes have verified its validity,
   240  delivered it by executing its state changes, and committed those changes. The `Tx` itself,
   241  in `[]byte` form, is stored in a block and appended to the blockchain.
   242  
   243  ## Next {hide}
   244  
   245  Learn about [accounts](./accounts.md) {hide}