/
operations.go
449 lines (396 loc) · 15 KB
/
operations.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package simulation
import (
"bytes"
"math/rand"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/simapp/helpers"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/quarantine"
"github.com/cosmos/cosmos-sdk/x/quarantine/keeper"
"github.com/cosmos/cosmos-sdk/x/simulation"
)
// Quarantine message types.
var (
TypeMsgOptIn = sdk.MsgTypeURL(&quarantine.MsgOptIn{})
TypeMsgOptOut = sdk.MsgTypeURL(&quarantine.MsgOptOut{})
TypeMsgAccept = sdk.MsgTypeURL(&quarantine.MsgAccept{})
TypeMsgDecline = sdk.MsgTypeURL(&quarantine.MsgDecline{})
TypeMsgUpdateAutoResponses = sdk.MsgTypeURL(&quarantine.MsgUpdateAutoResponses{})
)
// Simulation operation weights constants.
const (
OpMsgOptIn = "op_weight_quarantine_msg_opt_in"
OpMsgOptOut = "op_weight_quarantine_msg_opt_out"
OpMsgAccept = "op_weight_quarantine_msg_accept"
OpMsgDecline = "op_weight_quarantine_msg_decline"
OpMsgUpdateAutoResponses = "op_weight_quarantine_msg_update_auto_responses"
)
// Default weights.
const (
WeightMsgOptIn = 100
WeightMsgOptOut = 50
WeightMsgAccept = 50
WeightMsgDecline = 20
WeightMsgUpdateAutoResponses = 50
)
// WeightedOperations returns all the operations from the module with their respective weights
func WeightedOperations(
appParams simtypes.AppParams, cdc codec.JSONCodec,
ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k keeper.Keeper, _ cdctypes.AnyUnpacker,
) simulation.WeightedOperations {
var (
weightMsgOptIn int
weightMsgOptOut int
weightMsgAccept int
weightMsgDecline int
weightMsgUpdateAutoResponses int
)
appParams.GetOrGenerate(cdc, OpMsgOptIn, &weightMsgOptIn, nil,
func(_ *rand.Rand) { weightMsgOptIn = WeightMsgOptIn })
appParams.GetOrGenerate(cdc, OpMsgOptOut, &weightMsgOptOut, nil,
func(_ *rand.Rand) { weightMsgOptOut = WeightMsgOptOut })
appParams.GetOrGenerate(cdc, OpMsgAccept, &weightMsgAccept, nil,
func(_ *rand.Rand) { weightMsgAccept = WeightMsgAccept })
appParams.GetOrGenerate(cdc, OpMsgDecline, &weightMsgDecline, nil,
func(_ *rand.Rand) { weightMsgDecline = WeightMsgDecline })
appParams.GetOrGenerate(cdc, OpMsgUpdateAutoResponses, &weightMsgUpdateAutoResponses, nil,
func(_ *rand.Rand) { weightMsgUpdateAutoResponses = WeightMsgUpdateAutoResponses })
return simulation.WeightedOperations{
simulation.NewWeightedOperation(weightMsgOptIn, SimulateMsgOptIn(ak, bk)),
simulation.NewWeightedOperation(weightMsgOptOut, SimulateMsgOptOut(ak, bk, k)),
simulation.NewWeightedOperation(weightMsgAccept, SimulateMsgAccept(ak, bk, k)),
simulation.NewWeightedOperation(weightMsgDecline, SimulateMsgDecline(ak, bk, k)),
simulation.NewWeightedOperation(weightMsgUpdateAutoResponses, SimulateMsgUpdateAutoResponses(ak, bk, k)),
}
}
// SimulateMsgOptIn opts an account into quarantine.
func SimulateMsgOptIn(ak quarantine.AccountKeeper, bk quarantine.BankKeeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
acct, _ := simtypes.RandomAcc(r, accs)
msg := &quarantine.MsgOptIn{
ToAddress: acct.Address.String(),
}
msgType := TypeMsgOptIn
spendableCoins := bk.SpendableCoins(ctx, acct.Address)
fees, err := simtypes.RandomFees(r, ctx, spendableCoins)
if err != nil {
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "fee error"), nil, err
}
account := ak.GetAccount(ctx, acct.Address)
encCfg := simappparams.MakeTestEncodingConfig()
tx, err := helpers.GenSignedMockTx(
r,
encCfg.TxConfig,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
chainID,
[]uint64{account.GetAccountNumber()},
[]uint64{account.GetSequence()},
acct.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to generate mock tx"), nil, err
}
_, _, err = app.SimDeliver(encCfg.TxConfig.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to deliver tx"), nil, err
}
return simtypes.NewOperationMsg(msg, true, "", codec.NewProtoCodec(encCfg.InterfaceRegistry)), nil, nil
}
}
// SimulateMsgOptOut opts an account out of quarantine.
func SimulateMsgOptOut(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k keeper.Keeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
msg := &quarantine.MsgOptOut{}
msgType := TypeMsgOptOut
// 3 in 4 chance of using a quarantined account.
// 1 in 4 chance of using a random account.
var acct simtypes.Account
if r.Intn(4) != 0 {
addr := randomQuarantinedAccount(ctx, r, k)
if len(addr) == 0 {
return simtypes.NoOpMsg(msgType, msgType, "no addresses opted in yet"), nil, nil
}
acctInd := findAccount(accs, addr)
if acctInd < 0 {
return simtypes.NoOpMsg(msgType, msgType, "account not found for quarantined address"), nil, nil
}
acct = accs[acctInd]
msg.ToAddress = addr.String()
}
if len(msg.ToAddress) == 0 {
acct, _ = simtypes.RandomAcc(r, accs)
msg.ToAddress = acct.Address.String()
}
spendableCoins := bk.SpendableCoins(ctx, acct.Address)
fees, err := simtypes.RandomFees(r, ctx, spendableCoins)
if err != nil {
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "fee error"), nil, err
}
account := ak.GetAccount(ctx, acct.Address)
encCfg := simappparams.MakeTestEncodingConfig()
tx, err := helpers.GenSignedMockTx(
r,
encCfg.TxConfig,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
chainID,
[]uint64{account.GetAccountNumber()},
[]uint64{account.GetSequence()},
acct.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to generate mock tx"), nil, err
}
_, _, err = app.SimDeliver(encCfg.TxConfig.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to deliver tx"), nil, err
}
return simtypes.NewOperationMsg(msg, true, "", codec.NewProtoCodec(encCfg.InterfaceRegistry)), nil, nil
}
}
// SimulateMsgAccept accepts quarantined funds.
func SimulateMsgAccept(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k keeper.Keeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
msg := &quarantine.MsgAccept{}
msgType := TypeMsgAccept
// 3 in 4 chance of accepting actually quarantined funds (if any exist).
// 1 in 4 chance of the accept being meaningless.
// Then, 1 in 5 chance of it being permanent.
var acct simtypes.Account
if r.Intn(4) != 0 {
funds := randomQuarantinedFunds(ctx, r, k)
if funds == nil {
return simtypes.NoOpMsg(msgType, msgType, "no funds yet quarantined"), nil, nil
}
msg.ToAddress = funds.ToAddress
msg.FromAddresses = funds.UnacceptedFromAddresses
addr, err := sdk.AccAddressFromBech32(msg.ToAddress)
if err != nil {
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "invalid to address in quarantined funds"), nil, err
}
acctInd := findAccount(accs, addr)
if acctInd < 0 {
return simtypes.NoOpMsg(msgType, msgType, "account not found for to address"), nil, nil
}
acct = accs[acctInd]
}
if len(msg.ToAddress) == 0 {
acct, _ = simtypes.RandomAcc(r, accs)
fromAcct, _ := simtypes.RandomAcc(r, accs)
msg.ToAddress = acct.Address.String()
msg.FromAddresses = append(msg.FromAddresses, fromAcct.Address.String())
}
msg.Permanent = r.Intn(5) == 0
spendableCoins := bk.SpendableCoins(ctx, acct.Address)
fees, err := simtypes.RandomFees(r, ctx, spendableCoins)
if err != nil {
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "fee error"), nil, err
}
account := ak.GetAccount(ctx, acct.Address)
encCfg := simappparams.MakeTestEncodingConfig()
tx, err := helpers.GenSignedMockTx(
r,
encCfg.TxConfig,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
chainID,
[]uint64{account.GetAccountNumber()},
[]uint64{account.GetSequence()},
acct.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to generate mock tx"), nil, err
}
_, _, err = app.SimDeliver(encCfg.TxConfig.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to deliver tx"), nil, err
}
return simtypes.NewOperationMsg(msg, true, "", codec.NewProtoCodec(encCfg.InterfaceRegistry)), nil, nil
}
}
// SimulateMsgDecline declines quarantined funds.
func SimulateMsgDecline(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k keeper.Keeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
msg := &quarantine.MsgDecline{}
msgType := TypeMsgDecline
// 3 in 4 chance of declining actually quarantined funds (if any exist).
// 1 in 4 chance of the decline being meaningless.
// Then, 1 in 5 chance of it being permanent.
var acct simtypes.Account
if r.Intn(4) != 0 {
funds := randomQuarantinedFunds(ctx, r, k)
if funds == nil {
return simtypes.NoOpMsg(msgType, msgType, "no funds yet quarantined"), nil, nil
}
msg.ToAddress = funds.ToAddress
msg.FromAddresses = funds.UnacceptedFromAddresses
addr, err := sdk.AccAddressFromBech32(msg.ToAddress)
if err != nil {
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "invalid to address in quarantined funds"), nil, err
}
acctInd := findAccount(accs, addr)
if acctInd < 0 {
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "account not found for to address"), nil, nil
}
acct = accs[acctInd]
}
if len(msg.ToAddress) == 0 {
acct, _ = simtypes.RandomAcc(r, accs)
fromAcct, _ := simtypes.RandomAcc(r, accs)
msg.ToAddress = acct.Address.String()
msg.FromAddresses = append(msg.FromAddresses, fromAcct.Address.String())
}
msg.Permanent = r.Intn(5) == 0
spendableCoins := bk.SpendableCoins(ctx, acct.Address)
fees, err := simtypes.RandomFees(r, ctx, spendableCoins)
if err != nil {
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "fee error"), nil, err
}
account := ak.GetAccount(ctx, acct.Address)
encCfg := simappparams.MakeTestEncodingConfig()
tx, err := helpers.GenSignedMockTx(
r,
encCfg.TxConfig,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
chainID,
[]uint64{account.GetAccountNumber()},
[]uint64{account.GetSequence()},
acct.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to generate mock tx"), nil, err
}
_, _, err = app.SimDeliver(encCfg.TxConfig.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to deliver tx"), nil, err
}
return simtypes.NewOperationMsg(msg, true, "", codec.NewProtoCodec(encCfg.InterfaceRegistry)), nil, nil
}
}
// SimulateMsgUpdateAutoResponses updates an accounts auto-responses
func SimulateMsgUpdateAutoResponses(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k keeper.Keeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
msg := &quarantine.MsgUpdateAutoResponses{}
msgType := TypeMsgUpdateAutoResponses
// 3 in 4 chance of using a quarantined account.
// 1 to 4 entries.
// Each entry: 75% accept, 25% decline, 5% unspecified.
var acct simtypes.Account
if r.Intn(4) != 0 {
addr := randomQuarantinedAccount(ctx, r, k)
if len(addr) == 0 {
return simtypes.NoOpMsg(msgType, msgType, "no addresses opted in yet"), nil, nil
}
acctInd := findAccount(accs, addr)
if acctInd < 0 {
return simtypes.NoOpMsg(msgType, msgType, "account not found for quarantined address"), nil, nil
}
acct = accs[acctInd]
msg.ToAddress = addr.String()
}
if len(msg.ToAddress) == 0 {
acct, _ = simtypes.RandomAcc(r, accs)
msg.ToAddress = acct.Address.String()
}
entryCount := r.Intn(3) + 1
for len(msg.Updates) < entryCount {
entry := &quarantine.AutoResponseUpdate{}
rAcct, _ := simtypes.RandomAcc(r, accs)
entry.FromAddress = rAcct.Address.String()
respR := r.Intn(20)
switch respR {
case 0:
entry.Response = quarantine.AUTO_RESPONSE_UNSPECIFIED
case 1, 2, 3, 4, 5:
entry.Response = quarantine.AUTO_RESPONSE_DECLINE
case 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19:
entry.Response = quarantine.AUTO_RESPONSE_ACCEPT
default:
err := sdkerrors.ErrLogic.Wrapf("response type random number case %d not present in switch", respR)
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, ""), nil, err
}
msg.Updates = append(msg.Updates, entry)
}
spendableCoins := bk.SpendableCoins(ctx, acct.Address)
fees, err := simtypes.RandomFees(r, ctx, spendableCoins)
if err != nil {
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "fee error"), nil, err
}
account := ak.GetAccount(ctx, acct.Address)
encCfg := simappparams.MakeTestEncodingConfig()
tx, err := helpers.GenSignedMockTx(
r,
encCfg.TxConfig,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
chainID,
[]uint64{account.GetAccountNumber()},
[]uint64{account.GetSequence()},
acct.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to generate mock tx"), nil, err
}
_, _, err = app.SimDeliver(encCfg.TxConfig.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to deliver tx"), nil, err
}
return simtypes.NewOperationMsg(msg, true, "", codec.NewProtoCodec(encCfg.InterfaceRegistry)), nil, nil
}
}
func randomQuarantinedAccount(ctx sdk.Context, r *rand.Rand, k keeper.Keeper) sdk.AccAddress {
allQuarantinedAddrs := []*sdk.AccAddress{}
k.IterateQuarantinedAccounts(ctx, func(toAddr sdk.AccAddress) bool {
allQuarantinedAddrs = append(allQuarantinedAddrs, &toAddr)
return false
})
rv := randomEntry(r, allQuarantinedAddrs)
if rv == nil || len(*rv) == 0 {
return nil
}
return *rv
}
func randomQuarantinedFunds(ctx sdk.Context, r *rand.Rand, k keeper.Keeper) *quarantine.QuarantinedFunds {
return randomEntry(r, k.GetAllQuarantinedFunds(ctx))
}
func randomEntry[V any](r *rand.Rand, addrs []*V) *V {
if len(addrs) == 0 {
return nil
}
return addrs[r.Intn(len(addrs))]
}
func findAccount(accounts []simtypes.Account, addr sdk.AccAddress) int {
for i := range accounts {
if bytes.Equal(addr, accounts[i].Address) {
return i
}
}
return -1
}