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