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.

87 lines
2.5 KiB

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