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.

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