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.

110 lines
3.2 KiB

7 years ago
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/merkle"
  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 merkle hash of the parameters to store in the block header
  70. func (params *ConsensusParams) Hash() []byte {
  71. return merkle.SimpleHashFromMap(map[string]merkle.Hasher{
  72. "block_size_max_bytes": aminoHasher(params.BlockSize.MaxBytes),
  73. "block_size_max_gas": aminoHasher(params.BlockSize.MaxGas),
  74. "evidence_params_max_age": aminoHasher(params.EvidenceParams.MaxAge),
  75. })
  76. }
  77. // Update returns a copy of the params with updates from the non-zero fields of p2.
  78. // NOTE: note: must not modify the original
  79. func (params ConsensusParams) Update(params2 *abci.ConsensusParams) ConsensusParams {
  80. res := params // explicit copy
  81. if params2 == nil {
  82. return res
  83. }
  84. // we must defensively consider any structs may be nil
  85. if params2.BlockSize != nil {
  86. res.BlockSize.MaxBytes = params2.BlockSize.MaxBytes
  87. res.BlockSize.MaxGas = params2.BlockSize.MaxGas
  88. }
  89. if params2.EvidenceParams != nil {
  90. res.EvidenceParams.MaxAge = params2.EvidenceParams.MaxAge
  91. }
  92. return res
  93. }