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