/
operations_feegrant.go
182 lines (153 loc) · 5.16 KB
/
operations_feegrant.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
package simulation
import (
"math/rand"
"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/x/feegrant"
"github.com/desmos-labs/desmos/v7/testutil/simtesting"
"github.com/desmos-labs/desmos/v7/x/subspaces/keeper"
"github.com/desmos-labs/desmos/v7/x/subspaces/types"
)
// DONTCOVER
// SimulateMsgGrantAllowance tests and runs a single MsgGrantAllowance
func SimulateMsgGrantAllowance(
k *keeper.Keeper, ak authkeeper.AccountKeeper, bk bankkeeper.Keeper,
) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
// Get the data
subspaceID, granter, grantee, signer, skip := randomGrantAllowanceFields(r, ctx, accs, k, ak)
if skip {
return simtypes.NoOpMsg(types.RouterKey, "MsgGrantAllowance", "skip"), nil, nil
}
// Build the message
msg := types.NewMsgGrantAllowance(subspaceID, granter, grantee, &feegrant.BasicAllowance{})
// Send the message
return simtesting.SendMsg(r, app, ak, bk, msg, ctx, signer)
}
}
// randomGrantAllowanceFields returns the data used to build a random MsgGrantAllowance
func randomGrantAllowanceFields(
r *rand.Rand, ctx sdk.Context, accs []simtypes.Account, k *keeper.Keeper, ak authkeeper.AccountKeeper,
) (subspaceID uint64, granter string, grantee types.Grantee, signer simtypes.Account, skip bool) {
// Get a subspace id
subspaces := k.GetAllSubspaces(ctx)
if len(subspaces) == 0 {
// Skip because there are no subspaces
skip = true
return
}
subspaceID = RandomSubspace(r, subspaces).ID
// Get a granter
granters := k.GetUsersWithRootPermissions(ctx, subspaceID, types.NewPermissions(types.PermissionManageAllowances))
granter = RandomAddress(r, granters)
if len(granters) == 0 {
skip = true
return
}
// 50% of having a user grantee, otherwise a group grantee
if r.Intn(100) < 50 {
accounts := ak.GetAllAccounts(ctx)
granteeAddr := RandomAuthAccount(r, accounts).GetAddress().String()
if k.HasUserGrant(ctx, subspaceID, granteeAddr) {
// Skip because grant does exist
skip = true
return
}
if granteeAddr == granter {
// Skip because granting to itself is not allowed
skip = true
return
}
grantee = types.NewUserGrantee(granteeAddr)
} else {
groups := k.GetSubspaceUserGroups(ctx, subspaceID)
if len(groups) == 0 {
// Skip because there are no groups
skip = true
return
}
group := RandomGroup(r, groups)
if group.ID == 0 {
// Skip because we cannot grant the default group
skip = true
return
}
if k.HasGroupGrant(ctx, subspaceID, group.ID) {
// Skip because grant does exist
skip = true
return
}
grantee = types.NewGroupGrantee(group.ID)
}
// Get a signer account
acc := GetAccount(granter, accs)
if acc == nil {
// Skip the operation without error as the account is not valid
skip = true
return
}
signer = *acc
return subspaceID, granter, grantee, signer, false
}
// --------------------------------------------------------------------------------------------------------------------
// SimulateMsgRevokeAllowance tests and runs a single MsgRevokeAllowance
func SimulateMsgRevokeAllowance(
k *keeper.Keeper, ak authkeeper.AccountKeeper, bk bankkeeper.Keeper,
) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
// Get the data
subspaceID, granter, grantee, signer, skip := randomRevokeAllowanceFields(r, ctx, accs, k)
if skip {
return simtypes.NoOpMsg(types.RouterKey, "MsgRevokeAllowance", "skip"), nil, nil
}
// Build the message
msg := types.NewMsgRevokeAllowance(subspaceID, granter, grantee)
// Send the message
return simtesting.SendMsg(r, app, ak, bk, msg, ctx, signer)
}
}
// randomRevokeAllowanceFields returns the data used to build a random MsgRevokeAllowance
func randomRevokeAllowanceFields(
r *rand.Rand, ctx sdk.Context, accs []simtypes.Account, k *keeper.Keeper,
) (subspaceID uint64, granter string, grantee types.Grantee, signer simtypes.Account, skip bool) {
// 50% of having user grants, otherwise group grants
var grants []types.Grant
if r.Intn(100) < 50 {
grants = k.GetAllUserGrants(ctx)
} else {
grants = k.GetAllUserGroupsGrants(ctx)
}
if len(grants) == 0 {
// Skip because there are no grants
skip = true
return
}
grant := RandomGrant(r, grants)
subspaceID = grant.SubspaceID
grantee = grant.Grantee.GetCachedValue().(types.Grantee)
// Get a granter
granters := k.GetUsersWithRootPermissions(ctx, subspaceID, types.NewPermissions(types.PermissionManageAllowances))
if len(granters) == 0 {
skip = true
return
}
granter = RandomAddress(r, granters)
// Get a signer account
acc := GetAccount(granter, accs)
if acc == nil {
// Skip the operation without error as the account is not valid
skip = true
return
}
signer = *acc
return subspaceID, granter, grantee, signer, false
}