github.com/cosmos/cosmos-sdk@v0.50.10/x/gov/types/v1beta1/genesis.go (about) 1 package v1beta1 2 3 import ( 4 "fmt" 5 6 "cosmossdk.io/math" 7 8 "github.com/cosmos/cosmos-sdk/codec/types" 9 ) 10 11 // NewGenesisState creates a new genesis state for the governance module 12 func NewGenesisState(startingProposalID uint64, dp DepositParams, vp VotingParams, tp TallyParams) *GenesisState { 13 return &GenesisState{ 14 StartingProposalId: startingProposalID, 15 DepositParams: dp, 16 VotingParams: vp, 17 TallyParams: tp, 18 } 19 } 20 21 // DefaultGenesisState defines the default governance genesis state 22 func DefaultGenesisState() *GenesisState { 23 return NewGenesisState( 24 DefaultStartingProposalID, 25 DefaultDepositParams(), 26 DefaultVotingParams(), 27 DefaultTallyParams(), 28 ) 29 } 30 31 // Equal returns true if the GenesisStates are equal 32 func (data GenesisState) Equal(other GenesisState) bool { 33 return data.StartingProposalId == other.StartingProposalId && 34 data.Deposits.Equal(other.Deposits) && 35 data.Votes.Equal(other.Votes) && 36 data.Proposals.Equal(other.Proposals) && 37 data.DepositParams.Equal(other.DepositParams) && 38 data.TallyParams.Equal(other.TallyParams) && 39 data.VotingParams.Equal(other.VotingParams) 40 } 41 42 // Empty returns true if a GenesisState is empty 43 func (data GenesisState) Empty() bool { 44 return data.Equal(GenesisState{}) 45 } 46 47 // ValidateGenesis checks if parameters are within valid ranges 48 func ValidateGenesis(data *GenesisState) error { 49 threshold := data.TallyParams.Threshold 50 if threshold.IsNegative() || threshold.GT(math.LegacyOneDec()) { 51 return fmt.Errorf("governance vote threshold should be positive and less or equal to one, is %s", 52 threshold.String()) 53 } 54 55 veto := data.TallyParams.VetoThreshold 56 if veto.IsNegative() || veto.GT(math.LegacyOneDec()) { 57 return fmt.Errorf("governance vote veto threshold should be positive and less or equal to one, is %s", 58 veto.String()) 59 } 60 61 if !data.DepositParams.MinDeposit.IsValid() { 62 return fmt.Errorf("governance deposit amount must be a valid sdk.Coins amount, is %s", 63 data.DepositParams.MinDeposit.String()) 64 } 65 66 return nil 67 } 68 69 var _ types.UnpackInterfacesMessage = GenesisState{} 70 71 // UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces 72 func (data GenesisState) UnpackInterfaces(unpacker types.AnyUnpacker) error { 73 for _, p := range data.Proposals { 74 err := p.UnpackInterfaces(unpacker) 75 if err != nil { 76 return err 77 } 78 } 79 return nil 80 }