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.

120 lines
4.0 KiB

  1. # State
  2. The state contains information whose cryptographic digest is included in block headers, and thus is
  3. necessary for validating new blocks. For instance, the validators set and the results of
  4. transactions are never included in blocks, but their Merkle roots are - the state keeps track of them.
  5. Note that the `State` object itself is an implementation detail, since it is never
  6. included in a block or gossiped over the network, and we never compute
  7. its hash. Thus we do not include here details of how the `State` object is
  8. persisted or queried. That said, the types it contains are part of the specification, since
  9. their Merkle roots are included in blocks and their values are used in
  10. validation.
  11. ```go
  12. type State struct {
  13. ChainID string
  14. InitialHeight int64
  15. LastBlockHeight int64
  16. LastBlockID types.BlockID
  17. LastBlockTime time.Time
  18. Version Version
  19. LastResults []Result
  20. AppHash []byte
  21. LastValidators ValidatorSet
  22. Validators ValidatorSet
  23. NextValidators ValidatorSet
  24. ConsensusParams ConsensusParams
  25. }
  26. ```
  27. The chain ID and initial height are taken from the genesis file, and not changed again. The
  28. initial height will be `1` in the typical case, `0` is an invalid value.
  29. Note there is a hard-coded limit of 10000 validators. This is inherited from the
  30. limit on the number of votes in a commit.
  31. Further information on [`Validator`'s](./data_structures.md#validator),
  32. [`ValidatorSet`'s](./data_structures.md#validatorset) and
  33. [`ConsensusParams`'s](./data_structures.md#consensusparams) can
  34. be found in [data structures](./data_structures.md)
  35. ## Execution
  36. State gets updated at the end of executing a block. Of specific interest is `ResponseEndBlock` and
  37. `ResponseCommit`
  38. ```go
  39. type ResponseEndBlock struct {
  40. ValidatorUpdates []ValidatorUpdate `protobuf:"bytes,1,rep,name=validator_updates,json=validatorUpdates,proto3" json:"validator_updates"`
  41. ConsensusParamUpdates *types1.ConsensusParams `protobuf:"bytes,2,opt,name=consensus_param_updates,json=consensusParamUpdates,proto3" json:"consensus_param_updates,omitempty"`
  42. Events []Event `protobuf:"bytes,3,rep,name=events,proto3" json:"events,omitempty"`
  43. }
  44. ```
  45. where
  46. ```go
  47. type ValidatorUpdate struct {
  48. PubKey crypto.PublicKey `protobuf:"bytes,1,opt,name=pub_key,json=pubKey,proto3" json:"pub_key"`
  49. Power int64 `protobuf:"varint,2,opt,name=power,proto3" json:"power,omitempty"`
  50. }
  51. ```
  52. and
  53. ```go
  54. type ResponseCommit struct {
  55. // reserve 1
  56. Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"`
  57. RetainHeight int64 `protobuf:"varint,3,opt,name=retain_height,json=retainHeight,proto3" json:"retain_height,omitempty"`
  58. }
  59. ```
  60. `ValidatorUpdates` are used to add and remove validators to the current set as well as update
  61. validator power. Setting validator power to 0 in `ValidatorUpdate` will cause the validator to be
  62. removed. `ConsensusParams` are safely copied across (i.e. if a field is nil it gets ignored) and the
  63. `Data` from the `ResponseCommit` is used as the `AppHash`
  64. ## Version
  65. ```go
  66. type Version struct {
  67. consensus Consensus
  68. software string
  69. }
  70. ```
  71. [`Consensus`](./data_structures.md#version) contains the protocol version for the blockchain and the
  72. application.
  73. ## Block
  74. The total size of a block is limited in bytes by the `ConsensusParams.Block.MaxBytes`.
  75. Proposed blocks must be less than this size, and will be considered invalid
  76. otherwise.
  77. Blocks should additionally be limited by the amount of "gas" consumed by the
  78. transactions in the block, though this is not yet implemented.
  79. ## Evidence
  80. For evidence in a block to be valid, it must satisfy:
  81. ```go
  82. block.Header.Time-evidence.Time < ConsensusParams.Evidence.MaxAgeDuration &&
  83. block.Header.Height-evidence.Height < ConsensusParams.Evidence.MaxAgeNumBlocks
  84. ```
  85. A block must not contain more than `ConsensusParams.Evidence.MaxBytes` of evidence. This is
  86. implemented to mitigate spam attacks.
  87. ## Validator
  88. Validators from genesis file and `ResponseEndBlock` must have pubkeys of type ∈
  89. `ConsensusParams.Validator.PubKeyTypes`.