2019-08-08 12:51:18 -07:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2021-12-16 10:23:47 -08:00
|
|
|
"errors"
|
2019-08-08 12:51:18 -07:00
|
|
|
"fmt"
|
|
|
|
|
2020-05-19 13:17:29 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec/types"
|
2019-08-08 12:51:18 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewGenesisState creates a new genesis state for the governance module
|
2020-08-11 07:22:30 -07:00
|
|
|
func NewGenesisState(startingProposalID uint64, dp DepositParams, vp VotingParams, tp TallyParams) *GenesisState {
|
|
|
|
return &GenesisState{
|
2020-08-14 01:55:54 -07:00
|
|
|
StartingProposalId: startingProposalID,
|
2021-12-16 10:23:47 -08:00
|
|
|
DepositParams: &dp,
|
|
|
|
VotingParams: &vp,
|
|
|
|
TallyParams: &tp,
|
2019-08-08 12:51:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultGenesisState defines the default governance genesis state
|
2020-08-11 07:22:30 -07:00
|
|
|
func DefaultGenesisState() *GenesisState {
|
2019-08-08 12:51:18 -07:00
|
|
|
return NewGenesisState(
|
|
|
|
DefaultStartingProposalID,
|
|
|
|
DefaultDepositParams(),
|
|
|
|
DefaultVotingParams(),
|
|
|
|
DefaultTallyParams(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-06-11 14:32:24 -07:00
|
|
|
// Empty returns true if a GenesisState is empty
|
|
|
|
func (data GenesisState) Empty() bool {
|
2021-12-16 10:23:47 -08:00
|
|
|
return data.StartingProposalId == 0 ||
|
|
|
|
data.DepositParams == nil ||
|
|
|
|
data.VotingParams == nil ||
|
|
|
|
data.TallyParams == nil
|
2019-08-08 12:51:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateGenesis checks if parameters are within valid ranges
|
2020-08-11 07:22:30 -07:00
|
|
|
func ValidateGenesis(data *GenesisState) error {
|
2021-12-16 10:23:47 -08:00
|
|
|
if data.StartingProposalId == 0 {
|
|
|
|
return errors.New("Starting proposal id must be greater than 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := validateTallyParams(data.TallyParams); err != nil {
|
|
|
|
return fmt.Errorf("invalid tally params: %w", err)
|
2019-08-08 12:51:18 -07:00
|
|
|
}
|
|
|
|
|
2021-12-16 10:23:47 -08:00
|
|
|
if err := validateVotingParams(data.VotingParams); err != nil {
|
|
|
|
return fmt.Errorf("invalid voting params: %w", err)
|
2019-08-08 12:51:18 -07:00
|
|
|
}
|
|
|
|
|
2021-12-16 10:23:47 -08:00
|
|
|
if err := validateDepositParams(data.DepositParams); err != nil {
|
|
|
|
return fmt.Errorf("invalid deposit params: %w", err)
|
2019-08-08 12:51:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-19 13:17:29 -07:00
|
|
|
|
|
|
|
var _ types.UnpackInterfacesMessage = GenesisState{}
|
|
|
|
|
|
|
|
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
|
|
|
|
func (data GenesisState) UnpackInterfaces(unpacker types.AnyUnpacker) error {
|
|
|
|
for _, p := range data.Proposals {
|
|
|
|
err := p.UnpackInterfaces(unpacker)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|