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.

332 lines
16 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. # What is Tendermint?
  2. Tendermint is software for securely and consistently replicating an
  3. application on many machines. By securely, we mean that Tendermint works
  4. even if up to 1/3 of machines fail in arbitrary ways. By consistently,
  5. we mean that every non-faulty machine sees the same transaction log and
  6. computes the same state. Secure and consistent replication is a
  7. fundamental problem in distributed systems; it plays a critical role in
  8. the fault tolerance of a broad range of applications, from currencies,
  9. to elections, to infrastructure orchestration, and beyond.
  10. The ability to tolerate machines failing in arbitrary ways, including
  11. becoming malicious, is known as Byzantine fault tolerance (BFT). The
  12. theory of BFT is decades old, but software implementations have only
  13. became popular recently, due largely to the success of "blockchain
  14. technology" like Bitcoin and Ethereum. Blockchain technology is just a
  15. reformalization of BFT in a more modern setting, with emphasis on
  16. peer-to-peer networking and cryptographic authentication. The name
  17. derives from the way transactions are batched in blocks, where each
  18. block contains a cryptographic hash of the previous one, forming a
  19. chain. In practice, the blockchain data structure actually optimizes BFT
  20. design.
  21. Tendermint consists of two chief technical components: a blockchain
  22. consensus engine and a generic application interface. The consensus
  23. engine, called Tendermint Core, ensures that the same transactions are
  24. recorded on every machine in the same order. The application interface,
  25. called the Application BlockChain Interface (ABCI), enables the
  26. transactions to be processed in any programming language. Unlike other
  27. blockchain and consensus solutions, which come pre-packaged with built
  28. in state machines (like a fancy key-value store, or a quirky scripting
  29. language), developers can use Tendermint for BFT state machine
  30. replication of applications written in whatever programming language and
  31. development environment is right for them.
  32. Tendermint is designed to be easy-to-use, simple-to-understand, highly
  33. performant, and useful for a wide variety of distributed applications.
  34. ## Tendermint vs. X
  35. Tendermint is broadly similar to two classes of software. The first
  36. class consists of distributed key-value stores, like Zookeeper, etcd,
  37. and consul, which use non-BFT consensus. The second class is known as
  38. "blockchain technology", and consists of both cryptocurrencies like
  39. Bitcoin and Ethereum, and alternative distributed ledger designs like
  40. Hyperledger's Burrow.
  41. ### Zookeeper, etcd, consul
  42. Zookeeper, etcd, and consul are all implementations of a key-value store
  43. atop a classical, non-BFT consensus algorithm. Zookeeper uses a version
  44. of Paxos called Zookeeper Atomic Broadcast, while etcd and consul use
  45. the Raft consensus algorithm, which is much younger and simpler. A
  46. typical cluster contains 3-5 machines, and can tolerate crash failures
  47. in up to 1/2 of the machines, but even a single Byzantine fault can
  48. destroy the system.
  49. Each offering provides a slightly different implementation of a
  50. featureful key-value store, but all are generally focused around
  51. providing basic services to distributed systems, such as dynamic
  52. configuration, service discovery, locking, leader-election, and so on.
  53. Tendermint is in essence similar software, but with two key differences:
  54. - It is Byzantine Fault Tolerant, meaning it can only tolerate up to a
  55. 1/3 of failures, but those failures can include arbitrary behaviour -
  56. including hacking and malicious attacks. - It does not specify a
  57. particular application, like a fancy key-value store. Instead, it
  58. focuses on arbitrary state machine replication, so developers can build
  59. the application logic that's right for them, from key-value store to
  60. cryptocurrency to e-voting platform and beyond.
  61. The layout of this Tendermint website content is also ripped directly
  62. and without shame from [consul.io](https://www.consul.io/) and the other
  63. [Hashicorp sites](https://www.hashicorp.com/#tools).
  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/tendermint/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/develop/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/develop/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. ![](assets/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. ![](assets/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](http://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. [See here
  269. for high resolution
  270. version](https://github.com/mobfoundry/hackatom/blob/master/tminfo.pdf).
  271. ![](assets/tm-transaction-flow.png)