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

     1  package simulation
     2  
     3  import (
     4  	"math/rand"
     5  	"time"
     6  
     7  	codectypes "github.com/cosmos/cosmos-sdk/codec/types"
     8  	sdk "github.com/cosmos/cosmos-sdk/types"
     9  	"github.com/cosmos/cosmos-sdk/types/module"
    10  	simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
    11  	banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
    12  	"github.com/cosmos/cosmos-sdk/x/group"
    13  )
    14  
    15  const (
    16  	GroupInfo       = "group-info"
    17  	GroupMembers    = "group-members"
    18  	GroupPolicyInfo = "group-policy-info"
    19  	GroupProposals  = "group-proposals"
    20  	GroupVote       = "group-vote"
    21  )
    22  
    23  func checkAccExists(acc sdk.AccAddress, g []*group.GroupMember, lastIndex int) bool {
    24  	s := acc.String()
    25  	for i := 0; i < lastIndex; i++ {
    26  		if g[i].Member.Address == s {
    27  			return true
    28  		}
    29  	}
    30  	return false
    31  }
    32  
    33  func getGroups(r *rand.Rand, accounts []simtypes.Account) []*group.GroupInfo {
    34  	groups := make([]*group.GroupInfo, 3)
    35  	for i := 0; i < 3; i++ {
    36  		acc, _ := simtypes.RandomAcc(r, accounts)
    37  		groups[i] = &group.GroupInfo{
    38  			Id:          uint64(i + 1),
    39  			Admin:       acc.Address.String(),
    40  			Metadata:    simtypes.RandStringOfLength(r, 10),
    41  			Version:     1,
    42  			TotalWeight: "10",
    43  		}
    44  	}
    45  	return groups
    46  }
    47  
    48  func getGroupMembers(r *rand.Rand, accounts []simtypes.Account) []*group.GroupMember {
    49  	groupMembers := make([]*group.GroupMember, 3)
    50  	for i := 0; i < 3; i++ {
    51  		acc, _ := simtypes.RandomAcc(r, accounts)
    52  		for checkAccExists(acc.Address, groupMembers, i) {
    53  			acc, _ = simtypes.RandomAcc(r, accounts)
    54  		}
    55  		groupMembers[i] = &group.GroupMember{
    56  			GroupId: uint64(i + 1),
    57  			Member: &group.Member{
    58  				Address:  acc.Address.String(),
    59  				Weight:   "10",
    60  				Metadata: simtypes.RandStringOfLength(r, 10),
    61  			},
    62  		}
    63  	}
    64  	return groupMembers
    65  }
    66  
    67  func getGroupPolicies(r *rand.Rand, simState *module.SimulationState) []*group.GroupPolicyInfo {
    68  	var groupPolicies []*group.GroupPolicyInfo
    69  
    70  	usedAccs := make(map[string]bool)
    71  	for i := 0; i < 3; i++ {
    72  		acc, _ := simtypes.RandomAcc(r, simState.Accounts)
    73  
    74  		if usedAccs[acc.Address.String()] {
    75  			if len(usedAccs) != len(simState.Accounts) {
    76  				// Go again if the account is used and there are more to take from
    77  				i--
    78  			}
    79  
    80  			continue
    81  		}
    82  		usedAccs[acc.Address.String()] = true
    83  
    84  		any, err := codectypes.NewAnyWithValue(group.NewThresholdDecisionPolicy("10", time.Second, 0))
    85  		if err != nil {
    86  			panic(err)
    87  		}
    88  		groupPolicies = append(groupPolicies, &group.GroupPolicyInfo{
    89  			GroupId:        uint64(i + 1),
    90  			Admin:          acc.Address.String(),
    91  			Address:        acc.Address.String(),
    92  			Version:        1,
    93  			DecisionPolicy: any,
    94  			Metadata:       simtypes.RandStringOfLength(r, 10),
    95  		})
    96  	}
    97  	return groupPolicies
    98  }
    99  
   100  func getProposals(r *rand.Rand, simState *module.SimulationState, groupPolicies []*group.GroupPolicyInfo) []*group.Proposal {
   101  	proposals := make([]*group.Proposal, 3)
   102  	proposers := []string{simState.Accounts[0].Address.String(), simState.Accounts[1].Address.String()}
   103  	for i := 0; i < 3; i++ {
   104  		idx := r.Intn(len(groupPolicies))
   105  		groupPolicyAddress := groupPolicies[idx].Address
   106  		to, _ := simtypes.RandomAcc(r, simState.Accounts)
   107  
   108  		submittedAt := time.Unix(0, 0)
   109  		timeout := submittedAt.Add(time.Second * 1000).UTC()
   110  
   111  		proposal := &group.Proposal{
   112  			Id:                 uint64(i + 1),
   113  			Proposers:          proposers,
   114  			GroupPolicyAddress: groupPolicyAddress,
   115  			GroupVersion:       uint64(i + 1),
   116  			GroupPolicyVersion: uint64(i + 1),
   117  			Status:             group.PROPOSAL_STATUS_SUBMITTED,
   118  			FinalTallyResult: group.TallyResult{
   119  				YesCount:        "1",
   120  				NoCount:         "1",
   121  				AbstainCount:    "1",
   122  				NoWithVetoCount: "0",
   123  			},
   124  			ExecutorResult:  group.PROPOSAL_EXECUTOR_RESULT_NOT_RUN,
   125  			Metadata:        simtypes.RandStringOfLength(r, 50),
   126  			SubmitTime:      submittedAt,
   127  			VotingPeriodEnd: timeout,
   128  		}
   129  		err := proposal.SetMsgs([]sdk.Msg{&banktypes.MsgSend{
   130  			FromAddress: groupPolicyAddress,
   131  			ToAddress:   to.Address.String(),
   132  			Amount:      sdk.NewCoins(sdk.NewInt64Coin("test", 10)),
   133  		}})
   134  		if err != nil {
   135  			panic(err)
   136  		}
   137  
   138  		proposals[i] = proposal
   139  	}
   140  
   141  	return proposals
   142  }
   143  
   144  func getVotes(r *rand.Rand, simState *module.SimulationState) []*group.Vote {
   145  	votes := make([]*group.Vote, 3)
   146  
   147  	for i := 0; i < 3; i++ {
   148  		votes[i] = &group.Vote{
   149  			ProposalId: uint64(i + 1),
   150  			Voter:      simState.Accounts[i].Address.String(),
   151  			Option:     getVoteOption(i),
   152  			Metadata:   simtypes.RandStringOfLength(r, 50),
   153  			SubmitTime: time.Unix(0, 0),
   154  		}
   155  	}
   156  
   157  	return votes
   158  }
   159  
   160  func getVoteOption(index int) group.VoteOption {
   161  	switch index {
   162  	case 0:
   163  		return group.VOTE_OPTION_YES
   164  	case 1:
   165  		return group.VOTE_OPTION_NO
   166  	case 2:
   167  		return group.VOTE_OPTION_ABSTAIN
   168  	default:
   169  		return group.VOTE_OPTION_NO_WITH_VETO
   170  	}
   171  }
   172  
   173  // RandomizedGenState generates a random GenesisState for the group module.
   174  func RandomizedGenState(simState *module.SimulationState) {
   175  	// The test requires we have at least 3 accounts.
   176  	if len(simState.Accounts) < 3 {
   177  		return
   178  	}
   179  
   180  	// groups
   181  	var groups []*group.GroupInfo
   182  	simState.AppParams.GetOrGenerate(GroupInfo, &groups, simState.Rand, func(r *rand.Rand) { groups = getGroups(r, simState.Accounts) })
   183  
   184  	// group members
   185  	var members []*group.GroupMember
   186  	simState.AppParams.GetOrGenerate(GroupMembers, &members, simState.Rand, func(r *rand.Rand) { members = getGroupMembers(r, simState.Accounts) })
   187  
   188  	// group policies
   189  	var groupPolicies []*group.GroupPolicyInfo
   190  	simState.AppParams.GetOrGenerate(GroupPolicyInfo, &groupPolicies, simState.Rand, func(r *rand.Rand) { groupPolicies = getGroupPolicies(r, simState) })
   191  
   192  	// proposals
   193  	var proposals []*group.Proposal
   194  	simState.AppParams.GetOrGenerate(GroupProposals, &proposals, simState.Rand, func(r *rand.Rand) { proposals = getProposals(r, simState, groupPolicies) })
   195  
   196  	// votes
   197  	var votes []*group.Vote
   198  	simState.AppParams.GetOrGenerate(GroupVote, &votes, simState.Rand, func(r *rand.Rand) { votes = getVotes(r, simState) })
   199  
   200  	groupGenesis := group.GenesisState{
   201  		GroupSeq:       3,
   202  		Groups:         groups,
   203  		GroupMembers:   members,
   204  		GroupPolicySeq: 3,
   205  		GroupPolicies:  groupPolicies,
   206  		ProposalSeq:    3,
   207  		Proposals:      proposals,
   208  		Votes:          votes,
   209  	}
   210  
   211  	simState.GenState[group.ModuleName] = simState.Cdc.MustMarshalJSON(&groupGenesis)
   212  }