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