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