github.com/Finschia/finschia-sdk@v0.48.1/x/gov/types/genesis.go (about)

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