/
genesis.go
296 lines (249 loc) · 8.6 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package simulation
// DONTCOVER
import (
"fmt"
"math/rand"
"time"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/feegrant"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/desmos-labs/desmos/v7/x/subspaces/types"
)
// RandomizeGenState generates a random GenesisState for subspaces
func RandomizeGenState(simState *module.SimulationState) {
subspaces := randomSubspaces(simState.Rand, simState.Accounts)
sections := randomSections(simState.Rand, subspaces)
groups := randomUserGroups(simState.Rand, subspaces)
members := randomUserGroupsMembers(simState.Rand, simState.Accounts, groups)
acl := randomACL(simState.Rand, simState.Accounts, subspaces)
initialSubspaceID, subspacesData := getSubspacesDataEntries(subspaces, sections, groups)
grants := append(randomUserGrants(simState.Rand, simState.Accounts, subspaces), randomGroupGrants(simState.Rand, groups)...)
// Create the genesis and sanitize it
subspacesGenesis := types.NewGenesisState(initialSubspaceID, subspacesData, subspaces, sections, acl, groups, members, grants)
subspacesGenesis = sanitizeGenesis(subspacesGenesis)
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(subspacesGenesis)
}
// randomSubspaces generates a random slice of subspaces
func randomSubspaces(r *rand.Rand, accs []simtypes.Account) (subspaces []types.Subspace) {
subspacesNumber := r.Intn(100)
subspaces = make([]types.Subspace, subspacesNumber)
for index := 0; index < subspacesNumber; index++ {
randData := GenerateRandomSubspace(r, accs)
subspaces[index] = types.NewSubspace(
uint64(index+1),
randData.Name,
randData.Description,
randData.Treasury,
randData.Owner,
randData.Creator,
time.Now(),
randData.AdditionalFeeTokens,
)
}
return subspaces
}
// randomSections generates a random slice of sections
func randomSections(r *rand.Rand, subspaces []types.Subspace) (sections []types.Section) {
if len(subspaces) == 0 {
return nil
}
sectionsNumber := r.Intn(20)
sections = make([]types.Section, sectionsNumber)
for i := 0; i < sectionsNumber; i++ {
subspace := RandomSubspace(r, subspaces)
// Generate a random section
sections[i] = types.NewSection(
subspace.ID,
uint32(i)+1,
0,
RandomSectionName(r),
RandomSectionDescription(r),
)
}
return sections
}
// randomUserGroups generates a random slice of user group details
func randomUserGroups(r *rand.Rand, subspaces []types.Subspace) []types.UserGroup {
if len(subspaces) == 0 {
return nil
}
groupsNumber := r.Intn(30)
groups := make([]types.UserGroup, groupsNumber)
for i := 0; i < groupsNumber; i++ {
subspace := RandomSubspace(r, subspaces)
groupID := uint32(i + 1)
// Get a random permission
permission := RandomPermission(r, validPermissions)
// Build the group details
groups[i] = types.NewUserGroup(subspace.ID, 0, groupID, RandomName(r), RandomDescription(r), permission)
}
return groups
}
// randomUserGroupsMembers generates a random slice of user group members
func randomUserGroupsMembers(r *rand.Rand, accounts []simtypes.Account, groups []types.UserGroup) []types.UserGroupMemberEntry {
if len(groups) == 0 {
return nil
}
var membersEntries []types.UserGroupMemberEntry
for _, group := range groups {
for i := 0; i < r.Intn(10); i++ {
account, _ := simtypes.RandomAcc(r, accounts)
membersEntries = append(membersEntries, types.NewUserGroupMemberEntry(group.SubspaceID, group.ID, account.Address.String()))
}
}
return membersEntries
}
// getSubspacesDataEntries returns the subspace data entries based on the given data
func getSubspacesDataEntries(
subspaces []types.Subspace, sections []types.Section, groups []types.UserGroup,
) (initialSubspaceID uint64, subspacesData []types.SubspaceData) {
maxSubspaceID := uint64(0)
initialSectionID := map[uint64]uint32{}
initialGroupIDS := map[uint64]uint32{}
for _, subspace := range subspaces {
if subspace.ID > initialSubspaceID {
maxSubspaceID = subspace.ID
}
// Get the max section id
maxSectionID := uint32(0)
for _, section := range sections {
if section.SubspaceID == subspace.ID && section.ID > maxSectionID {
maxSectionID = section.ID
}
}
initialSectionID[subspace.ID] = maxSectionID + 1
// Get the max group id
maxGroupID := uint32(0)
for _, group := range groups {
if group.SubspaceID == subspace.ID && group.ID > maxGroupID {
maxGroupID = group.ID
}
}
initialGroupIDS[subspace.ID] = maxGroupID + 1
}
subspacesData = make([]types.SubspaceData, len(subspaces))
for i, subspace := range subspaces {
subspacesData[i] = types.NewSubspaceData(subspace.ID, initialSectionID[subspace.ID], initialGroupIDS[subspace.ID])
}
return maxSubspaceID + 1, subspacesData
}
// randomACL generates a random slice of ACL entries
func randomACL(r *rand.Rand, accounts []simtypes.Account, subspaces []types.Subspace) (entries []types.UserPermission) {
if len(subspaces) == 0 {
return nil
}
aclEntriesNumber := r.Intn(40)
entries = make([]types.UserPermission, aclEntriesNumber)
for index := 0; index < aclEntriesNumber; index++ {
subspace := RandomSubspace(r, subspaces)
account, _ := simtypes.RandomAcc(r, accounts)
// Get a random permission
permission := RandomPermission(r, validPermissions)
// Crete the entry
entries[index] = types.NewUserPermission(subspace.ID, 0, account.Address.String(), permission)
}
return entries
}
// randomUserGrants returns randomly generated user grants
func randomUserGrants(r *rand.Rand, accounts []simtypes.Account, subspaces []types.Subspace) []types.Grant {
if len(subspaces) == 0 {
return nil
}
grantsNumber := r.Intn(len(subspaces))
grants := make([]types.Grant, grantsNumber)
for i := 0; i < grantsNumber; {
subspace := RandomSubspace(r, subspaces)
grantee, _ := simtypes.RandomAcc(r, accounts)
if subspace.Treasury == grantee.Address.String() {
continue
}
grant := types.NewGrant(subspace.ID, subspace.Treasury, types.NewUserGrantee(grantee.Address.String()), &feegrant.BasicAllowance{})
if !containsGrant(grants, grant) {
grants[i] = grant
i++
}
}
return grants
}
// randomGroupGrants returns randomly generated group grants
func randomGroupGrants(r *rand.Rand, groups []types.UserGroup) []types.Grant {
if len(groups) == 0 {
return nil
}
grantsNumber := r.Intn(len(groups))
grants := make([]types.Grant, grantsNumber)
for i := 0; i < grantsNumber; {
group := RandomGroup(r, groups)
granter := types.GetTreasuryAddress(group.SubspaceID)
// Default group can not have any grant
if group.ID == 0 {
continue
}
grant := types.NewGrant(group.SubspaceID, granter.String(), types.NewGroupGrantee(group.ID), &feegrant.BasicAllowance{})
if !containsGrant(grants, grant) {
grants[i] = grant
i++
}
}
return grants
}
// containsGrant checks if the grants does exist or not
func containsGrant(slice []types.Grant, grant types.Grant) bool {
for _, g := range slice {
if g.SubspaceID == grant.SubspaceID && g.Grantee.Equal(grant.Grantee) {
return true
}
}
return false
}
// --------------------------------------------------------------------------------------------------------------------
// sanitizeGenesis sanitizes the given genesis by removing all the double subspaces,
// groups or ACL entries that might be there
func sanitizeGenesis(genesis *types.GenesisState) *types.GenesisState {
return types.NewGenesisState(
genesis.InitialSubspaceID,
genesis.SubspacesData,
genesis.Subspaces,
genesis.Sections,
sanitizeUserPermissions(genesis.UserPermissions),
sanitizeUserGroups(genesis.UserGroups),
genesis.UserGroupsMembers,
genesis.Grants,
)
}
// sanitizeUserGroups sanitizes the given slice by removing all the double groups
func sanitizeUserGroups(slice []types.UserGroup) []types.UserGroup {
groups := map[string]bool{}
for _, value := range slice {
groups[fmt.Sprintf("%d%s", value.SubspaceID, value.Name)] = true
}
var unique []types.UserGroup
for id := range groups {
EntryLoop:
for _, group := range slice {
if id == fmt.Sprintf("%d%s", group.SubspaceID, group.Name) {
unique = append(unique, group)
break EntryLoop
}
}
}
return unique
}
// sanitizeSubspaces sanitizes the given slice by removing all the double entries
func sanitizeUserPermissions(slice []types.UserPermission) []types.UserPermission {
entries := map[string]bool{}
for _, value := range slice {
entries[fmt.Sprintf("%d%s", value.SubspaceID, value.User)] = true
}
var unique []types.UserPermission
for id := range entries {
EntryLoop:
for _, entry := range slice {
if id == fmt.Sprintf("%d%s", entry.SubspaceID, entry.User) {
unique = append(unique, entry)
break EntryLoop
}
}
}
return unique
}