cosmos-sdk/x/group/simulation/genesis.go
Marie Gauthier f52d87003c
feat: Add simulations to group module (#10723)
## Description

Closes: #9901



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2022-01-21 10:52:26 +00:00

200 lines
5.5 KiB
Go

package simulation
import (
"math/rand"
"time"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/group"
)
const (
GroupInfo = "group-info"
GroupMembers = "group-members"
GroupPolicyInfo = "group-policy-info"
GroupProposals = "group-proposals"
GroupVote = "group-vote"
)
func getGroups(r *rand.Rand, accounts []simtypes.Account) []*group.GroupInfo {
groups := make([]*group.GroupInfo, 3)
for i := 0; i < 3; i++ {
acc, _ := simtypes.RandomAcc(r, accounts)
groups[i] = &group.GroupInfo{
GroupId: uint64(i + 1),
Admin: acc.Address.String(),
Metadata: []byte(simtypes.RandStringOfLength(r, 10)),
Version: 1,
TotalWeight: "10",
}
}
return groups
}
func getGroupMembers(r *rand.Rand, accounts []simtypes.Account) []*group.GroupMember {
groupMembers := make([]*group.GroupMember, 3)
for i := 0; i < 3; i++ {
acc, _ := simtypes.RandomAcc(r, accounts)
groupMembers[i] = &group.GroupMember{
GroupId: uint64(i + 1),
Member: &group.Member{
Address: acc.Address.String(),
Weight: "10",
Metadata: []byte(simtypes.RandStringOfLength(r, 10)),
},
}
}
return groupMembers
}
func getGroupPolicies(r *rand.Rand, simState *module.SimulationState) []*group.GroupPolicyInfo {
groupPolicies := make([]*group.GroupPolicyInfo, 3)
for i := 0; i < 3; i++ {
acc, _ := simtypes.RandomAcc(r, simState.Accounts)
any, err := codectypes.NewAnyWithValue(group.NewThresholdDecisionPolicy("10", time.Second*time.Duration(1)))
if err != nil {
panic(err)
}
groupPolicies[i] = &group.GroupPolicyInfo{
GroupId: uint64(i + 1),
Admin: acc.Address.String(),
Address: acc.Address.String(),
Version: 1,
DecisionPolicy: any,
Metadata: []byte(simtypes.RandStringOfLength(r, 10)),
}
}
return groupPolicies
}
func getProposals(r *rand.Rand, simState *module.SimulationState) []*group.Proposal {
proposals := make([]*group.Proposal, 3)
proposers := []string{simState.Accounts[0].Address.String(), simState.Accounts[1].Address.String()}
for i := 0; i < 3; i++ {
from, _ := simtypes.RandomAcc(r, simState.Accounts)
to, _ := simtypes.RandomAcc(r, simState.Accounts)
fromAddr := from.Address.String()
submittedAt := time.Unix(0, 0)
timeout := submittedAt.Add(time.Second * 1000).UTC()
proposal := &group.Proposal{
ProposalId: uint64(i + 1),
Proposers: proposers,
Address: fromAddr,
GroupVersion: uint64(i + 1),
GroupPolicyVersion: uint64(i + 1),
Status: group.ProposalStatusSubmitted,
Result: group.ProposalResultAccepted,
VoteState: group.Tally{
YesCount: "1",
NoCount: "1",
AbstainCount: "1",
VetoCount: "0",
},
ExecutorResult: group.ProposalExecutorResultNotRun,
Metadata: []byte(simtypes.RandStringOfLength(r, 50)),
SubmittedAt: submittedAt,
Timeout: timeout,
}
err := proposal.SetMsgs([]sdk.Msg{&banktypes.MsgSend{
FromAddress: fromAddr,
ToAddress: to.Address.String(),
Amount: sdk.NewCoins(sdk.NewInt64Coin("test", 10)),
}})
if err != nil {
panic(err)
}
proposals[i] = proposal
}
return proposals
}
func getVotes(r *rand.Rand, simState *module.SimulationState) []*group.Vote {
votes := make([]*group.Vote, 3)
for i := 0; i < 3; i++ {
votes[i] = &group.Vote{
ProposalId: uint64(i + 1),
Voter: simState.Accounts[i].Address.String(),
Choice: getVoteChoice(i),
Metadata: []byte(simtypes.RandStringOfLength(r, 50)),
SubmittedAt: time.Unix(0, 0),
}
}
return votes
}
func getVoteChoice(index int) group.Choice {
switch index {
case 0:
return group.Choice_CHOICE_YES
case 1:
return group.Choice_CHOICE_NO
case 2:
return group.Choice_CHOICE_ABSTAIN
default:
return group.Choice_CHOICE_VETO
}
}
// RandomizedGenState generates a random GenesisState for the group module.
func RandomizedGenState(simState *module.SimulationState) {
// groups
var groups []*group.GroupInfo
simState.AppParams.GetOrGenerate(
simState.Cdc, GroupInfo, &groups, simState.Rand,
func(r *rand.Rand) { groups = getGroups(r, simState.Accounts) },
)
// group members
var members []*group.GroupMember
simState.AppParams.GetOrGenerate(
simState.Cdc, GroupMembers, &members, simState.Rand,
func(r *rand.Rand) { members = getGroupMembers(r, simState.Accounts) },
)
// group accounts
var groupPolicies []*group.GroupPolicyInfo
simState.AppParams.GetOrGenerate(
simState.Cdc, GroupPolicyInfo, &groupPolicies, simState.Rand,
func(r *rand.Rand) { groupPolicies = getGroupPolicies(r, simState) },
)
// proposals
var proposals []*group.Proposal
simState.AppParams.GetOrGenerate(
simState.Cdc, GroupProposals, &proposals, simState.Rand,
func(r *rand.Rand) { proposals = getProposals(r, simState) },
)
// votes
var votes []*group.Vote
simState.AppParams.GetOrGenerate(
simState.Cdc, GroupVote, &votes, simState.Rand,
func(r *rand.Rand) { votes = getVotes(r, simState) },
)
groupGenesis := group.GenesisState{
GroupSeq: 3,
Groups: groups,
GroupMembers: members,
GroupPolicySeq: 3,
GroupPolicies: groupPolicies,
ProposalSeq: 3,
Proposals: proposals,
Votes: votes,
}
simState.GenState[group.ModuleName] = simState.Cdc.MustMarshalJSON(&groupGenesis)
}