tendermint/types/params.go

164 lines
4.7 KiB
Go
Raw Normal View History

package types
import (
2018-06-21 21:59:02 -07:00
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/tmhash"
2018-07-01 19:36:49 -07:00
cmn "github.com/tendermint/tendermint/libs/common"
)
2017-09-18 14:00:09 -07:00
const (
// MaxBlockSizeBytes is the maximum permitted size of the blocks.
2018-04-03 06:50:53 -07:00
MaxBlockSizeBytes = 104857600 // 100MB
// BlockPartSizeBytes is the size of one block part.
BlockPartSizeBytes = 65536 // 64kB
2017-09-18 14:00:09 -07:00
)
// ConsensusParams contains consensus critical parameters that determine the
// validity of blocks.
type ConsensusParams struct {
BlockSize BlockSizeParams `json:"block_size"`
Evidence EvidenceParams `json:"evidence"`
Validator ValidatorParams `json:"validator"`
2017-09-15 21:16:49 -07:00
}
// BlockSizeParams define limits on the block size.
type BlockSizeParams struct {
MaxBytes int64 `json:"max_bytes"`
2017-12-21 13:47:32 -08:00
MaxGas int64 `json:"max_gas"`
2017-09-15 21:16:49 -07:00
}
2017-11-01 12:57:38 -07:00
// EvidenceParams determine how we handle evidence of malfeasance
type EvidenceParams struct {
2017-12-26 17:34:57 -08:00
MaxAge int64 `json:"max_age"` // only accept new evidence more recent than this
2017-11-01 12:57:38 -07:00
}
// ValidatorParams restrict the public key types validators can use.
// NOTE: uses ABCI pubkey naming, not Amino routes.
type ValidatorParams struct {
PubKeyTypes []string `json:"pub_key_types"`
}
// DefaultConsensusParams returns a default ConsensusParams.
func DefaultConsensusParams() *ConsensusParams {
return &ConsensusParams{
DefaultBlockSizeParams(),
2017-11-01 12:57:38 -07:00
DefaultEvidenceParams(),
DefaultValidatorParams(),
2017-09-15 21:16:49 -07:00
}
}
// DefaultBlockSizeParams returns a default BlockSizeParams.
func DefaultBlockSizeParams() BlockSizeParams {
return BlockSizeParams{
MaxBytes: 22020096, // 21MB
2017-09-15 21:16:49 -07:00
MaxGas: -1,
}
}
// DefaultEvidenceParams Params returns a default EvidenceParams.
2017-11-01 12:57:38 -07:00
func DefaultEvidenceParams() EvidenceParams {
return EvidenceParams{
MaxAge: 100000, // 27.8 hrs at 1block/s
2017-11-01 12:57:38 -07:00
}
}
// DefaultValidatorParams returns a default ValidatorParams, which allows
// only ed25519 pubkeys.
func DefaultValidatorParams() ValidatorParams {
return ValidatorParams{[]string{ABCIPubKeyTypeEd25519}}
}
// Validate validates the ConsensusParams to ensure all values are within their
// allowed limits, and returns an error if they are not.
func (params *ConsensusParams) Validate() error {
2017-12-19 10:43:15 -08:00
if params.BlockSize.MaxBytes <= 0 {
return cmn.NewError("BlockSize.MaxBytes must be greater than 0. Got %d",
params.BlockSize.MaxBytes)
}
2018-04-03 06:50:53 -07:00
if params.BlockSize.MaxBytes > MaxBlockSizeBytes {
return cmn.NewError("BlockSize.MaxBytes is too big. %d > %d",
2018-04-03 06:50:53 -07:00
params.BlockSize.MaxBytes, MaxBlockSizeBytes)
2017-09-18 14:00:09 -07:00
}
if params.BlockSize.MaxGas < -1 {
return cmn.NewError("BlockSize.MaxGas must be greater or equal to -1. Got %d",
params.BlockSize.MaxGas)
}
if params.Evidence.MaxAge <= 0 {
return cmn.NewError("EvidenceParams.MaxAge must be greater than 0. Got %d",
params.Evidence.MaxAge)
}
if len(params.Validator.PubKeyTypes) == 0 {
return cmn.NewError("len(Validator.PubKeyTypes) must be greater than 0")
}
// Check if keyType is a known ABCIPubKeyType
for i := 0; i < len(params.Validator.PubKeyTypes); i++ {
keyType := params.Validator.PubKeyTypes[i]
if _, ok := ABCIPubKeyTypesToAminoRoutes[keyType]; !ok {
return cmn.NewError("params.Validator.PubKeyTypes[%d], %s, is an unknown pubkey type",
i, keyType)
}
}
return nil
}
2017-12-14 01:13:31 -08:00
// Hash returns a hash of the parameters to store in the block header
// No Merkle tree here, only three values are hashed here
// thus benefit from saving space < drawbacks from proofs' overhead
// Revisit this function if new fields are added to ConsensusParams
2017-12-14 01:13:31 -08:00
func (params *ConsensusParams) Hash() []byte {
hasher := tmhash.New()
bz := cdcEncode(params)
if bz == nil {
panic("cannot fail to encode ConsensusParams")
}
hasher.Write(bz)
return hasher.Sum(nil)
2017-12-14 01:13:31 -08:00
}
2017-12-21 13:47:32 -08:00
func (params *ConsensusParams) Equals(params2 *ConsensusParams) bool {
return params.BlockSize == params2.BlockSize &&
params.Evidence == params2.Evidence &&
stringSliceEqual(params.Validator.PubKeyTypes, params2.Validator.PubKeyTypes)
}
func stringSliceEqual(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}
2017-12-21 13:47:32 -08:00
// Update returns a copy of the params with updates from the non-zero fields of p2.
// NOTE: note: must not modify the original
func (params ConsensusParams) Update(params2 *abci.ConsensusParams) ConsensusParams {
res := params // explicit copy
if params2 == nil {
return res
}
// we must defensively consider any structs may be nil
if params2.BlockSize != nil {
res.BlockSize.MaxBytes = params2.BlockSize.MaxBytes
res.BlockSize.MaxGas = params2.BlockSize.MaxGas
2017-12-21 13:47:32 -08:00
}
if params2.Evidence != nil {
res.Evidence.MaxAge = params2.Evidence.MaxAge
}
if params2.Validator != nil {
res.Validator.PubKeyTypes = params2.Validator.PubKeyTypes
2017-12-21 13:47:32 -08:00
}
return res
}