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