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

217 lines
6.5 KiB
Go
Raw Normal View History

package types
2018-06-21 17:19:14 -07:00
import (
"fmt"
"time"
2020-03-02 11:13:42 -08:00
"gopkg.in/yaml.v2"
2018-06-21 17:19:14 -07:00
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
2018-06-21 17:19:14 -07:00
)
// Default period for deposits & voting
const (
DefaultPeriod time.Duration = time.Hour * 24 * 2 // 2 days
)
// Default governance params
var (
DefaultMinDepositTokens = sdk.TokensFromConsensusPower(10)
DefaultQuorum = sdk.NewDecWithPrec(334, 3)
DefaultThreshold = sdk.NewDecWithPrec(5, 1)
DefaultVeto = sdk.NewDecWithPrec(334, 3)
)
// Parameter store key
var (
ParamStoreKeyDepositParams = []byte("depositparams")
ParamStoreKeyVotingParams = []byte("votingparams")
ParamStoreKeyTallyParams = []byte("tallyparams")
)
// ParamKeyTable - Key declaration for parameters
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable(
paramtypes.NewParamSetPair(ParamStoreKeyDepositParams, DepositParams{}, validateDepositParams),
paramtypes.NewParamSetPair(ParamStoreKeyVotingParams, VotingParams{}, validateVotingParams),
paramtypes.NewParamSetPair(ParamStoreKeyTallyParams, TallyParams{}, validateTallyParams),
)
}
// DepositParams defines the params around deposits for governance
type DepositParams struct {
2019-07-05 16:25:56 -07:00
MinDeposit sdk.Coins `json:"min_deposit,omitempty" yaml:"min_deposit,omitempty"` // Minimum deposit for a proposal to enter voting period.
MaxDepositPeriod time.Duration `json:"max_deposit_period,omitempty" yaml:"max_deposit_period,omitempty"` // Maximum period for Atom holders to deposit on a proposal. Initial value: 2 months
2018-06-21 17:19:14 -07:00
}
Merge PR #4159: Module/Genesis Generalization * first commit * gaia cleanup * ... * staking multihooks * missing module function return args * bank module name constant * working, module interface for x/ * got this thing compiling * make test compiles and passes * remove expanded simulation invariants * genesis issue * continued * continued * register crisis routes thought mm * begin blocker to mm * end blocker to mm * empty routes not initialized * move gaia initChainer sanity check to baseapp * remove codecs from module manager * reorging genesis stuff * module manager passed by reference/bugfixes from working last commit int int * move invariant checks from gaia to crisis * typo * basic refactors cmd/gaia/init * working * MultiStakingHooks from types to x/staking/types int * default module manager order of operations from input modules * working * typo * add AppModuleBasic * moduleBasicManager / non-test code compiles * working attempting to get tests passing * make test passes * sim random genesis fix * export bug * ... * genutil module * genutil working * refactored - happy with non-testing code in cmd/ * ... * lint fixes * comment improvement * cli test fix * compile housing * working through compile errors * working gettin' compilin' * non-test code compiles * move testnet to its own module * reworking tests int * bez staging PR 1 comments * concise module function-of names * moved all tests from genesis_test.go to other genutil tests * genaccounts package, add genutil and genaccounts to app.go * docs for genutil genaccounts * genaccounts iterate fn * non-test code with genaccounts/ now compiles * working test compiling * debugging tests * resolved all make test compile errors * test debuggin * resolved all unit tests, introduced param module * cli-test compile fixes * staking initialization bug * code comment improvements, changelog entries * BasicGaiaApp -> ModuleBasics * highlevel explanation in types/module.go * @alexanderbez comment revisions * @fedekunze PR comments * @alexanderbez PR comments (x2) * @cwgoes comments (minor updates) * @fedekunze suggestions * panic on init with multiple validator updates from different modules * initchain panic makes validate genesis fail int * AppModuleGenesis seperation int * test * remove init panic logic in validate genesis replaced with TODO * set maxprocs to match system's GOMAXPROCS * Update circleci * Cap maxprocs in CI to 4 * @alexanderbez recent comments addressed * less blocks in twouble sims int * runsim error output flag * -e on import_export as well * error out int * Try to fix failures * runsim
2019-05-16 08:25:32 -07:00
// NewDepositParams creates a new DepositParams object
func NewDepositParams(minDeposit sdk.Coins, maxDepositPeriod time.Duration) DepositParams {
return DepositParams{
MinDeposit: minDeposit,
MaxDepositPeriod: maxDepositPeriod,
}
}
// DefaultDepositParams default parameters for deposits
func DefaultDepositParams() DepositParams {
return NewDepositParams(
sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, DefaultMinDepositTokens)),
DefaultPeriod,
)
}
// String implements stringer insterface
func (dp DepositParams) String() string {
2020-03-02 11:13:42 -08:00
out, _ := yaml.Marshal(dp)
return string(out)
}
// Equal checks equality of DepositParams
2019-01-07 14:24:04 -08:00
func (dp DepositParams) Equal(dp2 DepositParams) bool {
return dp.MinDeposit.IsEqual(dp2.MinDeposit) && dp.MaxDepositPeriod == dp2.MaxDepositPeriod
}
2019-12-10 08:48:57 -08:00
func validateDepositParams(i interface{}) error {
v, ok := i.(DepositParams)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if !v.MinDeposit.IsValid() {
return fmt.Errorf("invalid minimum deposit: %s", v.MinDeposit)
}
if v.MaxDepositPeriod <= 0 {
return fmt.Errorf("maximum deposit period must be positive: %d", v.MaxDepositPeriod)
}
return nil
}
// TallyParams defines the params around Tallying votes in governance
type TallyParams struct {
2019-07-05 16:25:56 -07:00
Quorum sdk.Dec `json:"quorum,omitempty" yaml:"quorum,omitempty"` // Minimum percentage of total stake needed to vote for a result to be considered valid
Threshold sdk.Dec `json:"threshold,omitempty" yaml:"threshold,omitempty"` // Minimum proportion of Yes votes for proposal to pass. Initial value: 0.5
Veto sdk.Dec `json:"veto,omitempty" yaml:"veto,omitempty"` // Minimum value of Veto votes to Total votes ratio for proposal to be vetoed. Initial value: 1/3
2018-06-21 17:19:14 -07:00
}
Merge PR #4159: Module/Genesis Generalization * first commit * gaia cleanup * ... * staking multihooks * missing module function return args * bank module name constant * working, module interface for x/ * got this thing compiling * make test compiles and passes * remove expanded simulation invariants * genesis issue * continued * continued * register crisis routes thought mm * begin blocker to mm * end blocker to mm * empty routes not initialized * move gaia initChainer sanity check to baseapp * remove codecs from module manager * reorging genesis stuff * module manager passed by reference/bugfixes from working last commit int int * move invariant checks from gaia to crisis * typo * basic refactors cmd/gaia/init * working * MultiStakingHooks from types to x/staking/types int * default module manager order of operations from input modules * working * typo * add AppModuleBasic * moduleBasicManager / non-test code compiles * working attempting to get tests passing * make test passes * sim random genesis fix * export bug * ... * genutil module * genutil working * refactored - happy with non-testing code in cmd/ * ... * lint fixes * comment improvement * cli test fix * compile housing * working through compile errors * working gettin' compilin' * non-test code compiles * move testnet to its own module * reworking tests int * bez staging PR 1 comments * concise module function-of names * moved all tests from genesis_test.go to other genutil tests * genaccounts package, add genutil and genaccounts to app.go * docs for genutil genaccounts * genaccounts iterate fn * non-test code with genaccounts/ now compiles * working test compiling * debugging tests * resolved all make test compile errors * test debuggin * resolved all unit tests, introduced param module * cli-test compile fixes * staking initialization bug * code comment improvements, changelog entries * BasicGaiaApp -> ModuleBasics * highlevel explanation in types/module.go * @alexanderbez comment revisions * @fedekunze PR comments * @alexanderbez PR comments (x2) * @cwgoes comments (minor updates) * @fedekunze suggestions * panic on init with multiple validator updates from different modules * initchain panic makes validate genesis fail int * AppModuleGenesis seperation int * test * remove init panic logic in validate genesis replaced with TODO * set maxprocs to match system's GOMAXPROCS * Update circleci * Cap maxprocs in CI to 4 * @alexanderbez recent comments addressed * less blocks in twouble sims int * runsim error output flag * -e on import_export as well * error out int * Try to fix failures * runsim
2019-05-16 08:25:32 -07:00
// NewTallyParams creates a new TallyParams object
func NewTallyParams(quorum, threshold, veto sdk.Dec) TallyParams {
return TallyParams{
Quorum: quorum,
Threshold: threshold,
Veto: veto,
}
}
// DefaultTallyParams default parameters for tallying
func DefaultTallyParams() TallyParams {
return NewTallyParams(DefaultQuorum, DefaultThreshold, DefaultVeto)
}
2020-03-02 11:13:42 -08:00
// Equal checks equality of TallyParams
func (tp TallyParams) Equal(other TallyParams) bool {
return tp.Quorum.Equal(other.Quorum) && tp.Threshold.Equal(other.Threshold) && tp.Veto.Equal(other.Veto)
}
// String implements stringer insterface
func (tp TallyParams) String() string {
2020-03-02 11:13:42 -08:00
out, _ := yaml.Marshal(tp)
return string(out)
}
2019-12-10 08:48:57 -08:00
func validateTallyParams(i interface{}) error {
v, ok := i.(TallyParams)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.Quorum.IsNegative() {
return fmt.Errorf("quorom cannot be negative: %s", v.Quorum)
}
if v.Quorum.GT(sdk.OneDec()) {
return fmt.Errorf("quorom too large: %s", v)
}
if !v.Threshold.IsPositive() {
return fmt.Errorf("vote threshold must be positive: %s", v.Threshold)
}
if v.Threshold.GT(sdk.OneDec()) {
return fmt.Errorf("vote threshold too large: %s", v)
}
if !v.Veto.IsPositive() {
return fmt.Errorf("veto threshold must be positive: %s", v.Threshold)
}
if v.Veto.GT(sdk.OneDec()) {
return fmt.Errorf("veto threshold too large: %s", v)
}
return nil
}
// VotingParams defines the params around Voting in governance
type VotingParams struct {
2019-07-05 16:25:56 -07:00
VotingPeriod time.Duration `json:"voting_period,omitempty" yaml:"voting_period,omitempty"` // Length of the voting period.
2018-06-21 17:19:14 -07:00
}
Merge PR #4159: Module/Genesis Generalization * first commit * gaia cleanup * ... * staking multihooks * missing module function return args * bank module name constant * working, module interface for x/ * got this thing compiling * make test compiles and passes * remove expanded simulation invariants * genesis issue * continued * continued * register crisis routes thought mm * begin blocker to mm * end blocker to mm * empty routes not initialized * move gaia initChainer sanity check to baseapp * remove codecs from module manager * reorging genesis stuff * module manager passed by reference/bugfixes from working last commit int int * move invariant checks from gaia to crisis * typo * basic refactors cmd/gaia/init * working * MultiStakingHooks from types to x/staking/types int * default module manager order of operations from input modules * working * typo * add AppModuleBasic * moduleBasicManager / non-test code compiles * working attempting to get tests passing * make test passes * sim random genesis fix * export bug * ... * genutil module * genutil working * refactored - happy with non-testing code in cmd/ * ... * lint fixes * comment improvement * cli test fix * compile housing * working through compile errors * working gettin' compilin' * non-test code compiles * move testnet to its own module * reworking tests int * bez staging PR 1 comments * concise module function-of names * moved all tests from genesis_test.go to other genutil tests * genaccounts package, add genutil and genaccounts to app.go * docs for genutil genaccounts * genaccounts iterate fn * non-test code with genaccounts/ now compiles * working test compiling * debugging tests * resolved all make test compile errors * test debuggin * resolved all unit tests, introduced param module * cli-test compile fixes * staking initialization bug * code comment improvements, changelog entries * BasicGaiaApp -> ModuleBasics * highlevel explanation in types/module.go * @alexanderbez comment revisions * @fedekunze PR comments * @alexanderbez PR comments (x2) * @cwgoes comments (minor updates) * @fedekunze suggestions * panic on init with multiple validator updates from different modules * initchain panic makes validate genesis fail int * AppModuleGenesis seperation int * test * remove init panic logic in validate genesis replaced with TODO * set maxprocs to match system's GOMAXPROCS * Update circleci * Cap maxprocs in CI to 4 * @alexanderbez recent comments addressed * less blocks in twouble sims int * runsim error output flag * -e on import_export as well * error out int * Try to fix failures * runsim
2019-05-16 08:25:32 -07:00
// NewVotingParams creates a new VotingParams object
func NewVotingParams(votingPeriod time.Duration) VotingParams {
return VotingParams{
VotingPeriod: votingPeriod,
}
}
// DefaultVotingParams default parameters for voting
func DefaultVotingParams() VotingParams {
return NewVotingParams(DefaultPeriod)
}
2020-03-02 11:13:42 -08:00
// Equal checks equality of TallyParams
func (vp VotingParams) Equal(other VotingParams) bool {
return vp.VotingPeriod == other.VotingPeriod
}
// String implements stringer interface
func (vp VotingParams) String() string {
2020-03-02 11:13:42 -08:00
out, _ := yaml.Marshal(vp)
return string(out)
}
2019-12-10 08:48:57 -08:00
func validateVotingParams(i interface{}) error {
v, ok := i.(VotingParams)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.VotingPeriod <= 0 {
return fmt.Errorf("voting period must be positive: %s", v.VotingPeriod)
}
return nil
}
// Params returns all of the governance params
type Params struct {
2019-07-05 16:25:56 -07:00
VotingParams VotingParams `json:"voting_params" yaml:"voting_params"`
TallyParams TallyParams `json:"tally_params" yaml:"tally_params"`
DepositParams DepositParams `json:"deposit_params" yaml:"deposit_params"`
}
func (gp Params) String() string {
return gp.VotingParams.String() + "\n" +
gp.TallyParams.String() + "\n" + gp.DepositParams.String()
}
// NewParams creates a new gov Params instance
func NewParams(vp VotingParams, tp TallyParams, dp DepositParams) Params {
return Params{
VotingParams: vp,
DepositParams: dp,
TallyParams: tp,
}
}
// DefaultParams default governance params
func DefaultParams() Params {
return NewParams(DefaultVotingParams(), DefaultTallyParams(), DefaultDepositParams())
}