mirror of
https://github.com/tendermint/tendermint.git
synced 2026-02-10 14:00:33 +00:00
* [cherry-picked] abci++: add proto fields for enabling vote extensions (#8587) This pull requests adds the protocol buffer field for the `ABCI.VoteExtensionsEnableHeight` parameter. This proto field is threaded throughout all of the relevant places where consensus params are used and referenced. This PR also adds validation of the consensus param updates. Previous consensus param changes didn't depend on _previous_ versions of the params, so this change adds a method for validating against the old params as well. closes: #8453 * Re-sync some things with original patch * fixes * Remove 'Skip' from TestApp_VoteExtensions * Fix all unit tests * Appease linter * Update types/params.go Co-authored-by: Thane Thomson <connect@thanethomson.com> Co-authored-by: William Banfield <4561443+williambanfield@users.noreply.github.com> Co-authored-by: Thane Thomson <connect@thanethomson.com>
324 lines
9.6 KiB
Go
324 lines
9.6 KiB
Go
package types
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/tendermint/tendermint/crypto/ed25519"
|
|
"github.com/tendermint/tendermint/crypto/secp256k1"
|
|
"github.com/tendermint/tendermint/crypto/tmhash"
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
|
)
|
|
|
|
const (
|
|
// MaxBlockSizeBytes is the maximum permitted size of the blocks.
|
|
MaxBlockSizeBytes = 104857600 // 100MB
|
|
|
|
// BlockPartSizeBytes is the size of one block part.
|
|
BlockPartSizeBytes uint32 = 65536 // 64kB
|
|
|
|
// MaxBlockPartsCount is the maximum number of block parts.
|
|
MaxBlockPartsCount = (MaxBlockSizeBytes / BlockPartSizeBytes) + 1
|
|
|
|
ABCIPubKeyTypeEd25519 = ed25519.KeyType
|
|
ABCIPubKeyTypeSecp256k1 = secp256k1.KeyType
|
|
)
|
|
|
|
var ABCIPubKeyTypesToNames = map[string]string{
|
|
ABCIPubKeyTypeEd25519: ed25519.PubKeyName,
|
|
ABCIPubKeyTypeSecp256k1: secp256k1.PubKeyName,
|
|
}
|
|
|
|
// ConsensusParams contains consensus critical parameters that determine the
|
|
// validity of blocks.
|
|
type ConsensusParams struct {
|
|
Block BlockParams `json:"block"`
|
|
Evidence EvidenceParams `json:"evidence"`
|
|
Validator ValidatorParams `json:"validator"`
|
|
Version VersionParams `json:"version"`
|
|
ABCI ABCIParams `json:"abci"`
|
|
}
|
|
|
|
// BlockParams define limits on the block size and gas plus minimum time
|
|
// between blocks.
|
|
type BlockParams struct {
|
|
MaxBytes int64 `json:"max_bytes"`
|
|
MaxGas int64 `json:"max_gas"`
|
|
}
|
|
|
|
// EvidenceParams determine how we handle evidence of malfeasance.
|
|
type EvidenceParams struct {
|
|
MaxAgeNumBlocks int64 `json:"max_age_num_blocks"` // only accept new evidence more recent than this
|
|
MaxAgeDuration time.Duration `json:"max_age_duration"`
|
|
MaxBytes int64 `json:"max_bytes"`
|
|
}
|
|
|
|
// ValidatorParams restrict the public key types validators can use.
|
|
// NOTE: uses ABCI pubkey naming, not Amino names.
|
|
type ValidatorParams struct {
|
|
PubKeyTypes []string `json:"pub_key_types"`
|
|
}
|
|
|
|
type VersionParams struct {
|
|
App uint64 `json:"app"`
|
|
}
|
|
|
|
// ABCIParams configure ABCI functionality specific to the Application Blockchain
|
|
// Interface.
|
|
type ABCIParams struct {
|
|
VoteExtensionsEnableHeight int64 `json:"vote_extensions_enable_height"`
|
|
}
|
|
|
|
// VoteExtensionsEnabled returns true if vote extensions are enabled at height h
|
|
// and false otherwise.
|
|
func (a ABCIParams) VoteExtensionsEnabled(h int64) bool {
|
|
if a.VoteExtensionsEnableHeight == 0 {
|
|
return false
|
|
}
|
|
return a.VoteExtensionsEnableHeight <= h
|
|
}
|
|
|
|
// DefaultConsensusParams returns a default ConsensusParams.
|
|
func DefaultConsensusParams() *ConsensusParams {
|
|
return &ConsensusParams{
|
|
Block: DefaultBlockParams(),
|
|
Evidence: DefaultEvidenceParams(),
|
|
Validator: DefaultValidatorParams(),
|
|
Version: DefaultVersionParams(),
|
|
ABCI: DefaultABCIParams(),
|
|
}
|
|
}
|
|
|
|
// DefaultBlockParams returns a default BlockParams.
|
|
func DefaultBlockParams() BlockParams {
|
|
return BlockParams{
|
|
MaxBytes: 22020096, // 21MB
|
|
MaxGas: -1,
|
|
}
|
|
}
|
|
|
|
// DefaultEvidenceParams returns a default EvidenceParams.
|
|
func DefaultEvidenceParams() EvidenceParams {
|
|
return EvidenceParams{
|
|
MaxAgeNumBlocks: 100000, // 27.8 hrs at 1block/s
|
|
MaxAgeDuration: 48 * time.Hour,
|
|
MaxBytes: 1048576, // 1MB
|
|
}
|
|
}
|
|
|
|
// DefaultValidatorParams returns a default ValidatorParams, which allows
|
|
// only ed25519 pubkeys.
|
|
func DefaultValidatorParams() ValidatorParams {
|
|
return ValidatorParams{
|
|
PubKeyTypes: []string{ABCIPubKeyTypeEd25519},
|
|
}
|
|
}
|
|
|
|
func DefaultVersionParams() VersionParams {
|
|
return VersionParams{
|
|
App: 0,
|
|
}
|
|
}
|
|
|
|
func DefaultABCIParams() ABCIParams {
|
|
return ABCIParams{
|
|
// When set to 0, vote extensions are not required.
|
|
VoteExtensionsEnableHeight: 0,
|
|
}
|
|
}
|
|
|
|
func IsValidPubkeyType(params ValidatorParams, pubkeyType string) bool {
|
|
for i := 0; i < len(params.PubKeyTypes); i++ {
|
|
if params.PubKeyTypes[i] == pubkeyType {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Validate validates the ConsensusParams to ensure all values are within their
|
|
// allowed limits, and returns an error if they are not.
|
|
func (params ConsensusParams) ValidateBasic() error {
|
|
if params.Block.MaxBytes <= 0 {
|
|
return fmt.Errorf("block.MaxBytes must be greater than 0. Got %d",
|
|
params.Block.MaxBytes)
|
|
}
|
|
if params.Block.MaxBytes > MaxBlockSizeBytes {
|
|
return fmt.Errorf("block.MaxBytes is too big. %d > %d",
|
|
params.Block.MaxBytes, MaxBlockSizeBytes)
|
|
}
|
|
|
|
if params.Block.MaxGas < -1 {
|
|
return fmt.Errorf("block.MaxGas must be greater or equal to -1. Got %d",
|
|
params.Block.MaxGas)
|
|
}
|
|
|
|
if params.Evidence.MaxAgeNumBlocks <= 0 {
|
|
return fmt.Errorf("evidence.MaxAgeNumBlocks must be greater than 0. Got %d",
|
|
params.Evidence.MaxAgeNumBlocks)
|
|
}
|
|
|
|
if params.Evidence.MaxAgeDuration <= 0 {
|
|
return fmt.Errorf("evidence.MaxAgeDuration must be grater than 0 if provided, Got %v",
|
|
params.Evidence.MaxAgeDuration)
|
|
}
|
|
|
|
if params.Evidence.MaxBytes > params.Block.MaxBytes {
|
|
return fmt.Errorf("evidence.MaxBytesEvidence is greater than upper bound, %d > %d",
|
|
params.Evidence.MaxBytes, params.Block.MaxBytes)
|
|
}
|
|
|
|
if params.Evidence.MaxBytes < 0 {
|
|
return fmt.Errorf("evidence.MaxBytes must be non negative. Got: %d",
|
|
params.Evidence.MaxBytes)
|
|
}
|
|
|
|
if params.ABCI.VoteExtensionsEnableHeight < 0 {
|
|
return fmt.Errorf("ABCI.VoteExtensionsEnableHeight cannot be negative. Got: %d", params.ABCI.VoteExtensionsEnableHeight)
|
|
}
|
|
|
|
if len(params.Validator.PubKeyTypes) == 0 {
|
|
return errors.New("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 := ABCIPubKeyTypesToNames[keyType]; !ok {
|
|
return fmt.Errorf("params.Validator.PubKeyTypes[%d], %s, is an unknown pubkey type",
|
|
i, keyType)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (params ConsensusParams) ValidateUpdate(updated *tmproto.ConsensusParams, h int64) error {
|
|
if updated.Abci == nil {
|
|
return nil
|
|
}
|
|
if params.ABCI.VoteExtensionsEnableHeight == updated.Abci.VoteExtensionsEnableHeight {
|
|
return nil
|
|
}
|
|
if params.ABCI.VoteExtensionsEnableHeight != 0 && updated.Abci.VoteExtensionsEnableHeight == 0 {
|
|
return errors.New("vote extensions cannot be disabled once enabled")
|
|
}
|
|
if updated.Abci.VoteExtensionsEnableHeight <= h {
|
|
return fmt.Errorf("VoteExtensionsEnableHeight cannot be updated to a past height, "+
|
|
"initial height: %d, current height %d",
|
|
params.ABCI.VoteExtensionsEnableHeight, h)
|
|
}
|
|
if params.ABCI.VoteExtensionsEnableHeight <= h {
|
|
return fmt.Errorf("VoteExtensionsEnableHeight cannot be modified once"+
|
|
"the initial height has occurred, "+
|
|
"initial height: %d, current height %d",
|
|
params.ABCI.VoteExtensionsEnableHeight, h)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Hash returns a hash of a subset of the parameters to store in the block header.
|
|
// Only the Block.MaxBytes and Block.MaxGas are included in the hash.
|
|
// This allows the ConsensusParams to evolve more without breaking the block
|
|
// protocol. No need for a Merkle tree here, just a small struct to hash.
|
|
func (params ConsensusParams) Hash() []byte {
|
|
hasher := tmhash.New()
|
|
|
|
hp := tmproto.HashedParams{
|
|
BlockMaxBytes: params.Block.MaxBytes,
|
|
BlockMaxGas: params.Block.MaxGas,
|
|
}
|
|
|
|
bz, err := hp.Marshal()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
_, err = hasher.Write(bz)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return hasher.Sum(nil)
|
|
}
|
|
|
|
// 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 *tmproto.ConsensusParams) ConsensusParams {
|
|
res := params // explicit copy
|
|
|
|
if params2 == nil {
|
|
return res
|
|
}
|
|
|
|
// we must defensively consider any structs may be nil
|
|
if params2.Block != nil {
|
|
res.Block.MaxBytes = params2.Block.MaxBytes
|
|
res.Block.MaxGas = params2.Block.MaxGas
|
|
}
|
|
if params2.Evidence != nil {
|
|
res.Evidence.MaxAgeNumBlocks = params2.Evidence.MaxAgeNumBlocks
|
|
res.Evidence.MaxAgeDuration = params2.Evidence.MaxAgeDuration
|
|
res.Evidence.MaxBytes = params2.Evidence.MaxBytes
|
|
}
|
|
if params2.Validator != nil {
|
|
// Copy params2.Validator.PubkeyTypes, and set result's value to the copy.
|
|
// This avoids having to initialize the slice to 0 values, and then write to it again.
|
|
res.Validator.PubKeyTypes = append([]string{}, params2.Validator.PubKeyTypes...)
|
|
}
|
|
if params2.Version != nil {
|
|
res.Version.App = params2.Version.App
|
|
}
|
|
if params2.Abci != nil {
|
|
res.ABCI.VoteExtensionsEnableHeight = params2.Abci.GetVoteExtensionsEnableHeight()
|
|
}
|
|
return res
|
|
}
|
|
|
|
func (params *ConsensusParams) ToProto() tmproto.ConsensusParams {
|
|
return tmproto.ConsensusParams{
|
|
Block: &tmproto.BlockParams{
|
|
MaxBytes: params.Block.MaxBytes,
|
|
MaxGas: params.Block.MaxGas,
|
|
},
|
|
Evidence: &tmproto.EvidenceParams{
|
|
MaxAgeNumBlocks: params.Evidence.MaxAgeNumBlocks,
|
|
MaxAgeDuration: params.Evidence.MaxAgeDuration,
|
|
MaxBytes: params.Evidence.MaxBytes,
|
|
},
|
|
Validator: &tmproto.ValidatorParams{
|
|
PubKeyTypes: params.Validator.PubKeyTypes,
|
|
},
|
|
Version: &tmproto.VersionParams{
|
|
App: params.Version.App,
|
|
},
|
|
Abci: &tmproto.ABCIParams{
|
|
VoteExtensionsEnableHeight: params.ABCI.VoteExtensionsEnableHeight,
|
|
},
|
|
}
|
|
}
|
|
|
|
func ConsensusParamsFromProto(pbParams tmproto.ConsensusParams) ConsensusParams {
|
|
c := ConsensusParams{
|
|
Block: BlockParams{
|
|
MaxBytes: pbParams.Block.MaxBytes,
|
|
MaxGas: pbParams.Block.MaxGas,
|
|
},
|
|
Evidence: EvidenceParams{
|
|
MaxAgeNumBlocks: pbParams.Evidence.MaxAgeNumBlocks,
|
|
MaxAgeDuration: pbParams.Evidence.MaxAgeDuration,
|
|
MaxBytes: pbParams.Evidence.MaxBytes,
|
|
},
|
|
Validator: ValidatorParams{
|
|
PubKeyTypes: pbParams.Validator.PubKeyTypes,
|
|
},
|
|
Version: VersionParams{
|
|
App: pbParams.Version.App,
|
|
},
|
|
}
|
|
if pbParams.Abci != nil {
|
|
c.ABCI.VoteExtensionsEnableHeight = pbParams.Abci.GetVoteExtensionsEnableHeight()
|
|
}
|
|
return c
|
|
}
|