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.

218 lines
9.2 KiB

  1. Block Structure
  2. ===============
  3. The tendermint consensus engine records all agreements by a
  4. supermajority of nodes into a blockchain, which is replicated among all
  5. nodes. This blockchain is accessible via various rpc endpoints, mainly
  6. ``/block?height=`` to get the full block, as well as
  7. ``/blockchain?minHeight=_&maxHeight=_`` to get a list of headers. But
  8. what exactly is stored in these blocks?
  9. Block
  10. ~~~~~
  11. A
  12. `Block <https://godoc.org/github.com/tendermint/tendermint/types#Block>`__
  13. contains:
  14. - a `Header <#header>`__ contains merkle hashes for various chain
  15. states
  16. - the
  17. `Data <https://godoc.org/github.com/tendermint/tendermint/types#Data>`__
  18. is all transactions which are to be processed
  19. - the `LastCommit <#commit>`__ > 2/3 signatures for the last block
  20. The signatures returned along with block ``H`` are those validating
  21. block ``H-1``. This can be a little confusing, but we must also consider
  22. that the ``Header`` also contains the ``LastCommitHash``. It would be
  23. impossible for a Header to include the commits that sign it, as it would
  24. cause an infinite loop here. But when we get block ``H``, we find
  25. ``Header.LastCommitHash``, which must match the hash of ``LastCommit``.
  26. Header
  27. ~~~~~~
  28. The
  29. `Header <https://godoc.org/github.com/tendermint/tendermint/types#Header>`__
  30. contains lots of information (follow link for up-to-date info). Notably,
  31. it maintains the ``Height``, the ``LastBlockID`` (to make it a chain),
  32. and hashes of the data, the app state, and the validator set. This is
  33. important as the only item that is signed by the validators is the
  34. ``Header``, and all other data must be validated against one of the
  35. merkle hashes in the ``Header``.
  36. The ``DataHash`` can provide a nice check on the
  37. `Data <https://godoc.org/github.com/tendermint/tendermint/types#Data>`__
  38. returned in this same block. If you are subscribed to new blocks, via
  39. tendermint RPC, in order to display or process the new transactions you
  40. should at least validate that the ``DataHash`` is valid. If it is
  41. important to verify autheniticity, you must wait for the ``LastCommit``
  42. from the next block to make sure the block header (including
  43. ``DataHash``) was properly signed.
  44. The ``ValidatorHash`` contains a hash of the current
  45. `Validators <https://godoc.org/github.com/tendermint/tendermint/types#Validator>`__.
  46. Tracking all changes in the validator set is complex, but a client can
  47. quickly compare this hash with the `hash of the currently known
  48. validators <https://godoc.org/github.com/tendermint/tendermint/types#ValidatorSet.Hash>`__
  49. to see if there have been changes.
  50. The ``AppHash`` serves as the basis for validating any merkle proofs
  51. that come from the ABCI application. It represents the
  52. state of the actual application, rather that the state of the blockchain
  53. itself. This means it's necessary in order to perform any business
  54. logic, such as verifying an account balance.
  55. **Note** After the transactions are committed to a block, they still
  56. need to be processed in a separate step, which happens between the
  57. blocks. If you find a given transaction in the block at height ``H``,
  58. the effects of running that transaction will be first visible in the
  59. ``AppHash`` from the block header at height ``H+1``.
  60. Like the ``LastCommit`` issue, this is a requirement of the immutability
  61. of the block chain, as the application only applies transactions *after*
  62. they are commited to the chain.
  63. Commit
  64. ~~~~~~
  65. The
  66. `Commit <https://godoc.org/github.com/tendermint/tendermint/types#Commit>`__
  67. contains a set of
  68. `Votes <https://godoc.org/github.com/tendermint/tendermint/types#Vote>`__
  69. that were made by the validator set to reach consensus on this block.
  70. This is the key to the security in any PoS system, and actually no data
  71. that cannot be traced back to a block header with a valid set of Votes
  72. can be trusted. Thus, getting the Commit data and verifying the votes is
  73. extremely important.
  74. As mentioned above, in order to find the ``precommit votes`` for block
  75. header ``H``, we need to query block ``H+1``. Then we need to check the
  76. votes, make sure they really are for that block, and properly formatted.
  77. Much of this code is implemented in Go in the
  78. `light-client <https://github.com/tendermint/light-client>`__ package.
  79. If you look at the code, you will notice that we need to provide the
  80. ``chainID`` of the blockchain in order to properly calculate the votes.
  81. This is to protect anyone from swapping votes between chains to fake (or
  82. frame) a validator. Also note that this ``chainID`` is in the
  83. ``genesis.json`` from *Tendermint*, not the ``genesis.json`` from the
  84. basecoin app (`that is a different
  85. chainID... <https://github.com/cosmos/cosmos-sdk/issues/32>`__).
  86. Once we have those votes, and we calculated the proper `sign
  87. bytes <https://godoc.org/github.com/tendermint/tendermint/types#Vote.WriteSignBytes>`__
  88. using the chainID and a `nice helper
  89. function <https://godoc.org/github.com/tendermint/tendermint/types#SignBytes>`__,
  90. we can verify them. The light client is responsible for maintaining a
  91. set of validators that we trust. Each vote only stores the validators
  92. ``Address``, as well as the ``Signature``. Assuming we have a local copy
  93. of the trusted validator set, we can look up the ``Public Key`` of the
  94. validator given its ``Address``, then verify that the ``Signature``
  95. matches the ``SignBytes`` and ``Public Key``. Then we sum up the total
  96. voting power of all validators, whose votes fulfilled all these
  97. stringent requirements. If the total number of voting power for a single
  98. block is greater than 2/3 of all voting power, then we can finally trust
  99. the block header, the AppHash, and the proof we got from the ABCI
  100. application.
  101. Vote Sign Bytes
  102. ^^^^^^^^^^^^^^^
  103. The ``sign-bytes`` of a vote is produced by taking a
  104. `stable-json <https://github.com/substack/json-stable-stringify>`__-like
  105. deterministic JSON `wire <./wire-protocol.html>`__ encoding of
  106. the vote (excluding the ``Signature`` field), and wrapping it with
  107. ``{"chain_id":"my_chain","vote":...}``.
  108. For example, a precommit vote might have the following ``sign-bytes``:
  109. .. code:: json
  110. {"chain_id":"my_chain","vote":{"block_hash":"611801F57B4CE378DF1A3FFF1216656E89209A99","block_parts_header":{"hash":"B46697379DBE0774CC2C3B656083F07CA7E0F9CE","total":123},"height":1234,"round":1,"type":2}}
  111. Block Hash
  112. ~~~~~~~~~~
  113. The `block
  114. hash <https://godoc.org/github.com/tendermint/tendermint/types#Block.Hash>`__
  115. is the `Simple Tree hash <./merkle.html#simple-tree-with-dictionaries>`__
  116. of the fields of the block ``Header`` encoded as a list of
  117. ``KVPair``\ s.
  118. Transaction
  119. ~~~~~~~~~~~
  120. A transaction is any sequence of bytes. It is up to your
  121. ABCI application to accept or reject transactions.
  122. BlockID
  123. ~~~~~~~
  124. Many of these data structures refer to the
  125. `BlockID <https://godoc.org/github.com/tendermint/tendermint/types#BlockID>`__,
  126. which is the ``BlockHash`` (hash of the block header, also referred to
  127. by the next block) along with the ``PartSetHeader``. The
  128. ``PartSetHeader`` is explained below and is used internally to
  129. orchestrate the p2p propogation. For clients, it is basically opaque
  130. bytes, but they must match for all votes.
  131. PartSetHeader
  132. ~~~~~~~~~~~~~
  133. The
  134. `PartSetHeader <https://godoc.org/github.com/tendermint/tendermint/types#PartSetHeader>`__
  135. contains the total number of pieces in a
  136. `PartSet <https://godoc.org/github.com/tendermint/tendermint/types#PartSet>`__,
  137. and the Merkle root hash of those pieces.
  138. PartSet
  139. ~~~~~~~
  140. PartSet is used to split a byteslice of data into parts (pieces) for
  141. transmission. By splitting data into smaller parts and computing a
  142. Merkle root hash on the list, you can verify that a part is legitimately
  143. part of the complete data, and the part can be forwarded to other peers
  144. before all the parts are known. In short, it's a fast way to securely
  145. propagate a large chunk of data (like a block) over a gossip network.
  146. PartSet was inspired by the LibSwift project.
  147. Usage:
  148. .. code:: go
  149. data := RandBytes(2 << 20) // Something large
  150. partSet := NewPartSetFromData(data)
  151. partSet.Total() // Total number of 4KB parts
  152. partSet.Count() // Equal to the Total, since we already have all the parts
  153. partSet.Hash() // The Merkle root hash
  154. partSet.BitArray() // A BitArray of partSet.Total() 1's
  155. header := partSet.Header() // Send this to the peer
  156. header.Total // Total number of parts
  157. header.Hash // The merkle root hash
  158. // Now we'll reconstruct the data from the parts
  159. partSet2 := NewPartSetFromHeader(header)
  160. partSet2.Total() // Same total as partSet.Total()
  161. partSet2.Count() // Zero, since this PartSet doesn't have any parts yet.
  162. partSet2.Hash() // Same hash as in partSet.Hash()
  163. partSet2.BitArray() // A BitArray of partSet.Total() 0's
  164. // In a gossip network the parts would arrive in arbitrary order, perhaps
  165. // in response to explicit requests for parts, or optimistically in response
  166. // to the receiving peer's partSet.BitArray().
  167. for !partSet2.IsComplete() {
  168. part := receivePartFromGossipNetwork()
  169. added, err := partSet2.AddPart(part)
  170. if err != nil {
  171. // A wrong part,
  172. // the merkle trail does not hash to partSet2.Hash()
  173. } else if !added {
  174. // A duplicate part already received
  175. }
  176. }
  177. data2, _ := ioutil.ReadAll(partSet2.GetReader())
  178. bytes.Equal(data, data2) // true