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.

198 lines
6.0 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
  26. // the Header.ConsensusHash.
  27. type HashedParams struct {
  28. BlockMaxBytes int64
  29. BlockMaxGas int64
  30. }
  31. // BlockParams define limits on the block size and gas plus minimum time
  32. // between blocks.
  33. type BlockParams struct {
  34. MaxBytes int64 `json:"max_bytes"`
  35. MaxGas int64 `json:"max_gas"`
  36. // Minimum time increment between consecutive blocks (in milliseconds)
  37. // Not exposed to the application.
  38. TimeIotaMs int64 `json:"time_iota_ms"`
  39. }
  40. // EvidenceParams determine how we handle evidence of malfeasance.
  41. type EvidenceParams struct {
  42. MaxAgeNumBlocks int64 `json:"max_age_num_blocks"` // only accept new evidence more recent than this
  43. MaxAgeDuration time.Duration `json:"max_age_duration"`
  44. }
  45. // ValidatorParams restrict the public key types validators can use.
  46. // NOTE: uses ABCI pubkey naming, not Amino names.
  47. type ValidatorParams struct {
  48. PubKeyTypes []string `json:"pub_key_types"`
  49. }
  50. // DefaultConsensusParams returns a default ConsensusParams.
  51. func DefaultConsensusParams() *ConsensusParams {
  52. return &ConsensusParams{
  53. DefaultBlockParams(),
  54. DefaultEvidenceParams(),
  55. DefaultValidatorParams(),
  56. }
  57. }
  58. // DefaultBlockParams returns a default BlockParams.
  59. func DefaultBlockParams() BlockParams {
  60. return BlockParams{
  61. MaxBytes: 22020096, // 21MB
  62. MaxGas: -1,
  63. TimeIotaMs: 1000, // 1s
  64. }
  65. }
  66. // DefaultEvidenceParams Params returns a default EvidenceParams.
  67. func DefaultEvidenceParams() EvidenceParams {
  68. return EvidenceParams{
  69. MaxAgeNumBlocks: 100000, // 27.8 hrs at 1block/s
  70. MaxAgeDuration: 48 * time.Hour,
  71. }
  72. }
  73. // DefaultValidatorParams returns a default ValidatorParams, which allows
  74. // only ed25519 pubkeys.
  75. func DefaultValidatorParams() ValidatorParams {
  76. return ValidatorParams{[]string{ABCIPubKeyTypeEd25519}}
  77. }
  78. func (params *ValidatorParams) IsValidPubkeyType(pubkeyType string) bool {
  79. for i := 0; i < len(params.PubKeyTypes); i++ {
  80. if params.PubKeyTypes[i] == pubkeyType {
  81. return true
  82. }
  83. }
  84. return false
  85. }
  86. // Validate validates the ConsensusParams to ensure all values are within their
  87. // allowed limits, and returns an error if they are not.
  88. func (params *ConsensusParams) Validate() error {
  89. if params.Block.MaxBytes <= 0 {
  90. return errors.Errorf("block.MaxBytes must be greater than 0. Got %d",
  91. params.Block.MaxBytes)
  92. }
  93. if params.Block.MaxBytes > MaxBlockSizeBytes {
  94. return errors.Errorf("block.MaxBytes is too big. %d > %d",
  95. params.Block.MaxBytes, MaxBlockSizeBytes)
  96. }
  97. if params.Block.MaxGas < -1 {
  98. return errors.Errorf("block.MaxGas must be greater or equal to -1. Got %d",
  99. params.Block.MaxGas)
  100. }
  101. if params.Block.TimeIotaMs <= 0 {
  102. return errors.Errorf("block.TimeIotaMs must be greater than 0. Got %v",
  103. params.Block.TimeIotaMs)
  104. }
  105. if params.Evidence.MaxAgeNumBlocks <= 0 {
  106. return errors.Errorf("evidenceParams.MaxAgeNumBlocks must be greater than 0. Got %d",
  107. params.Evidence.MaxAgeNumBlocks)
  108. }
  109. if params.Evidence.MaxAgeDuration <= 0 {
  110. return errors.Errorf("evidenceParams.MaxAgeDuration must be grater than 0 if provided, Got %v",
  111. params.Evidence.MaxAgeDuration)
  112. }
  113. if len(params.Validator.PubKeyTypes) == 0 {
  114. return errors.New("len(Validator.PubKeyTypes) must be greater than 0")
  115. }
  116. // Check if keyType is a known ABCIPubKeyType
  117. for i := 0; i < len(params.Validator.PubKeyTypes); i++ {
  118. keyType := params.Validator.PubKeyTypes[i]
  119. if _, ok := ABCIPubKeyTypesToAminoNames[keyType]; !ok {
  120. return errors.Errorf("params.Validator.PubKeyTypes[%d], %s, is an unknown pubkey type",
  121. i, keyType)
  122. }
  123. }
  124. return nil
  125. }
  126. // Hash returns a hash of a subset of the parameters to store in the block header.
  127. // Only the Block.MaxBytes and Block.MaxGas are included in the hash.
  128. // This allows the ConsensusParams to evolve more without breaking the block
  129. // protocol. No need for a Merkle tree here, just a small struct to hash.
  130. func (params *ConsensusParams) Hash() []byte {
  131. hasher := tmhash.New()
  132. bz := cdcEncode(HashedParams{
  133. params.Block.MaxBytes,
  134. params.Block.MaxGas,
  135. })
  136. if bz == nil {
  137. panic("cannot fail to encode ConsensusParams")
  138. }
  139. hasher.Write(bz)
  140. return hasher.Sum(nil)
  141. }
  142. func (params *ConsensusParams) Equals(params2 *ConsensusParams) bool {
  143. return params.Block == params2.Block &&
  144. params.Evidence == params2.Evidence &&
  145. tmstrings.StringSliceEqual(params.Validator.PubKeyTypes, params2.Validator.PubKeyTypes)
  146. }
  147. // Update returns a copy of the params with updates from the non-zero fields of p2.
  148. // NOTE: note: must not modify the original
  149. func (params ConsensusParams) Update(params2 *abci.ConsensusParams) ConsensusParams {
  150. res := params // explicit copy
  151. if params2 == nil {
  152. return res
  153. }
  154. // we must defensively consider any structs may be nil
  155. if params2.Block != nil {
  156. res.Block.MaxBytes = params2.Block.MaxBytes
  157. res.Block.MaxGas = params2.Block.MaxGas
  158. }
  159. if params2.Evidence != nil {
  160. res.Evidence.MaxAgeNumBlocks = params2.Evidence.MaxAgeNumBlocks
  161. res.Evidence.MaxAgeDuration = params2.Evidence.MaxAgeDuration
  162. }
  163. if params2.Validator != nil {
  164. // Copy params2.Validator.PubkeyTypes, and set result's value to the copy.
  165. // This avoids having to initialize the slice to 0 values, and then write to it again.
  166. res.Validator.PubKeyTypes = append([]string{}, params2.Validator.PubKeyTypes...)
  167. }
  168. return res
  169. }