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.

175 lines
5.4 KiB

7 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/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. }
  27. }
  28. // DefaultBlockParams returns a default BlockParams.
  29. func DefaultBlockParams() tmproto.BlockParams {
  30. return tmproto.BlockParams{
  31. MaxBytes: 22020096, // 21MB
  32. MaxGas: -1,
  33. TimeIotaMs: 1000, // 1s
  34. }
  35. }
  36. // DefaultEvidenceParams Params returns a default EvidenceParams.
  37. func DefaultEvidenceParams() tmproto.EvidenceParams {
  38. return tmproto.EvidenceParams{
  39. MaxAgeNumBlocks: 100000, // 27.8 hrs at 1block/s
  40. MaxAgeDuration: 48 * time.Hour,
  41. MaxNum: 50,
  42. }
  43. }
  44. // DefaultValidatorParams returns a default ValidatorParams, which allows
  45. // only ed25519 pubkeys.
  46. func DefaultValidatorParams() tmproto.ValidatorParams {
  47. return tmproto.ValidatorParams{
  48. PubKeyTypes: []string{ABCIPubKeyTypeEd25519},
  49. }
  50. }
  51. func IsValidPubkeyType(params tmproto.ValidatorParams, pubkeyType string) bool {
  52. for i := 0; i < len(params.PubKeyTypes); i++ {
  53. if params.PubKeyTypes[i] == pubkeyType {
  54. return true
  55. }
  56. }
  57. return false
  58. }
  59. // Validate validates the ConsensusParams to ensure all values are within their
  60. // allowed limits, and returns an error if they are not.
  61. func ValidateConsensusParams(params tmproto.ConsensusParams) error {
  62. if params.Block.MaxBytes <= 0 {
  63. return fmt.Errorf("block.MaxBytes must be greater than 0. Got %d",
  64. params.Block.MaxBytes)
  65. }
  66. if params.Block.MaxBytes > MaxBlockSizeBytes {
  67. return fmt.Errorf("block.MaxBytes is too big. %d > %d",
  68. params.Block.MaxBytes, MaxBlockSizeBytes)
  69. }
  70. if params.Block.MaxGas < -1 {
  71. return fmt.Errorf("block.MaxGas must be greater or equal to -1. Got %d",
  72. params.Block.MaxGas)
  73. }
  74. if params.Block.TimeIotaMs <= 0 {
  75. return fmt.Errorf("block.TimeIotaMs must be greater than 0. Got %v",
  76. params.Block.TimeIotaMs)
  77. }
  78. if params.Evidence.MaxAgeNumBlocks <= 0 {
  79. return fmt.Errorf("evidenceParams.MaxAgeNumBlocks must be greater than 0. Got %d",
  80. params.Evidence.MaxAgeNumBlocks)
  81. }
  82. if params.Evidence.MaxAgeDuration <= 0 {
  83. return fmt.Errorf("evidenceParams.MaxAgeDuration must be grater than 0 if provided, Got %v",
  84. params.Evidence.MaxAgeDuration)
  85. }
  86. if params.Evidence.MaxNum > MaxEvidencePerBlock {
  87. return fmt.Errorf("evidenceParams.MaxNumEvidence is greater than upper bound, %d > %d",
  88. params.Evidence.MaxNum, MaxEvidencePerBlock)
  89. }
  90. if int64(params.Evidence.MaxNum)*MaxEvidenceBytes > params.Block.MaxBytes {
  91. return fmt.Errorf("total possible evidence size is bigger than block.MaxBytes, %d > %d",
  92. int64(params.Evidence.MaxNum)*MaxEvidenceBytes, params.Block.MaxBytes)
  93. }
  94. if len(params.Validator.PubKeyTypes) == 0 {
  95. return errors.New("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 fmt.Errorf("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 HashConsensusParams(params tmproto.ConsensusParams) []byte {
  112. hasher := tmhash.New()
  113. hp := tmproto.HashedParams{
  114. BlockMaxBytes: params.Block.MaxBytes,
  115. BlockMaxGas: params.Block.MaxGas,
  116. }
  117. bz, err := hp.Marshal()
  118. if err != nil {
  119. panic(err)
  120. }
  121. hasher.Write(bz)
  122. return hasher.Sum(nil)
  123. }
  124. // Update returns a copy of the params with updates from the non-zero fields of p2.
  125. // NOTE: note: must not modify the original
  126. func UpdateConsensusParams(params tmproto.ConsensusParams, params2 *abci.ConsensusParams) tmproto.ConsensusParams {
  127. res := params // explicit copy
  128. if params2 == nil {
  129. return res
  130. }
  131. // we must defensively consider any structs may be nil
  132. if params2.Block != nil {
  133. res.Block.MaxBytes = params2.Block.MaxBytes
  134. res.Block.MaxGas = params2.Block.MaxGas
  135. }
  136. if params2.Evidence != nil {
  137. res.Evidence.MaxAgeNumBlocks = params2.Evidence.MaxAgeNumBlocks
  138. res.Evidence.MaxAgeDuration = params2.Evidence.MaxAgeDuration
  139. res.Evidence.MaxNum = params2.Evidence.MaxNum
  140. }
  141. if params2.Validator != nil {
  142. // Copy params2.Validator.PubkeyTypes, and set result's value to the copy.
  143. // This avoids having to initialize the slice to 0 values, and then write to it again.
  144. res.Validator.PubKeyTypes = append([]string{}, params2.Validator.PubKeyTypes...)
  145. }
  146. return res
  147. }