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.

183 lines
5.5 KiB

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