github.com/okex/exchain@v1.8.0/libs/tendermint/types/params.go (about)

     1  package types
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/pkg/errors"
     7  
     8  	abci "github.com/okex/exchain/libs/tendermint/abci/types"
     9  	"github.com/okex/exchain/libs/tendermint/crypto/tmhash"
    10  	tmstrings "github.com/okex/exchain/libs/tendermint/libs/strings"
    11  )
    12  
    13  const (
    14  	// MaxBlockSizeBytes is the maximum permitted size of the blocks.
    15  	MaxBlockSizeBytes = 104857600 // 100MB
    16  
    17  	// TimeoutCommit is set for the stable of blockTime
    18  	TimeoutCommit = 3800 // 3.8s
    19  )
    20  
    21  var (
    22  	// BlockPartSizeBytes is the size of one block part.
    23  	BlockPartSizeBytes = 65536 // 64kB
    24  
    25  	// MaxBlockPartsCount is the maximum number of block parts.
    26  	MaxBlockPartsCount = (MaxBlockSizeBytes / BlockPartSizeBytes) + 1
    27  )
    28  
    29  // ConsensusParams contains consensus critical parameters that determine the
    30  // validity of blocks.
    31  type ConsensusParams struct {
    32  	Block     BlockParams     `json:"block"`
    33  	Evidence  EvidenceParams  `json:"evidence"`
    34  	Validator ValidatorParams `json:"validator"`
    35  }
    36  
    37  // HashedParams is a subset of ConsensusParams.
    38  // It is amino encoded and hashed into
    39  // the Header.ConsensusHash.
    40  type HashedParams struct {
    41  	BlockMaxBytes int64
    42  	BlockMaxGas   int64
    43  }
    44  
    45  // BlockParams define limits on the block size and gas plus minimum time
    46  // between blocks.
    47  type BlockParams struct {
    48  	MaxBytes int64 `json:"max_bytes"`
    49  	MaxGas   int64 `json:"max_gas"`
    50  	// Minimum time increment between consecutive blocks (in milliseconds)
    51  	// Not exposed to the application.
    52  	TimeIotaMs int64 `json:"time_iota_ms"`
    53  }
    54  
    55  // EvidenceParams determine how we handle evidence of malfeasance.
    56  type EvidenceParams struct {
    57  	MaxAgeNumBlocks int64         `json:"max_age_num_blocks"` // only accept new evidence more recent than this
    58  	MaxAgeDuration  time.Duration `json:"max_age_duration"`
    59  }
    60  
    61  // ValidatorParams restrict the public key types validators can use.
    62  // NOTE: uses ABCI pubkey naming, not Amino names.
    63  type ValidatorParams struct {
    64  	PubKeyTypes []string `json:"pub_key_types"`
    65  }
    66  
    67  func UpdateBlockPartSizeBytes(size int) {
    68  	if size < 32 {
    69  		size = 32
    70  	}
    71  	BlockPartSizeBytes = size
    72  	MaxBlockPartsCount = (MaxBlockSizeBytes / BlockPartSizeBytes) + 1
    73  }
    74  
    75  // DefaultConsensusParams returns a default ConsensusParams.
    76  func DefaultConsensusParams() *ConsensusParams {
    77  	return &ConsensusParams{
    78  		DefaultBlockParams(),
    79  		DefaultEvidenceParams(),
    80  		DefaultValidatorParams(),
    81  	}
    82  }
    83  
    84  // DefaultBlockParams returns a default BlockParams.
    85  func DefaultBlockParams() BlockParams {
    86  	return BlockParams{
    87  		MaxBytes:   22020096, // 21MB
    88  		MaxGas:     -1,
    89  		TimeIotaMs: 1000, // 1s
    90  	}
    91  }
    92  
    93  // DefaultEvidenceParams Params returns a default EvidenceParams.
    94  func DefaultEvidenceParams() EvidenceParams {
    95  	return EvidenceParams{
    96  		MaxAgeNumBlocks: 100000, // 27.8 hrs at 1block/s
    97  		MaxAgeDuration:  48 * time.Hour,
    98  	}
    99  }
   100  
   101  // DefaultValidatorParams returns a default ValidatorParams, which allows
   102  // only ed25519 pubkeys.
   103  func DefaultValidatorParams() ValidatorParams {
   104  	return ValidatorParams{[]string{ABCIPubKeyTypeEd25519}}
   105  }
   106  
   107  func (params *ValidatorParams) IsValidPubkeyType(pubkeyType string) bool {
   108  	for i := 0; i < len(params.PubKeyTypes); i++ {
   109  		if params.PubKeyTypes[i] == pubkeyType {
   110  			return true
   111  		}
   112  	}
   113  	return false
   114  }
   115  
   116  // Validate validates the ConsensusParams to ensure all values are within their
   117  // allowed limits, and returns an error if they are not.
   118  func (params *ConsensusParams) Validate() error {
   119  	if params.Block.MaxBytes <= 0 {
   120  		return errors.Errorf("block.MaxBytes must be greater than 0. Got %d",
   121  			params.Block.MaxBytes)
   122  	}
   123  	if params.Block.MaxBytes > MaxBlockSizeBytes {
   124  		return errors.Errorf("block.MaxBytes is too big. %d > %d",
   125  			params.Block.MaxBytes, MaxBlockSizeBytes)
   126  	}
   127  
   128  	if params.Block.MaxGas < -1 {
   129  		return errors.Errorf("block.MaxGas must be greater or equal to -1. Got %d",
   130  			params.Block.MaxGas)
   131  	}
   132  
   133  	if params.Block.TimeIotaMs <= 0 {
   134  		return errors.Errorf("block.TimeIotaMs must be greater than 0. Got %v",
   135  			params.Block.TimeIotaMs)
   136  	}
   137  
   138  	if params.Evidence.MaxAgeNumBlocks <= 0 {
   139  		return errors.Errorf("evidenceParams.MaxAgeNumBlocks must be greater than 0. Got %d",
   140  			params.Evidence.MaxAgeNumBlocks)
   141  	}
   142  
   143  	if params.Evidence.MaxAgeDuration <= 0 {
   144  		return errors.Errorf("evidenceParams.MaxAgeDuration must be grater than 0 if provided, Got %v",
   145  			params.Evidence.MaxAgeDuration)
   146  	}
   147  
   148  	if len(params.Validator.PubKeyTypes) == 0 {
   149  		return errors.New("len(Validator.PubKeyTypes) must be greater than 0")
   150  	}
   151  
   152  	// Check if keyType is a known ABCIPubKeyType
   153  	for i := 0; i < len(params.Validator.PubKeyTypes); i++ {
   154  		keyType := params.Validator.PubKeyTypes[i]
   155  		if _, ok := ABCIPubKeyTypesToAminoNames[keyType]; !ok {
   156  			return errors.Errorf("params.Validator.PubKeyTypes[%d], %s, is an unknown pubkey type",
   157  				i, keyType)
   158  		}
   159  	}
   160  
   161  	return nil
   162  }
   163  
   164  // Hash returns a hash of a subset of the parameters to store in the block header.
   165  // Only the Block.MaxBytes and Block.MaxGas are included in the hash.
   166  // This allows the ConsensusParams to evolve more without breaking the block
   167  // protocol. No need for a Merkle tree here, just a small struct to hash.
   168  func (params *ConsensusParams) Hash() []byte {
   169  	hasher := tmhash.New()
   170  	bz := cdcEncode(HashedParams{
   171  		params.Block.MaxBytes,
   172  		params.Block.MaxGas,
   173  	})
   174  	if bz == nil {
   175  		panic("cannot fail to encode ConsensusParams")
   176  	}
   177  	hasher.Write(bz)
   178  	return hasher.Sum(nil)
   179  }
   180  
   181  func (params *ConsensusParams) Equals(params2 *ConsensusParams) bool {
   182  	return params.Block == params2.Block &&
   183  		params.Evidence == params2.Evidence &&
   184  		tmstrings.StringSliceEqual(params.Validator.PubKeyTypes, params2.Validator.PubKeyTypes)
   185  }
   186  
   187  // Update returns a copy of the params with updates from the non-zero fields of p2.
   188  // NOTE: note: must not modify the original
   189  func (params ConsensusParams) Update(params2 *abci.ConsensusParams) ConsensusParams {
   190  	res := params // explicit copy
   191  
   192  	if params2 == nil {
   193  		return res
   194  	}
   195  
   196  	// we must defensively consider any structs may be nil
   197  	if params2.Block != nil {
   198  		res.Block.MaxBytes = params2.Block.MaxBytes
   199  		res.Block.MaxGas = params2.Block.MaxGas
   200  	}
   201  	if params2.Evidence != nil {
   202  		res.Evidence.MaxAgeNumBlocks = params2.Evidence.MaxAgeNumBlocks
   203  		res.Evidence.MaxAgeDuration = params2.Evidence.MaxAgeDuration
   204  	}
   205  	if params2.Validator != nil {
   206  		// Copy params2.Validator.PubkeyTypes, and set result's value to the copy.
   207  		// This avoids having to initialize the slice to 0 values, and then write to it again.
   208  		res.Validator.PubKeyTypes = append([]string{}, params2.Validator.PubKeyTypes...)
   209  	}
   210  	return res
   211  }