github.com/cosmos/cosmos-sdk@v0.50.10/x/staking/types/params.go (about) 1 package types 2 3 import ( 4 "errors" 5 "fmt" 6 "strings" 7 "time" 8 9 "cosmossdk.io/math" 10 11 "github.com/cosmos/cosmos-sdk/codec" 12 sdk "github.com/cosmos/cosmos-sdk/types" 13 ) 14 15 // Staking params default values 16 const ( 17 // DefaultUnbondingTime reflects three weeks in seconds as the default 18 // unbonding time. 19 // TODO: Justify our choice of default here. 20 DefaultUnbondingTime time.Duration = time.Hour * 24 * 7 * 3 21 22 // Default maximum number of bonded validators 23 DefaultMaxValidators uint32 = 100 24 25 // Default maximum entries in a UBD/RED pair 26 DefaultMaxEntries uint32 = 7 27 28 // DefaultHistorical entries is 10000. Apps that don't use IBC can ignore this 29 // value by not adding the staking module to the application module manager's 30 // SetOrderBeginBlockers. 31 DefaultHistoricalEntries uint32 = 10000 32 ) 33 34 // DefaultMinCommissionRate is set to 0% 35 var DefaultMinCommissionRate = math.LegacyZeroDec() 36 37 // NewParams creates a new Params instance 38 func NewParams(unbondingTime time.Duration, maxValidators, maxEntries, historicalEntries uint32, bondDenom string, minCommissionRate math.LegacyDec) Params { 39 return Params{ 40 UnbondingTime: unbondingTime, 41 MaxValidators: maxValidators, 42 MaxEntries: maxEntries, 43 HistoricalEntries: historicalEntries, 44 BondDenom: bondDenom, 45 MinCommissionRate: minCommissionRate, 46 } 47 } 48 49 // DefaultParams returns a default set of parameters. 50 func DefaultParams() Params { 51 return NewParams( 52 DefaultUnbondingTime, 53 DefaultMaxValidators, 54 DefaultMaxEntries, 55 DefaultHistoricalEntries, 56 sdk.DefaultBondDenom, 57 DefaultMinCommissionRate, 58 ) 59 } 60 61 // unmarshal the current staking params value from store key or panic 62 func MustUnmarshalParams(cdc *codec.LegacyAmino, value []byte) Params { 63 params, err := UnmarshalParams(cdc, value) 64 if err != nil { 65 panic(err) 66 } 67 68 return params 69 } 70 71 // unmarshal the current staking params value from store key 72 func UnmarshalParams(cdc *codec.LegacyAmino, value []byte) (params Params, err error) { 73 err = cdc.Unmarshal(value, ¶ms) 74 if err != nil { 75 return 76 } 77 78 return 79 } 80 81 // validate a set of params 82 func (p Params) Validate() error { 83 if err := validateUnbondingTime(p.UnbondingTime); err != nil { 84 return err 85 } 86 87 if err := validateMaxValidators(p.MaxValidators); err != nil { 88 return err 89 } 90 91 if err := validateMaxEntries(p.MaxEntries); err != nil { 92 return err 93 } 94 95 if err := validateBondDenom(p.BondDenom); err != nil { 96 return err 97 } 98 99 if err := validateMinCommissionRate(p.MinCommissionRate); err != nil { 100 return err 101 } 102 103 if err := validateHistoricalEntries(p.HistoricalEntries); err != nil { 104 return err 105 } 106 107 return nil 108 } 109 110 func validateUnbondingTime(i interface{}) error { 111 v, ok := i.(time.Duration) 112 if !ok { 113 return fmt.Errorf("invalid parameter type: %T", i) 114 } 115 116 if v <= 0 { 117 return fmt.Errorf("unbonding time must be positive: %d", v) 118 } 119 120 return nil 121 } 122 123 func validateMaxValidators(i interface{}) error { 124 v, ok := i.(uint32) 125 if !ok { 126 return fmt.Errorf("invalid parameter type: %T", i) 127 } 128 129 if v == 0 { 130 return fmt.Errorf("max validators must be positive: %d", v) 131 } 132 133 return nil 134 } 135 136 func validateMaxEntries(i interface{}) error { 137 v, ok := i.(uint32) 138 if !ok { 139 return fmt.Errorf("invalid parameter type: %T", i) 140 } 141 142 if v == 0 { 143 return fmt.Errorf("max entries must be positive: %d", v) 144 } 145 146 return nil 147 } 148 149 func validateHistoricalEntries(i interface{}) error { 150 _, ok := i.(uint32) 151 if !ok { 152 return fmt.Errorf("invalid parameter type: %T", i) 153 } 154 155 return nil 156 } 157 158 func validateBondDenom(i interface{}) error { 159 v, ok := i.(string) 160 if !ok { 161 return fmt.Errorf("invalid parameter type: %T", i) 162 } 163 164 if strings.TrimSpace(v) == "" { 165 return errors.New("bond denom cannot be blank") 166 } 167 168 if err := sdk.ValidateDenom(v); err != nil { 169 return err 170 } 171 172 return nil 173 } 174 175 func ValidatePowerReduction(i interface{}) error { 176 v, ok := i.(math.Int) 177 if !ok { 178 return fmt.Errorf("invalid parameter type: %T", i) 179 } 180 181 if v.LT(math.NewInt(1)) { 182 return fmt.Errorf("power reduction cannot be lower than 1") 183 } 184 185 return nil 186 } 187 188 func validateMinCommissionRate(i interface{}) error { 189 v, ok := i.(math.LegacyDec) 190 if !ok { 191 return fmt.Errorf("invalid parameter type: %T", i) 192 } 193 194 if v.IsNil() { 195 return fmt.Errorf("minimum commission rate cannot be nil: %s", v) 196 } 197 if v.IsNegative() { 198 return fmt.Errorf("minimum commission rate cannot be negative: %s", v) 199 } 200 if v.GT(math.LegacyOneDec()) { 201 return fmt.Errorf("minimum commission rate cannot be greater than 100%%: %s", v) 202 } 203 204 return nil 205 }