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.

188 lines
5.6 KiB

6 years ago
  1. package types
  2. import (
  3. "errors"
  4. "fmt"
  5. "time"
  6. abci "github.com/tendermint/tendermint/abci/types"
  7. "github.com/tendermint/tendermint/crypto/tmhash"
  8. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  9. )
  10. const (
  11. // MaxBlockSizeBytes is the maximum permitted size of the blocks.
  12. MaxBlockSizeBytes = 104857600 // 100MB
  13. // BlockPartSizeBytes is the size of one block part.
  14. BlockPartSizeBytes uint32 = 65536 // 64kB
  15. // MaxBlockPartsCount is the maximum number of block parts.
  16. MaxBlockPartsCount = (MaxBlockSizeBytes / BlockPartSizeBytes) + 1
  17. // Restrict the upper bound of the amount of evidence (uses uint16 for safe conversion)
  18. MaxEvidencePerBlock = 65535
  19. )
  20. // DefaultConsensusParams returns a default ConsensusParams.
  21. func DefaultConsensusParams() *tmproto.ConsensusParams {
  22. return &tmproto.ConsensusParams{
  23. Block: DefaultBlockParams(),
  24. Evidence: DefaultEvidenceParams(),
  25. Validator: DefaultValidatorParams(),
  26. Version: DefaultVersionParams(),
  27. }
  28. }
  29. // DefaultBlockParams returns a default BlockParams.
  30. func DefaultBlockParams() tmproto.BlockParams {
  31. return tmproto.BlockParams{
  32. MaxBytes: 22020096, // 21MB
  33. MaxGas: -1,
  34. TimeIotaMs: 1000, // 1s
  35. }
  36. }
  37. // DefaultEvidenceParams returns a default EvidenceParams.
  38. func DefaultEvidenceParams() tmproto.EvidenceParams {
  39. return tmproto.EvidenceParams{
  40. MaxAgeNumBlocks: 100000, // 27.8 hrs at 1block/s
  41. MaxAgeDuration: 48 * time.Hour,
  42. MaxNum: 50,
  43. }
  44. }
  45. // DefaultValidatorParams returns a default ValidatorParams, which allows
  46. // only ed25519 pubkeys.
  47. func DefaultValidatorParams() tmproto.ValidatorParams {
  48. return tmproto.ValidatorParams{
  49. PubKeyTypes: []string{ABCIPubKeyTypeEd25519},
  50. }
  51. }
  52. func DefaultVersionParams() tmproto.VersionParams {
  53. return tmproto.VersionParams{
  54. AppVersion: 0,
  55. }
  56. }
  57. func IsValidPubkeyType(params tmproto.ValidatorParams, pubkeyType string) bool {
  58. for i := 0; i < len(params.PubKeyTypes); i++ {
  59. if params.PubKeyTypes[i] == pubkeyType {
  60. return true
  61. }
  62. }
  63. return false
  64. }
  65. // Validate validates the ConsensusParams to ensure all values are within their
  66. // allowed limits, and returns an error if they are not.
  67. func ValidateConsensusParams(params tmproto.ConsensusParams) error {
  68. if params.Block.MaxBytes <= 0 {
  69. return fmt.Errorf("block.MaxBytes must be greater than 0. Got %d",
  70. params.Block.MaxBytes)
  71. }
  72. if params.Block.MaxBytes > MaxBlockSizeBytes {
  73. return fmt.Errorf("block.MaxBytes is too big. %d > %d",
  74. params.Block.MaxBytes, MaxBlockSizeBytes)
  75. }
  76. if params.Block.MaxGas < -1 {
  77. return fmt.Errorf("block.MaxGas must be greater or equal to -1. Got %d",
  78. params.Block.MaxGas)
  79. }
  80. if params.Block.TimeIotaMs <= 0 {
  81. return fmt.Errorf("block.TimeIotaMs must be greater than 0. Got %v",
  82. params.Block.TimeIotaMs)
  83. }
  84. if params.Evidence.MaxAgeNumBlocks <= 0 {
  85. return fmt.Errorf("evidenceParams.MaxAgeNumBlocks must be greater than 0. Got %d",
  86. params.Evidence.MaxAgeNumBlocks)
  87. }
  88. if params.Evidence.MaxAgeDuration <= 0 {
  89. return fmt.Errorf("evidenceParams.MaxAgeDuration must be grater than 0 if provided, Got %v",
  90. params.Evidence.MaxAgeDuration)
  91. }
  92. if params.Evidence.MaxNum > MaxEvidencePerBlock {
  93. return fmt.Errorf("evidenceParams.MaxNumEvidence is greater than upper bound, %d > %d",
  94. params.Evidence.MaxNum, MaxEvidencePerBlock)
  95. }
  96. if int64(params.Evidence.MaxNum)*MaxEvidenceBytes > params.Block.MaxBytes {
  97. return fmt.Errorf("total possible evidence size is bigger than block.MaxBytes, %d > %d",
  98. int64(params.Evidence.MaxNum)*MaxEvidenceBytes, params.Block.MaxBytes)
  99. }
  100. if len(params.Validator.PubKeyTypes) == 0 {
  101. return errors.New("len(Validator.PubKeyTypes) must be greater than 0")
  102. }
  103. // Check if keyType is a known ABCIPubKeyType
  104. for i := 0; i < len(params.Validator.PubKeyTypes); i++ {
  105. keyType := params.Validator.PubKeyTypes[i]
  106. if _, ok := ABCIPubKeyTypesToNames[keyType]; !ok {
  107. return fmt.Errorf("params.Validator.PubKeyTypes[%d], %s, is an unknown pubkey type",
  108. i, keyType)
  109. }
  110. }
  111. return nil
  112. }
  113. // Hash returns a hash of a subset of the parameters to store in the block header.
  114. // Only the Block.MaxBytes and Block.MaxGas are included in the hash.
  115. // This allows the ConsensusParams to evolve more without breaking the block
  116. // protocol. No need for a Merkle tree here, just a small struct to hash.
  117. func HashConsensusParams(params tmproto.ConsensusParams) []byte {
  118. hasher := tmhash.New()
  119. hp := tmproto.HashedParams{
  120. BlockMaxBytes: params.Block.MaxBytes,
  121. BlockMaxGas: params.Block.MaxGas,
  122. }
  123. bz, err := hp.Marshal()
  124. if err != nil {
  125. panic(err)
  126. }
  127. _, err = hasher.Write(bz)
  128. if err != nil {
  129. panic(err)
  130. }
  131. return hasher.Sum(nil)
  132. }
  133. // Update returns a copy of the params with updates from the non-zero fields of p2.
  134. // NOTE: note: must not modify the original
  135. func UpdateConsensusParams(params tmproto.ConsensusParams, params2 *abci.ConsensusParams) tmproto.ConsensusParams {
  136. res := params // explicit copy
  137. if params2 == nil {
  138. return res
  139. }
  140. // we must defensively consider any structs may be nil
  141. if params2.Block != nil {
  142. res.Block.MaxBytes = params2.Block.MaxBytes
  143. res.Block.MaxGas = params2.Block.MaxGas
  144. }
  145. if params2.Evidence != nil {
  146. res.Evidence.MaxAgeNumBlocks = params2.Evidence.MaxAgeNumBlocks
  147. res.Evidence.MaxAgeDuration = params2.Evidence.MaxAgeDuration
  148. res.Evidence.MaxNum = params2.Evidence.MaxNum
  149. }
  150. if params2.Validator != nil {
  151. // Copy params2.Validator.PubkeyTypes, and set result's value to the copy.
  152. // This avoids having to initialize the slice to 0 values, and then write to it again.
  153. res.Validator.PubKeyTypes = append([]string{}, params2.Validator.PubKeyTypes...)
  154. }
  155. if params2.Version != nil {
  156. res.Version.AppVersion = params2.Version.AppVersion
  157. }
  158. return res
  159. }