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.

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