github.com/cosmos/cosmos-sdk@v0.50.10/x/group/genesis.go (about)

     1  package group
     2  
     3  import (
     4  	"fmt"
     5  
     6  	errorsmod "cosmossdk.io/errors"
     7  
     8  	"github.com/cosmos/cosmos-sdk/codec/types"
     9  	sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
    10  )
    11  
    12  // NewGenesisState creates a new genesis state with default values.
    13  func NewGenesisState() *GenesisState {
    14  	return &GenesisState{}
    15  }
    16  
    17  // Validate performs basic genesis state validation returning an error upon any
    18  // failure.
    19  func (s GenesisState) Validate() error {
    20  	groups := make(map[uint64]GroupInfo)
    21  	groupPolicies := make(map[string]GroupPolicyInfo)
    22  	groupMembers := make(map[uint64]GroupMember)
    23  	proposals := make(map[uint64]Proposal)
    24  
    25  	for _, g := range s.Groups {
    26  		if err := g.ValidateBasic(); err != nil {
    27  			return errorsmod.Wrap(err, "Group validation failed")
    28  		}
    29  		groups[g.Id] = *g
    30  	}
    31  
    32  	for _, g := range s.GroupPolicies {
    33  
    34  		// check that group with group policy's GroupId exists
    35  		if _, exists := groups[g.GroupId]; !exists {
    36  			return errorsmod.Wrap(sdkerrors.ErrNotFound, fmt.Sprintf("group with GroupId %d doesn't exist", g.GroupId))
    37  		}
    38  
    39  		if err := g.ValidateBasic(); err != nil {
    40  			return errorsmod.Wrap(err, "GroupPolicy validation failed")
    41  		}
    42  		groupPolicies[g.Address] = *g
    43  	}
    44  
    45  	for _, g := range s.GroupMembers {
    46  
    47  		// check that group with group member's GroupId exists
    48  		if _, exists := groups[g.GroupId]; !exists {
    49  			return errorsmod.Wrap(sdkerrors.ErrNotFound, fmt.Sprintf("group member with GroupId %d doesn't exist", g.GroupId))
    50  		}
    51  
    52  		if err := g.ValidateBasic(); err != nil {
    53  			return errorsmod.Wrap(err, "GroupMember validation failed")
    54  		}
    55  		groupMembers[g.GroupId] = *g
    56  	}
    57  
    58  	for _, p := range s.Proposals {
    59  
    60  		// check that group policy with proposal address exists
    61  		if _, exists := groupPolicies[p.GroupPolicyAddress]; !exists {
    62  			return errorsmod.Wrap(sdkerrors.ErrNotFound, fmt.Sprintf("group policy account with address %s doesn't correspond to proposal address", p.GroupPolicyAddress))
    63  		}
    64  
    65  		if err := p.ValidateBasic(); err != nil {
    66  			return errorsmod.Wrap(err, "Proposal validation failed")
    67  		}
    68  		proposals[p.Id] = *p
    69  	}
    70  
    71  	for _, v := range s.Votes {
    72  
    73  		if err := v.ValidateBasic(); err != nil {
    74  			return errorsmod.Wrap(err, "Vote validation failed")
    75  		}
    76  
    77  		// check that proposal exists
    78  		if _, exists := proposals[v.ProposalId]; !exists {
    79  			return errorsmod.Wrap(sdkerrors.ErrNotFound, fmt.Sprintf("proposal with ProposalId %d doesn't exist", v.ProposalId))
    80  		}
    81  	}
    82  	return nil
    83  }
    84  
    85  // UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
    86  func (s GenesisState) UnpackInterfaces(unpacker types.AnyUnpacker) error {
    87  	for _, g := range s.GroupPolicies {
    88  		err := g.UnpackInterfaces(unpacker)
    89  		if err != nil {
    90  			return err
    91  		}
    92  	}
    93  	for _, p := range s.Proposals {
    94  		err := p.UnpackInterfaces(unpacker)
    95  		if err != nil {
    96  			return err
    97  		}
    98  	}
    99  	return nil
   100  }