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.

214 lines
6.7 KiB

7 years ago
  1. package types
  2. import (
  3. "time"
  4. "github.com/pkg/errors"
  5. abci "github.com/tendermint/tendermint/abci/types"
  6. "github.com/tendermint/tendermint/crypto/tmhash"
  7. tmstrings "github.com/tendermint/tendermint/libs/strings"
  8. )
  9. const (
  10. // MaxBlockSizeBytes is the maximum permitted size of the blocks.
  11. MaxBlockSizeBytes = 104857600 // 100MB
  12. // BlockPartSizeBytes is the size of one block part.
  13. BlockPartSizeBytes = 65536 // 64kB
  14. // MaxBlockPartsCount is the maximum number of block parts.
  15. MaxBlockPartsCount = (MaxBlockSizeBytes / BlockPartSizeBytes) + 1
  16. )
  17. // ConsensusParams contains consensus critical parameters that determine the
  18. // validity of blocks.
  19. type ConsensusParams struct {
  20. Block BlockParams `json:"block"`
  21. Evidence EvidenceParams `json:"evidence"`
  22. Validator ValidatorParams `json:"validator"`
  23. }
  24. // HashedParams is a subset of ConsensusParams.
  25. // It is amino encoded and hashed into the Header.ConsensusHash.
  26. type HashedParams struct {
  27. BlockMaxBytes int64
  28. BlockMaxGas int64
  29. }
  30. // BlockParams defines limits on the block size and gas plus minimum time
  31. // between blocks.
  32. type BlockParams struct {
  33. MaxBytes int64 `json:"max_bytes"`
  34. MaxGas int64 `json:"max_gas"`
  35. // Minimum time increment between consecutive blocks (in milliseconds)
  36. // Not exposed to the application.
  37. TimeIotaMs int64 `json:"time_iota_ms"`
  38. }
  39. // EvidenceParams determines how we handle evidence of malfeasance.
  40. //
  41. // Evidence older than MaxAgeNumBlocks && MaxAgeDuration is considered
  42. // stale and ignored.
  43. //
  44. // In Cosmos-SDK based blockchains, MaxAgeDuration is usually equal to the
  45. // unbonding period. MaxAgeNumBlocks is calculated by dividing the unboding
  46. // period by the average block time (e.g. 2 weeks / 6s per block = 2d8h).
  47. type EvidenceParams struct {
  48. // Max age of evidence, in blocks.
  49. //
  50. // The basic formula for calculating this is: MaxAgeDuration / {average block
  51. // time}.
  52. MaxAgeNumBlocks int64 `json:"max_age_num_blocks"`
  53. // Max age of evidence, in time.
  54. //
  55. // It should correspond with an app's "unbonding period" or other similar
  56. // mechanism for handling [Nothing-At-Stake
  57. // attacks](https://github.com/ethereum/wiki/wiki/Proof-of-Stake-FAQ#what-is-the-nothing-at-stake-problem-and-how-can-it-be-fixed).
  58. MaxAgeDuration time.Duration `json:"max_age_duration"`
  59. }
  60. // ValidatorParams restrict the public key types validators can use.
  61. // NOTE: uses ABCI pubkey naming, not Amino names.
  62. type ValidatorParams struct {
  63. PubKeyTypes []string `json:"pub_key_types"`
  64. }
  65. // DefaultConsensusParams returns a default ConsensusParams.
  66. func DefaultConsensusParams() *ConsensusParams {
  67. return &ConsensusParams{
  68. DefaultBlockParams(),
  69. DefaultEvidenceParams(),
  70. DefaultValidatorParams(),
  71. }
  72. }
  73. // DefaultBlockParams returns a default BlockParams.
  74. func DefaultBlockParams() BlockParams {
  75. return BlockParams{
  76. MaxBytes: 22020096, // 21MB
  77. MaxGas: -1,
  78. TimeIotaMs: 1000, // 1s
  79. }
  80. }
  81. // DefaultEvidenceParams returns a default EvidenceParams.
  82. func DefaultEvidenceParams() EvidenceParams {
  83. return EvidenceParams{
  84. MaxAgeNumBlocks: 100000, // 27.8 hrs at 1block/s
  85. MaxAgeDuration: 48 * time.Hour,
  86. }
  87. }
  88. // DefaultValidatorParams returns a default ValidatorParams, which allows
  89. // only ed25519 pubkeys.
  90. func DefaultValidatorParams() ValidatorParams {
  91. return ValidatorParams{[]string{ABCIPubKeyTypeEd25519}}
  92. }
  93. func (params *ValidatorParams) IsValidPubkeyType(pubkeyType string) bool {
  94. for i := 0; i < len(params.PubKeyTypes); i++ {
  95. if params.PubKeyTypes[i] == pubkeyType {
  96. return true
  97. }
  98. }
  99. return false
  100. }
  101. // Validate validates the ConsensusParams to ensure all values are within their
  102. // allowed limits, and returns an error if they are not.
  103. func (params *ConsensusParams) Validate() error {
  104. if params.Block.MaxBytes <= 0 {
  105. return errors.Errorf("block.MaxBytes must be greater than 0. Got %d",
  106. params.Block.MaxBytes)
  107. }
  108. if params.Block.MaxBytes > MaxBlockSizeBytes {
  109. return errors.Errorf("block.MaxBytes is too big. %d > %d",
  110. params.Block.MaxBytes, MaxBlockSizeBytes)
  111. }
  112. if params.Block.MaxGas < -1 {
  113. return errors.Errorf("block.MaxGas must be greater or equal to -1. Got %d",
  114. params.Block.MaxGas)
  115. }
  116. if params.Block.TimeIotaMs <= 0 {
  117. return errors.Errorf("block.TimeIotaMs must be greater than 0. Got %v",
  118. params.Block.TimeIotaMs)
  119. }
  120. if params.Evidence.MaxAgeNumBlocks <= 0 {
  121. return errors.Errorf("evidenceParams.MaxAgeNumBlocks must be greater than 0. Got %d",
  122. params.Evidence.MaxAgeNumBlocks)
  123. }
  124. if params.Evidence.MaxAgeDuration <= 0 {
  125. return errors.Errorf("evidenceParams.MaxAgeDuration must be grater than 0 if provided, Got %v",
  126. params.Evidence.MaxAgeDuration)
  127. }
  128. if len(params.Validator.PubKeyTypes) == 0 {
  129. return errors.New("len(Validator.PubKeyTypes) must be greater than 0")
  130. }
  131. // Check if keyType is a known ABCIPubKeyType
  132. for i := 0; i < len(params.Validator.PubKeyTypes); i++ {
  133. keyType := params.Validator.PubKeyTypes[i]
  134. if _, ok := ABCIPubKeyTypesToAminoNames[keyType]; !ok {
  135. return errors.Errorf("params.Validator.PubKeyTypes[%d], %s, is an unknown pubkey type",
  136. i, keyType)
  137. }
  138. }
  139. return nil
  140. }
  141. // Hash returns a hash of a subset of the parameters to store in the block header.
  142. // Only the Block.MaxBytes and Block.MaxGas are included in the hash.
  143. // This allows the ConsensusParams to evolve more without breaking the block
  144. // protocol. No need for a Merkle tree here, just a small struct to hash.
  145. func (params *ConsensusParams) Hash() []byte {
  146. hasher := tmhash.New()
  147. bz := cdcEncode(HashedParams{
  148. params.Block.MaxBytes,
  149. params.Block.MaxGas,
  150. })
  151. if bz == nil {
  152. panic("cannot fail to encode ConsensusParams")
  153. }
  154. hasher.Write(bz)
  155. return hasher.Sum(nil)
  156. }
  157. func (params *ConsensusParams) Equals(params2 *ConsensusParams) bool {
  158. return params.Block == params2.Block &&
  159. params.Evidence == params2.Evidence &&
  160. tmstrings.StringSliceEqual(params.Validator.PubKeyTypes, params2.Validator.PubKeyTypes)
  161. }
  162. // Update returns a copy of the params with updates from the non-zero fields of p2.
  163. // NOTE: note: must not modify the original
  164. func (params ConsensusParams) Update(params2 *abci.ConsensusParams) ConsensusParams {
  165. res := params // explicit copy
  166. if params2 == nil {
  167. return res
  168. }
  169. // we must defensively consider any structs may be nil
  170. if params2.Block != nil {
  171. res.Block.MaxBytes = params2.Block.MaxBytes
  172. res.Block.MaxGas = params2.Block.MaxGas
  173. }
  174. if params2.Evidence != nil {
  175. res.Evidence.MaxAgeNumBlocks = params2.Evidence.MaxAgeNumBlocks
  176. res.Evidence.MaxAgeDuration = params2.Evidence.MaxAgeDuration
  177. }
  178. if params2.Validator != nil {
  179. // Copy params2.Validator.PubkeyTypes, and set result's value to the copy.
  180. // This avoids having to initialize the slice to 0 values, and then write to it again.
  181. res.Validator.PubKeyTypes = append([]string{}, params2.Validator.PubKeyTypes...)
  182. }
  183. return res
  184. }