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

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