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.

186 lines
5.8 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 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. ProofTrialPeriod: 50000, // half MaxAgeNumBlocks
  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 IsValidPubkeyType(params tmproto.ValidatorParams, pubkeyType string) bool {
  53. for i := 0; i < len(params.PubKeyTypes); i++ {
  54. if params.PubKeyTypes[i] == pubkeyType {
  55. return true
  56. }
  57. }
  58. return false
  59. }
  60. // Validate validates the ConsensusParams to ensure all values are within their
  61. // allowed limits, and returns an error if they are not.
  62. func ValidateConsensusParams(params tmproto.ConsensusParams) error {
  63. if params.Block.MaxBytes <= 0 {
  64. return fmt.Errorf("block.MaxBytes must be greater than 0. Got %d",
  65. params.Block.MaxBytes)
  66. }
  67. if params.Block.MaxBytes > MaxBlockSizeBytes {
  68. return fmt.Errorf("block.MaxBytes is too big. %d > %d",
  69. params.Block.MaxBytes, MaxBlockSizeBytes)
  70. }
  71. if params.Block.MaxGas < -1 {
  72. return fmt.Errorf("block.MaxGas must be greater or equal to -1. Got %d",
  73. params.Block.MaxGas)
  74. }
  75. if params.Block.TimeIotaMs <= 0 {
  76. return fmt.Errorf("block.TimeIotaMs must be greater than 0. Got %v",
  77. params.Block.TimeIotaMs)
  78. }
  79. if params.Evidence.MaxAgeNumBlocks <= 0 {
  80. return fmt.Errorf("evidenceParams.MaxAgeNumBlocks must be greater than 0. Got %d",
  81. params.Evidence.MaxAgeNumBlocks)
  82. }
  83. if params.Evidence.MaxAgeDuration <= 0 {
  84. return fmt.Errorf("evidenceParams.MaxAgeDuration must be grater than 0 if provided, Got %v",
  85. params.Evidence.MaxAgeDuration)
  86. }
  87. if params.Evidence.MaxNum > MaxEvidencePerBlock {
  88. return fmt.Errorf("evidenceParams.MaxNumEvidence is greater than upper bound, %d > %d",
  89. params.Evidence.MaxNum, MaxEvidencePerBlock)
  90. }
  91. if int64(params.Evidence.MaxNum)*MaxEvidenceBytes > params.Block.MaxBytes {
  92. return fmt.Errorf("total possible evidence size is bigger than block.MaxBytes, %d > %d",
  93. int64(params.Evidence.MaxNum)*MaxEvidenceBytes, params.Block.MaxBytes)
  94. }
  95. if params.Evidence.ProofTrialPeriod <= 0 {
  96. return fmt.Errorf("evidenceParams.ProofTrialPeriod must be grater than 0 if provided, Got %v",
  97. params.Evidence.ProofTrialPeriod)
  98. }
  99. if params.Evidence.ProofTrialPeriod >= params.Evidence.MaxAgeNumBlocks {
  100. return fmt.Errorf("evidenceParams.ProofTrialPeriod must be smaller than evidenceParams.MaxAgeNumBlocks, %d > %d",
  101. params.Evidence.ProofTrialPeriod, params.Evidence.MaxAgeDuration)
  102. }
  103. if len(params.Validator.PubKeyTypes) == 0 {
  104. return errors.New("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 := ABCIPubKeyTypesToNames[keyType]; !ok {
  110. return fmt.Errorf("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 HashConsensusParams(params tmproto.ConsensusParams) []byte {
  121. hasher := tmhash.New()
  122. hp := tmproto.HashedParams{
  123. BlockMaxBytes: params.Block.MaxBytes,
  124. BlockMaxGas: params.Block.MaxGas,
  125. }
  126. bz, err := hp.Marshal()
  127. if err != nil {
  128. panic(err)
  129. }
  130. hasher.Write(bz)
  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. return res
  156. }