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