2022-01-21 02:52:26 -08:00
|
|
|
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{
|
2022-02-14 05:48:17 -08:00
|
|
|
Id: uint64(i + 1),
|
2022-01-21 02:52:26 -08:00
|
|
|
Admin: acc.Address.String(),
|
2022-03-01 00:35:37 -08:00
|
|
|
Metadata: simtypes.RandStringOfLength(r, 10),
|
2022-01-21 02:52:26 -08:00
|
|
|
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",
|
2022-03-01 00:35:37 -08:00
|
|
|
Metadata: simtypes.RandStringOfLength(r, 10),
|
2022-01-21 02:52:26 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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,
|
2022-03-01 00:35:37 -08:00
|
|
|
Metadata: simtypes.RandStringOfLength(r, 10),
|
2022-01-21 02:52:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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{
|
2022-02-14 05:48:17 -08:00
|
|
|
Id: uint64(i + 1),
|
2022-01-21 02:52:26 -08:00
|
|
|
Proposers: proposers,
|
|
|
|
Address: fromAddr,
|
|
|
|
GroupVersion: uint64(i + 1),
|
|
|
|
GroupPolicyVersion: uint64(i + 1),
|
2022-02-14 05:48:17 -08:00
|
|
|
Status: group.PROPOSAL_STATUS_SUBMITTED,
|
|
|
|
Result: group.PROPOSAL_RESULT_ACCEPTED,
|
|
|
|
FinalTallyResult: group.TallyResult{
|
|
|
|
YesCount: "1",
|
|
|
|
NoCount: "1",
|
|
|
|
AbstainCount: "1",
|
|
|
|
NoWithVetoCount: "0",
|
2022-01-21 02:52:26 -08:00
|
|
|
},
|
2022-02-14 05:48:17 -08:00
|
|
|
ExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_NOT_RUN,
|
2022-03-01 00:35:37 -08:00
|
|
|
Metadata: simtypes.RandStringOfLength(r, 50),
|
2022-02-14 05:48:17 -08:00
|
|
|
SubmitTime: submittedAt,
|
2022-01-21 02:52:26 -08:00
|
|
|
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{
|
2022-02-14 05:48:17 -08:00
|
|
|
ProposalId: uint64(i + 1),
|
|
|
|
Voter: simState.Accounts[i].Address.String(),
|
|
|
|
Option: getVoteOption(i),
|
2022-03-01 00:35:37 -08:00
|
|
|
Metadata: simtypes.RandStringOfLength(r, 50),
|
2022-02-14 05:48:17 -08:00
|
|
|
SubmitTime: time.Unix(0, 0),
|
2022-01-21 02:52:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return votes
|
|
|
|
}
|
|
|
|
|
2022-02-14 05:48:17 -08:00
|
|
|
func getVoteOption(index int) group.VoteOption {
|
2022-01-21 02:52:26 -08:00
|
|
|
switch index {
|
|
|
|
case 0:
|
2022-02-14 05:48:17 -08:00
|
|
|
return group.VOTE_OPTION_YES
|
2022-01-21 02:52:26 -08:00
|
|
|
case 1:
|
2022-02-14 05:48:17 -08:00
|
|
|
return group.VOTE_OPTION_NO
|
2022-01-21 02:52:26 -08:00
|
|
|
case 2:
|
2022-02-14 05:48:17 -08:00
|
|
|
return group.VOTE_OPTION_ABSTAIN
|
2022-01-21 02:52:26 -08:00
|
|
|
default:
|
2022-02-14 05:48:17 -08:00
|
|
|
return group.VOTE_OPTION_NO_WITH_VETO
|
2022-01-21 02:52:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|