github.com/badrootd/celestia-core@v0.0.0-20240305091328-aa4207a4b25d/docs/introduction/what-is-cometbft.md (about)

     1  ---
     2  order: 5
     3  ---
     4  
     5  # What is CometBFT
     6  
     7  CometBFT is software for securely and consistently replicating an
     8  application on many machines. By securely, we mean that CometBFT works
     9  as long as less than 1/3 of machines fail in arbitrary ways. By consistently,
    10  we mean that every non-faulty machine sees the same transaction log and
    11  computes the same state. Secure and consistent replication is a
    12  fundamental problem in distributed systems; it plays a critical role in
    13  the fault tolerance of a broad range of applications, from currencies,
    14  to elections, to infrastructure orchestration, and beyond.
    15  
    16  The ability to tolerate machines failing in arbitrary ways, including
    17  becoming malicious, is known as Byzantine fault tolerance (BFT). The
    18  theory of BFT is decades old, but software implementations have only
    19  became popular recently, due largely to the success of "blockchain
    20  technology" like Bitcoin and Ethereum. Blockchain technology is just a
    21  reformalization of BFT in a more modern setting, with emphasis on
    22  peer-to-peer networking and cryptographic authentication. The name
    23  derives from the way transactions are batched in blocks, where each
    24  block contains a cryptographic hash of the previous one, forming a
    25  chain. In practice, the blockchain data structure actually optimizes BFT
    26  design.
    27  
    28  CometBFT consists of two chief technical components: a blockchain
    29  consensus engine and a generic application interface.
    30  The consensus engine,
    31  which is based on [Tendermint consensus algorithm][tendermint-paper],
    32  ensures that the same transactions are
    33  recorded on every machine in the same order. The application interface,
    34  called the Application BlockChain Interface (ABCI), enables the
    35  transactions to be processed in any programming language. Unlike other
    36  blockchain and consensus solutions, which come pre-packaged with built
    37  in state machines (like a fancy key-value store, or a quirky scripting
    38  language), developers can use CometBFT for BFT state machine
    39  replication of applications written in whatever programming language and
    40  development environment is right for them.
    41  
    42  CometBFT is designed to be easy-to-use, simple-to-understand, highly
    43  performant, and useful for a wide variety of distributed applications.
    44  
    45  ## CometBFT vs. X
    46  
    47  CometBFT is broadly similar to two classes of software. The first
    48  class consists of distributed key-value stores, like Zookeeper, etcd,
    49  and consul, which use non-BFT consensus. The second class is known as
    50  "blockchain technology", and consists of both cryptocurrencies like
    51  Bitcoin and Ethereum, and alternative distributed ledger designs like
    52  Hyperledger's Burrow.
    53  
    54  ### Zookeeper, etcd, consul
    55  
    56  Zookeeper, etcd, and consul are all implementations of a key-value store
    57  atop a classical, non-BFT consensus algorithm. Zookeeper uses a version
    58  of Paxos called Zookeeper Atomic Broadcast, while etcd and consul use
    59  the Raft consensus algorithm, which is much younger and simpler. A
    60  typical cluster contains 3-5 machines, and can tolerate crash failures
    61  in up to 1/2 of the machines, but even a single Byzantine fault can
    62  destroy the system.
    63  
    64  Each offering provides a slightly different implementation of a
    65  featureful key-value store, but all are generally focused around
    66  providing basic services to distributed systems, such as dynamic
    67  configuration, service discovery, locking, leader-election, and so on.
    68  
    69  CometBFT is in essence similar software, but with two key differences:
    70  
    71  - It is Byzantine Fault Tolerant, meaning it can only tolerate less than 1/3
    72    of machines failing, but those failures can include arbitrary behavior -
    73    including hacking and malicious attacks.
    74  - It does not specify a
    75    particular application, like a fancy key-value store. Instead, it
    76    focuses on arbitrary state machine replication, so developers can build
    77    the application logic that's right for them, from key-value store to
    78    cryptocurrency to e-voting platform and beyond.
    79  
    80  ### Bitcoin, Ethereum, etc
    81  
    82  [Tendermint consensus algorithm][tendermint-paper], adopted by CometBFT,
    83  emerged in the tradition of cryptocurrencies like Bitcoin,
    84  Ethereum, etc. with the goal of providing a more efficient and secure
    85  consensus algorithm than Bitcoin's Proof of Work. In the early days,
    86  Tendermint consensus-based blockchains had a simple currency built in, and to participate in
    87  consensus, users had to "bond" units of the currency into a security
    88  deposit which could be revoked if they misbehaved -this is what made
    89  Tendermint consensus a Proof-of-Stake algorithm.
    90  
    91  Since then, CometBFT has evolved to be a general purpose blockchain
    92  consensus engine that can host arbitrary application states. That means
    93  it can be used as a plug-and-play replacement for the consensus engines
    94  of other blockchain software. So one can take the current Ethereum code
    95  base, whether in Rust, or Go, or Haskell, and run it as an ABCI
    96  application using CometBFT. Indeed, [we did that with
    97  Ethereum](https://github.com/cosmos/ethermint). And we plan to do
    98  the same for Bitcoin, ZCash, and various other deterministic
    99  applications as well.
   100  
   101  Another example of a cryptocurrency application built on CometBFT is
   102  [the Cosmos network](http://cosmos.network).
   103  
   104  ### Other Blockchain Projects
   105  
   106  [Fabric](https://github.com/hyperledger/fabric) takes a similar approach
   107  to CometBFT, but is more opinionated about how the state is managed,
   108  and requires that all application behavior runs in potentially many
   109  docker containers, modules it calls "chaincode". It uses an
   110  implementation of [PBFT](http://pmg.csail.mit.edu/papers/osdi99.pdf).
   111  from a team at IBM that is [augmented to handle potentially
   112  non-deterministic
   113  chaincode](https://drops.dagstuhl.de/opus/volltexte/2017/7093/pdf/LIPIcs-OPODIS-2016-24.pdf).
   114  It is possible to implement this docker-based behavior as an ABCI app in
   115  CometBFT, though extending CometBFT to handle non-determinism
   116  remains for future work.
   117  
   118  [Burrow](https://github.com/hyperledger/burrow) is an implementation of
   119  the Ethereum Virtual Machine and Ethereum transaction mechanics, with
   120  additional features for a name-registry, permissions, and native
   121  contracts, and an alternative blockchain API. It uses CometBFT as its
   122  consensus engine, and provides a particular application state.
   123  
   124  ## ABCI Overview
   125  
   126  The [Application BlockChain Interface
   127  (ABCI)](https://github.com/cometbft/cometbft/tree/v0.34.x/abci)
   128  allows for Byzantine Fault Tolerant replication of applications
   129  written in any programming language.
   130  
   131  ### Motivation
   132  
   133  Thus far, all blockchains "stacks" (such as
   134  [Bitcoin](https://github.com/bitcoin/bitcoin)) have had a monolithic
   135  design. That is, each blockchain stack is a single program that handles
   136  all the concerns of a decentralized ledger; this includes P2P
   137  connectivity, the "mempool" broadcasting of transactions, consensus on
   138  the most recent block, account balances, Turing-complete contracts,
   139  user-level permissions, etc.
   140  
   141  Using a monolithic architecture is typically bad practice in computer
   142  science. It makes it difficult to reuse components of the code, and
   143  attempts to do so result in complex maintenance procedures for forks of
   144  the codebase. This is especially true when the codebase is not modular
   145  in design and suffers from "spaghetti code".
   146  
   147  Another problem with monolithic design is that it limits you to the
   148  language of the blockchain stack (or vice versa). In the case of
   149  Ethereum which supports a Turing-complete bytecode virtual-machine, it
   150  limits you to languages that compile down to that bytecode; today, those
   151  are Serpent and Solidity.
   152  
   153  In contrast, our approach is to decouple the consensus engine and P2P
   154  layers from the details of the application state of the particular
   155  blockchain application. We do this by abstracting away the details of
   156  the application to an interface, which is implemented as a socket
   157  protocol.
   158  
   159  Thus we have an interface, the Application BlockChain Interface (ABCI),
   160  and its primary implementation, the Tendermint Socket Protocol (TSP, or
   161  Teaspoon).
   162  
   163  ### Intro to ABCI
   164  
   165  [CometBFT](https://github.com/cometbft/cometbft), the
   166  "consensus engine", communicates with the application via a socket
   167  protocol that satisfies the ABCI, the CometBFT Socket Protocol.
   168  
   169  To draw an analogy, let's talk about a well-known cryptocurrency,
   170  Bitcoin. Bitcoin is a cryptocurrency blockchain where each node
   171  maintains a fully audited Unspent Transaction Output (UTXO) database. If
   172  one wanted to create a Bitcoin-like system on top of ABCI, CometBFT
   173  would be responsible for
   174  
   175  - Sharing blocks and transactions between nodes
   176  - Establishing a canonical/immutable order of transactions
   177    (the blockchain)
   178  
   179  The application will be responsible for
   180  
   181  - Maintaining the UTXO database
   182  - Validating cryptographic signatures of transactions
   183  - Preventing transactions from spending non-existent transactions
   184  - Allowing clients to query the UTXO database.
   185  
   186  CometBFT is able to decompose the blockchain design by offering a very
   187  simple API (i.e. the ABCI) between the application process and consensus
   188  process.
   189  
   190  The ABCI consists of 3 primary message types that get delivered from the
   191  core to the application. The application replies with corresponding
   192  response messages.
   193  
   194  The messages are specified here: [ABCI Message
   195  Types](https://github.com/cometbft/cometbft/blob/v0.34.x/proto/tendermint/abci/types.proto).
   196  
   197  The **DeliverTx** message is the work horse of the application. Each
   198  transaction in the blockchain is delivered with this message. The
   199  application needs to validate each transaction received with the
   200  **DeliverTx** message against the current state, application protocol,
   201  and the cryptographic credentials of the transaction. A validated
   202  transaction then needs to update the application state — by binding a
   203  value into a key values store, or by updating the UTXO database, for
   204  instance.
   205  
   206  The **CheckTx** message is similar to **DeliverTx**, but it's only for
   207  validating transactions. CometBFT's mempool first checks the
   208  validity of a transaction with **CheckTx**, and only relays valid
   209  transactions to its peers. For instance, an application may check an
   210  incrementing sequence number in the transaction and return an error upon
   211  **CheckTx** if the sequence number is old. Alternatively, they might use
   212  a capabilities based system that requires capabilities to be renewed
   213  with every transaction.
   214  
   215  The **Commit** message is used to compute a cryptographic commitment to
   216  the current application state, to be placed into the next block header.
   217  This has some handy properties. Inconsistencies in updating that state
   218  will now appear as blockchain forks which catches a whole class of
   219  programming errors. This also simplifies the development of secure
   220  lightweight clients, as Merkle-hash proofs can be verified by checking
   221  against the block hash, and that the block hash is signed by a quorum.
   222  
   223  There can be multiple ABCI socket connections to an application.
   224  CometBFT creates three ABCI connections to the application; one
   225  for the validation of transactions when broadcasting in the mempool, one
   226  for the consensus engine to run block proposals, and one more for
   227  querying the application state.
   228  
   229  It's probably evident that applications designers need to very carefully
   230  design their message handlers to create a blockchain that does anything
   231  useful but this architecture provides a place to start. The diagram
   232  below illustrates the flow of messages via ABCI.
   233  
   234  ![abci](../imgs/abci.png)
   235  
   236  ## A Note on Determinism
   237  
   238  The logic for blockchain transaction processing must be deterministic.
   239  If the application logic weren't deterministic, consensus would not be
   240  reached among the CometBFT replica nodes.
   241  
   242  Solidity on Ethereum is a great language of choice for blockchain
   243  applications because, among other reasons, it is a completely
   244  deterministic programming language. However, it's also possible to
   245  create deterministic applications using existing popular languages like
   246  Java, C++, Python, or Go. Game programmers and blockchain developers are
   247  already familiar with creating deterministic programs by avoiding
   248  sources of non-determinism such as:
   249  
   250  - random number generators (without deterministic seeding)
   251  - race conditions on threads (or avoiding threads altogether)
   252  - the system clock
   253  - uninitialized memory (in unsafe programming languages like C
   254    or C++)
   255  - [floating point
   256    arithmetic](http://gafferongames.com/networking-for-game-programmers/floating-point-determinism/)
   257  - language features that are random (e.g. map iteration in Go)
   258  
   259  While programmers can avoid non-determinism by being careful, it is also
   260  possible to create a special linter or static analyzer for each language
   261  to check for determinism. In the future we may work with partners to
   262  create such tools.
   263  
   264  ## Consensus Overview
   265  
   266  CometBFT adopts [Tendermint consensus][tendermint-paper],
   267  an easy-to-understand, mostly asynchronous, BFT consensus algorithm.
   268  The algorithm follows a simple state machine that looks like this:
   269  
   270  ![consensus-logic](../imgs/consensus_logic.png)
   271  
   272  Participants in the algorithm are called **validators**; they take turns
   273  proposing blocks of transactions and voting on them. Blocks are
   274  committed in a chain, with one block at each **height**. A block may
   275  fail to be committed, in which case the algorithm moves to the next
   276  **round**, and a new validator gets to propose a block for that height.
   277  Two stages of voting are required to successfully commit a block; we
   278  call them **pre-vote** and **pre-commit**. A block is committed when
   279  more than 2/3 of validators pre-commit for the same block in the same
   280  round.
   281  
   282  There is a picture of a couple doing the polka because validators are
   283  doing something like a polka dance. When more than two-thirds of the
   284  validators pre-vote for the same block, we call that a **polka**. Every
   285  pre-commit must be justified by a polka in the same round.
   286  
   287  Validators may fail to commit a block for a number of reasons; the
   288  current proposer may be offline, or the network may be slow. Tendermint consensus
   289  allows them to establish that a validator should be skipped. Validators
   290  wait a small amount of time to receive a complete proposal block from
   291  the proposer before voting to move to the next round. This reliance on a
   292  timeout is what makes Tendermint consensus a weakly synchronous algorithm, rather
   293  than an asynchronous one. However, the rest of the algorithm is
   294  asynchronous, and validators only make progress after hearing from more
   295  than two-thirds of the validator set. A simplifying element of
   296  Tendermint consensus is that it uses the same mechanism to commit a block as it
   297  does to skip to the next round.
   298  
   299  Assuming less than one-third of the validators are Byzantine, Tendermint consensus algorithm
   300  guarantees that safety will never be violated - that is, validators will
   301  never commit conflicting blocks at the same height. To do this it
   302  introduces a few **locking** rules which modulate which paths can be
   303  followed in the flow diagram. Once a validator precommits a block, it is
   304  locked on that block. Then,
   305  
   306  1. it must prevote for the block it is locked on
   307  2. it can only unlock, and precommit for a new block, if there is a
   308      polka for that block in a later round
   309  
   310  ## Stake
   311  
   312  In many systems, not all validators will have the same "weight" in the
   313  consensus protocol. Thus, we are not so much interested in one-third or
   314  two-thirds of the validators, but in those proportions of the total
   315  voting power, which may not be uniformly distributed across individual
   316  validators.
   317  
   318  Since CometBFT can replicate arbitrary applications, it is possible to
   319  define a currency, and denominate the voting power in that currency.
   320  When voting power is denominated in a native currency, the system is
   321  often referred to as Proof-of-Stake. Validators can be forced, by logic
   322  in the application, to "bond" their currency holdings in a security
   323  deposit that can be destroyed if they're found to misbehave in the
   324  consensus protocol. This adds an economic element to the security of the
   325  protocol, allowing one to quantify the cost of violating the assumption
   326  that less than one-third of voting power is Byzantine.
   327  
   328  The [Cosmos Network](https://cosmos.network) is designed to use this
   329  Proof-of-Stake mechanism across an array of cryptocurrencies implemented
   330  as ABCI applications.
   331  
   332  [tendermint-paper]: https://arxiv.org/abs/1807.04938