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.

477 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
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. - `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. The timestamp of the first block must be equal to the genesis time (since
  174. there's no votes to compute the median).
  175. ```
  176. if block.Header.Height == 1 {
  177. block.Header.Timestamp == genesisTime
  178. }
  179. ```
  180. See the section on [BFT time](../consensus/bft-time.md) for more details.
  181. ### NumTxs
  182. ```go
  183. block.Header.NumTxs == len(block.Txs.Txs)
  184. ```
  185. Number of transactions included in the block.
  186. ### TotalTxs
  187. ```go
  188. block.Header.TotalTxs == prevBlock.Header.TotalTxs + block.Header.NumTxs
  189. ```
  190. The cumulative sum of all transactions included in this blockchain.
  191. The first block has `block.Header.TotalTxs = block.Header.NumberTxs`.
  192. ### LastBlockID
  193. LastBlockID is the previous block's BlockID:
  194. ```go
  195. prevBlockParts := MakeParts(prevBlock, state.LastConsensusParams.BlockGossip.BlockPartSize)
  196. block.Header.LastBlockID == BlockID {
  197. Hash: SimpleMerkleRoot(prevBlock.Header),
  198. PartsHeader{
  199. Hash: SimpleMerkleRoot(prevBlockParts),
  200. Total: len(prevBlockParts),
  201. },
  202. }
  203. ```
  204. Note: it depends on the ConsensusParams,
  205. which are held in the `state` and may be updated by the application.
  206. The first block has `block.Header.LastBlockID == BlockID{}`.
  207. ### LastCommitHash
  208. ```go
  209. block.Header.LastCommitHash == SimpleMerkleRoot(block.LastCommit)
  210. ```
  211. Simple Merkle root of the votes included in the block.
  212. These are the votes that committed the previous block.
  213. The first block has `block.Header.LastCommitHash == []byte{}`
  214. ### DataHash
  215. ```go
  216. block.Header.DataHash == SimpleMerkleRoot(block.Txs.Txs)
  217. ```
  218. Simple Merkle root of the transactions included in the block.
  219. ### ValidatorsHash
  220. ```go
  221. block.ValidatorsHash == SimpleMerkleRoot(state.Validators)
  222. ```
  223. Simple Merkle root of the current validator set that is committing the block.
  224. This can be used to validate the `LastCommit` included in the next block.
  225. ### NextValidatorsHash
  226. ```go
  227. block.NextValidatorsHash == SimpleMerkleRoot(state.NextValidators)
  228. ```
  229. Simple Merkle root of the next validator set that will be the validator set that commits the next block.
  230. This is included so that the current validator set gets a chance to sign the
  231. next validator sets Merkle root.
  232. ### ConsensusParamsHash
  233. ```go
  234. block.ConsensusParamsHash == tmhash(amino(state.ConsensusParams))
  235. ```
  236. Hash of the amino-encoded consensus parameters.
  237. ### AppHash
  238. ```go
  239. block.AppHash == state.AppHash
  240. ```
  241. 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.
  242. The first block has `block.Header.AppHash == []byte{}`.
  243. ### LastResultsHash
  244. ```go
  245. block.ResultsHash == SimpleMerkleRoot(state.LastResults)
  246. ```
  247. Simple Merkle root of the results of the transactions in the previous block.
  248. The first block has `block.Header.ResultsHash == []byte{}`.
  249. ## EvidenceHash
  250. ```go
  251. block.EvidenceHash == SimpleMerkleRoot(block.Evidence)
  252. ```
  253. Simple Merkle root of the evidence of Byzantine behaviour included in this block.
  254. ### ProposerAddress
  255. ```go
  256. block.Header.ProposerAddress in state.Validators
  257. ```
  258. Address of the original proposer of the block. Must be a current validator.
  259. ## Txs
  260. Arbitrary length array of arbitrary length byte-arrays.
  261. ## LastCommit
  262. The first height is an exception - it requires the LastCommit to be empty:
  263. ```go
  264. if block.Header.Height == 1 {
  265. len(b.LastCommit) == 0
  266. }
  267. ```
  268. Otherwise, we require:
  269. ```go
  270. len(block.LastCommit) == len(state.LastValidators)
  271. talliedVotingPower := 0
  272. for i, vote := range block.LastCommit{
  273. if vote == nil{
  274. continue
  275. }
  276. vote.Type == 2
  277. vote.Height == block.LastCommit.Height()
  278. vote.Round == block.LastCommit.Round()
  279. vote.BlockID == block.LastBlockID
  280. val := state.LastValidators[i]
  281. vote.Verify(block.ChainID, val.PubKey) == true
  282. talliedVotingPower += val.VotingPower
  283. }
  284. talliedVotingPower > (2/3) * TotalVotingPower(state.LastValidators)
  285. ```
  286. Includes one (possibly nil) vote for every current validator.
  287. Non-nil votes must be Precommits.
  288. All votes must be for the same height and round.
  289. All votes must be for the previous block.
  290. All votes must have a valid signature from the corresponding validator.
  291. The sum total of the voting power of the validators that voted
  292. must be greater than 2/3 of the total voting power of the complete validator set.
  293. ### Vote
  294. A vote is a signed message broadcast in the consensus for a particular block at a particular height and round.
  295. When stored in the blockchain or propagated over the network, votes are encoded in Amino.
  296. For signing, votes are represented via `CanonicalVote` and also encoded using amino (protobuf compatible) via
  297. `Vote.SignBytes` which includes the `ChainID`, and uses a different ordering of
  298. the fields.
  299. We define a method `Verify` that returns `true` if the signature verifies against the pubkey for the `SignBytes`
  300. using the given ChainID:
  301. ```go
  302. func (vote *Vote) Verify(chainID string, pubKey crypto.PubKey) error {
  303. if !bytes.Equal(pubKey.Address(), vote.ValidatorAddress) {
  304. return ErrVoteInvalidValidatorAddress
  305. }
  306. if !pubKey.VerifyBytes(vote.SignBytes(chainID), vote.Signature) {
  307. return ErrVoteInvalidSignature
  308. }
  309. return nil
  310. }
  311. ```
  312. where `pubKey.Verify` performs the appropriate digital signature verification of the `pubKey`
  313. against the given signature and message bytes.
  314. ## Evidence
  315. There is currently only one kind of evidence, `DuplicateVoteEvidence`.
  316. DuplicateVoteEvidence `ev` is valid if
  317. - `ev.VoteA` and `ev.VoteB` can be verified with `ev.PubKey`
  318. - `ev.VoteA` and `ev.VoteB` have the same `Height, Round, Address, Index, Type`
  319. - `ev.VoteA.BlockID != ev.VoteB.BlockID`
  320. - `(block.Height - ev.VoteA.Height) < MAX_EVIDENCE_AGE`
  321. # Execution
  322. Once a block is validated, it can be executed against the state.
  323. The state follows this recursive equation:
  324. ```go
  325. state(1) = InitialState
  326. state(h+1) <- Execute(state(h), ABCIApp, block(h))
  327. ```
  328. where `InitialState` includes the initial consensus parameters and validator set,
  329. and `ABCIApp` is an ABCI application that can return results and changes to the validator
  330. set (TODO). Execute is defined as:
  331. ```go
  332. Execute(s State, app ABCIApp, block Block) State {
  333. // Fuction ApplyBlock executes block of transactions against the app and returns the new root hash of the app state,
  334. // modifications to the validator set and the changes of the consensus parameters.
  335. AppHash, ValidatorChanges, ConsensusParamChanges := app.ApplyBlock(block)
  336. return State{
  337. LastResults: abciResponses.DeliverTxResults,
  338. AppHash: AppHash,
  339. LastValidators: state.Validators,
  340. Validators: state.NextValidators,
  341. NextValidators: UpdateValidators(state.NextValidators, ValidatorChanges),
  342. ConsensusParams: UpdateConsensusParams(state.ConsensusParams, ConsensusParamChanges),
  343. }
  344. }
  345. ```