-
Notifications
You must be signed in to change notification settings - Fork 210
/
operations.go
281 lines (232 loc) · 9.3 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
package simulation
import (
"errors"
"fmt"
"math/rand"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp/helpers"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/simulation"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
types "github.com/akash-network/akash-api/go/node/market/v1beta3"
appparams "github.com/akash-network/node/app/params"
testsim "github.com/akash-network/node/testutil/sim"
keepers "github.com/akash-network/node/x/market/handler"
)
// Simulation operation weights constants
const (
OpWeightMsgCreateBid = "op_weight_msg_create_bid" // nolint gosec
OpWeightMsgCloseBid = "op_weight_msg_close_bid" // nolint gosec
OpWeightMsgCloseLease = "op_weight_msg_close_lease" // nolint gosec
)
// WeightedOperations returns all the operations from the module with their respective weights
func WeightedOperations(
appParams simtypes.AppParams, cdc codec.JSONCodec, ak govtypes.AccountKeeper,
ks keepers.Keepers) simulation.WeightedOperations {
var (
weightMsgCreateBid int
weightMsgCloseBid int
weightMsgCloseLease int
)
appParams.GetOrGenerate(
cdc, OpWeightMsgCreateBid, &weightMsgCreateBid, nil, func(r *rand.Rand) {
weightMsgCreateBid = appparams.DefaultWeightMsgCreateBid
},
)
appParams.GetOrGenerate(
cdc, OpWeightMsgCloseBid, &weightMsgCloseBid, nil, func(r *rand.Rand) {
weightMsgCloseBid = appparams.DefaultWeightMsgCloseBid
},
)
appParams.GetOrGenerate(
cdc, OpWeightMsgCloseLease, &weightMsgCloseLease, nil, func(r *rand.Rand) {
weightMsgCloseLease = appparams.DefaultWeightMsgCloseLease
},
)
return simulation.WeightedOperations{
simulation.NewWeightedOperation(
weightMsgCreateBid,
SimulateMsgCreateBid(ak, ks),
),
simulation.NewWeightedOperation(
weightMsgCloseBid,
SimulateMsgCloseBid(ak, ks),
),
simulation.NewWeightedOperation(
weightMsgCloseLease,
SimulateMsgCloseLease(ak, ks),
),
}
}
// SimulateMsgCreateBid generates a MsgCreateBid with random values
func SimulateMsgCreateBid(ak govtypes.AccountKeeper, ks keepers.Keepers) simtypes.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accounts []simtypes.Account,
chainID string) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
orders := getOrdersWithState(ctx, ks, types.OrderOpen)
if len(orders) == 0 {
return simtypes.NoOpMsg(types.ModuleName, types.MsgTypeCreateBid, "no open orders found"), nil, nil
}
// Get random order
order := orders[testsim.RandIdx(r, len(orders)-1)]
providers := getProviders(ctx, ks)
if len(providers) == 0 {
return simtypes.NoOpMsg(types.ModuleName, types.MsgTypeCreateBid, "no providers found"), nil, nil
}
// Get random deployment
provider := providers[testsim.RandIdx(r, len(providers)-1)]
ownerAddr, convertErr := sdk.AccAddressFromBech32(provider.Owner)
if convertErr != nil {
return simtypes.NoOpMsg(types.ModuleName, types.MsgTypeCreateBid, "error while converting address"), nil, convertErr
}
simAccount, found := simtypes.FindAccount(accounts, ownerAddr)
if !found {
return simtypes.NoOpMsg(types.ModuleName, types.MsgTypeCreateBid, "unable to find provider"),
nil, fmt.Errorf("provider with %s not found", provider.Owner)
}
if provider.Owner == order.ID().Owner {
return simtypes.NoOpMsg(types.ModuleName, types.MsgTypeCreateBid, "provider and order owner cannot be same"),
nil, nil
}
depositAmount := minDeposit
account := ak.GetAccount(ctx, simAccount.Address)
spendable := ks.Bank.SpendableCoins(ctx, account.GetAddress())
if spendable.AmountOf(depositAmount.Denom).LT(depositAmount.Amount.MulRaw(2)) {
return simtypes.NoOpMsg(types.ModuleName, types.MsgTypeCreateBid, "out of money"), nil, nil
}
spendable = spendable.Sub(sdk.NewCoins(depositAmount))
fees, err := simtypes.RandomFees(r, ctx, spendable)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.MsgTypeCreateBid, "unable to generate fees"), nil, err
}
msg := types.NewMsgCreateBid(order.OrderID, simAccount.Address, order.Price(), depositAmount)
txGen := simappparams.MakeTestEncodingConfig().TxConfig
tx, err := helpers.GenTx(
txGen,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
chainID,
[]uint64{account.GetAccountNumber()},
[]uint64{account.GetSequence()},
simAccount.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
}
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
switch {
case err == nil:
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
case errors.Is(err, types.ErrBidExists):
return simtypes.NewOperationMsg(msg, false, "", nil), nil, nil
default:
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver mock tx"), nil, err
}
}
}
// SimulateMsgCloseBid generates a MsgCloseBid with random values
func SimulateMsgCloseBid(ak govtypes.AccountKeeper, ks keepers.Keepers) simtypes.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accounts []simtypes.Account,
chainID string) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
var bids []types.Bid
ks.Market.WithBids(ctx, func(bid types.Bid) bool {
if bid.State == types.BidActive {
lease, ok := ks.Market.GetLease(ctx, types.LeaseID(bid.BidID))
if ok && lease.State == types.LeaseActive {
bids = append(bids, bid)
}
}
return false
})
if len(bids) == 0 {
return simtypes.NoOpMsg(types.ModuleName, types.MsgTypeCloseBid, "no matched bids found"), nil, nil
}
// Get random bid
bid := bids[testsim.RandIdx(r, len(bids)-1)]
providerAddr, convertErr := sdk.AccAddressFromBech32(bid.ID().Provider)
if convertErr != nil {
return simtypes.NoOpMsg(types.ModuleName, types.MsgTypeCloseBid, "error while converting address"), nil, convertErr
}
simAccount, found := simtypes.FindAccount(accounts, providerAddr)
if !found {
return simtypes.NoOpMsg(types.ModuleName, types.MsgTypeCloseBid, "unable to find bid with provider"),
nil, fmt.Errorf("bid with %s not found", bid.ID().Provider)
}
account := ak.GetAccount(ctx, simAccount.Address)
spendable := ks.Bank.SpendableCoins(ctx, account.GetAddress())
fees, err := simtypes.RandomFees(r, ctx, spendable)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.MsgTypeCloseBid, "unable to generate fees"), nil, err
}
msg := types.NewMsgCloseBid(bid.BidID)
txGen := simappparams.MakeTestEncodingConfig().TxConfig
tx, err := helpers.GenTx(
txGen,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
chainID,
[]uint64{account.GetAccountNumber()},
[]uint64{account.GetSequence()},
simAccount.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
}
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
}
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
}
}
// SimulateMsgCloseLease generates a MsgCloseLease with random values
func SimulateMsgCloseLease(ak govtypes.AccountKeeper, ks keepers.Keepers) simtypes.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accounts []simtypes.Account,
chainID string) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
// orders := getOrdersWithState(ctx, ks, types.OrderActive)
// if len(orders) == 0 {
// return simtypes.NoOpMsg(types.ModuleName, types.MsgTypeCloseLease, "no orders with state matched found"), nil, nil
// }
// // Get random order
// order := orders[testsim.RandIdx(r, len(orders) - 1)]
// ownerAddr, convertErr := sdk.AccAddressFromBech32(order.ID().Owner)
// if convertErr != nil {
// return simtypes.NoOpMsg(types.ModuleName, types.MsgTypeCloseLease, "error while converting address"), nil, convertErr
// }
// simAccount, found := simtypes.FindAccount(accounts, ownerAddr)
// if !found {
// return simtypes.NoOpMsg(types.ModuleName, types.MsgTypeCloseLease, "unable to find order"),
// nil, errors.Errorf("order with %s not found", order.ID().Owner)
// }
// account := ak.GetAccount(ctx, simAccount.Address)
// spendable := ks.Bank.SpendableCoins(ctx, account.GetAddress())
// fees, err := simtypes.RandomFees(r, ctx, spendable)
// if err != nil {
// return simtypes.NoOpMsg(types.ModuleName, types.MsgTypeCloseBid, "unable to generate fees"), nil, err
// }
// msg := types.NewMsgCloseLease(order.OrderID)
// txGen := simappparams.MakeTestEncodingConfig().TxConfig
// tx, err := helpers.GenTx(
// txGen,
// []sdk.Msg{msg},
// fees,
// helpers.DefaultGenTxGas,
// chainID,
// []uint64{account.GetAccountNumber()},
// []uint64{account.GetSequence()},
// simAccount.PrivKey,
// )
// if err != nil {
// return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
// }
// _, _, err = app.Deliver(txGen.TxEncoder(), tx)
// if err != nil {
// return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
// }
return simtypes.NoOpMsg(types.ModuleName, types.MsgTypeCloseLease, "skipping"), nil, nil
}
}