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.

102 lines
3.1 KiB

  1. package types
  2. import (
  3. "github.com/pkg/errors"
  4. "github.com/tendermint/tmlibs/merkle"
  5. )
  6. const (
  7. maxBlockSizeBytes = 104857600 // 100MB
  8. )
  9. // ConsensusParams contains consensus critical parameters
  10. // that determine the validity of blocks.
  11. type ConsensusParams struct {
  12. BlockSizeParams `json:"block_size_params"`
  13. TxSizeParams `json:"tx_size_params"`
  14. BlockGossipParams `json:"block_gossip_params"`
  15. }
  16. // BlockSizeParams contain limits on the block size.
  17. type BlockSizeParams struct {
  18. MaxBytes int `json:"max_bytes"` // NOTE: must not be 0 nor greater than 100MB
  19. MaxTxs int `json:"max_txs"`
  20. MaxGas int `json:"max_gas"`
  21. }
  22. // TxSizeParams contain limits on the tx size.
  23. type TxSizeParams struct {
  24. MaxBytes int `json:"max_bytes"`
  25. MaxGas int `json:"max_gas"`
  26. }
  27. // BlockGossipParams determine consensus critical elements of how blocks are gossiped
  28. type BlockGossipParams struct {
  29. BlockPartSizeBytes int `json:"block_part_size_bytes"` // NOTE: must not be 0
  30. }
  31. // DefaultConsensusParams returns a default ConsensusParams.
  32. func DefaultConsensusParams() *ConsensusParams {
  33. return &ConsensusParams{
  34. DefaultBlockSizeParams(),
  35. DefaultTxSizeParams(),
  36. DefaultBlockGossipParams(),
  37. }
  38. }
  39. // DefaultBlockSizeParams returns a default BlockSizeParams.
  40. func DefaultBlockSizeParams() BlockSizeParams {
  41. return BlockSizeParams{
  42. MaxBytes: 22020096, // 21MB
  43. MaxTxs: 100000,
  44. MaxGas: -1,
  45. }
  46. }
  47. // DefaultTxSizeParams returns a default TxSizeParams.
  48. func DefaultTxSizeParams() TxSizeParams {
  49. return TxSizeParams{
  50. MaxBytes: 10240, // 10kB
  51. MaxGas: -1,
  52. }
  53. }
  54. // DefaultBlockGossipParams returns a default BlockGossipParams.
  55. func DefaultBlockGossipParams() BlockGossipParams {
  56. return BlockGossipParams{
  57. BlockPartSizeBytes: 65536, // 64kB,
  58. }
  59. }
  60. // Validate validates the ConsensusParams to ensure all values
  61. // are within their allowed limits, and returns an error if they are not.
  62. func (params *ConsensusParams) Validate() error {
  63. // ensure some values are greater than 0
  64. if params.BlockSizeParams.MaxBytes <= 0 {
  65. return errors.Errorf("BlockSizeParams.MaxBytes must be greater than 0. Got %d", params.BlockSizeParams.MaxBytes)
  66. }
  67. if params.BlockGossipParams.BlockPartSizeBytes <= 0 {
  68. return errors.Errorf("BlockGossipParams.BlockPartSizeBytes must be greater than 0. Got %d", params.BlockGossipParams.BlockPartSizeBytes)
  69. }
  70. // ensure blocks aren't too big
  71. if params.BlockSizeParams.MaxBytes > maxBlockSizeBytes {
  72. return errors.Errorf("BlockSizeParams.MaxBytes is too big. %d > %d",
  73. params.BlockSizeParams.MaxBytes, maxBlockSizeBytes)
  74. }
  75. return nil
  76. }
  77. // Hash returns a merkle hash of the parameters to store
  78. // in the block header
  79. func (params *ConsensusParams) Hash() []byte {
  80. return merkle.SimpleHashFromMap(map[string]interface{}{
  81. "block_gossip_part_size_bytes": params.BlockGossipParams.BlockPartSizeBytes,
  82. "block_size_max_bytes": params.BlockSizeParams.MaxBytes,
  83. "block_size_max_gas": params.BlockSizeParams.MaxGas,
  84. "block_size_max_txs": params.BlockSizeParams.MaxTxs,
  85. "tx_size_max_bytes": params.TxSizeParams.MaxBytes,
  86. "tx_size_max_gas": params.TxSizeParams.MaxGas,
  87. })
  88. }