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.

459 lines
12 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. # Tendermint Blockchain
  2. Here we describe the data structures in the Tendermint blockchain and the rules for validating them.
  3. ## Data Structures
  4. The Tendermint blockchains consists of a short list of basic data types:
  5. - `Block`
  6. - `Header`
  7. - `BlockID`
  8. - `Time`
  9. - `Data` (for transactions)
  10. - `Commit` and `Vote`
  11. - `EvidenceData` and `Evidence`
  12. ## Block
  13. A block consists of a header, transactions, votes (the commit),
  14. and a list of evidence of malfeasance (ie. signing conflicting votes).
  15. ```go
  16. type Block struct {
  17. Header Header
  18. Txs Data
  19. Evidence EvidenceData
  20. LastCommit Commit
  21. }
  22. ```
  23. Note the `LastCommit` is the set of votes that committed the last block.
  24. ## Header
  25. A block header contains metadata about the block and about the consensus, as well as commitments to
  26. the data in the current block, the previous block, and the results returned by the application:
  27. ```go
  28. type Header struct {
  29. // basic block info
  30. ChainID string
  31. Height int64
  32. Time Time
  33. NumTxs int64
  34. TotalTxs int64
  35. // prev block info
  36. LastBlockID BlockID
  37. // hashes of block data
  38. LastCommitHash []byte // commit from validators from the last block
  39. DataHash []byte // Merkle root of transactions
  40. // hashes from the app output from the prev block
  41. ValidatorsHash []byte // validators for the current block
  42. NextValidatorsHash []byte // validators for the next block
  43. ConsensusHash []byte // consensus params for current block
  44. AppHash []byte // state after txs from the previous block
  45. LastResultsHash []byte // root hash of all results from the txs from the previous block
  46. // consensus info
  47. EvidenceHash []byte // evidence included in the block
  48. ProposerAddress []byte // original proposer of the block
  49. ```
  50. Further details on each of these fields is described below.
  51. ## BlockID
  52. The `BlockID` contains two distinct Merkle roots of the block.
  53. The first, used as the block's main hash, is the Merkle root
  54. of all the fields in the header. The second, used for secure gossipping of
  55. the block during consensus, is the Merkle root of the complete serialized block
  56. cut into parts. The `BlockID` includes these two hashes, as well as the number of
  57. parts.
  58. ```go
  59. type BlockID struct {
  60. Hash []byte
  61. Parts PartsHeader
  62. }
  63. type PartsHeader struct {
  64. Hash []byte
  65. Total int32
  66. }
  67. ```
  68. TODO: link to details of merkle sums.
  69. ## Time
  70. Tendermint uses the
  71. [Google.Protobuf.WellKnownTypes.Timestamp](https://developers.google.com/protocol-buffers/docs/reference/csharp/class/google/protobuf/well-known-types/timestamp)
  72. format, which uses two integers, one for Seconds and for Nanoseconds.
  73. NOTE: there is currently a small divergence between Tendermint and the
  74. Google.Protobuf.WellKnownTypes.Timestamp that should be resolved. See [this
  75. issue](https://github.com/tendermint/go-amino/issues/223) for details.
  76. ## Data
  77. Data is just a wrapper for a list of transactions, where transactions are
  78. arbitrary byte arrays:
  79. ```
  80. type Data struct {
  81. Txs [][]byte
  82. }
  83. ```
  84. ## Commit
  85. Commit is a simple wrapper for a list of votes, with one vote for each
  86. validator. It also contains the relevant BlockID:
  87. ```
  88. type Commit struct {
  89. BlockID BlockID
  90. Precommits []Vote
  91. }
  92. ```
  93. NOTE: this will likely change to reduce the commit size by eliminating redundant
  94. information - see [issue #1648](https://github.com/tendermint/tendermint/issues/1648).
  95. ## Vote
  96. A vote is a signed message from a validator for a particular block.
  97. The vote includes information about the validator signing it.
  98. ```go
  99. type Vote struct {
  100. ValidatorAddress []byte
  101. ValidatorIndex int
  102. Height int64
  103. Round int
  104. Timestamp Time
  105. Type int8
  106. BlockID BlockID
  107. Signature []byte
  108. }
  109. ```
  110. There are two types of votes:
  111. a _prevote_ has `vote.Type == 1` and
  112. a _precommit_ has `vote.Type == 2`.
  113. ## Signature
  114. Signatures in Tendermint are raw bytes representing the underlying signature.
  115. The only signature scheme currently supported for Tendermint validators is
  116. ED25519. The signature is the raw 64-byte ED25519 signature.
  117. ## EvidenceData
  118. EvidenceData is a simple wrapper for a list of evidence:
  119. ```
  120. type EvidenceData struct {
  121. Evidence []Evidence
  122. }
  123. ```
  124. ## Evidence
  125. Evidence in Tendermint is implemented as an interface.
  126. This means any evidence is encoded using its Amino prefix.
  127. There is currently only a single type, the `DuplicateVoteEvidence`.
  128. ```
  129. // amino name: "tendermint/DuplicateVoteEvidence"
  130. type DuplicateVoteEvidence struct {
  131. PubKey PubKey
  132. VoteA Vote
  133. VoteB Vote
  134. }
  135. ```
  136. ## Validation
  137. Here we describe the validation rules for every element in a block.
  138. Blocks which do not satisfy these rules are considered invalid.
  139. We abuse notation by using something that looks like Go, supplemented with English.
  140. A statement such as `x == y` is an assertion - if it fails, the item is invalid.
  141. We refer to certain globally available objects:
  142. `block` is the block under consideration,
  143. `prevBlock` is the `block` at the previous height,
  144. and `state` keeps track of the validator set, the consensus parameters
  145. and other results from the application. At the point when `block` is the block under consideration,
  146. the current version of the `state` corresponds to the state
  147. after executing transactions from the `prevBlock`.
  148. Elements of an object are accessed as expected,
  149. ie. `block.Header`.
  150. See [here](https://github.com/tendermint/tendermint/blob/master/docs/spec/blockchain/state.md) for the definition of `state`.
  151. ### Header
  152. A Header is valid if its corresponding fields are valid.
  153. ### ChainID
  154. ```
  155. len(block.ChainID) < 50
  156. ```
  157. ChainID must be maximum 50 UTF-8 symbols.
  158. ### Height
  159. ```go
  160. block.Header.Height > 0
  161. block.Header.Height == prevBlock.Header.Height + 1
  162. ```
  163. The height is an incrementing integer. The first block has `block.Header.Height == 1`.
  164. ### Time
  165. ```
  166. block.Header.Timestamp >= prevBlock.Header.Timestamp + 1 ms
  167. block.Header.Timestamp == MedianTime(block.LastCommit, state.LastValidators)
  168. ```
  169. The block timestamp must be monotonic.
  170. It must equal the weighted median of the timestamps of the valid votes in the block.LastCommit.
  171. Note: the timestamp of a vote must be greater by at least one millisecond than that of the
  172. block being voted on.
  173. See the section on [BFT time](../consensus/bft-time.md) for more details.
  174. ### NumTxs
  175. ```go
  176. block.Header.NumTxs == len(block.Txs.Txs)
  177. ```
  178. Number of transactions included in the block.
  179. ### TotalTxs
  180. ```go
  181. block.Header.TotalTxs == prevBlock.Header.TotalTxs + block.Header.NumTxs
  182. ```
  183. The cumulative sum of all transactions included in this blockchain.
  184. The first block has `block.Header.TotalTxs = block.Header.NumberTxs`.
  185. ### LastBlockID
  186. LastBlockID is the previous block's BlockID:
  187. ```go
  188. prevBlockParts := MakeParts(prevBlock, state.LastConsensusParams.BlockGossip.BlockPartSize)
  189. block.Header.LastBlockID == BlockID {
  190. Hash: SimpleMerkleRoot(prevBlock.Header),
  191. PartsHeader{
  192. Hash: SimpleMerkleRoot(prevBlockParts),
  193. Total: len(prevBlockParts),
  194. },
  195. }
  196. ```
  197. Note: it depends on the ConsensusParams,
  198. which are held in the `state` and may be updated by the application.
  199. The first block has `block.Header.LastBlockID == BlockID{}`.
  200. ### LastCommitHash
  201. ```go
  202. block.Header.LastCommitHash == SimpleMerkleRoot(block.LastCommit)
  203. ```
  204. Simple Merkle root of the votes included in the block.
  205. These are the votes that committed the previous block.
  206. The first block has `block.Header.LastCommitHash == []byte{}`
  207. ### DataHash
  208. ```go
  209. block.Header.DataHash == SimpleMerkleRoot(block.Txs.Txs)
  210. ```
  211. Simple Merkle root of the transactions included in the block.
  212. ### ValidatorsHash
  213. ```go
  214. block.ValidatorsHash == SimpleMerkleRoot(state.Validators)
  215. ```
  216. Simple Merkle root of the current validator set that is committing the block.
  217. This can be used to validate the `LastCommit` included in the next block.
  218. ### NextValidatorsHash
  219. ```go
  220. block.NextValidatorsHash == SimpleMerkleRoot(state.NextValidators)
  221. ```
  222. Simple Merkle root of the next validator set that will be the validator set that commits the next block.
  223. This is included so that the current validator set gets a chance to sign the
  224. next validator sets Merkle root.
  225. ### ConsensusParamsHash
  226. ```go
  227. block.ConsensusParamsHash == SimpleMerkleRoot(state.ConsensusParams)
  228. ```
  229. Simple Merkle root of the consensus parameters.
  230. ### AppHash
  231. ```go
  232. block.AppHash == state.AppHash
  233. ```
  234. Arbitrary byte array returned by the application after executing and commiting the previous block. It serves as the basis for validating any merkle proofs that comes from the ABCI application and represents the state of the actual application rather than the state of the blockchain itself.
  235. The first block has `block.Header.AppHash == []byte{}`.
  236. ### LastResultsHash
  237. ```go
  238. block.ResultsHash == SimpleMerkleRoot(state.LastResults)
  239. ```
  240. Simple Merkle root of the results of the transactions in the previous block.
  241. The first block has `block.Header.ResultsHash == []byte{}`.
  242. ## EvidenceHash
  243. ```go
  244. block.EvidenceHash == SimpleMerkleRoot(block.Evidence)
  245. ```
  246. Simple Merkle root of the evidence of Byzantine behaviour included in this block.
  247. ### ProposerAddress
  248. ```go
  249. block.Header.ProposerAddress in state.Validators
  250. ```
  251. Address of the original proposer of the block. Must be a current validator.
  252. ## Txs
  253. Arbitrary length array of arbitrary length byte-arrays.
  254. ## LastCommit
  255. The first height is an exception - it requires the LastCommit to be empty:
  256. ```go
  257. if block.Header.Height == 1 {
  258. len(b.LastCommit) == 0
  259. }
  260. ```
  261. Otherwise, we require:
  262. ```go
  263. len(block.LastCommit) == len(state.LastValidators)
  264. talliedVotingPower := 0
  265. for i, vote := range block.LastCommit{
  266. if vote == nil{
  267. continue
  268. }
  269. vote.Type == 2
  270. vote.Height == block.LastCommit.Height()
  271. vote.Round == block.LastCommit.Round()
  272. vote.BlockID == block.LastBlockID
  273. val := state.LastValidators[i]
  274. vote.Verify(block.ChainID, val.PubKey) == true
  275. talliedVotingPower += val.VotingPower
  276. }
  277. talliedVotingPower > (2/3) * TotalVotingPower(state.LastValidators)
  278. ```
  279. Includes one (possibly nil) vote for every current validator.
  280. Non-nil votes must be Precommits.
  281. All votes must be for the same height and round.
  282. All votes must be for the previous block.
  283. All votes must have a valid signature from the corresponding validator.
  284. The sum total of the voting power of the validators that voted
  285. must be greater than 2/3 of the total voting power of the complete validator set.
  286. ### Vote
  287. A vote is a signed message broadcast in the consensus for a particular block at a particular height and round.
  288. When stored in the blockchain or propagated over the network, votes are encoded in TMBIN.
  289. For signing, votes are encoded in JSON, and the ChainID is included, in the form of the `CanonicalSignBytes`.
  290. We define a method `Verify` that returns `true` if the signature verifies against the pubkey for the CanonicalSignBytes
  291. using the given ChainID:
  292. ```go
  293. func (v Vote) Verify(chainID string, pubKey PubKey) bool {
  294. return pubKey.Verify(v.Signature, CanonicalSignBytes(chainID, v))
  295. }
  296. ```
  297. where `pubKey.Verify` performs the appropriate digital signature verification of the `pubKey`
  298. against the given signature and message bytes.
  299. ## Evidence
  300. There is currently only one kind of evidence, `DuplicateVoteEvidence`.
  301. DuplicateVoteEvidence `ev` is valid if
  302. - `ev.VoteA` and `ev.VoteB` can be verified with `ev.PubKey`
  303. - `ev.VoteA` and `ev.VoteB` have the same `Height, Round, Address, Index, Type`
  304. - `ev.VoteA.BlockID != ev.VoteB.BlockID`
  305. - `(block.Height - ev.VoteA.Height) < MAX_EVIDENCE_AGE`
  306. # Execution
  307. Once a block is validated, it can be executed against the state.
  308. The state follows this recursive equation:
  309. ```go
  310. state(1) = InitialState
  311. state(h+1) <- Execute(state(h), ABCIApp, block(h))
  312. ```
  313. where `InitialState` includes the initial consensus parameters and validator set,
  314. and `ABCIApp` is an ABCI application that can return results and changes to the validator
  315. set (TODO). Execute is defined as:
  316. ```go
  317. Execute(s State, app ABCIApp, block Block) State {
  318. // Fuction ApplyBlock executes block of transactions against the app and returns the new root hash of the app state,
  319. // modifications to the validator set and the changes of the consensus parameters.
  320. AppHash, ValidatorChanges, ConsensusParamChanges := app.ApplyBlock(block)
  321. return State{
  322. LastResults: abciResponses.DeliverTxResults,
  323. AppHash: AppHash,
  324. LastValidators: state.Validators,
  325. Validators: state.NextValidators,
  326. NextValidators: UpdateValidators(state.NextValidators, ValidatorChanges),
  327. ConsensusParams: UpdateConsensusParams(state.ConsensusParams, ConsensusParamChanges),
  328. }
  329. }
  330. ```