-
Notifications
You must be signed in to change notification settings - Fork 0
/
genesis.go
197 lines (174 loc) · 5.51 KB
/
genesis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package simulation
import (
"math/rand"
"time"
codectypes "github.com/blockgenx/blockgen-sdk/codec/types"
sdk "github.com/blockgenx/blockgen-sdk/types"
"github.com/blockgenx/blockgen-sdk/types/module"
simtypes "github.com/blockgenx/blockgen-sdk/types/simulation"
banktypes "github.com/blockgenx/blockgen-sdk/x/bank/types"
"github.com/blockgenx/blockgen-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: 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: 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, 0))
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: simtypes.RandStringOfLength(r, 10),
}
}
return groupPolicies
}
func getProposals(r *rand.Rand, simState *module.SimulationState, groupPolicies []*group.GroupPolicyInfo) []*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++ {
idx := r.Intn(len(groupPolicies))
groupPolicyAddress := groupPolicies[idx].Address
to, _ := simtypes.RandomAcc(r, simState.Accounts)
submittedAt := time.Unix(0, 0)
timeout := submittedAt.Add(time.Second * 1000).UTC()
proposal := &group.Proposal{
Id: uint64(i + 1),
Proposers: proposers,
GroupPolicyAddress: groupPolicyAddress,
GroupVersion: uint64(i + 1),
GroupPolicyVersion: uint64(i + 1),
Status: group.PROPOSAL_STATUS_SUBMITTED,
FinalTallyResult: group.TallyResult{
YesCount: "1",
NoCount: "1",
AbstainCount: "1",
NoWithVetoCount: "0",
},
ExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_NOT_RUN,
Metadata: simtypes.RandStringOfLength(r, 50),
SubmitTime: submittedAt,
VotingPeriodEnd: timeout,
}
err := proposal.SetMsgs([]sdk.Msg{&banktypes.MsgSend{
FromAddress: groupPolicyAddress,
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: 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 policies
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, groupPolicies) },
)
// 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)
}