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.

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