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.

115 lines
3.3 KiB

7 years ago
7 years ago
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. BlockSize `json:"block_size_params"`
  17. EvidenceParams `json:"evidence_params"`
  18. }
  19. // BlockSize contain limits on the block size.
  20. type BlockSize struct {
  21. MaxBytes int64 `json:"max_bytes"`
  22. MaxGas int64 `json:"max_gas"`
  23. }
  24. // EvidenceParams determine how we handle evidence of malfeasance
  25. type EvidenceParams struct {
  26. MaxAge int64 `json:"max_age"` // only accept new evidence more recent than this
  27. }
  28. // DefaultConsensusParams returns a default ConsensusParams.
  29. func DefaultConsensusParams() *ConsensusParams {
  30. return &ConsensusParams{
  31. DefaultBlockSize(),
  32. DefaultEvidenceParams(),
  33. }
  34. }
  35. // DefaultBlockSize returns a default BlockSize.
  36. func DefaultBlockSize() BlockSize {
  37. return BlockSize{
  38. MaxBytes: 22020096, // 21MB
  39. MaxGas: -1,
  40. }
  41. }
  42. // DefaultEvidenceParams Params returns a default EvidenceParams.
  43. func DefaultEvidenceParams() EvidenceParams {
  44. return EvidenceParams{
  45. MaxAge: 100000, // 27.8 hrs at 1block/s
  46. }
  47. }
  48. // Validate validates the ConsensusParams to ensure all values are within their
  49. // allowed limits, and returns an error if they are not.
  50. func (params *ConsensusParams) Validate() error {
  51. if params.BlockSize.MaxBytes <= 0 {
  52. return cmn.NewError("BlockSize.MaxBytes must be greater than 0. Got %d",
  53. params.BlockSize.MaxBytes)
  54. }
  55. if params.BlockSize.MaxBytes > MaxBlockSizeBytes {
  56. return cmn.NewError("BlockSize.MaxBytes is too big. %d > %d",
  57. params.BlockSize.MaxBytes, MaxBlockSizeBytes)
  58. }
  59. if params.BlockSize.MaxGas < -1 {
  60. return cmn.NewError("BlockSize.MaxGas must be greater or equal to -1. Got %d",
  61. params.BlockSize.MaxGas)
  62. }
  63. if params.EvidenceParams.MaxAge <= 0 {
  64. return cmn.NewError("EvidenceParams.MaxAge must be greater than 0. Got %d",
  65. params.EvidenceParams.MaxAge)
  66. }
  67. return nil
  68. }
  69. // Hash returns a hash of the parameters to store in the block header
  70. // No Merkle tree here, only three values are hashed here
  71. // thus benefit from saving space < drawbacks from proofs' overhead
  72. // Revisit this function if new fields are added to ConsensusParams
  73. func (params *ConsensusParams) Hash() []byte {
  74. hasher := tmhash.New()
  75. bz := cdcEncode(params)
  76. if bz == nil {
  77. panic("cannot fail to encode ConsensusParams")
  78. }
  79. hasher.Write(bz)
  80. return hasher.Sum(nil)
  81. }
  82. // Update returns a copy of the params with updates from the non-zero fields of p2.
  83. // NOTE: note: must not modify the original
  84. func (params ConsensusParams) Update(params2 *abci.ConsensusParams) ConsensusParams {
  85. res := params // explicit copy
  86. if params2 == nil {
  87. return res
  88. }
  89. // we must defensively consider any structs may be nil
  90. if params2.BlockSize != nil {
  91. res.BlockSize.MaxBytes = params2.BlockSize.MaxBytes
  92. res.BlockSize.MaxGas = params2.BlockSize.MaxGas
  93. }
  94. if params2.EvidenceParams != nil {
  95. res.EvidenceParams.MaxAge = params2.EvidenceParams.MaxAge
  96. }
  97. return res
  98. }