cosmos-sdk/x/staking/types/params.go

208 lines
4.8 KiB
Go
Raw Normal View History

Merge PR #1119: Unbonding, Redelegation * stake/fees spec updates * staking overview.md revisions, moving files * docs reorganization * staking spec state revisions * transaction stake updates * complete staking spec update * WIP adding unbonding/redelegation commands * added msg types for unbonding, redelegation * stake sub-package reorg * working stake reorg * modify lcd tests to not use hardcoded json strings * add description update * index keys * key managment for unbonding redelegation complete * update stake errors * completed handleMsgCompleteUnbonding fn * updated to use begin/complete unbonding/redelegation * fix token shares bug * develop docs into unbonding * got non-tests compiling after merge develop * working fixing tests * PrivlegedKeeper -> PrivilegedKeeper * tests compile * fix some tests * fixing tests * remove PrivilegedKeeper * get unbonding bug * only rpc sig verification failed tests now * move percent unbonding/redelegation to the CLI and out of handler logic * remove min unbonding height * add lcd txs * add pool sanity checks, fix a buncha tests * fix ante. set lcd log to debug (#1322) * redelegation tests, adding query functionality for bonds * add self-delegations at genesis ref #1165 * PR comments (mostly) addressed * cleanup, added Query LCD functionality * test cleanup/fixes * fix governance test * SlashValidatorSet -> ValidatorSet * changelog * stake lcd fix * x/auth: fix chainID in ante * fix lcd test * fix lint, update lint make command for spelling * lowercase error string * don't expose coinkeeper in staking * remove a few duplicate lines in changelog * chain_id in stake lcd tests * added transient redelegation * 'transient' => 'transitive' * Re-add nolint instruction * Fix tiny linter error
2018-06-26 19:00:12 -07:00
package types
import (
2019-12-10 08:48:57 -08:00
"errors"
"fmt"
2019-12-10 08:48:57 -08:00
"strings"
"time"
2018-05-18 15:57:47 -07:00
yaml "gopkg.in/yaml.v2"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)
// Staking params default values
2018-10-05 10:56:17 -07:00
const (
// DefaultUnbondingTime reflects three weeks in seconds as the default
2018-10-05 10:56:17 -07:00
// unbonding time.
// TODO: Justify our choice of default here.
DefaultUnbondingTime time.Duration = time.Hour * 24 * 7 * 3
// Default maximum number of bonded validators
DefaultMaxValidators uint32 = 100
// Default maximum entries in a UBD/RED pair
DefaultMaxEntries uint32 = 7
// DefaultHistorical entries is 10000. Apps that don't use IBC can ignore this
// value by not adding the staking module to the application module manager's
// SetOrderBeginBlockers.
DefaultHistoricalEntries uint32 = 10000
2018-10-05 10:56:17 -07:00
)
2018-09-27 11:52:29 -07:00
var (
KeyUnbondingTime = []byte("UnbondingTime")
KeyMaxValidators = []byte("MaxValidators")
KeyMaxEntries = []byte("MaxEntries")
KeyBondDenom = []byte("BondDenom")
KeyHistoricalEntries = []byte("HistoricalEntries")
KeyPowerReduction = []byte("PowerReduction")
2018-09-17 08:28:13 -07:00
)
var _ paramtypes.ParamSet = (*Params)(nil)
2018-10-06 06:50:58 -07:00
// ParamTable for staking module
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
// NewParams creates a new Params instance
revert: Turn staking power reduction into an on-chain param (#9495) <!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Closes: #9447 This PR partially reverts #8505. Namely: - it removes PowerReduction as a staking on-chain param - however, it keeps #8505's API changes regarding adding a `powerReduction` function argument to staking functions. This allows us to rely less on global variables in said functions. <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2021-06-14 07:45:15 -07:00
func NewParams(unbondingTime time.Duration, maxValidators, maxEntries, historicalEntries uint32, bondDenom string) Params {
return Params{
UnbondingTime: unbondingTime,
MaxValidators: maxValidators,
MaxEntries: maxEntries,
HistoricalEntries: historicalEntries,
BondDenom: bondDenom,
}
}
// Implements params.ParamSet
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),
}
}
// DefaultParams returns a default set of parameters.
func DefaultParams() Params {
return NewParams(
DefaultUnbondingTime,
DefaultMaxValidators,
DefaultMaxEntries,
DefaultHistoricalEntries,
sdk.DefaultBondDenom,
)
}
// String returns a human readable string representation of the parameters.
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
// unmarshal the current staking params value from store key or panic
func MustUnmarshalParams(cdc *codec.LegacyAmino, value []byte) Params {
params, err := UnmarshalParams(cdc, value)
if err != nil {
panic(err)
}
return params
}
// unmarshal the current staking params value from store key
func UnmarshalParams(cdc *codec.LegacyAmino, value []byte) (params Params, err error) {
err = cdc.Unmarshal(value, &params)
if err != nil {
return
}
return
}
// 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-12-10 08:48:57 -08:00
if err := validateMaxValidators(p.MaxValidators); err != nil {
return err
}
2019-12-10 08:48:57 -08:00
if err := validateMaxEntries(p.MaxEntries); err != nil {
return err
}
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 {
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 {
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
}
func validateHistoricalEntries(i interface{}) error {
_, ok := i.(uint32)
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")
}
2019-12-10 08:48:57 -08:00
if err := sdk.ValidateDenom(v); err != nil {
return err
}
return nil
}
2021-04-07 16:56:39 -07:00
func ValidatePowerReduction(i interface{}) error {
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
}