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.

163 lines
4.7 KiB

7 years ago
7 years ago
7 years ago
  1. package types
  2. import (
  3. abci "github.com/tendermint/tendermint/abci/types"
  4. "github.com/tendermint/tendermint/crypto/tmhash"
  5. cmn "github.com/tendermint/tendermint/libs/common"
  6. )
  7. const (
  8. // MaxBlockSizeBytes is the maximum permitted size of the blocks.
  9. MaxBlockSizeBytes = 104857600 // 100MB
  10. // BlockPartSizeBytes is the size of one block part.
  11. BlockPartSizeBytes = 65536 // 64kB
  12. )
  13. // ConsensusParams contains consensus critical parameters that determine the
  14. // validity of blocks.
  15. type ConsensusParams struct {
  16. BlockSize BlockSizeParams `json:"block_size"`
  17. Evidence EvidenceParams `json:"evidence"`
  18. Validator ValidatorParams `json:"validator"`
  19. }
  20. // BlockSizeParams define limits on the block size.
  21. type BlockSizeParams struct {
  22. MaxBytes int64 `json:"max_bytes"`
  23. MaxGas int64 `json:"max_gas"`
  24. }
  25. // EvidenceParams determine how we handle evidence of malfeasance
  26. type EvidenceParams struct {
  27. MaxAge int64 `json:"max_age"` // only accept new evidence more recent than this
  28. }
  29. // ValidatorParams restrict the public key types validators can use.
  30. // NOTE: uses ABCI pubkey naming, not Amino routes.
  31. type ValidatorParams struct {
  32. PubKeyTypes []string `json:"pub_key_types"`
  33. }
  34. // DefaultConsensusParams returns a default ConsensusParams.
  35. func DefaultConsensusParams() *ConsensusParams {
  36. return &ConsensusParams{
  37. DefaultBlockSizeParams(),
  38. DefaultEvidenceParams(),
  39. DefaultValidatorParams(),
  40. }
  41. }
  42. // DefaultBlockSizeParams returns a default BlockSizeParams.
  43. func DefaultBlockSizeParams() BlockSizeParams {
  44. return BlockSizeParams{
  45. MaxBytes: 22020096, // 21MB
  46. MaxGas: -1,
  47. }
  48. }
  49. // DefaultEvidenceParams Params returns a default EvidenceParams.
  50. func DefaultEvidenceParams() EvidenceParams {
  51. return EvidenceParams{
  52. MaxAge: 100000, // 27.8 hrs at 1block/s
  53. }
  54. }
  55. // DefaultValidatorParams returns a default ValidatorParams, which allows
  56. // only ed25519 pubkeys.
  57. func DefaultValidatorParams() ValidatorParams {
  58. return ValidatorParams{[]string{ABCIPubKeyTypeEd25519}}
  59. }
  60. // Validate validates the ConsensusParams to ensure all values are within their
  61. // allowed limits, and returns an error if they are not.
  62. func (params *ConsensusParams) Validate() error {
  63. if params.BlockSize.MaxBytes <= 0 {
  64. return cmn.NewError("BlockSize.MaxBytes must be greater than 0. Got %d",
  65. params.BlockSize.MaxBytes)
  66. }
  67. if params.BlockSize.MaxBytes > MaxBlockSizeBytes {
  68. return cmn.NewError("BlockSize.MaxBytes is too big. %d > %d",
  69. params.BlockSize.MaxBytes, MaxBlockSizeBytes)
  70. }
  71. if params.BlockSize.MaxGas < -1 {
  72. return cmn.NewError("BlockSize.MaxGas must be greater or equal to -1. Got %d",
  73. params.BlockSize.MaxGas)
  74. }
  75. if params.Evidence.MaxAge <= 0 {
  76. return cmn.NewError("EvidenceParams.MaxAge must be greater than 0. Got %d",
  77. params.Evidence.MaxAge)
  78. }
  79. if len(params.Validator.PubKeyTypes) == 0 {
  80. return cmn.NewError("len(Validator.PubKeyTypes) must be greater than 0")
  81. }
  82. // Check if keyType is a known ABCIPubKeyType
  83. for i := 0; i < len(params.Validator.PubKeyTypes); i++ {
  84. keyType := params.Validator.PubKeyTypes[i]
  85. if _, ok := ABCIPubKeyTypesToAminoRoutes[keyType]; !ok {
  86. return cmn.NewError("params.Validator.PubKeyTypes[%d], %s, is an unknown pubkey type",
  87. i, keyType)
  88. }
  89. }
  90. return nil
  91. }
  92. // Hash returns a hash of the parameters to store in the block header
  93. // No Merkle tree here, only three values are hashed here
  94. // thus benefit from saving space < drawbacks from proofs' overhead
  95. // Revisit this function if new fields are added to ConsensusParams
  96. func (params *ConsensusParams) Hash() []byte {
  97. hasher := tmhash.New()
  98. bz := cdcEncode(params)
  99. if bz == nil {
  100. panic("cannot fail to encode ConsensusParams")
  101. }
  102. hasher.Write(bz)
  103. return hasher.Sum(nil)
  104. }
  105. func (params *ConsensusParams) Equals(params2 *ConsensusParams) bool {
  106. return params.BlockSize == params2.BlockSize &&
  107. params.Evidence == params2.Evidence &&
  108. stringSliceEqual(params.Validator.PubKeyTypes, params2.Validator.PubKeyTypes)
  109. }
  110. func stringSliceEqual(a, b []string) bool {
  111. if len(a) != len(b) {
  112. return false
  113. }
  114. for i := 0; i < len(a); i++ {
  115. if a[i] != b[i] {
  116. return false
  117. }
  118. }
  119. return true
  120. }
  121. // Update returns a copy of the params with updates from the non-zero fields of p2.
  122. // NOTE: note: must not modify the original
  123. func (params ConsensusParams) Update(params2 *abci.ConsensusParams) ConsensusParams {
  124. res := params // explicit copy
  125. if params2 == nil {
  126. return res
  127. }
  128. // we must defensively consider any structs may be nil
  129. if params2.BlockSize != nil {
  130. res.BlockSize.MaxBytes = params2.BlockSize.MaxBytes
  131. res.BlockSize.MaxGas = params2.BlockSize.MaxGas
  132. }
  133. if params2.Evidence != nil {
  134. res.Evidence.MaxAge = params2.Evidence.MaxAge
  135. }
  136. if params2.Validator != nil {
  137. res.Validator.PubKeyTypes = params2.Validator.PubKeyTypes
  138. }
  139. return res
  140. }