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