tendermint/types/params.go

157 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"
2018-06-30 22:40:03 -07:00
"github.com/tendermint/tendermint/crypto/merkle"
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
2017-09-18 14:00:09 -07:00
)
// ConsensusParams contains consensus critical parameters
// that determine the validity of blocks.
type ConsensusParams struct {
2017-11-01 12:57:38 -07:00
BlockSize `json:"block_size_params"`
TxSize `json:"tx_size_params"`
BlockGossip `json:"block_gossip_params"`
EvidenceParams `json:"evidence_params"`
2017-09-15 21:16:49 -07:00
}
2017-12-19 10:43:15 -08:00
// BlockSize contain limits on the block size.
type BlockSize struct {
2017-12-21 13:47:32 -08:00
MaxBytes int `json:"max_bytes"` // NOTE: must not be 0 nor greater than 100MB
MaxTxs int `json:"max_txs"`
MaxGas int64 `json:"max_gas"`
2017-09-15 21:16:49 -07:00
}
2017-12-19 10:43:15 -08:00
// TxSize contain limits on the tx size.
type TxSize struct {
2017-12-21 13:47:32 -08:00
MaxBytes int `json:"max_bytes"`
MaxGas int64 `json:"max_gas"`
2017-09-15 21:16:49 -07:00
}
2017-12-19 10:43:15 -08:00
// BlockGossip determine consensus critical elements of how blocks are gossiped
type BlockGossip struct {
2017-09-15 21:16:49 -07:00
BlockPartSizeBytes int `json:"block_part_size_bytes"` // NOTE: must not be 0
}
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
}
// DefaultConsensusParams returns a default ConsensusParams.
func DefaultConsensusParams() *ConsensusParams {
return &ConsensusParams{
2017-12-19 10:43:15 -08:00
DefaultBlockSize(),
DefaultTxSize(),
DefaultBlockGossip(),
2017-11-01 12:57:38 -07:00
DefaultEvidenceParams(),
2017-09-15 21:16:49 -07:00
}
}
2017-12-19 10:43:15 -08:00
// DefaultBlockSize returns a default BlockSize.
func DefaultBlockSize() BlockSize {
return BlockSize{
2017-09-15 21:16:49 -07:00
MaxBytes: 22020096, // 21MB
MaxTxs: 10000,
2017-09-15 21:16:49 -07:00
MaxGas: -1,
}
}
2017-12-19 10:43:15 -08:00
// DefaultTxSize returns a default TxSize.
func DefaultTxSize() TxSize {
return TxSize{
2017-09-15 21:16:49 -07:00
MaxBytes: 10240, // 10kB
MaxGas: -1,
}
}
2017-12-19 10:43:15 -08:00
// DefaultBlockGossip returns a default BlockGossip.
func DefaultBlockGossip() BlockGossip {
return BlockGossip{
2017-09-15 21:16:49 -07:00
BlockPartSizeBytes: 65536, // 64kB,
}
}
2017-11-01 12:57:38 -07:00
// DefaultEvidence Params returns a default EvidenceParams.
func DefaultEvidenceParams() EvidenceParams {
return EvidenceParams{
MaxAge: 100000, // 27.8 hrs at 1block/s
2017-11-01 12:57:38 -07:00
}
}
// 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-09-18 14:00:09 -07:00
// ensure some values are greater than 0
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)
}
2017-12-19 10:43:15 -08:00
if params.BlockGossip.BlockPartSizeBytes <= 0 {
return cmn.NewError("BlockGossip.BlockPartSizeBytes must be greater than 0. Got %d", params.BlockGossip.BlockPartSizeBytes)
}
2017-09-18 14:00:09 -07:00
// ensure blocks aren't too big
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
}
return nil
}
2017-12-14 01:13:31 -08:00
// Hash returns a merkle hash of the parameters to store
// in the block header
func (params *ConsensusParams) Hash() []byte {
2018-02-03 00:23:10 -08:00
return merkle.SimpleHashFromMap(map[string]merkle.Hasher{
"block_gossip_part_size_bytes": aminoHasher(params.BlockGossip.BlockPartSizeBytes),
"block_size_max_bytes": aminoHasher(params.BlockSize.MaxBytes),
"block_size_max_gas": aminoHasher(params.BlockSize.MaxGas),
"block_size_max_txs": aminoHasher(params.BlockSize.MaxTxs),
"tx_size_max_bytes": aminoHasher(params.TxSize.MaxBytes),
"tx_size_max_gas": aminoHasher(params.TxSize.MaxGas),
2017-12-14 01:13:31 -08:00
})
}
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
// XXX: it's cast city over here. It's ok because we only do int32->int
// but still, watch it champ.
if params2.BlockSize != nil {
if params2.BlockSize.MaxBytes > 0 {
res.BlockSize.MaxBytes = int(params2.BlockSize.MaxBytes)
}
if params2.BlockSize.MaxTxs > 0 {
res.BlockSize.MaxTxs = int(params2.BlockSize.MaxTxs)
}
if params2.BlockSize.MaxGas > 0 {
res.BlockSize.MaxGas = params2.BlockSize.MaxGas
}
}
if params2.TxSize != nil {
if params2.TxSize.MaxBytes > 0 {
res.TxSize.MaxBytes = int(params2.TxSize.MaxBytes)
}
if params2.TxSize.MaxGas > 0 {
res.TxSize.MaxGas = params2.TxSize.MaxGas
}
}
if params2.BlockGossip != nil {
if params2.BlockGossip.BlockPartSizeBytes > 0 {
res.BlockGossip.BlockPartSizeBytes = int(params2.BlockGossip.BlockPartSizeBytes)
}
}
return res
}