Skip to content

Transaction flow

Carlos Rodriguez edited this page Mar 11, 2023 · 3 revisions

This document attempts to provide a trace through the codebases of CometBFT and Cosmos SDK to show some key code parts that are executed as a transaction flows through the system and becomes part of a block. This is not by any means giving a complete, detailed step by step trace; this document simply offers links to certain relevant parts of the code that should help the reader navigate through the codebases and explore in more detail, if necessary.

To illustrate with a concrete example how a transaction is executed a blockchain was run locally using Cosmos SDK simd sample application binary and a bank transfer from one account to another was performed. Please check out the logs generated by the local chain, since they contains extra comments explaining some key actions that happen through the generation of the block. The transaction with MsgSend is executed in block at height 6, so feel free also to check blocks 5, 6, 7, and the transaction JSON files.

This document is updated for CometBFT v0.37 and Cosmos SDK v0.47.

Table of Contents

Useful resources

The information in the links below should give extra context and more colour to the ideas touched upon in this document:

Application wiring

Application wiring in app.go is a big topic and not every detail will be covered here. For more information, check out the BaseApp sections in the Developer Portal and the Cosmos SDK documentation, and the Anatomy of a Cosmos SDK Application section of the Cosmos SDK documentation. Here we will focus on the wiring related to the logic executed at the beginning and end of every block.

BaseApp is a boilerplate implementation of the core of a Cosmos SDK application. This abstraction implements functionalities that every Cosmos application-specific chain needs, starting with an implementation of Application Blockchain Interface (ABCI), which allows the state machine communicate with the underlying consensus engine (e.g. CometBFT).

BeginBlocker and EndBlocker are functions part of the BeginBlockAppModule and BeginBlockAppModule interfaces respectively, and developers of SDK modules can optionally implement them to add automatic execution of logic to their module at the beginning and at the end of each block respectively (i.e. when the BeginBlock and EndBlock ABCI messages are received from the underlying consensus engine). Read the section BeginBlocker and EndBlocker in the Cosmos SDK documentation for more information.

Application bootstrapping

When the application bootstraps, we can execute transactions and queries. The Node Client section of the Cosmos SDK documentation will ore details of how the full node starts. Here we will focus to explore sections of the codebase where the application initializes and connects to the ABCI client and the CometBFT HTTP RPC server. The ABCI client will allow the underlying consensus engine to communicate with the ABCI application (to send ABCI messages like CheckTx or DeliverTx); and the RPC server will be used by the application to broadcast transactions to the CometBFT node.

Begin block

On a new block the ABCI client (i.e. CometBFT) will send a BeginBlock request to the ABCI server (i.e. the application) to give the opportunity to run logic at the beginning of every block. CometBFT sends the current block hash and header to the application, before it sends any of the transactions.

Broadcasting a transaction

For illustration purposes we are going to submit a transaction to perform a bank transfer between 2 accounts. This is the CLI command I used with the blockchain running locally:

> simd tx bank send cosmos13tktw8hvst7w5rns0v5kt4cxqeaa6yvt380j34 cosmos15k5g0h7zjf78wmq9q88ujwzf2cyxeudrt48zw0 100stake \
--keyring-backend test \
--chain-id chain1 \ 
--home ../../gm/chain1 \ 
--node http://localhost:27000

For more information on how to submit transactions, check the section Generating, Signing and Broadcasting Transactions of the Cosmos SDK documentation.

Adding the transaction to the mempool

To learn more about ABCI methods and messages, check out the Methods and Types scetion of CometBFT documentation.

The CheckTx ABCI method controls what transactions are considered for inclusion in a block. CometBFT's mempool first checks the validity of a transaction with CheckTx, and only relays valid transactions to its peers.

End block

EndBlock ABCI method is called after all the transactions for the current block have been delivered, but prior to the block's Commit message.

Committing the block

Two stages of voting are required to successfully commit a block; they are called pre-vote and pre-commit. A block is committed when more than 2/3 of validators pre-commit for the same block in the same round.

The DeliverTx ABCI method delivers transactions from CometBFT to the application.

Clone this wiki locally