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

141 lines
3.7 KiB
Go
Raw Normal View History

package types
import (
"fmt"
refactor: Implementing sigs.k8s.io YAML to remove .proto yaml annotations (#9780) ## Description Draft of: #9705 Started off with changing codec `MarshalYaml` function to directly go from JSON to yaml using the new library. Replaced the only usage of UnmarshalYaml per request. --- ### 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... - [x] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [x] reviewed state machine logic - [x] reviewed API design and naming - [x] reviewed documentation is accurate - [x] reviewed tests and test coverage - [x] manually tested (if applicable)
2021-09-24 07:37:34 -07:00
"sigs.k8s.io/yaml"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)
// Parameter keys
var (
ParamStoreKeyCommunityTax = []byte("communitytax")
ParamStoreKeyBaseProposerReward = []byte("baseproposerreward")
ParamStoreKeyBonusProposerReward = []byte("bonusproposerreward")
ParamStoreKeyWithdrawAddrEnabled = []byte("withdrawaddrenabled")
)
// ParamKeyTable returns the parameter key table.
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
// DefaultParams returns default distribution parameters
func DefaultParams() Params {
return Params{
CommunityTax: sdk.NewDecWithPrec(2, 2), // 2%
BaseProposerReward: sdk.NewDecWithPrec(1, 2), // 1%
BonusProposerReward: sdk.NewDecWithPrec(4, 2), // 4%
WithdrawAddrEnabled: true,
}
}
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
// ParamSetPairs returns the parameter set pairs.
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(ParamStoreKeyCommunityTax, &p.CommunityTax, validateCommunityTax),
paramtypes.NewParamSetPair(ParamStoreKeyBaseProposerReward, &p.BaseProposerReward, validateBaseProposerReward),
paramtypes.NewParamSetPair(ParamStoreKeyBonusProposerReward, &p.BonusProposerReward, validateBonusProposerReward),
paramtypes.NewParamSetPair(ParamStoreKeyWithdrawAddrEnabled, &p.WithdrawAddrEnabled, validateWithdrawAddrEnabled),
}
}
// ValidateBasic performs basic validation on distribution parameters.
func (p Params) ValidateBasic() error {
if p.CommunityTax.IsNegative() || p.CommunityTax.GT(sdk.OneDec()) {
return fmt.Errorf(
"community tax should be non-negative and less than one: %s", p.CommunityTax,
)
}
if p.BaseProposerReward.IsNegative() {
return fmt.Errorf(
"base proposer reward should be positive: %s", p.BaseProposerReward,
)
}
if p.BonusProposerReward.IsNegative() {
return fmt.Errorf(
"bonus proposer reward should be positive: %s", p.BonusProposerReward,
)
}
if v := p.BaseProposerReward.Add(p.BonusProposerReward).Add(p.CommunityTax); v.GT(sdk.OneDec()) {
return fmt.Errorf(
"sum of base, bonus proposer rewards, and community tax cannot be greater than one: %s", v,
)
}
return nil
}
func validateCommunityTax(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNil() {
return fmt.Errorf("community tax must be not nil")
}
if v.IsNegative() {
return fmt.Errorf("community tax must be positive: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("community tax too large: %s", v)
}
return nil
}
func validateBaseProposerReward(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNil() {
return fmt.Errorf("base proposer reward must be not nil")
}
if v.IsNegative() {
return fmt.Errorf("base proposer reward must be positive: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("base proposer reward too large: %s", v)
}
return nil
}
func validateBonusProposerReward(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNil() {
return fmt.Errorf("bonus proposer reward must be not nil")
}
if v.IsNegative() {
return fmt.Errorf("bonus proposer reward must be positive: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("bonus proposer reward too large: %s", v)
}
return nil
}
func validateWithdrawAddrEnabled(i interface{}) error {
_, ok := i.(bool)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}