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.

156 lines
4.5 KiB

7 years ago
  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. EvidenceParams `json:"evidence_params"`
  17. }
  18. // BlockSize contain limits on the block size.
  19. type BlockSize struct {
  20. MaxBytes int `json:"max_bytes"` // NOTE: must not be 0 nor greater than 100MB
  21. MaxTxs int `json:"max_txs"`
  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. MaxTxs: 100000,
  51. MaxGas: -1,
  52. }
  53. }
  54. // DefaultTxSize returns a default TxSize.
  55. func DefaultTxSize() TxSize {
  56. return TxSize{
  57. MaxBytes: 10240, // 10kB
  58. MaxGas: -1,
  59. }
  60. }
  61. // DefaultBlockGossip returns a default BlockGossip.
  62. func DefaultBlockGossip() BlockGossip {
  63. return BlockGossip{
  64. BlockPartSizeBytes: 65536, // 64kB,
  65. }
  66. }
  67. // DefaultEvidence Params returns a default EvidenceParams.
  68. func DefaultEvidenceParams() EvidenceParams {
  69. return EvidenceParams{
  70. MaxAge: 100000, // 27.8 hrs at 1block/s
  71. }
  72. }
  73. // Validate validates the ConsensusParams to ensure all values
  74. // are within their allowed limits, and returns an error if they are not.
  75. func (params *ConsensusParams) Validate() error {
  76. // ensure some values are greater than 0
  77. if params.BlockSize.MaxBytes <= 0 {
  78. return errors.Errorf("BlockSize.MaxBytes must be greater than 0. Got %d", params.BlockSize.MaxBytes)
  79. }
  80. if params.BlockGossip.BlockPartSizeBytes <= 0 {
  81. return errors.Errorf("BlockGossip.BlockPartSizeBytes must be greater than 0. Got %d", params.BlockGossip.BlockPartSizeBytes)
  82. }
  83. // ensure blocks aren't too big
  84. if params.BlockSize.MaxBytes > maxBlockSizeBytes {
  85. return errors.Errorf("BlockSize.MaxBytes is too big. %d > %d",
  86. params.BlockSize.MaxBytes, maxBlockSizeBytes)
  87. }
  88. return nil
  89. }
  90. // Hash returns a merkle hash of the parameters to store
  91. // in the block header
  92. func (params *ConsensusParams) Hash() []byte {
  93. return merkle.SimpleHashFromMap(map[string]merkle.Hasher{
  94. "block_gossip_part_size_bytes": wireHasher(params.BlockGossip.BlockPartSizeBytes),
  95. "block_size_max_bytes": wireHasher(params.BlockSize.MaxBytes),
  96. "block_size_max_gas": wireHasher(params.BlockSize.MaxGas),
  97. "block_size_max_txs": wireHasher(params.BlockSize.MaxTxs),
  98. "tx_size_max_bytes": wireHasher(params.TxSize.MaxBytes),
  99. "tx_size_max_gas": wireHasher(params.TxSize.MaxGas),
  100. })
  101. }
  102. // Update returns a copy of the params with updates from the non-zero fields of p2.
  103. // NOTE: note: must not modify the original
  104. func (params ConsensusParams) Update(params2 *abci.ConsensusParams) ConsensusParams {
  105. res := params // explicit copy
  106. if params2 == nil {
  107. return res
  108. }
  109. // we must defensively consider any structs may be nil
  110. // XXX: it's cast city over here. It's ok because we only do int32->int
  111. // but still, watch it champ.
  112. if params2.BlockSize != nil {
  113. if params2.BlockSize.MaxBytes > 0 {
  114. res.BlockSize.MaxBytes = int(params2.BlockSize.MaxBytes)
  115. }
  116. if params2.BlockSize.MaxTxs > 0 {
  117. res.BlockSize.MaxTxs = int(params2.BlockSize.MaxTxs)
  118. }
  119. if params2.BlockSize.MaxGas > 0 {
  120. res.BlockSize.MaxGas = params2.BlockSize.MaxGas
  121. }
  122. }
  123. if params2.TxSize != nil {
  124. if params2.TxSize.MaxBytes > 0 {
  125. res.TxSize.MaxBytes = int(params2.TxSize.MaxBytes)
  126. }
  127. if params2.TxSize.MaxGas > 0 {
  128. res.TxSize.MaxGas = params2.TxSize.MaxGas
  129. }
  130. }
  131. if params2.BlockGossip != nil {
  132. if params2.BlockGossip.BlockPartSizeBytes > 0 {
  133. res.BlockGossip.BlockPartSizeBytes = int(params2.BlockGossip.BlockPartSizeBytes)
  134. }
  135. }
  136. return res
  137. }