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.

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