200 lines
5.5 KiB
Go
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{
|
|
Id: 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{
|
|
Id: uint64(i + 1),
|
|
Proposers: proposers,
|
|
Address: fromAddr,
|
|
GroupVersion: uint64(i + 1),
|
|
GroupPolicyVersion: uint64(i + 1),
|
|
Status: group.PROPOSAL_STATUS_SUBMITTED,
|
|
Result: group.PROPOSAL_RESULT_ACCEPTED,
|
|
FinalTallyResult: group.TallyResult{
|
|
YesCount: "1",
|
|
NoCount: "1",
|
|
AbstainCount: "1",
|
|
NoWithVetoCount: "0",
|
|
},
|
|
ExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_NOT_RUN,
|
|
Metadata: []byte(simtypes.RandStringOfLength(r, 50)),
|
|
SubmitTime: 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(),
|
|
Option: getVoteOption(i),
|
|
Metadata: []byte(simtypes.RandStringOfLength(r, 50)),
|
|
SubmitTime: time.Unix(0, 0),
|
|
}
|
|
}
|
|
|
|
return votes
|
|
}
|
|
|
|
func getVoteOption(index int) group.VoteOption {
|
|
switch index {
|
|
case 0:
|
|
return group.VOTE_OPTION_YES
|
|
case 1:
|
|
return group.VOTE_OPTION_NO
|
|
case 2:
|
|
return group.VOTE_OPTION_ABSTAIN
|
|
default:
|
|
return group.VOTE_OPTION_NO_WITH_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)
|
|
}
|