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.

391 lines
9.8 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
  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`, `Header`, `Vote`, `BlockID`, `Signature`, and `Evidence`.
  6. ## Block
  7. A block consists of a header, a list of transactions, a list of votes (the commit),
  8. and a list of evidence if malfeasance (ie. signing conflicting votes).
  9. ```
  10. type Block struct {
  11. Header Header
  12. Txs [][]byte
  13. LastCommit []Vote
  14. Evidence []Evidence
  15. }
  16. ```
  17. ## Header
  18. A block header contains metadata about the block and about the consensus, as well as commitments to
  19. the data in the current block, the previous block, and the results returned by the application:
  20. ```
  21. type Header struct {
  22. // block metadata
  23. Version string // Version string
  24. ChainID string // ID of the chain
  25. Height int64 // current block height
  26. Time int64 // UNIX time, in millisconds
  27. // current block
  28. NumTxs int64 // Number of txs in this block
  29. TxHash []byte // SimpleMerkle of the block.Txs
  30. LastCommitHash []byte // SimpleMerkle of the block.LastCommit
  31. // previous block
  32. TotalTxs int64 // prevBlock.TotalTxs + block.NumTxs
  33. LastBlockID BlockID // BlockID of prevBlock
  34. // application
  35. ResultsHash []byte // SimpleMerkle of []abci.Result from prevBlock
  36. AppHash []byte // Arbitrary state digest
  37. ValidatorsHash []byte // SimpleMerkle of the ValidatorSet
  38. ConsensusParamsHash []byte // SimpleMerkle of the ConsensusParams
  39. // consensus
  40. Proposer []byte // Address of the block proposer
  41. EvidenceHash []byte // SimpleMerkle of []Evidence
  42. }
  43. ```
  44. Further details on each of this fields is taken up below.
  45. ## BlockID
  46. The `BlockID` contains two distinct Merkle roots of the block.
  47. The first, used as the block's main hash, is the Merkle root
  48. of all the fields in the header. The second, used for secure gossipping of
  49. the block during consensus, is the Merkle root of the complete serialized block
  50. cut into parts. The `BlockID` includes these two hashes, as well as the number of
  51. parts.
  52. ```
  53. type BlockID struct {
  54. Hash []byte
  55. Parts PartsHeader
  56. }
  57. type PartsHeader struct {
  58. Hash []byte
  59. Total int32
  60. }
  61. ```
  62. ## Vote
  63. A vote is a signed message from a validator for a particular block.
  64. The vote includes information about the validator signing it.
  65. ```
  66. type Vote struct {
  67. Timestamp int64
  68. Address []byte
  69. Index int
  70. Height int64
  71. Round int
  72. Type int8
  73. BlockID BlockID
  74. Signature Signature
  75. }
  76. ```
  77. There are two types of votes:
  78. a prevote has `vote.Type == 1` and
  79. a precommit has `vote.Type == 2`.
  80. ## Signature
  81. Tendermint allows for multiple signature schemes to be used by prepending a single type-byte
  82. to the signature bytes. Different signatures may also come with fixed or variable lengths.
  83. Currently, Tendermint supports Ed25519 and Secp256k1.
  84. ### ED25519
  85. An ED25519 signature has `Type == 0x1`. It looks like:
  86. ```
  87. // Implements Signature
  88. type Ed25519Signature struct {
  89. Type int8 = 0x1
  90. Signature [64]byte
  91. }
  92. ```
  93. where `Signature` is the 64 byte signature.
  94. ### Secp256k1
  95. A `Secp256k1` signature has `Type == 0x2`. It looks like:
  96. ```
  97. // Implements Signature
  98. type Secp256k1Signature struct {
  99. Type int8 = 0x2
  100. Signature []byte
  101. }
  102. ```
  103. where `Signature` is the DER encoded signature, ie:
  104. ```
  105. 0x30 <length of whole message> <0x02> <length of R> <R> 0x2 <length of S> <S>.
  106. ```
  107. ## Evidence
  108. TODO
  109. # Validation
  110. Here we describe the validation rules for every element in a block.
  111. Blocks which do not satisfy these rules are considered invalid.
  112. We abuse notation by using something that looks like Go, supplemented with English.
  113. A statement such as `x == y` is an assertion - if it fails, the item is invalid.
  114. We refer to certain globally available objects:
  115. `block` is the block under consideration,
  116. `prevBlock` is the `block` at the previous height,
  117. and `state` keeps track of the validator set, the consensus parameters
  118. and other results from the application.
  119. Elements of an object are accessed as expected,
  120. ie. `block.Header`. See [here](state.md) for the definition of `state`.
  121. ## Header
  122. A Header is valid if its corresponding fields are valid.
  123. ### Version
  124. Arbitrary string.
  125. ### ChainID
  126. Arbitrary constant string.
  127. ### Height
  128. ```
  129. block.Header.Height > 0
  130. block.Header.Height == prevBlock.Header.Height + 1
  131. ```
  132. The height is an incrementing integer. The first block has `block.Header.Height == 1`.
  133. ### Time
  134. The median of the timestamps of the valid votes in the block.LastCommit.
  135. Corresponds to the number of nanoseconds, with millisecond resolution, since January 1, 1970.
  136. Note the timestamp in a vote must be greater by at least one millisecond than that of the
  137. block being voted on.
  138. ### NumTxs
  139. ```
  140. block.Header.NumTxs == len(block.Txs)
  141. ```
  142. Number of transactions included in the block.
  143. ### TxHash
  144. ```
  145. block.Header.TxHash == SimpleMerkleRoot(block.Txs)
  146. ```
  147. Simple Merkle root of the transactions in the block.
  148. ### LastCommitHash
  149. ```
  150. block.Header.LastCommitHash == SimpleMerkleRoot(block.LastCommit)
  151. ```
  152. Simple Merkle root of the votes included in the block.
  153. These are the votes that committed the previous block.
  154. The first block has `block.Header.LastCommitHash == []byte{}`
  155. ### TotalTxs
  156. ```
  157. block.Header.TotalTxs == prevBlock.Header.TotalTxs + block.Header.NumTxs
  158. ```
  159. The cumulative sum of all transactions included in this blockchain.
  160. The first block has `block.Header.TotalTxs = block.Header.NumberTxs`.
  161. ### LastBlockID
  162. ```
  163. prevBlockParts := MakeParts(prevBlock, state.LastConsensusParams.BlockGossip.BlockPartSize)
  164. block.Header.LastBlockID == BlockID {
  165. Hash: SimpleMerkleRoot(prevBlock.Header),
  166. PartsHeader{
  167. Hash: SimpleMerkleRoot(prevBlockParts),
  168. Total: len(prevBlockParts),
  169. },
  170. }
  171. ```
  172. Previous block's BlockID. Note it depends on the ConsensusParams,
  173. which are held in the `state` and may be updated by the application.
  174. The first block has `block.Header.LastBlockID == BlockID{}`.
  175. ### ResultsHash
  176. ```
  177. block.ResultsHash == SimpleMerkleRoot(state.LastResults)
  178. ```
  179. Simple Merkle root of the results of the transactions in the previous block.
  180. The first block has `block.Header.ResultsHash == []byte{}`.
  181. ### AppHash
  182. ```
  183. block.AppHash == state.AppHash
  184. ```
  185. Arbitrary byte array returned by the application after executing and commiting the previous block.
  186. The first block has `block.Header.AppHash == []byte{}`.
  187. ### ValidatorsHash
  188. ```
  189. block.ValidatorsHash == SimpleMerkleRoot(state.Validators)
  190. ```
  191. Simple Merkle root of the current validator set that is committing the block.
  192. This can be used to validate the `LastCommit` included in the next block.
  193. May be updated by the application.
  194. ### ConsensusParamsHash
  195. ```
  196. block.ConsensusParamsHash == SimpleMerkleRoot(state.ConsensusParams)
  197. ```
  198. Simple Merkle root of the consensus parameters.
  199. May be updated by the application.
  200. ### Proposer
  201. ```
  202. block.Header.Proposer in state.Validators
  203. ```
  204. Original proposer of the block. Must be a current validator.
  205. NOTE: this field can only be further verified by real-time participants in the consensus.
  206. This is because the same block can be proposed in multiple rounds for the same height
  207. and we do not track the initial round the block was proposed.
  208. ### EvidenceHash
  209. ```
  210. block.EvidenceHash == SimpleMerkleRoot(block.Evidence)
  211. ```
  212. Simple Merkle root of the evidence of Byzantine behaviour included in this block.
  213. ## Txs
  214. Arbitrary length array of arbitrary length byte-arrays.
  215. ## LastCommit
  216. The first height is an exception - it requires the LastCommit to be empty:
  217. ```
  218. if block.Header.Height == 1 {
  219. len(b.LastCommit) == 0
  220. }
  221. ```
  222. Otherwise, we require:
  223. ```
  224. len(block.LastCommit) == len(state.LastValidators)
  225. talliedVotingPower := 0
  226. for i, vote := range block.LastCommit{
  227. if vote == nil{
  228. continue
  229. }
  230. vote.Type == 2
  231. vote.Height == block.LastCommit.Height()
  232. vote.Round == block.LastCommit.Round()
  233. vote.BlockID == block.LastBlockID
  234. val := state.LastValidators[i]
  235. vote.Verify(block.ChainID, val.PubKey) == true
  236. talliedVotingPower += val.VotingPower
  237. }
  238. talliedVotingPower > (2/3) * TotalVotingPower(state.LastValidators)
  239. ```
  240. Includes one (possibly nil) vote for every current validator.
  241. Non-nil votes must be Precommits.
  242. All votes must be for the same height and round.
  243. All votes must be for the previous block.
  244. All votes must have a valid signature from the corresponding validator.
  245. The sum total of the voting power of the validators that voted
  246. must be greater than 2/3 of the total voting power of the complete validator set.
  247. ### Vote
  248. A vote is a signed message broadcast in the consensus for a particular block at a particular height and round.
  249. When stored in the blockchain or propagated over the network, votes are encoded in TMBIN.
  250. For signing, votes are encoded in JSON, and the ChainID is included, in the form of the `CanonicalSignBytes`.
  251. We define a method `Verify` that returns `true` if the signature verifies against the pubkey for the CanonicalSignBytes
  252. using the given ChainID:
  253. ```
  254. func (v Vote) Verify(chainID string, pubKey PubKey) bool {
  255. return pubKey.Verify(v.Signature, CanonicalSignBytes(chainID, v))
  256. }
  257. ```
  258. where `pubKey.Verify` performs the approprioate digital signature verification of the `pubKey`
  259. against the given signature and message bytes.
  260. ## Evidence
  261. ```
  262. ```
  263. Every piece of evidence contains two conflicting votes from a single validator that
  264. was active at the height indicated in the votes.
  265. The votes must not be too old.
  266. # Execution
  267. Once a block is validated, it can be executed against the state.
  268. The state follows the recursive equation:
  269. ```
  270. app = NewABCIApp
  271. state(1) = InitialState
  272. state(h+1) <- Execute(state(h), app, block(h))
  273. ```