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.

185 lines
5.5 KiB

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