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.

150 lines
4.5 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. )
  11. // ConsensusParams contains consensus critical parameters
  12. // that determine the validity of blocks.
  13. type ConsensusParams struct {
  14. BlockSize `json:"block_size_params"`
  15. TxSize `json:"tx_size_params"`
  16. BlockGossip `json:"block_gossip_params"`
  17. EvidenceParams `json:"evidence_params"`
  18. }
  19. // BlockSize contain limits on the block size.
  20. type BlockSize struct {
  21. MaxBytes int `json:"max_txs_bytes"` // NOTE: must not be 0 nor greater than 100MB
  22. MaxGas int64 `json:"max_gas"`
  23. }
  24. // TxSize contain limits on the tx size.
  25. type TxSize struct {
  26. MaxBytes int `json:"max_bytes"`
  27. MaxGas int64 `json:"max_gas"`
  28. }
  29. // BlockGossip determine consensus critical elements of how blocks are gossiped
  30. type BlockGossip struct {
  31. BlockPartSizeBytes int `json:"block_part_size_bytes"` // NOTE: must not be 0
  32. }
  33. // EvidenceParams determine how we handle evidence of malfeasance
  34. type EvidenceParams struct {
  35. MaxAge int64 `json:"max_age"` // only accept new evidence more recent than this
  36. }
  37. // DefaultConsensusParams returns a default ConsensusParams.
  38. func DefaultConsensusParams() *ConsensusParams {
  39. return &ConsensusParams{
  40. DefaultBlockSize(),
  41. DefaultTxSize(),
  42. DefaultBlockGossip(),
  43. DefaultEvidenceParams(),
  44. }
  45. }
  46. // DefaultBlockSize returns a default BlockSize.
  47. func DefaultBlockSize() BlockSize {
  48. return BlockSize{
  49. MaxBytes: 22020096, // 21MB
  50. MaxGas: -1,
  51. }
  52. }
  53. // DefaultTxSize returns a default TxSize.
  54. func DefaultTxSize() TxSize {
  55. return TxSize{
  56. MaxBytes: 10240, // 10kB
  57. MaxGas: -1,
  58. }
  59. }
  60. // DefaultBlockGossip returns a default BlockGossip.
  61. func DefaultBlockGossip() BlockGossip {
  62. return BlockGossip{
  63. BlockPartSizeBytes: 65536, // 64kB,
  64. }
  65. }
  66. // DefaultEvidence Params returns a default EvidenceParams.
  67. func DefaultEvidenceParams() EvidenceParams {
  68. return EvidenceParams{
  69. MaxAge: 100000, // 27.8 hrs at 1block/s
  70. }
  71. }
  72. // Validate validates the ConsensusParams to ensure all values
  73. // are within their allowed limits, and returns an error if they are not.
  74. func (params *ConsensusParams) Validate() error {
  75. // ensure some values are greater than 0
  76. if params.BlockSize.MaxBytes <= 0 {
  77. return cmn.NewError("BlockSize.MaxBytes must be greater than 0. Got %d", params.BlockSize.MaxBytes)
  78. }
  79. if params.BlockGossip.BlockPartSizeBytes <= 0 {
  80. return cmn.NewError("BlockGossip.BlockPartSizeBytes must be greater than 0. Got %d", params.BlockGossip.BlockPartSizeBytes)
  81. }
  82. // ensure blocks aren't too big
  83. if params.BlockSize.MaxBytes > MaxBlockSizeBytes {
  84. return cmn.NewError("BlockSize.MaxBytes is too big. %d > %d",
  85. params.BlockSize.MaxBytes, MaxBlockSizeBytes)
  86. }
  87. return nil
  88. }
  89. // Hash returns a merkle hash of the parameters to store
  90. // in the block header
  91. func (params *ConsensusParams) Hash() []byte {
  92. return merkle.SimpleHashFromMap(map[string]merkle.Hasher{
  93. "block_gossip_part_size_bytes": aminoHasher(params.BlockGossip.BlockPartSizeBytes),
  94. "block_size_max_bytes": aminoHasher(params.BlockSize.MaxBytes),
  95. "block_size_max_gas": aminoHasher(params.BlockSize.MaxGas),
  96. "tx_size_max_bytes": aminoHasher(params.TxSize.MaxBytes),
  97. "tx_size_max_gas": aminoHasher(params.TxSize.MaxGas),
  98. })
  99. }
  100. // Update returns a copy of the params with updates from the non-zero fields of p2.
  101. // NOTE: note: must not modify the original
  102. func (params ConsensusParams) Update(params2 *abci.ConsensusParams) ConsensusParams {
  103. res := params // explicit copy
  104. if params2 == nil {
  105. return res
  106. }
  107. // we must defensively consider any structs may be nil
  108. // XXX: it's cast city over here. It's ok because we only do int32->int
  109. // but still, watch it champ.
  110. if params2.BlockSize != nil {
  111. if params2.BlockSize.MaxBytes > 0 {
  112. res.BlockSize.MaxBytes = int(params2.BlockSize.MaxBytes)
  113. }
  114. if params2.BlockSize.MaxGas > 0 {
  115. res.BlockSize.MaxGas = params2.BlockSize.MaxGas
  116. }
  117. }
  118. if params2.TxSize != nil {
  119. if params2.TxSize.MaxBytes > 0 {
  120. res.TxSize.MaxBytes = int(params2.TxSize.MaxBytes)
  121. }
  122. if params2.TxSize.MaxGas > 0 {
  123. res.TxSize.MaxGas = params2.TxSize.MaxGas
  124. }
  125. }
  126. if params2.BlockGossip != nil {
  127. if params2.BlockGossip.BlockPartSizeBytes > 0 {
  128. res.BlockGossip.BlockPartSizeBytes = int(params2.BlockGossip.BlockPartSizeBytes)
  129. }
  130. }
  131. return res
  132. }