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

71 lines
2.3 KiB
Go
Raw Normal View History

package types
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// GenesisState - all staking state that must be provided at genesis
type GenesisState struct {
StartingProposalID uint64 `json:"starting_proposal_id" yaml:"starting_proposal_id"`
Deposits Deposits `json:"deposits" yaml:"deposits"`
Votes Votes `json:"votes" yaml:"votes"`
Proposals Proposals `json:"proposals" yaml:"proposals"`
DepositParams DepositParams `json:"deposit_params" yaml:"deposit_params"`
VotingParams VotingParams `json:"voting_params" yaml:"voting_params"`
TallyParams TallyParams `json:"tally_params" yaml:"tally_params"`
}
// NewGenesisState creates a new genesis state for the governance module
func NewGenesisState(startingProposalID uint64, dp DepositParams, vp VotingParams, tp TallyParams) GenesisState {
return GenesisState{
StartingProposalID: startingProposalID,
DepositParams: dp,
VotingParams: vp,
TallyParams: tp,
}
}
// DefaultGenesisState defines the default governance genesis state
func DefaultGenesisState() GenesisState {
return NewGenesisState(
DefaultStartingProposalID,
DefaultDepositParams(),
DefaultVotingParams(),
DefaultTallyParams(),
)
}
// IsEmpty returns true if a GenesisState is empty
func (data GenesisState) IsEmpty() bool {
2020-03-02 11:13:42 -08:00
return data.Deposits == nil &&
data.Votes == nil &&
data.Proposals == nil &&
data.DepositParams.Equal(DepositParams{}) &&
data.TallyParams.Equal(TallyParams{}) &&
data.VotingParams.Equal(VotingParams{})
}
// ValidateGenesis checks if parameters are within valid ranges
func ValidateGenesis(data GenesisState) error {
threshold := data.TallyParams.Threshold
if threshold.IsNegative() || threshold.GT(sdk.OneDec()) {
2019-08-19 09:06:27 -07:00
return fmt.Errorf("governance vote threshold should be positive and less or equal to one, is %s",
threshold.String())
}
veto := data.TallyParams.Veto
if veto.IsNegative() || veto.GT(sdk.OneDec()) {
2019-08-19 09:06:27 -07:00
return fmt.Errorf("governance vote veto threshold should be positive and less or equal to one, is %s",
veto.String())
}
if !data.DepositParams.MinDeposit.IsValid() {
2019-08-19 09:06:27 -07:00
return fmt.Errorf("governance deposit amount must be a valid sdk.Coins amount, is %s",
data.DepositParams.MinDeposit.String())
}
return nil
}