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.

196 lines
6.1 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. ProofTrialPeriod: 50000, // half MaxAgeNumBlocks
  44. }
  45. }
  46. // DefaultValidatorParams returns a default ValidatorParams, which allows
  47. // only ed25519 pubkeys.
  48. func DefaultValidatorParams() tmproto.ValidatorParams {
  49. return tmproto.ValidatorParams{
  50. PubKeyTypes: []string{ABCIPubKeyTypeEd25519},
  51. }
  52. }
  53. func DefaultVersionParams() tmproto.VersionParams {
  54. return tmproto.VersionParams{
  55. AppVersion: 0,
  56. }
  57. }
  58. func IsValidPubkeyType(params tmproto.ValidatorParams, pubkeyType string) bool {
  59. for i := 0; i < len(params.PubKeyTypes); i++ {
  60. if params.PubKeyTypes[i] == pubkeyType {
  61. return true
  62. }
  63. }
  64. return false
  65. }
  66. // Validate validates the ConsensusParams to ensure all values are within their
  67. // allowed limits, and returns an error if they are not.
  68. func ValidateConsensusParams(params tmproto.ConsensusParams) error {
  69. if params.Block.MaxBytes <= 0 {
  70. return fmt.Errorf("block.MaxBytes must be greater than 0. Got %d",
  71. params.Block.MaxBytes)
  72. }
  73. if params.Block.MaxBytes > MaxBlockSizeBytes {
  74. return fmt.Errorf("block.MaxBytes is too big. %d > %d",
  75. params.Block.MaxBytes, MaxBlockSizeBytes)
  76. }
  77. if params.Block.MaxGas < -1 {
  78. return fmt.Errorf("block.MaxGas must be greater or equal to -1. Got %d",
  79. params.Block.MaxGas)
  80. }
  81. if params.Block.TimeIotaMs <= 0 {
  82. return fmt.Errorf("block.TimeIotaMs must be greater than 0. Got %v",
  83. params.Block.TimeIotaMs)
  84. }
  85. if params.Evidence.MaxAgeNumBlocks <= 0 {
  86. return fmt.Errorf("evidenceParams.MaxAgeNumBlocks must be greater than 0. Got %d",
  87. params.Evidence.MaxAgeNumBlocks)
  88. }
  89. if params.Evidence.MaxAgeDuration <= 0 {
  90. return fmt.Errorf("evidenceParams.MaxAgeDuration must be grater than 0 if provided, Got %v",
  91. params.Evidence.MaxAgeDuration)
  92. }
  93. if params.Evidence.MaxNum > MaxEvidencePerBlock {
  94. return fmt.Errorf("evidenceParams.MaxNumEvidence is greater than upper bound, %d > %d",
  95. params.Evidence.MaxNum, MaxEvidencePerBlock)
  96. }
  97. if int64(params.Evidence.MaxNum)*MaxEvidenceBytes > params.Block.MaxBytes {
  98. return fmt.Errorf("total possible evidence size is bigger than block.MaxBytes, %d > %d",
  99. int64(params.Evidence.MaxNum)*MaxEvidenceBytes, params.Block.MaxBytes)
  100. }
  101. if params.Evidence.ProofTrialPeriod <= 0 {
  102. return fmt.Errorf("evidenceParams.ProofTrialPeriod must be grater than 0 if provided, Got %v",
  103. params.Evidence.ProofTrialPeriod)
  104. }
  105. if params.Evidence.ProofTrialPeriod >= params.Evidence.MaxAgeNumBlocks {
  106. return fmt.Errorf("evidenceParams.ProofTrialPeriod must be smaller than evidenceParams.MaxAgeNumBlocks, %d > %d",
  107. params.Evidence.ProofTrialPeriod, params.Evidence.MaxAgeDuration)
  108. }
  109. if len(params.Validator.PubKeyTypes) == 0 {
  110. return errors.New("len(Validator.PubKeyTypes) must be greater than 0")
  111. }
  112. // Check if keyType is a known ABCIPubKeyType
  113. for i := 0; i < len(params.Validator.PubKeyTypes); i++ {
  114. keyType := params.Validator.PubKeyTypes[i]
  115. if _, ok := ABCIPubKeyTypesToNames[keyType]; !ok {
  116. return fmt.Errorf("params.Validator.PubKeyTypes[%d], %s, is an unknown pubkey type",
  117. i, keyType)
  118. }
  119. }
  120. return nil
  121. }
  122. // Hash returns a hash of a subset of the parameters to store in the block header.
  123. // Only the Block.MaxBytes and Block.MaxGas are included in the hash.
  124. // This allows the ConsensusParams to evolve more without breaking the block
  125. // protocol. No need for a Merkle tree here, just a small struct to hash.
  126. func HashConsensusParams(params tmproto.ConsensusParams) []byte {
  127. hasher := tmhash.New()
  128. hp := tmproto.HashedParams{
  129. BlockMaxBytes: params.Block.MaxBytes,
  130. BlockMaxGas: params.Block.MaxGas,
  131. }
  132. bz, err := hp.Marshal()
  133. if err != nil {
  134. panic(err)
  135. }
  136. hasher.Write(bz)
  137. return hasher.Sum(nil)
  138. }
  139. // Update returns a copy of the params with updates from the non-zero fields of p2.
  140. // NOTE: note: must not modify the original
  141. func UpdateConsensusParams(params tmproto.ConsensusParams, params2 *abci.ConsensusParams) tmproto.ConsensusParams {
  142. res := params // explicit copy
  143. if params2 == nil {
  144. return res
  145. }
  146. // we must defensively consider any structs may be nil
  147. if params2.Block != nil {
  148. res.Block.MaxBytes = params2.Block.MaxBytes
  149. res.Block.MaxGas = params2.Block.MaxGas
  150. }
  151. if params2.Evidence != nil {
  152. res.Evidence.MaxAgeNumBlocks = params2.Evidence.MaxAgeNumBlocks
  153. res.Evidence.MaxAgeDuration = params2.Evidence.MaxAgeDuration
  154. res.Evidence.MaxNum = params2.Evidence.MaxNum
  155. }
  156. if params2.Validator != nil {
  157. // Copy params2.Validator.PubkeyTypes, and set result's value to the copy.
  158. // This avoids having to initialize the slice to 0 values, and then write to it again.
  159. res.Validator.PubKeyTypes = append([]string{}, params2.Validator.PubKeyTypes...)
  160. }
  161. if params2.Version != nil {
  162. res.Version.AppVersion = params2.Version.AppVersion
  163. }
  164. return res
  165. }