State machine
At its core, a blockchain is a replicated deterministic state machine. A state machine is a computer science concept whereby a machine can have multiple states, but only one at any given time. There is astate
, which describes the current state of the system, and transactions
, that trigger state transitions.
Given a state S and a transaction T, the state machine will return a new state S’.
CometBFT
Thanks to the Cosmos SDK, developers just have to define the state machine, and CometBFT will handle replication over the network for them.prevote
and a precommit
on it, and if all the transactions that it contains are valid. The validator set can be changed by rules written in the state-machine.
ABCI
CometBFT passes transactions to the application through an interface called the ABCI, which the application must implement.CheckTx
: When a transaction is received by CometBFT, it is passed to the application to check if a few basic requirements are met.CheckTx
is used to protect the mempool of full-nodes against spam transactions. . A special handler called theAnteHandler
is used to execute a series of validation steps such as checking for sufficient fees and validating the signatures. If the checks are valid, the transaction is added to the mempool and relayed to peer nodes. Note that transactions are not processed (i.e. no modification of the state occurs) withCheckTx
since they have not been included in a block yet.DeliverTx
: When a valid block is received by CometBFT, each transaction in the block is passed to the application viaDeliverTx
in order to be processed. It is during this stage that the state transitions occur. TheAnteHandler
executes again, along with the actualMsg
service RPC for each message in the transaction.BeginBlock
/EndBlock
: These messages are executed at the beginning and the end of each block, whether the block contains transactions or not. It is useful to trigger automatic execution of logic. Proceed with caution though, as computationally expensive loops could slow down your blockchain, or even freeze it if the loop is infinite.