/
operations.go
248 lines (214 loc) · 8.31 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
package simulation
import (
"math/rand"
"time"
"github.com/cosmos/cosmos-sdk/baseapp"
osmo_simulation "github.com/osmosis-labs/osmosis/x/simulation"
"github.com/cosmos/cosmos-sdk/codec"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
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/osmosis-labs/osmosis/x/lockup/keeper"
"github.com/osmosis-labs/osmosis/x/lockup/types"
)
// Simulation operation weights constants
const (
DefaultWeightMsgLockTokens int = 10
DefaultWeightMsgBeginUnlockingAll int = 10
DefaultWeightMsgUnlockTokens int = 10
DefaultWeightMsgBeginUnlocking int = 10
DefaultWeightMsgUnlockPeriodLock int = 10
OpWeightMsgLockTokens = "op_weight_msg_create_lockup"
OpWeightMsgBeginUnlockingAll = "op_weight_msg_begin_unlocking_all"
OpWeightMsgUnlockTokens = "op_weight_msg_unlock_tokens"
OpWeightMsgBeginUnlocking = "op_weight_msg_begin_unlocking"
OpWeightMsgUnlockPeriodLock = "op_weight_msg_unlock_period_lock"
)
// WeightedOperations returns all the operations from the module with their respective weights
func WeightedOperations(
appParams simtypes.AppParams, cdc codec.JSONMarshaler, ak stakingTypes.AccountKeeper,
bk stakingTypes.BankKeeper, k keeper.Keeper,
) simulation.WeightedOperations {
var (
weightMsgLockTokens int
weightMsgBeginUnlockingAll int
weightMsgUnlockTokens int
weightMsgBeginUnlocking int
weightMsgUnlockPeriodLock int
)
appParams.GetOrGenerate(cdc, OpWeightMsgLockTokens, &weightMsgLockTokens, nil,
func(_ *rand.Rand) {
weightMsgLockTokens = DefaultWeightMsgLockTokens
weightMsgBeginUnlockingAll = DefaultWeightMsgBeginUnlockingAll
weightMsgUnlockTokens = DefaultWeightMsgUnlockTokens
weightMsgBeginUnlocking = DefaultWeightMsgBeginUnlocking
weightMsgUnlockPeriodLock = DefaultWeightMsgUnlockPeriodLock
},
)
return simulation.WeightedOperations{
simulation.NewWeightedOperation(
weightMsgLockTokens,
SimulateMsgLockTokens(ak, bk, k),
),
simulation.NewWeightedOperation(
weightMsgBeginUnlockingAll,
SimulateMsgBeginUnlockingAll(ak, bk, k),
),
simulation.NewWeightedOperation(
weightMsgUnlockTokens,
SimulateMsgUnlockTokens(ak, bk, k),
),
simulation.NewWeightedOperation(
weightMsgBeginUnlocking,
SimulateMsgBeginUnlocking(ak, bk, k),
), simulation.NewWeightedOperation(
weightMsgUnlockPeriodLock,
SimulateMsgUnlockPeriodLock(ak, bk, k),
),
}
}
func genLockTokens(r *rand.Rand, acct simtypes.Account, coins sdk.Coins) (res sdk.Coins) {
numCoins := 1 + r.Intn(Min(coins.Len(), 6))
denomIndices := r.Perm(numCoins)
for i := 0; i < numCoins; i++ {
denom := coins[denomIndices[i]].Denom
amt, _ := simtypes.RandPositiveInt(r, coins[i].Amount)
res = append(res, sdk.Coin{Denom: denom, Amount: amt})
}
res.Sort()
return
}
func Min(x, y int) int {
if x < y {
return x
}
return y
}
func Max(x, y int) int {
if x > y {
return x
}
return y
}
// SimulateMsgLockTokens generates a MsgLockTokens with random values
func SimulateMsgLockTokens(ak stakingTypes.AccountKeeper, bk stakingTypes.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) {
simAccount, _ := simtypes.RandomAcc(r, accs)
simCoins := bk.SpendableCoins(ctx, simAccount.Address)
if simCoins.Len() <= 0 {
return simtypes.NoOpMsg(
types.ModuleName, types.TypeMsgLockTokens, "Account have no coin"), nil, nil
}
lockTokens := genLockTokens(r, simAccount, simCoins)
durationSecs := r.Intn(1 * 60 * 60 * 24 * 7) // range of 1 week
duration := time.Duration(durationSecs) * time.Second
msg := types.MsgLockTokens{
Owner: simAccount.Address.String(),
Duration: duration,
Coins: lockTokens,
}
txGen := simappparams.MakeTestEncodingConfig().TxConfig
return osmo_simulation.GenAndDeliverTxWithRandFees(
r, app, txGen, &msg, lockTokens, ctx, simAccount, ak, bk, types.ModuleName)
}
}
func SimulateMsgBeginUnlockingAll(ak stakingTypes.AccountKeeper, bk stakingTypes.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) {
simAccount, _ := simtypes.RandomAcc(r, accs)
simCoins := bk.SpendableCoins(ctx, simAccount.Address)
if simCoins.Len() <= 0 {
return simtypes.NoOpMsg(
types.ModuleName, types.TypeMsgBeginUnlockingAll, "Account have no coin"), nil, nil
}
msg := types.MsgBeginUnlockingAll{
Owner: simAccount.Address.String(),
}
txGen := simappparams.MakeTestEncodingConfig().TxConfig
return osmo_simulation.GenAndDeliverTxWithRandFees(
r, app, txGen, &msg, nil, ctx, simAccount, ak, bk, types.ModuleName)
}
}
func SimulateMsgUnlockTokens(ak stakingTypes.AccountKeeper, bk stakingTypes.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) {
simAccount, _ := simtypes.RandomAcc(r, accs)
simCoins := bk.SpendableCoins(ctx, simAccount.Address)
if simCoins.Len() <= 0 {
return simtypes.NoOpMsg(
types.ModuleName, types.TypeMsgUnlockTokens, "Account have no coin"), nil, nil
}
msg := types.MsgUnlockTokens{
Owner: simAccount.Address.String(),
}
txGen := simappparams.MakeTestEncodingConfig().TxConfig
return osmo_simulation.GenAndDeliverTxWithRandFees(
r, app, txGen, &msg, nil, ctx, simAccount, ak, bk, types.ModuleName)
}
}
func SimulateMsgBeginUnlocking(ak stakingTypes.AccountKeeper, bk stakingTypes.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) {
simAccount, _ := simtypes.RandomAcc(r, accs)
simCoins := bk.SpendableCoins(ctx, simAccount.Address)
if simCoins.Len() <= 0 {
return simtypes.NoOpMsg(
types.ModuleName, types.TypeMsgBeginUnlocking, "Account have no coin"), nil, nil
}
lock := RandomAccountLock(ctx, r, k, simAccount.Address)
if lock == nil {
return simtypes.NoOpMsg(
types.ModuleName, types.TypeMsgBeginUnlocking, "Account have no period lock"), nil, nil
}
msg := types.MsgBeginUnlocking{
Owner: simAccount.Address.String(),
ID: lock.ID,
}
txGen := simappparams.MakeTestEncodingConfig().TxConfig
return osmo_simulation.GenAndDeliverTxWithRandFees(
r, app, txGen, &msg, nil, ctx, simAccount, ak, bk, types.ModuleName)
}
}
func SimulateMsgUnlockPeriodLock(ak stakingTypes.AccountKeeper, bk stakingTypes.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) {
simAccount, _ := simtypes.RandomAcc(r, accs)
simCoins := bk.SpendableCoins(ctx, simAccount.Address)
if simCoins.Len() <= 0 {
return simtypes.NoOpMsg(
types.ModuleName, types.TypeMsgUnlockPeriodLock, "Account have no coin"), nil, nil
}
lock := RandomAccountLock(ctx, r, k, simAccount.Address)
if lock == nil {
return simtypes.NoOpMsg(
types.ModuleName, types.TypeMsgUnlockPeriodLock, "Account have no period lock"), nil, nil
}
// TODO: Switch this to instead be a future op on locking
if k.GetAccountUnlockableCoins(ctx, simAccount.Address).Empty() {
return simtypes.NoOpMsg(
types.ModuleName, types.TypeMsgUnlockPeriodLock, "Account hasn't started unlocking"), nil, nil
}
msg := types.MsgUnlockPeriodLock{
Owner: simAccount.Address.String(),
ID: lock.ID,
}
txGen := simappparams.MakeTestEncodingConfig().TxConfig
return osmo_simulation.GenAndDeliverTxWithRandFees(
r, app, txGen, &msg, nil, ctx, simAccount, ak, bk, types.ModuleName)
}
}
func RandomAccountLock(ctx sdk.Context, r *rand.Rand, k keeper.Keeper, addr sdk.AccAddress) *types.PeriodLock {
locks := k.GetAccountPeriodLocks(ctx, addr)
if len(locks) == 0 {
return nil
}
return &locks[r.Intn(len(locks))]
}