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.

173 lines
4.2 KiB

  1. # State
  2. ## State
  3. The state contains information whose cryptographic digest is included in block headers, and thus is
  4. necessary for validating new blocks. For instance, the validators set and the results of
  5. transactions are never included in blocks, but their Merkle roots are - the state keeps track of them.
  6. Note that the `State` object itself is an implementation detail, since it is never
  7. included in a block or gossipped over the network, and we never compute
  8. its hash. Thus we do not include here details of how the `State` object is
  9. persisted or queried. That said, the types it contains are part of the specification, since
  10. their Merkle roots are included in blocks and their values are used in
  11. validation.
  12. ```go
  13. type State struct {
  14. Version Version
  15. LastResults []Result
  16. AppHash []byte
  17. LastValidators []Validator
  18. Validators []Validator
  19. NextValidators []Validator
  20. ConsensusParams ConsensusParams
  21. }
  22. ```
  23. Note there is a hard-coded limit of 10000 validators. This is inherited from the
  24. limit on the number of votes in a commit.
  25. ### Version
  26. ```go
  27. type Version struct {
  28. consensus Consensus
  29. software string
  30. }
  31. ```
  32. The `Consensus` contains the protocol version for the blockchain and the
  33. application as two `uint64` values:
  34. ```go
  35. type Consensus struct {
  36. Block uint64
  37. App uint64
  38. }
  39. ```
  40. ### Result
  41. ```go
  42. type Result struct {
  43. Code uint32
  44. Data []byte
  45. }
  46. ```
  47. `Result` is the result of executing a transaction against the application.
  48. It returns a result code and an arbitrary byte array (ie. a return value).
  49. NOTE: the Result needs to be updated to include more fields returned from
  50. processing transactions, like gas variables and tags - see
  51. [issue 1007](https://github.com/tendermint/tendermint/issues/1007).
  52. ### Validator
  53. A validator is an active participant in the consensus with a public key and a voting power.
  54. Validator's also contain an address field, which is a hash digest of the PubKey.
  55. ```go
  56. type Validator struct {
  57. Address []byte
  58. PubKey PubKey
  59. VotingPower int64
  60. }
  61. ```
  62. When hashing the Validator struct, the address is not included,
  63. because it is redundant with the pubkey.
  64. The `state.Validators`, `state.LastValidators`, and `state.NextValidators`,
  65. must always be sorted by voting power, so that there is a canonical order for
  66. computing the MerkleRoot.
  67. We also define a `TotalVotingPower` function, to return the total voting power:
  68. ```go
  69. func TotalVotingPower(vals []Validators) int64{
  70. sum := 0
  71. for v := range vals{
  72. sum += v.VotingPower
  73. }
  74. return sum
  75. }
  76. ```
  77. ### ConsensusParams
  78. ConsensusParams define various limits for blockchain data structures.
  79. Like validator sets, they are set during genesis and can be updated by the application through ABCI.
  80. When hashed, only a subset of the params are included, to allow the params to
  81. evolve without breaking the header.
  82. ```go
  83. type ConsensusParams struct {
  84. Block
  85. Evidence
  86. Validator
  87. Version
  88. }
  89. type hashedParams struct {
  90. BlockMaxBytes int64
  91. BlockMaxGas int64
  92. }
  93. func (params ConsensusParams) Hash() []byte {
  94. SHA256(hashedParams{
  95. BlockMaxBytes: params.Block.MaxBytes,
  96. BlockMaxGas: params.Block.MaxGas,
  97. })
  98. }
  99. type BlockParams struct {
  100. MaxBytes int64
  101. MaxGas int64
  102. TimeIotaMs int64
  103. }
  104. type EvidenceParams struct {
  105. MaxAgeNumBlocks int64
  106. MaxAgeDuration time.Duration
  107. MaxNum uint32
  108. ProofTrialPeriod int64
  109. }
  110. type ValidatorParams struct {
  111. PubKeyTypes []string
  112. }
  113. type VersionParams struct {
  114. AppVersion uint64
  115. }
  116. ```
  117. #### Block
  118. The total size of a block is limited in bytes by the `ConsensusParams.Block.MaxBytes`.
  119. Proposed blocks must be less than this size, and will be considered invalid
  120. otherwise.
  121. Blocks should additionally be limited by the amount of "gas" consumed by the
  122. transactions in the block, though this is not yet implemented.
  123. The minimal time between consecutive blocks is controlled by the
  124. `ConsensusParams.Block.TimeIotaMs`.
  125. #### Evidence
  126. For evidence in a block to be valid, it must satisfy:
  127. ```go
  128. block.Header.Time-evidence.Time < ConsensusParams.Evidence.MaxAgeDuration &&
  129. block.Header.Height-evidence.Height < ConsensusParams.Evidence.MaxAgeNumBlocks
  130. ```
  131. #### Validator
  132. Validators from genesis file and `ResponseEndBlock` must have pubkeys of type ∈
  133. `ConsensusParams.Validator.PubKeyTypes`.