github.com/adoriasoft/tendermint@v0.34.0-dev1.0.20200722151356-96d84601a75a/types/params.go (about)

     1  package types
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"time"
     7  
     8  	abci "github.com/tendermint/tendermint/abci/types"
     9  	"github.com/tendermint/tendermint/crypto/tmhash"
    10  	tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
    11  )
    12  
    13  const (
    14  	// MaxBlockSizeBytes is the maximum permitted size of the blocks.
    15  	MaxBlockSizeBytes = 104857600 // 100MB
    16  
    17  	// BlockPartSizeBytes is the size of one block part.
    18  	BlockPartSizeBytes uint32 = 65536 // 64kB
    19  
    20  	// MaxBlockPartsCount is the maximum number of block parts.
    21  	MaxBlockPartsCount = (MaxBlockSizeBytes / BlockPartSizeBytes) + 1
    22  
    23  	// Restrict the upper bound of the amount of evidence (uses uint16 for safe conversion)
    24  	MaxEvidencePerBlock = 65535
    25  )
    26  
    27  // DefaultConsensusParams returns a default ConsensusParams.
    28  func DefaultConsensusParams() *tmproto.ConsensusParams {
    29  	return &tmproto.ConsensusParams{
    30  		Block:     DefaultBlockParams(),
    31  		Evidence:  DefaultEvidenceParams(),
    32  		Validator: DefaultValidatorParams(),
    33  		Version:   DefaultVersionParams(),
    34  	}
    35  }
    36  
    37  // DefaultBlockParams returns a default BlockParams.
    38  func DefaultBlockParams() tmproto.BlockParams {
    39  	return tmproto.BlockParams{
    40  		MaxBytes:   22020096, // 21MB
    41  		MaxGas:     -1,
    42  		TimeIotaMs: 1000, // 1s
    43  	}
    44  }
    45  
    46  // DefaultEvidenceParams returns a default EvidenceParams.
    47  func DefaultEvidenceParams() tmproto.EvidenceParams {
    48  	return tmproto.EvidenceParams{
    49  		MaxAgeNumBlocks:  100000, // 27.8 hrs at 1block/s
    50  		MaxAgeDuration:   48 * time.Hour,
    51  		MaxNum:           50,
    52  		ProofTrialPeriod: 50000, // half MaxAgeNumBlocks
    53  	}
    54  }
    55  
    56  // DefaultValidatorParams returns a default ValidatorParams, which allows
    57  // only ed25519 pubkeys.
    58  func DefaultValidatorParams() tmproto.ValidatorParams {
    59  	return tmproto.ValidatorParams{
    60  		PubKeyTypes: []string{ABCIPubKeyTypeEd25519},
    61  	}
    62  }
    63  
    64  func DefaultVersionParams() tmproto.VersionParams {
    65  	return tmproto.VersionParams{
    66  		AppVersion: 0,
    67  	}
    68  }
    69  
    70  func IsValidPubkeyType(params tmproto.ValidatorParams, pubkeyType string) bool {
    71  	for i := 0; i < len(params.PubKeyTypes); i++ {
    72  		if params.PubKeyTypes[i] == pubkeyType {
    73  			return true
    74  		}
    75  	}
    76  	return false
    77  }
    78  
    79  // Validate validates the ConsensusParams to ensure all values are within their
    80  // allowed limits, and returns an error if they are not.
    81  func ValidateConsensusParams(params tmproto.ConsensusParams) error {
    82  	if params.Block.MaxBytes <= 0 {
    83  		return fmt.Errorf("block.MaxBytes must be greater than 0. Got %d",
    84  			params.Block.MaxBytes)
    85  	}
    86  	if params.Block.MaxBytes > MaxBlockSizeBytes {
    87  		return fmt.Errorf("block.MaxBytes is too big. %d > %d",
    88  			params.Block.MaxBytes, MaxBlockSizeBytes)
    89  	}
    90  
    91  	if params.Block.MaxGas < -1 {
    92  		return fmt.Errorf("block.MaxGas must be greater or equal to -1. Got %d",
    93  			params.Block.MaxGas)
    94  	}
    95  
    96  	if params.Block.TimeIotaMs <= 0 {
    97  		return fmt.Errorf("block.TimeIotaMs must be greater than 0. Got %v",
    98  			params.Block.TimeIotaMs)
    99  	}
   100  
   101  	if params.Evidence.MaxAgeNumBlocks <= 0 {
   102  		return fmt.Errorf("evidenceParams.MaxAgeNumBlocks must be greater than 0. Got %d",
   103  			params.Evidence.MaxAgeNumBlocks)
   104  	}
   105  
   106  	if params.Evidence.MaxAgeDuration <= 0 {
   107  		return fmt.Errorf("evidenceParams.MaxAgeDuration must be grater than 0 if provided, Got %v",
   108  			params.Evidence.MaxAgeDuration)
   109  	}
   110  
   111  	if params.Evidence.MaxNum > MaxEvidencePerBlock {
   112  		return fmt.Errorf("evidenceParams.MaxNumEvidence is greater than upper bound, %d > %d",
   113  			params.Evidence.MaxNum, MaxEvidencePerBlock)
   114  	}
   115  
   116  	if int64(params.Evidence.MaxNum)*MaxEvidenceBytes > params.Block.MaxBytes {
   117  		return fmt.Errorf("total possible evidence size is bigger than block.MaxBytes, %d > %d",
   118  			int64(params.Evidence.MaxNum)*MaxEvidenceBytes, params.Block.MaxBytes)
   119  	}
   120  
   121  	if params.Evidence.ProofTrialPeriod <= 0 {
   122  		return fmt.Errorf("evidenceParams.ProofTrialPeriod must be grater than 0 if provided, Got %v",
   123  			params.Evidence.ProofTrialPeriod)
   124  	}
   125  
   126  	if params.Evidence.ProofTrialPeriod >= params.Evidence.MaxAgeNumBlocks {
   127  		return fmt.Errorf("evidenceParams.ProofTrialPeriod must be smaller than evidenceParams.MaxAgeNumBlocks,  %d > %d",
   128  			params.Evidence.ProofTrialPeriod, params.Evidence.MaxAgeDuration)
   129  	}
   130  
   131  	if len(params.Validator.PubKeyTypes) == 0 {
   132  		return errors.New("len(Validator.PubKeyTypes) must be greater than 0")
   133  	}
   134  
   135  	// Check if keyType is a known ABCIPubKeyType
   136  	for i := 0; i < len(params.Validator.PubKeyTypes); i++ {
   137  		keyType := params.Validator.PubKeyTypes[i]
   138  		if _, ok := ABCIPubKeyTypesToNames[keyType]; !ok {
   139  			return fmt.Errorf("params.Validator.PubKeyTypes[%d], %s, is an unknown pubkey type",
   140  				i, keyType)
   141  		}
   142  	}
   143  
   144  	return nil
   145  }
   146  
   147  // Hash returns a hash of a subset of the parameters to store in the block header.
   148  // Only the Block.MaxBytes and Block.MaxGas are included in the hash.
   149  // This allows the ConsensusParams to evolve more without breaking the block
   150  // protocol. No need for a Merkle tree here, just a small struct to hash.
   151  func HashConsensusParams(params tmproto.ConsensusParams) []byte {
   152  	hasher := tmhash.New()
   153  
   154  	hp := tmproto.HashedParams{
   155  		BlockMaxBytes: params.Block.MaxBytes,
   156  		BlockMaxGas:   params.Block.MaxGas,
   157  	}
   158  
   159  	bz, err := hp.Marshal()
   160  	if err != nil {
   161  		panic(err)
   162  	}
   163  
   164  	hasher.Write(bz)
   165  	return hasher.Sum(nil)
   166  }
   167  
   168  // Update returns a copy of the params with updates from the non-zero fields of p2.
   169  // NOTE: note: must not modify the original
   170  func UpdateConsensusParams(params tmproto.ConsensusParams, params2 *abci.ConsensusParams) tmproto.ConsensusParams {
   171  	res := params // explicit copy
   172  
   173  	if params2 == nil {
   174  		return res
   175  	}
   176  
   177  	// we must defensively consider any structs may be nil
   178  	if params2.Block != nil {
   179  		res.Block.MaxBytes = params2.Block.MaxBytes
   180  		res.Block.MaxGas = params2.Block.MaxGas
   181  	}
   182  	if params2.Evidence != nil {
   183  		res.Evidence.MaxAgeNumBlocks = params2.Evidence.MaxAgeNumBlocks
   184  		res.Evidence.MaxAgeDuration = params2.Evidence.MaxAgeDuration
   185  		res.Evidence.MaxNum = params2.Evidence.MaxNum
   186  	}
   187  	if params2.Validator != nil {
   188  		// Copy params2.Validator.PubkeyTypes, and set result's value to the copy.
   189  		// This avoids having to initialize the slice to 0 values, and then write to it again.
   190  		res.Validator.PubKeyTypes = append([]string{}, params2.Validator.PubKeyTypes...)
   191  	}
   192  	if params2.Version != nil {
   193  		res.Version.AppVersion = params2.Version.AppVersion
   194  	}
   195  	return res
   196  }