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.

162 lines
4.9 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 names.
  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. func (params *ValidatorParams) IsValidPubkeyType(pubkeyType string) bool {
  61. for i := 0; i < len(params.PubKeyTypes); i++ {
  62. if params.PubKeyTypes[i] == pubkeyType {
  63. return true
  64. }
  65. }
  66. return false
  67. }
  68. // Validate validates the ConsensusParams to ensure all values are within their
  69. // allowed limits, and returns an error if they are not.
  70. func (params *ConsensusParams) Validate() error {
  71. if params.BlockSize.MaxBytes <= 0 {
  72. return cmn.NewError("BlockSize.MaxBytes must be greater than 0. Got %d",
  73. params.BlockSize.MaxBytes)
  74. }
  75. if params.BlockSize.MaxBytes > MaxBlockSizeBytes {
  76. return cmn.NewError("BlockSize.MaxBytes is too big. %d > %d",
  77. params.BlockSize.MaxBytes, MaxBlockSizeBytes)
  78. }
  79. if params.BlockSize.MaxGas < -1 {
  80. return cmn.NewError("BlockSize.MaxGas must be greater or equal to -1. Got %d",
  81. params.BlockSize.MaxGas)
  82. }
  83. if params.Evidence.MaxAge <= 0 {
  84. return cmn.NewError("EvidenceParams.MaxAge must be greater than 0. Got %d",
  85. params.Evidence.MaxAge)
  86. }
  87. if len(params.Validator.PubKeyTypes) == 0 {
  88. return cmn.NewError("len(Validator.PubKeyTypes) must be greater than 0")
  89. }
  90. // Check if keyType is a known ABCIPubKeyType
  91. for i := 0; i < len(params.Validator.PubKeyTypes); i++ {
  92. keyType := params.Validator.PubKeyTypes[i]
  93. if _, ok := ABCIPubKeyTypesToAminoNames[keyType]; !ok {
  94. return cmn.NewError("params.Validator.PubKeyTypes[%d], %s, is an unknown pubkey type",
  95. i, keyType)
  96. }
  97. }
  98. return nil
  99. }
  100. // Hash returns a hash of the parameters to store in the block header
  101. // No Merkle tree here, only three values are hashed here
  102. // thus benefit from saving space < drawbacks from proofs' overhead
  103. // Revisit this function if new fields are added to ConsensusParams
  104. func (params *ConsensusParams) Hash() []byte {
  105. hasher := tmhash.New()
  106. bz := cdcEncode(params)
  107. if bz == nil {
  108. panic("cannot fail to encode ConsensusParams")
  109. }
  110. hasher.Write(bz)
  111. return hasher.Sum(nil)
  112. }
  113. func (params *ConsensusParams) Equals(params2 *ConsensusParams) bool {
  114. return params.BlockSize == params2.BlockSize &&
  115. params.Evidence == params2.Evidence &&
  116. cmn.StringSliceEqual(params.Validator.PubKeyTypes, params2.Validator.PubKeyTypes)
  117. }
  118. // Update returns a copy of the params with updates from the non-zero fields of p2.
  119. // NOTE: note: must not modify the original
  120. func (params ConsensusParams) Update(params2 *abci.ConsensusParams) ConsensusParams {
  121. res := params // explicit copy
  122. if params2 == nil {
  123. return res
  124. }
  125. // we must defensively consider any structs may be nil
  126. if params2.BlockSize != nil {
  127. res.BlockSize.MaxBytes = params2.BlockSize.MaxBytes
  128. res.BlockSize.MaxGas = params2.BlockSize.MaxGas
  129. }
  130. if params2.Evidence != nil {
  131. res.Evidence.MaxAge = params2.Evidence.MaxAge
  132. }
  133. if params2.Validator != nil {
  134. // Copy params2.Validator.PubkeyTypes, and set result's value to the copy.
  135. // This avoids having to initialize the slice to 0 values, and then write to it again.
  136. res.Validator.PubKeyTypes = append([]string{}, params2.Validator.PubKeyTypes...)
  137. }
  138. return res
  139. }