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.

329 lines
15 KiB

  1. # Byzantine Consensus Algorithm
  2. ## Terms
  3. - The network is composed of optionally connected *nodes*. Nodes
  4. directly connected to a particular node are called *peers*.
  5. - The consensus process in deciding the next block (at some *height*
  6. `H`) is composed of one or many *rounds*.
  7. - `NewHeight`, `Propose`, `Prevote`, `Precommit`, and `Commit`
  8. represent state machine states of a round. (aka `RoundStep` or
  9. just "step").
  10. - A node is said to be *at* a given height, round, and step, or at
  11. `(H,R,S)`, or at `(H,R)` in short to omit the step.
  12. - To *prevote* or *precommit* something means to broadcast a [prevote
  13. vote](https://godoc.org/github.com/tendermint/tendermint/types#Vote)
  14. or [first precommit
  15. vote](https://godoc.org/github.com/tendermint/tendermint/types#FirstPrecommit)
  16. for something.
  17. - A vote *at* `(H,R)` is a vote signed with the bytes for `H` and `R`
  18. included in its [sign-bytes](block-structure.html#vote-sign-bytes).
  19. - *+2/3* is short for "more than 2/3"
  20. - *1/3+* is short for "1/3 or more"
  21. - A set of +2/3 of prevotes for a particular block or `<nil>` at
  22. `(H,R)` is called a *proof-of-lock-change* or *PoLC* for short.
  23. ## State Machine Overview
  24. At each height of the blockchain a round-based protocol is run to
  25. determine the next block. Each round is composed of three *steps*
  26. (`Propose`, `Prevote`, and `Precommit`), along with two special steps
  27. `Commit` and `NewHeight`.
  28. In the optimal scenario, the order of steps is:
  29. ```
  30. NewHeight -> (Propose -> Prevote -> Precommit)+ -> Commit -> NewHeight ->...
  31. ```
  32. The sequence `(Propose -> Prevote -> Precommit)` is called a *round*.
  33. There may be more than one round required to commit a block at a given
  34. height. Examples for why more rounds may be required include:
  35. - The designated proposer was not online.
  36. - The block proposed by the designated proposer was not valid.
  37. - The block proposed by the designated proposer did not propagate
  38. in time.
  39. - The block proposed was valid, but +2/3 of prevotes for the proposed
  40. block were not received in time for enough validator nodes by the
  41. time they reached the `Precommit` step. Even though +2/3 of prevotes
  42. are necessary to progress to the next step, at least one validator
  43. may have voted `<nil>` or maliciously voted for something else.
  44. - The block proposed was valid, and +2/3 of prevotes were received for
  45. enough nodes, but +2/3 of precommits for the proposed block were not
  46. received for enough validator nodes.
  47. Some of these problems are resolved by moving onto the next round &
  48. proposer. Others are resolved by increasing certain round timeout
  49. parameters over each successive round.
  50. ## State Machine Diagram
  51. ```
  52. +-------------------------------------+
  53. v |(Wait til `CommmitTime+timeoutCommit`)
  54. +-----------+ +-----+-----+
  55. +----------> | Propose +--------------+ | NewHeight |
  56. | +-----------+ | +-----------+
  57. | | ^
  58. |(Else, after timeoutPrecommit) v |
  59. +-----+-----+ +-----------+ |
  60. | Precommit | <------------------------+ Prevote | |
  61. +-----+-----+ +-----------+ |
  62. |(When +2/3 Precommits for block found) |
  63. v |
  64. +--------------------------------------------------------------------+
  65. | Commit |
  66. | |
  67. | * Set CommitTime = now; |
  68. | * Wait for block, then stage/save/commit block; |
  69. +--------------------------------------------------------------------+
  70. ```
  71. Background Gossip
  72. =================
  73. A node may not have a corresponding validator private key, but it
  74. nevertheless plays an active role in the consensus process by relaying
  75. relevant meta-data, proposals, blocks, and votes to its peers. A node
  76. that has the private keys of an active validator and is engaged in
  77. signing votes is called a *validator-node*. All nodes (not just
  78. validator-nodes) have an associated state (the current height, round,
  79. and step) and work to make progress.
  80. Between two nodes there exists a `Connection`, and multiplexed on top of
  81. this connection are fairly throttled `Channel`s of information. An
  82. epidemic gossip protocol is implemented among some of these channels to
  83. bring peers up to speed on the most recent state of consensus. For
  84. example,
  85. - Nodes gossip `PartSet` parts of the current round's proposer's
  86. proposed block. A LibSwift inspired algorithm is used to quickly
  87. broadcast blocks across the gossip network.
  88. - Nodes gossip prevote/precommit votes. A node `NODE_A` that is ahead
  89. of `NODE_B` can send `NODE_B` prevotes or precommits for `NODE_B`'s
  90. current (or future) round to enable it to progress forward.
  91. - Nodes gossip prevotes for the proposed PoLC (proof-of-lock-change)
  92. round if one is proposed.
  93. - Nodes gossip to nodes lagging in blockchain height with block
  94. [commits](https://godoc.org/github.com/tendermint/tendermint/types#Commit)
  95. for older blocks.
  96. - Nodes opportunistically gossip `HasVote` messages to hint peers what
  97. votes it already has.
  98. - Nodes broadcast their current state to all neighboring peers. (but
  99. is not gossiped further)
  100. There's more, but let's not get ahead of ourselves here.
  101. ## Proposals
  102. A proposal is signed and published by the designated proposer at each
  103. round. The proposer is chosen by a deterministic and non-choking round
  104. robin selection algorithm that selects proposers in proportion to their
  105. voting power (see
  106. [implementation](https://github.com/tendermint/tendermint/blob/develop/types/validator_set.go)).
  107. A proposal at `(H,R)` is composed of a block and an optional latest
  108. `PoLC-Round < R` which is included iff the proposer knows of one. This
  109. hints the network to allow nodes to unlock (when safe) to ensure the
  110. liveness property.
  111. ## State Machine Spec
  112. ### Propose Step (height:H,round:R)
  113. Upon entering `Propose`: - The designated proposer proposes a block at
  114. `(H,R)`.
  115. The `Propose` step ends: - After `timeoutProposeR` after entering
  116. `Propose`. --> goto `Prevote(H,R)` - After receiving proposal block
  117. and all prevotes at `PoLC-Round`. --> goto `Prevote(H,R)` - After
  118. [common exit conditions](#common-exit-conditions)
  119. ### Prevote Step (height:H,round:R)
  120. Upon entering `Prevote`, each validator broadcasts its prevote vote.
  121. - First, if the validator is locked on a block since `LastLockRound`
  122. but now has a PoLC for something else at round `PoLC-Round` where
  123. `LastLockRound < PoLC-Round < R`, then it unlocks.
  124. - If the validator is still locked on a block, it prevotes that.
  125. - Else, if the proposed block from `Propose(H,R)` is good, it
  126. prevotes that.
  127. - Else, if the proposal is invalid or wasn't received on time, it
  128. prevotes `<nil>`.
  129. The `Prevote` step ends: - After +2/3 prevotes for a particular block or
  130. `<nil>`. -->; goto `Precommit(H,R)` - After `timeoutPrevote` after
  131. receiving any +2/3 prevotes. --> goto `Precommit(H,R)` - After
  132. [common exit conditions](#common-exit-conditions)
  133. ### Precommit Step (height:H,round:R)
  134. Upon entering `Precommit`, each validator broadcasts its precommit vote.
  135. - If the validator has a PoLC at `(H,R)` for a particular block `B`, it
  136. (re)locks (or changes lock to) and precommits `B` and sets
  137. `LastLockRound = R`. - Else, if the validator has a PoLC at `(H,R)` for
  138. `<nil>`, it unlocks and precommits `<nil>`. - Else, it keeps the lock
  139. unchanged and precommits `<nil>`.
  140. A precommit for `<nil>` means "I didn’t see a PoLC for this round, but I
  141. did get +2/3 prevotes and waited a bit".
  142. The Precommit step ends: - After +2/3 precommits for `<nil>`. -->
  143. goto `Propose(H,R+1)` - After `timeoutPrecommit` after receiving any
  144. +2/3 precommits. --> goto `Propose(H,R+1)` - After [common exit
  145. conditions](#common-exit-conditions)
  146. ### Common exit conditions
  147. - After +2/3 precommits for a particular block. --> goto
  148. `Commit(H)`
  149. - After any +2/3 prevotes received at `(H,R+x)`. --> goto
  150. `Prevote(H,R+x)`
  151. - After any +2/3 precommits received at `(H,R+x)`. --> goto
  152. `Precommit(H,R+x)`
  153. ### Commit Step (height:H)
  154. - Set `CommitTime = now()`
  155. - Wait until block is received. --> goto `NewHeight(H+1)`
  156. ### NewHeight Step (height:H)
  157. - Move `Precommits` to `LastCommit` and increment height.
  158. - Set `StartTime = CommitTime+timeoutCommit`
  159. - Wait until `StartTime` to receive straggler commits. --> goto
  160. `Propose(H,0)`
  161. ## Proofs
  162. ### Proof of Safety
  163. Assume that at most -1/3 of the voting power of validators is byzantine.
  164. If a validator commits block `B` at round `R`, it's because it saw +2/3
  165. of precommits at round `R`. This implies that 1/3+ of honest nodes are
  166. still locked at round `R' > R`. These locked validators will remain
  167. locked until they see a PoLC at `R' > R`, but this won't happen because
  168. 1/3+ are locked and honest, so at most -2/3 are available to vote for
  169. anything other than `B`.
  170. ### Proof of Liveness
  171. If 1/3+ honest validators are locked on two different blocks from
  172. different rounds, a proposers' `PoLC-Round` will eventually cause nodes
  173. locked from the earlier round to unlock. Eventually, the designated
  174. proposer will be one that is aware of a PoLC at the later round. Also,
  175. `timeoutProposalR` increments with round `R`, while the size of a
  176. proposal are capped, so eventually the network is able to "fully gossip"
  177. the whole proposal (e.g. the block & PoLC).
  178. ### Proof of Fork Accountability
  179. Define the JSet (justification-vote-set) at height `H` of a validator
  180. `V1` to be all the votes signed by the validator at `H` along with
  181. justification PoLC prevotes for each lock change. For example, if `V1`
  182. signed the following precommits: `Precommit(B1 @ round 0)`,
  183. `Precommit(<nil> @ round 1)`, `Precommit(B2 @ round 4)` (note that no
  184. precommits were signed for rounds 2 and 3, and that's ok),
  185. `Precommit(B1 @ round 0)` must be justified by a PoLC at round 0, and
  186. `Precommit(B2 @ round 4)` must be justified by a PoLC at round 4; but
  187. the precommit for `<nil>` at round 1 is not a lock-change by definition
  188. so the JSet for `V1` need not include any prevotes at round 1, 2, or 3
  189. (unless `V1` happened to have prevoted for those rounds).
  190. Further, define the JSet at height `H` of a set of validators `VSet` to
  191. be the union of the JSets for each validator in `VSet`. For a given
  192. commit by honest validators at round `R` for block `B` we can construct
  193. a JSet to justify the commit for `B` at `R`. We say that a JSet
  194. *justifies* a commit at `(H,R)` if all the committers (validators in the
  195. commit-set) are each justified in the JSet with no duplicitous vote
  196. signatures (by the committers).
  197. - **Lemma**: When a fork is detected by the existence of two
  198. conflicting [commits](./validators.html#commiting-a-block), the
  199. union of the JSets for both commits (if they can be compiled) must
  200. include double-signing by at least 1/3+ of the validator set.
  201. **Proof**: The commit cannot be at the same round, because that
  202. would immediately imply double-signing by 1/3+. Take the union of
  203. the JSets of both commits. If there is no double-signing by at least
  204. 1/3+ of the validator set in the union, then no honest validator
  205. could have precommitted any different block after the first commit.
  206. Yet, +2/3 did. Reductio ad absurdum.
  207. As a corollary, when there is a fork, an external process can determine
  208. the blame by requiring each validator to justify all of its round votes.
  209. Either we will find 1/3+ who cannot justify at least one of their votes,
  210. and/or, we will find 1/3+ who had double-signed.
  211. ### Alternative algorithm
  212. Alternatively, we can take the JSet of a commit to be the "full commit".
  213. That is, if light clients and validators do not consider a block to be
  214. committed unless the JSet of the commit is also known, then we get the
  215. desirable property that if there ever is a fork (e.g. there are two
  216. conflicting "full commits"), then 1/3+ of the validators are immediately
  217. punishable for double-signing.
  218. There are many ways to ensure that the gossip network efficiently share
  219. the JSet of a commit. One solution is to add a new message type that
  220. tells peers that this node has (or does not have) a +2/3 majority for B
  221. (or) at (H,R), and a bitarray of which votes contributed towards that
  222. majority. Peers can react by responding with appropriate votes.
  223. We will implement such an algorithm for the next iteration of the
  224. Tendermint consensus protocol.
  225. Other potential improvements include adding more data in votes such as
  226. the last known PoLC round that caused a lock change, and the last voted
  227. round/step (or, we may require that validators not skip any votes). This
  228. may make JSet verification/gossip logic easier to implement.
  229. ### Censorship Attacks
  230. Due to the definition of a block
  231. [commit](../../tendermint-core/validator.md#commiting-a-block), any 1/3+ coalition of
  232. validators can halt the blockchain by not broadcasting their votes. Such
  233. a coalition can also censor particular transactions by rejecting blocks
  234. that include these transactions, though this would result in a
  235. significant proportion of block proposals to be rejected, which would
  236. slow down the rate of block commits of the blockchain, reducing its
  237. utility and value. The malicious coalition might also broadcast votes in
  238. a trickle so as to grind blockchain block commits to a near halt, or
  239. engage in any combination of these attacks.
  240. If a global active adversary were also involved, it can partition the
  241. network in such a way that it may appear that the wrong subset of
  242. validators were responsible for the slowdown. This is not just a
  243. limitation of Tendermint, but rather a limitation of all consensus
  244. protocols whose network is potentially controlled by an active
  245. adversary.
  246. ### Overcoming Forks and Censorship Attacks
  247. For these types of attacks, a subset of the validators through external
  248. means should coordinate to sign a reorg-proposal that chooses a fork
  249. (and any evidence thereof) and the initial subset of validators with
  250. their signatures. Validators who sign such a reorg-proposal forego its
  251. collateral on all other forks. Clients should verify the signatures on
  252. the reorg-proposal, verify any evidence, and make a judgement or prompt
  253. the end-user for a decision. For example, a phone wallet app may prompt
  254. the user with a security warning, while a refrigerator may accept any
  255. reorg-proposal signed by +1/2 of the original validators.
  256. No non-synchronous Byzantine fault-tolerant algorithm can come to
  257. consensus when 1/3+ of validators are dishonest, yet a fork assumes that
  258. 1/3+ of validators have already been dishonest by double-signing or
  259. lock-changing without justification. So, signing the reorg-proposal is a
  260. coordination problem that cannot be solved by any non-synchronous
  261. protocol (i.e. automatically, and without making assumptions about the
  262. reliability of the underlying network). It must be provided by means
  263. external to the weakly-synchronous Tendermint consensus algorithm. For
  264. now, we leave the problem of reorg-proposal coordination to human
  265. coordination via internet media. Validators must take care to ensure
  266. that there are no significant network partitions, to avoid situations
  267. where two conflicting reorg-proposals are signed.
  268. Assuming that the external coordination medium and protocol is robust,
  269. it follows that forks are less of a concern than [censorship
  270. attacks](#censorship-attacks).