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.

185 lines
4.9 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 gossiped 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. ChainID string
  15. InitialHeight int64
  16. Version Version
  17. LastResults []Result
  18. AppHash []byte
  19. LastValidators []Validator
  20. Validators []Validator
  21. NextValidators []Validator
  22. ConsensusParams ConsensusParams
  23. }
  24. ```
  25. The chain ID and initial height are taken from the genesis file, and not changed again. The
  26. initial height will be `1` in the typical case, `0` is an invalid value.
  27. Note there is a hard-coded limit of 10000 validators. This is inherited from the
  28. limit on the number of votes in a commit.
  29. ### Version
  30. ```go
  31. type Version struct {
  32. consensus Consensus
  33. software string
  34. }
  35. ```
  36. The `Consensus` contains the protocol version for the blockchain and the
  37. application as two `uint64` values:
  38. ```go
  39. type Consensus struct {
  40. Block uint64
  41. App uint64
  42. }
  43. ```
  44. ### ResponseDeliverTx
  45. ```protobuf
  46. message ResponseDeliverTx {
  47. uint32 code = 1;
  48. bytes data = 2;
  49. string log = 3; // nondeterministic
  50. string info = 4; // nondeterministic
  51. int64 gas_wanted = 5;
  52. int64 gas_used = 6;
  53. repeated Event events = 7
  54. [(gogoproto.nullable) = false, (gogoproto.jsontag) = "events,omitempty"];
  55. string codespace = 8;
  56. }
  57. ```
  58. `ResponseDeliverTx` is the result of executing a transaction against the application.
  59. It returns a result code (`uint32`), an arbitrary byte array (`[]byte`) (ie. a return value), Log (`string`), Info (`string`), GasWanted (`int64`), GasUsed (`int64`), Events (`[]Events`) and a Codespace (`string`).
  60. ### Validator
  61. A validator is an active participant in the consensus with a public key and a voting power.
  62. Validator's also contain an address field, which is a hash digest of the PubKey.
  63. ```go
  64. type Validator struct {
  65. Address []byte
  66. PubKey PubKey
  67. VotingPower int64
  68. }
  69. ```
  70. When hashing the Validator struct, the address is not included,
  71. because it is redundant with the pubkey.
  72. The `state.Validators`, `state.LastValidators`, and `state.NextValidators`,
  73. must always be sorted by voting power, so that there is a canonical order for
  74. computing the MerkleRoot.
  75. We also define a `TotalVotingPower` function, to return the total voting power:
  76. ```go
  77. func TotalVotingPower(vals []Validators) int64{
  78. sum := 0
  79. for v := range vals{
  80. sum += v.VotingPower
  81. }
  82. return sum
  83. }
  84. ```
  85. ### ConsensusParams
  86. ConsensusParams define various limits for blockchain data structures.
  87. Like validator sets, they are set during genesis and can be updated by the application through ABCI.
  88. When hashed, only a subset of the params are included, to allow the params to
  89. evolve without breaking the header.
  90. ```protobuf
  91. message ConsensusParams {
  92. BlockParams block = 1;
  93. EvidenceParams evidence = 2;
  94. ValidatorParams validator = 3;
  95. VersionParams version = 4;
  96. }
  97. ```
  98. ```go
  99. type hashedParams struct {
  100. BlockMaxBytes int64
  101. BlockMaxGas int64
  102. }
  103. func HashConsensusParams() []byte {
  104. SHA256(hashedParams{
  105. BlockMaxBytes: params.Block.MaxBytes,
  106. BlockMaxGas: params.Block.MaxGas,
  107. })
  108. }
  109. ```
  110. ```protobuf
  111. message BlockParams {
  112. int64 max_bytes = 1;
  113. int64 max_gas = 2;
  114. int64 time_iota_ms = 3; // not exposed to the application
  115. }
  116. message EvidenceParams {
  117. int64 max_age_num_blocks = 1;
  118. google.protobuf.Duration max_age_duration = 2;
  119. uint32 max_num = 3;
  120. }
  121. message ValidatorParams {
  122. repeated string pub_key_types = 1;
  123. }
  124. message VersionParams {
  125. uint64 AppVersion = 1;
  126. }
  127. ```
  128. #### Block
  129. The total size of a block is limited in bytes by the `ConsensusParams.Block.MaxBytes`.
  130. Proposed blocks must be less than this size, and will be considered invalid
  131. otherwise.
  132. Blocks should additionally be limited by the amount of "gas" consumed by the
  133. transactions in the block, though this is not yet implemented.
  134. The minimal time between consecutive blocks is controlled by the
  135. `ConsensusParams.Block.TimeIotaMs`.
  136. #### Evidence
  137. For evidence in a block to be valid, it must satisfy:
  138. ```go
  139. block.Header.Time-evidence.Time < ConsensusParams.Evidence.MaxAgeDuration &&
  140. block.Header.Height-evidence.Height < ConsensusParams.Evidence.MaxAgeNumBlocks
  141. ```
  142. #### Validator
  143. Validators from genesis file and `ResponseEndBlock` must have pubkeys of type ∈
  144. `ConsensusParams.Validator.PubKeyTypes`.