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.

142 lines
4.0 KiB

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