From 8726f4203c31c3b16e52d6c26aa697264fc5a070 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Mon, 20 Mar 2023 12:11:21 +0100 Subject: [PATCH] fix: flaky group genesis sim (backport #15447) (#15461) Co-authored-by: Robert Zaremba --- x/group/simulation/genesis.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/x/group/simulation/genesis.go b/x/group/simulation/genesis.go index 7f30a4fc7303..66b471fa5e47 100644 --- a/x/group/simulation/genesis.go +++ b/x/group/simulation/genesis.go @@ -21,6 +21,16 @@ const ( GroupVote = "group-vote" ) +func checkAccExists(acc sdk.AccAddress, g []*group.GroupMember, lastIndex int) bool { + s := acc.String() + for i := 0; i < lastIndex; i++ { + if g[i].Member.Address == s { + return true + } + } + return false +} + func getGroups(r *rand.Rand, accounts []simtypes.Account) []*group.GroupInfo { groups := make([]*group.GroupInfo, 3) for i := 0; i < 3; i++ { @@ -40,6 +50,9 @@ func getGroupMembers(r *rand.Rand, accounts []simtypes.Account) []*group.GroupMe groupMembers := make([]*group.GroupMember, 3) for i := 0; i < 3; i++ { acc, _ := simtypes.RandomAcc(r, accounts) + for checkAccExists(acc.Address, groupMembers, i) { + acc, _ = simtypes.RandomAcc(r, accounts) + } groupMembers[i] = &group.GroupMember{ GroupId: uint64(i + 1), Member: &group.Member{ @@ -147,6 +160,11 @@ func getVoteOption(index int) group.VoteOption { // RandomizedGenState generates a random GenesisState for the group module. func RandomizedGenState(simState *module.SimulationState) { + // The test requires we have at least 3 accounts. + if len(simState.Accounts) < 3 { + return + } + // groups var groups []*group.GroupInfo simState.AppParams.GetOrGenerate(