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.

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