github.com/cosmos/cosmos-sdk@v0.50.10/x/gov/types/v1/params.go (about)

     1  package v1
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	sdkmath "cosmossdk.io/math"
     8  
     9  	sdk "github.com/cosmos/cosmos-sdk/types"
    10  )
    11  
    12  // Default period for deposits & voting
    13  const (
    14  	DefaultPeriod                         time.Duration = time.Hour * 24 * 2 // 2 days
    15  	DefaultExpeditedPeriod                time.Duration = time.Hour * 24 * 1 // 1 day
    16  	DefaultMinExpeditedDepositTokensRatio               = 5
    17  )
    18  
    19  // Default governance params
    20  var (
    21  	DefaultMinDepositTokens          = sdkmath.NewInt(10000000)
    22  	DefaultMinExpeditedDepositTokens = DefaultMinDepositTokens.Mul(sdkmath.NewInt(DefaultMinExpeditedDepositTokensRatio))
    23  	DefaultQuorum                    = sdkmath.LegacyNewDecWithPrec(334, 3)
    24  	DefaultThreshold                 = sdkmath.LegacyNewDecWithPrec(5, 1)
    25  	DefaultExpeditedThreshold        = sdkmath.LegacyNewDecWithPrec(667, 3)
    26  	DefaultVetoThreshold             = sdkmath.LegacyNewDecWithPrec(334, 3)
    27  	DefaultMinInitialDepositRatio    = sdkmath.LegacyZeroDec()
    28  	DefaultProposalCancelRatio       = sdkmath.LegacyMustNewDecFromStr("0.5")
    29  	DefaultProposalCancelDestAddress = ""
    30  	DefaultBurnProposalPrevote       = false // set to false to replicate behavior of when this change was made (0.47)
    31  	DefaultBurnVoteQuorom            = false // set to false to  replicate behavior of when this change was made (0.47)
    32  	DefaultBurnVoteVeto              = true  // set to true to replicate behavior of when this change was made (0.47)
    33  	DefaultMinDepositRatio           = sdkmath.LegacyMustNewDecFromStr("0.01")
    34  )
    35  
    36  // Deprecated: NewDepositParams creates a new DepositParams object
    37  func NewDepositParams(minDeposit sdk.Coins, maxDepositPeriod *time.Duration) DepositParams {
    38  	return DepositParams{
    39  		MinDeposit:       minDeposit,
    40  		MaxDepositPeriod: maxDepositPeriod,
    41  	}
    42  }
    43  
    44  // Deprecated: NewTallyParams creates a new TallyParams object
    45  func NewTallyParams(quorum, threshold, vetoThreshold string) TallyParams {
    46  	return TallyParams{
    47  		Quorum:        quorum,
    48  		Threshold:     threshold,
    49  		VetoThreshold: vetoThreshold,
    50  	}
    51  }
    52  
    53  // Deprecated: NewVotingParams creates a new VotingParams object
    54  func NewVotingParams(votingPeriod *time.Duration) VotingParams {
    55  	return VotingParams{
    56  		VotingPeriod: votingPeriod,
    57  	}
    58  }
    59  
    60  // NewParams creates a new Params instance with given values.
    61  func NewParams(
    62  	minDeposit, expeditedminDeposit sdk.Coins, maxDepositPeriod, votingPeriod, expeditedVotingPeriod time.Duration,
    63  	quorum, threshold, expeditedThreshold, vetoThreshold, minInitialDepositRatio, proposalCancelRatio, proposalCancelDest string,
    64  	burnProposalDeposit, burnVoteQuorum, burnVoteVeto bool, minDepositRatio string,
    65  ) Params {
    66  	return Params{
    67  		MinDeposit:                 minDeposit,
    68  		ExpeditedMinDeposit:        expeditedminDeposit,
    69  		MaxDepositPeriod:           &maxDepositPeriod,
    70  		VotingPeriod:               &votingPeriod,
    71  		ExpeditedVotingPeriod:      &expeditedVotingPeriod,
    72  		Quorum:                     quorum,
    73  		Threshold:                  threshold,
    74  		ExpeditedThreshold:         expeditedThreshold,
    75  		VetoThreshold:              vetoThreshold,
    76  		MinInitialDepositRatio:     minInitialDepositRatio,
    77  		ProposalCancelRatio:        proposalCancelRatio,
    78  		ProposalCancelDest:         proposalCancelDest,
    79  		BurnProposalDepositPrevote: burnProposalDeposit,
    80  		BurnVoteQuorum:             burnVoteQuorum,
    81  		BurnVoteVeto:               burnVoteVeto,
    82  		MinDepositRatio:            minDepositRatio,
    83  	}
    84  }
    85  
    86  // DefaultParams returns the default governance params
    87  func DefaultParams() Params {
    88  	return NewParams(
    89  		sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, DefaultMinDepositTokens)),
    90  		sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, DefaultMinExpeditedDepositTokens)),
    91  		DefaultPeriod,
    92  		DefaultPeriod,
    93  		DefaultExpeditedPeriod,
    94  		DefaultQuorum.String(),
    95  		DefaultThreshold.String(),
    96  		DefaultExpeditedThreshold.String(),
    97  		DefaultVetoThreshold.String(),
    98  		DefaultMinInitialDepositRatio.String(),
    99  		DefaultProposalCancelRatio.String(),
   100  		DefaultProposalCancelDestAddress,
   101  		DefaultBurnProposalPrevote,
   102  		DefaultBurnVoteQuorom,
   103  		DefaultBurnVoteVeto,
   104  		DefaultMinDepositRatio.String(),
   105  	)
   106  }
   107  
   108  // ValidateBasic performs basic validation on governance parameters.
   109  func (p Params) ValidateBasic() error {
   110  	minDeposit := sdk.Coins(p.MinDeposit)
   111  	if minDeposit.Empty() || !minDeposit.IsValid() {
   112  		return fmt.Errorf("invalid minimum deposit: %s", minDeposit)
   113  	}
   114  
   115  	if minExpeditedDeposit := sdk.Coins(p.ExpeditedMinDeposit); minExpeditedDeposit.Empty() || !minExpeditedDeposit.IsValid() {
   116  		return fmt.Errorf("invalid expedited minimum deposit: %s", minExpeditedDeposit)
   117  	} else if minExpeditedDeposit.IsAllLTE(minDeposit) {
   118  		return fmt.Errorf("expedited minimum deposit must be greater than minimum deposit: %s", minExpeditedDeposit)
   119  	}
   120  
   121  	if p.MaxDepositPeriod == nil {
   122  		return fmt.Errorf("maximum deposit period must not be nil: %d", p.MaxDepositPeriod)
   123  	}
   124  
   125  	if p.MaxDepositPeriod.Seconds() <= 0 {
   126  		return fmt.Errorf("maximum deposit period must be positive: %d", p.MaxDepositPeriod)
   127  	}
   128  
   129  	quorum, err := sdkmath.LegacyNewDecFromStr(p.Quorum)
   130  	if err != nil {
   131  		return fmt.Errorf("invalid quorum string: %w", err)
   132  	}
   133  	if quorum.IsNegative() {
   134  		return fmt.Errorf("quorom cannot be negative: %s", quorum)
   135  	}
   136  	if quorum.GT(sdkmath.LegacyOneDec()) {
   137  		return fmt.Errorf("quorom too large: %s", p.Quorum)
   138  	}
   139  
   140  	threshold, err := sdkmath.LegacyNewDecFromStr(p.Threshold)
   141  	if err != nil {
   142  		return fmt.Errorf("invalid threshold string: %w", err)
   143  	}
   144  	if !threshold.IsPositive() {
   145  		return fmt.Errorf("vote threshold must be positive: %s", threshold)
   146  	}
   147  	if threshold.GT(sdkmath.LegacyOneDec()) {
   148  		return fmt.Errorf("vote threshold too large: %s", threshold)
   149  	}
   150  
   151  	expeditedThreshold, err := sdkmath.LegacyNewDecFromStr(p.ExpeditedThreshold)
   152  	if err != nil {
   153  		return fmt.Errorf("invalid expedited threshold string: %w", err)
   154  	}
   155  	if !expeditedThreshold.IsPositive() {
   156  		return fmt.Errorf("expedited vote threshold must be positive: %s", expeditedThreshold)
   157  	}
   158  	if expeditedThreshold.GT(sdkmath.LegacyOneDec()) {
   159  		return fmt.Errorf("expedited vote threshold too large: %s", expeditedThreshold)
   160  	}
   161  	if expeditedThreshold.LTE(threshold) {
   162  		return fmt.Errorf("expedited vote threshold %s, must be greater than the regular threshold %s", expeditedThreshold, threshold)
   163  	}
   164  
   165  	vetoThreshold, err := sdkmath.LegacyNewDecFromStr(p.VetoThreshold)
   166  	if err != nil {
   167  		return fmt.Errorf("invalid vetoThreshold string: %w", err)
   168  	}
   169  	if !vetoThreshold.IsPositive() {
   170  		return fmt.Errorf("veto threshold must be positive: %s", vetoThreshold)
   171  	}
   172  	if vetoThreshold.GT(sdkmath.LegacyOneDec()) {
   173  		return fmt.Errorf("veto threshold too large: %s", vetoThreshold)
   174  	}
   175  
   176  	if p.VotingPeriod == nil {
   177  		return fmt.Errorf("voting period must not be nil: %d", p.VotingPeriod)
   178  	}
   179  	if p.VotingPeriod.Seconds() <= 0 {
   180  		return fmt.Errorf("voting period must be positive: %s", p.VotingPeriod)
   181  	}
   182  
   183  	if p.ExpeditedVotingPeriod == nil {
   184  		return fmt.Errorf("expedited voting period must not be nil: %d", p.ExpeditedVotingPeriod)
   185  	}
   186  	if p.ExpeditedVotingPeriod.Seconds() <= 0 {
   187  		return fmt.Errorf("expedited voting period must be positive: %s", p.ExpeditedVotingPeriod)
   188  	}
   189  	if p.ExpeditedVotingPeriod.Seconds() >= p.VotingPeriod.Seconds() {
   190  		return fmt.Errorf("expedited voting period %s must be strictly less that the regular voting period %s", p.ExpeditedVotingPeriod, p.VotingPeriod)
   191  	}
   192  
   193  	minInitialDepositRatio, err := sdkmath.LegacyNewDecFromStr(p.MinInitialDepositRatio)
   194  	if err != nil {
   195  		return fmt.Errorf("invalid mininum initial deposit ratio of proposal: %w", err)
   196  	}
   197  	if minInitialDepositRatio.IsNegative() {
   198  		return fmt.Errorf("mininum initial deposit ratio of proposal must be positive: %s", minInitialDepositRatio)
   199  	}
   200  	if minInitialDepositRatio.GT(sdkmath.LegacyOneDec()) {
   201  		return fmt.Errorf("mininum initial deposit ratio of proposal is too large: %s", minInitialDepositRatio)
   202  	}
   203  
   204  	proposalCancelRate, err := sdkmath.LegacyNewDecFromStr(p.ProposalCancelRatio)
   205  	if err != nil {
   206  		return fmt.Errorf("invalid burn rate of cancel proposal: %w", err)
   207  	}
   208  	if proposalCancelRate.IsNegative() {
   209  		return fmt.Errorf("burn rate of cancel proposal must be positive: %s", proposalCancelRate)
   210  	}
   211  	if proposalCancelRate.GT(sdkmath.LegacyOneDec()) {
   212  		return fmt.Errorf("burn rate of cancel proposal is too large: %s", proposalCancelRate)
   213  	}
   214  
   215  	if len(p.ProposalCancelDest) != 0 {
   216  		_, err := sdk.AccAddressFromBech32(p.ProposalCancelDest)
   217  		if err != nil {
   218  			return fmt.Errorf("deposits destination address is invalid: %s", p.ProposalCancelDest)
   219  		}
   220  	}
   221  
   222  	return nil
   223  }