State machine
At its core, a blockchain is a replicated deterministic state machine: multiple nodes independently execute the same state machine over the same ordered inputs and, as a result, reach the same state. A state machine is a computer science abstraction in which a system can exist in many possible states, but only one at any given time. It is defined by:- a state
S, which represents the current data of the system, and - transactions
T, which describe how the state can change.
S and a transaction T, the state machine computes a new state S':
B is an ordered list of transactions, and the resulting state is obtained by applying each transaction sequentially:
CometBFT: Networking and Consensus
Thanks to the Cosmos SDK, developers only need to define the application state machine. CometBFT then takes care of replicating that state machine across the network by handling networking and consensus. In this context:- Networking refers to how nodes discover each other, exchange messages, and propagate transaction and block data across a peer-to-peer network.
- Consensus refers to how nodes agree on a single, ordered sequence of blocks to apply to the state machine, even in the presence of faulty or malicious nodes.
V. One validator from V is selected as the proposer for the next block.
A proposed block is considered committed if more than two-thirds of the voting power in V sign both a prevote and a precommit for that block, and if all transactions it contains are valid according to the application. The validator set itself is part of the application state and can be updated by rules defined in the state machine.
ABCI
CometBFT passes transactions to the application through an interface called the ABCI (Application Blockchain Interface), which the application must implement. ABCI defines the methods through which CometBFT sends ordered transaction bytes to the application for execution and receives the corresponding results, without needing to understand the application’s state or logic.-
CheckTx: When a transaction is received by CometBFT, it is passed to the application to perform basic validity checks.CheckTxis used to protect the mempool of full nodes against spam transactions. A special handler called theAnteHandlerexecutes a series of validation steps, such as signature verification and fee checks.
If the checks pass, the transaction is added to the mempool and relayed to peer nodes. No state changes occur duringCheckTxsince the transaction has not yet been included in a block. -
DeliverTx: When a valid block is received by CometBFT, each transaction in the block is passed to the application viaDeliverTxand executed in order. This is the stage at which state transitions occur. TheAnteHandleris executed again, followed by the correspondingMsgservice handlers for each message in the transaction. -
BeginBlock/EndBlock: These methods are executed at the beginning and end of every block, regardless of whether the block contains transactions. They can be used to trigger automatic or time-based logic. Care must be taken to avoid computationally expensive or unbounded operations, as these would affect block processing time and could halt the chain.
baseapp.