You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

330 lines
16 KiB

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