2018-06-26 19:00:12 -07:00
|
|
|
package types
|
2018-05-12 15:22:59 -07:00
|
|
|
|
|
|
|
import (
|
2019-12-10 08:48:57 -08:00
|
|
|
"errors"
|
2018-08-21 08:32:54 -07:00
|
|
|
"fmt"
|
2019-12-10 08:48:57 -08:00
|
|
|
"strings"
|
2018-08-12 00:33:48 -07:00
|
|
|
"time"
|
2018-05-18 15:57:47 -07:00
|
|
|
|
2020-02-27 02:53:22 -08:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2019-02-13 15:01:50 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-02-19 03:52:20 -08:00
|
|
|
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
|
2018-05-12 15:22:59 -07:00
|
|
|
)
|
|
|
|
|
2019-07-31 07:46:45 -07:00
|
|
|
// Staking params default values
|
2018-10-05 10:56:17 -07:00
|
|
|
const (
|
2019-02-07 17:41:23 -08:00
|
|
|
// DefaultUnbondingTime reflects three weeks in seconds as the default
|
2018-10-05 10:56:17 -07:00
|
|
|
// unbonding time.
|
2019-02-07 17:41:23 -08:00
|
|
|
// TODO: Justify our choice of default here.
|
2019-07-31 07:46:45 -07:00
|
|
|
DefaultUnbondingTime time.Duration = time.Hour * 24 * 7 * 3
|
2019-02-07 17:41:23 -08:00
|
|
|
|
|
|
|
// Default maximum number of bonded validators
|
2020-02-06 11:21:02 -08:00
|
|
|
DefaultMaxValidators uint32 = 100
|
2019-02-07 17:41:23 -08:00
|
|
|
|
|
|
|
// Default maximum entries in a UBD/RED pair
|
2020-02-06 11:21:02 -08:00
|
|
|
DefaultMaxEntries uint32 = 7
|
2019-12-18 05:20:02 -08:00
|
|
|
|
2020-12-17 11:50:03 -08:00
|
|
|
// DefaultHistorical entries is 10000. Apps that don't use IBC can ignore this
|
2020-04-23 08:06:30 -07:00
|
|
|
// value by not adding the staking module to the application module manager's
|
|
|
|
// SetOrderBeginBlockers.
|
2020-12-17 11:50:03 -08:00
|
|
|
DefaultHistoricalEntries uint32 = 10000
|
2018-10-05 10:56:17 -07:00
|
|
|
)
|
2018-07-03 21:21:36 -07:00
|
|
|
|
2018-09-27 11:52:29 -07:00
|
|
|
var (
|
2019-12-18 05:20:02 -08:00
|
|
|
KeyUnbondingTime = []byte("UnbondingTime")
|
|
|
|
KeyMaxValidators = []byte("MaxValidators")
|
2020-05-29 06:58:44 -07:00
|
|
|
KeyMaxEntries = []byte("MaxEntries")
|
2019-12-18 05:20:02 -08:00
|
|
|
KeyBondDenom = []byte("BondDenom")
|
|
|
|
KeyHistoricalEntries = []byte("HistoricalEntries")
|
2021-01-27 02:24:00 -08:00
|
|
|
KeyPowerReduction = []byte("PowerReduction")
|
2018-09-17 08:28:13 -07:00
|
|
|
)
|
2018-08-31 02:03:43 -07:00
|
|
|
|
2020-02-19 03:52:20 -08:00
|
|
|
var _ paramtypes.ParamSet = (*Params)(nil)
|
2018-10-06 06:50:58 -07:00
|
|
|
|
2020-06-16 10:59:36 -07:00
|
|
|
// ParamTable for staking module
|
|
|
|
func ParamKeyTable() paramtypes.KeyTable {
|
|
|
|
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
|
|
|
|
}
|
|
|
|
|
2019-07-31 07:46:45 -07:00
|
|
|
// NewParams creates a new Params instance
|
2021-06-14 07:45:15 -07:00
|
|
|
func NewParams(unbondingTime time.Duration, maxValidators, maxEntries, historicalEntries uint32, bondDenom string) Params {
|
2019-01-24 14:44:31 -08:00
|
|
|
return Params{
|
2019-12-18 05:20:02 -08:00
|
|
|
UnbondingTime: unbondingTime,
|
|
|
|
MaxValidators: maxValidators,
|
|
|
|
MaxEntries: maxEntries,
|
|
|
|
HistoricalEntries: historicalEntries,
|
|
|
|
BondDenom: bondDenom,
|
2019-01-24 14:44:31 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 13:01:30 -07:00
|
|
|
// Implements params.ParamSet
|
2020-02-19 03:52:20 -08:00
|
|
|
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
|
|
|
|
return paramtypes.ParamSetPairs{
|
|
|
|
paramtypes.NewParamSetPair(KeyUnbondingTime, &p.UnbondingTime, validateUnbondingTime),
|
|
|
|
paramtypes.NewParamSetPair(KeyMaxValidators, &p.MaxValidators, validateMaxValidators),
|
|
|
|
paramtypes.NewParamSetPair(KeyMaxEntries, &p.MaxEntries, validateMaxEntries),
|
|
|
|
paramtypes.NewParamSetPair(KeyHistoricalEntries, &p.HistoricalEntries, validateHistoricalEntries),
|
|
|
|
paramtypes.NewParamSetPair(KeyBondDenom, &p.BondDenom, validateBondDenom),
|
2018-08-31 02:03:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 21:21:36 -07:00
|
|
|
// DefaultParams returns a default set of parameters.
|
2018-05-25 07:17:49 -07:00
|
|
|
func DefaultParams() Params {
|
2020-02-06 11:21:02 -08:00
|
|
|
return NewParams(
|
|
|
|
DefaultUnbondingTime,
|
|
|
|
DefaultMaxValidators,
|
|
|
|
DefaultMaxEntries,
|
|
|
|
DefaultHistoricalEntries,
|
|
|
|
sdk.DefaultBondDenom,
|
|
|
|
)
|
2018-05-12 15:22:59 -07:00
|
|
|
}
|
2018-08-21 08:32:54 -07:00
|
|
|
|
2019-01-22 09:28:39 -08:00
|
|
|
// String returns a human readable string representation of the parameters.
|
|
|
|
func (p Params) String() string {
|
2020-02-06 11:21:02 -08:00
|
|
|
out, _ := yaml.Marshal(p)
|
|
|
|
return string(out)
|
2018-08-21 08:32:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshal the current staking params value from store key or panic
|
2020-08-10 12:41:21 -07:00
|
|
|
func MustUnmarshalParams(cdc *codec.LegacyAmino, value []byte) Params {
|
2018-08-21 08:32:54 -07:00
|
|
|
params, err := UnmarshalParams(cdc, value)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2018-08-21 08:32:54 -07:00
|
|
|
return params
|
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshal the current staking params value from store key
|
2020-08-10 12:41:21 -07:00
|
|
|
func UnmarshalParams(cdc *codec.LegacyAmino, value []byte) (params Params, err error) {
|
2021-04-29 03:46:22 -07:00
|
|
|
err = cdc.Unmarshal(value, ¶ms)
|
2018-08-21 08:32:54 -07:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2018-08-21 08:32:54 -07:00
|
|
|
return
|
|
|
|
}
|
2019-02-07 17:41:23 -08:00
|
|
|
|
|
|
|
// validate a set of params
|
|
|
|
func (p Params) Validate() error {
|
2019-12-10 08:48:57 -08:00
|
|
|
if err := validateUnbondingTime(p.UnbondingTime); err != nil {
|
|
|
|
return err
|
2019-02-07 17:41:23 -08:00
|
|
|
}
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
if err := validateMaxValidators(p.MaxValidators); err != nil {
|
|
|
|
return err
|
2019-02-07 17:41:23 -08:00
|
|
|
}
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
if err := validateMaxEntries(p.MaxEntries); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
if err := validateBondDenom(p.BondDenom); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateUnbondingTime(i interface{}) error {
|
|
|
|
v, ok := i.(time.Duration)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid parameter type: %T", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v <= 0 {
|
|
|
|
return fmt.Errorf("unbonding time must be positive: %d", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateMaxValidators(i interface{}) error {
|
2020-02-06 11:21:02 -08:00
|
|
|
v, ok := i.(uint32)
|
2019-12-10 08:48:57 -08:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid parameter type: %T", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v == 0 {
|
|
|
|
return fmt.Errorf("max validators must be positive: %d", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateMaxEntries(i interface{}) error {
|
2020-02-06 11:21:02 -08:00
|
|
|
v, ok := i.(uint32)
|
2019-12-10 08:48:57 -08:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid parameter type: %T", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v == 0 {
|
|
|
|
return fmt.Errorf("max entries must be positive: %d", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-18 05:20:02 -08:00
|
|
|
func validateHistoricalEntries(i interface{}) error {
|
2020-02-06 11:21:02 -08:00
|
|
|
_, ok := i.(uint32)
|
2019-12-18 05:20:02 -08:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid parameter type: %T", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
func validateBondDenom(i interface{}) error {
|
|
|
|
v, ok := i.(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid parameter type: %T", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.TrimSpace(v) == "" {
|
|
|
|
return errors.New("bond denom cannot be blank")
|
|
|
|
}
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
if err := sdk.ValidateDenom(v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-07 17:41:23 -08:00
|
|
|
return nil
|
|
|
|
}
|
2021-01-27 02:24:00 -08:00
|
|
|
|
2021-04-07 16:56:39 -07:00
|
|
|
func ValidatePowerReduction(i interface{}) error {
|
2021-01-27 02:24:00 -08:00
|
|
|
v, ok := i.(sdk.Int)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid parameter type: %T", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.LT(sdk.NewInt(1)) {
|
|
|
|
return fmt.Errorf("power reduction cannot be lower than 1")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|