-
Notifications
You must be signed in to change notification settings - Fork 8
/
operations.go
363 lines (315 loc) · 11.4 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
package simulation
import (
"math/rand"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/testutil"
// codectypes "github.com/cosmos/cosmos-sdk/codec/types"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/sedaprotocol/seda-chain/x/vesting/types"
)
// Simulation operation weights constants
const (
OpWeightMsgCreateVestingAccount = "op_weight_msg_create_vesting_account" //nolint:gosec
OpWeightMsgClawback = "op_weight_msg_clawback" //nolint:gosec
DefaultWeightMsgCreateVestingAccount = 100
DefaultWeightMsgClawback = 75
)
// WeightedOperations returns all the operations from the module with their respective weights
func WeightedOperations(
appParams simtypes.AppParams,
_ codec.JSONCodec,
txGen client.TxConfig,
ak types.AccountKeeper,
bk types.BankKeeper,
sk types.StakingKeeper,
) simulation.WeightedOperations {
var weightMsgCreateVestingAccount int
var weightMsgClawback int
appParams.GetOrGenerate(OpWeightMsgCreateVestingAccount, &weightMsgCreateVestingAccount, nil, func(_ *rand.Rand) {
weightMsgCreateVestingAccount = DefaultWeightMsgCreateVestingAccount
})
appParams.GetOrGenerate(OpWeightMsgClawback, &weightMsgClawback, nil, func(_ *rand.Rand) {
weightMsgClawback = DefaultWeightMsgClawback
})
return simulation.WeightedOperations{
simulation.NewWeightedOperation(
weightMsgCreateVestingAccount,
SimulateMsgCreateVestingAccount(txGen, ak, bk, sk),
),
simulation.NewWeightedOperation(
weightMsgClawback,
SimulateMsgClawback(txGen, ak, bk, sk),
),
}
}
// SimulateMsgCreateVestingAccount generates a MsgCreateVestingAccount with random values.
func SimulateMsgCreateVestingAccount(
txGen client.TxConfig,
ak types.AccountKeeper,
bk types.BankKeeper,
_ types.StakingKeeper,
) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
msgType := sdk.MsgTypeURL(&types.MsgCreateVestingAccount{})
funder, _ := simtypes.RandomAcc(r, accs)
funderAcc := ak.GetAccount(ctx, funder.Address)
spendableCoins := bk.SpendableCoins(ctx, funderAcc.GetAddress())
if err := bk.IsSendEnabledCoins(ctx, spendableCoins...); err != nil {
return simtypes.NoOpMsg(types.ModuleName, msgType, err.Error()), nil, nil
}
sendCoins := simtypes.RandSubsetCoins(r, spendableCoins)
if sendCoins.Empty() {
return simtypes.NoOpMsg(types.ModuleName, msgType, "empty coins slice"), nil, nil
}
spendableCoins, hasNeg := spendableCoins.SafeSub(sendCoins...)
var fees sdk.Coins
var err error
if !hasNeg {
fees, err = simtypes.RandomFees(r, ctx, spendableCoins)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msgType, "unable to generate fees"), nil, err
}
}
recipient := simtypes.RandomAccounts(r, 1)[0]
msg := types.NewMsgCreateVestingAccount(
funderAcc.GetAddress(),
recipient.Address,
sendCoins,
ctx.BlockTime().Unix()+1000,
false,
)
tx, err := simtestutil.GenSignedMockTx(
r,
txGen,
[]sdk.Msg{msg},
fees,
simtestutil.DefaultGenTxGas,
chainID,
[]uint64{funderAcc.GetAccountNumber()},
[]uint64{funderAcc.GetSequence()},
funder.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "unable to generate mock tx"), nil, err
}
_, _, err = app.SimDeliver(txGen.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "unable to deliver tx"), nil, err
}
// TODO(#219) activate future operations
// // future operations
var futureOps []simtypes.FutureOperation
// // recipient stakes
// op := simulateMsgDelegate(txGen, ak, bk, sk, recipient, sendCoins)
// futureOps = append(futureOps, simtypes.FutureOperation{
// BlockHeight: int(ctx.BlockHeight()) + 1,
// Op: op,
// })
// // then funder claws back
// op2 := simulateMsgClawbackFutureOp(txGen, ak, bk, sk, recipient, funder)
// futureOps = append(futureOps, simtypes.FutureOperation{
// BlockHeight: int(ctx.BlockHeight()) + 1,
// Op: op2,
// })
return simtypes.NewOperationMsg(msg, true, ""), futureOps, nil
}
}
// SimulateMsgCreateVestingAccount generates a MsgCreateVestingAccount with random values.
func SimulateMsgClawback(
txGen client.TxConfig,
ak types.AccountKeeper,
bk types.BankKeeper,
_ types.StakingKeeper,
) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
msgType := sdk.MsgTypeURL(&types.MsgClawback{})
recipient, _ := simtypes.RandomAcc(r, accs)
recipientAcc := ak.GetAccount(ctx, recipient.Address)
vestingAcc, isClawback := recipientAcc.(*types.ClawbackContinuousVestingAccount)
if !isClawback {
return simtypes.NoOpMsg(types.ModuleName, msgType, "not a vesting account"), nil, nil
}
if vestingAcc.GetVestingCoins(ctx.BlockTime()).IsZero() {
return simtypes.NoOpMsg(types.ModuleName, msgType, "vesting account not vesting anymore"), nil, nil
}
funder, found := simtypes.FindAccount(accs, sdk.MustAccAddressFromBech32(vestingAcc.FunderAddress))
if !found {
return simtypes.NoOpMsg(types.ModuleName, msgType, "failed to find funder account"), nil, nil
}
funderAcc := ak.GetAccount(ctx, funder.Address)
spendableCoins := bk.SpendableCoins(ctx, funderAcc.GetAddress())
if err := bk.IsSendEnabledCoins(ctx, spendableCoins...); err != nil {
return simtypes.NoOpMsg(types.ModuleName, msgType, err.Error()), nil, nil
}
fees, err := simtypes.RandomFees(r, ctx, spendableCoins)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msgType, "unable to generate fees"), nil, err
}
msg := types.NewMsgClawback(
funderAcc.GetAddress(),
recipient.Address,
)
tx, err := simtestutil.GenSignedMockTx(
r,
txGen,
[]sdk.Msg{msg},
fees,
simtestutil.DefaultGenTxGas,
chainID,
[]uint64{funderAcc.GetAccountNumber()},
[]uint64{funderAcc.GetSequence()},
funder.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "unable to generate mock tx"), nil, err
}
_, _, err = app.SimDeliver(txGen.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "unable to deliver tx"), nil, err
}
return simtypes.NewOperationMsg(msg, true, ""), nil, nil
}
}
//nolint:unused
func simulateMsgDelegate(
_ client.TxConfig,
_ types.AccountKeeper,
_ types.BankKeeper,
sk types.StakingKeeper,
acc simtypes.Account,
originalVesting sdk.Coins,
) simtypes.Operation {
return func(
r *rand.Rand, _ *baseapp.BaseApp, ctx sdk.Context, _ []simtypes.Account, _ string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
msgType := sdk.MsgTypeURL(&stakingtypes.MsgDelegate{})
denom, err := sk.BondDenom(ctx)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msgType, "bond denom not found"), nil, err
}
vals, err := sk.GetAllValidators(ctx)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msgType, "unable to get validators"), nil, err
}
if len(vals) < 3 {
return simtypes.NoOpMsg(types.ModuleName, msgType, "number of validators equal zero"), nil, nil
}
// simAccount, _ := simtypes.RandomAcc(r, accs)
// Spare first two validators so we don't crash the network.
val, ok := testutil.RandSliceElem(r, vals[2:])
if !ok {
return simtypes.NoOpMsg(types.ModuleName, msgType, "unable to pick a validator"), nil, nil
}
if val.InvalidExRate() {
return simtypes.NoOpMsg(types.ModuleName, msgType, "validator's invalid echange rate"), nil, nil
}
// amount := bk.GetBalance(ctx, acc.Address, denom).Amount
// if !amount.IsPositive() {
// return simtypes.NoOpMsg(types.ModuleName, msgType, "balance is negative"), nil, nil
// }
// amount, err = simtypes.RandPositiveInt(r, amount)
// if err != nil {
// return simtypes.NoOpMsg(types.ModuleName, msgType, "unable to generate positive amount"), nil, err
// }
// bondAmt := sdk.NewCoin(denom, amount)
// account := ak.GetAccount(ctx, acc.Address)
// spendable := bk.SpendableCoins(ctx, account.GetAddress())
// fees := sdk.NewCoins()
// coins, hasNeg := spendable.SafeSub(bondAmt)
// if !hasNeg {
// fees, err = simtypes.RandomFees(r, ctx, coins)
// if err != nil {
// return simtypes.NoOpMsg(types.ModuleName, msgType, "unable to generate fees"), nil, err
// }
// }
found, bondAmt := originalVesting.Find(denom)
if !found {
panic("no bond denom in original vesting coins")
}
msg := stakingtypes.NewMsgDelegate(acc.Address.String(), val.GetOperator(), bondAmt)
// Removing sim check since we cannot know the account number in advance
// tx, err := simtestutil.GenSignedMockTx(
// r,
// txGen,
// []sdk.Msg{msg},
// fees,
// simtestutil.DefaultGenTxGas,
// chainID,
// []uint64{uint64(numAccs)}, //[]uint64{account.GetAccountNumber()},
// []uint64{0}, //[]uint64{account.GetSequence()},
// acc.PrivKey,
// )
// if err != nil {
// return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "unable to generate mock tx"), nil, err
// }
// _, _, err = app.SimDeliver(txGen.TxEncoder(), tx)
// if err != nil {
// return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "unable to deliver tx"), nil, err
// }
return simtypes.NewOperationMsg(msg, true, ""), nil, nil
}
}
//nolint:unused
func simulateMsgClawbackFutureOp(
txGen client.TxConfig,
ak types.AccountKeeper,
bk types.BankKeeper,
_ types.StakingKeeper,
recipient simtypes.Account,
funder simtypes.Account,
) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
msgType := sdk.MsgTypeURL(&types.MsgClawback{})
funder, found := simtypes.FindAccount(accs, funder.Address)
if !found {
return simtypes.NoOpMsg(types.ModuleName, msgType, "failed to find funder account"), nil, nil
}
funderAcc := ak.GetAccount(ctx, funder.Address)
spendableCoins := bk.SpendableCoins(ctx, funderAcc.GetAddress())
if err := bk.IsSendEnabledCoins(ctx, spendableCoins...); err != nil {
return simtypes.NoOpMsg(types.ModuleName, msgType, err.Error()), nil, nil
}
// fees, err := simtypes.RandomFees(r, ctx, spendableCoins)
// if err != nil {
// return simtypes.NoOpMsg(types.ModuleName, msgType, "unable to generate fees"), nil, err
// }
msg := types.NewMsgClawback(
funderAcc.GetAddress(),
recipient.Address,
)
tx, err := simtestutil.GenSignedMockTx(
r,
txGen,
[]sdk.Msg{msg},
sdk.NewCoins(), // fees,
simtestutil.DefaultGenTxGas,
chainID,
[]uint64{funderAcc.GetAccountNumber()},
[]uint64{funderAcc.GetSequence()},
funder.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "unable to generate mock tx"), nil, err
}
_, _, err = app.SimDeliver(txGen.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "unable to deliver tx"), nil, err
}
return simtypes.NewOperationMsg(msg, true, ""), nil, nil
}
}