-
Notifications
You must be signed in to change notification settings - Fork 2
/
operations.go
353 lines (352 loc) · 12.1 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
package simulation
//
//import (
// "encoding/json"
// "io/ioutil"
// "math/rand"
//
// wasmvmtypes "github.com/CosmWasm/wasmvm/types"
// "github.com/cosmos/cosmos-sdk/baseapp"
// simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
// sdk "github.com/cosmos/cosmos-sdk/types"
// sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
// "github.com/cosmos/cosmos-sdk/types/module"
// simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
// "github.com/cosmos/cosmos-sdk/x/simulation"
//
// "github.com/CosmWasm/wasmd/app/params"
// wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
// "github.com/CosmWasm/wasmd/x/wasm/keeper/testdata"
// "github.com/CosmWasm/wasmd/x/wasm/types"
//)
//
//// Simulation operation weights constants
////nolint:gosec
//const (
// OpWeightMsgStoreCode = "op_weight_msg_store_code"
// OpWeightMsgInstantiateContract = "op_weight_msg_instantiate_contract"
// OpWeightMsgExecuteContract = "op_weight_msg_execute_contract"
// OpReflectContractPath = "op_reflect_contract_path"
//)
//
//// WasmKeeper is a subset of the wasm keeper used by simulations
//type WasmKeeper interface {
// GetParams(ctx sdk.Context) types.Params
// IterateCodeInfos(ctx sdk.Context, cb func(uint64, types.CodeInfo) bool)
// IterateContractInfo(ctx sdk.Context, cb func(sdk.AccAddress, types.ContractInfo) bool)
// QuerySmart(ctx sdk.Context, contractAddr sdk.AccAddress, req []byte) ([]byte, error)
// PeekAutoIncrementID(ctx sdk.Context, lastIDKey []byte) uint64
//}
//type BankKeeper interface {
// simulation.BankKeeper
// IsSendEnabledCoin(ctx sdk.Context, coin sdk.Coin) bool
//}
//
//// WeightedOperations returns all the operations from the module with their respective weights
//func WeightedOperations(
// simstate *module.SimulationState,
// ak types.AccountKeeper,
// bk BankKeeper,
// wasmKeeper WasmKeeper,
//) simulation.WeightedOperations {
// var (
// weightMsgStoreCode int
// weightMsgInstantiateContract int
// weightMsgExecuteContract int
// wasmContractPath string
// )
//
// simstate.AppParams.GetOrGenerate(simstate.Cdc, OpWeightMsgStoreCode, &weightMsgStoreCode, nil,
// func(_ *rand.Rand) {
// weightMsgStoreCode = params.DefaultWeightMsgStoreCode
// },
// )
//
// simstate.AppParams.GetOrGenerate(simstate.Cdc, OpWeightMsgInstantiateContract, &weightMsgInstantiateContract, nil,
// func(_ *rand.Rand) {
// weightMsgInstantiateContract = params.DefaultWeightMsgInstantiateContract
// },
// )
// simstate.AppParams.GetOrGenerate(simstate.Cdc, OpWeightMsgExecuteContract, &weightMsgInstantiateContract, nil,
// func(_ *rand.Rand) {
// weightMsgExecuteContract = params.DefaultWeightMsgExecuteContract
// },
// )
// simstate.AppParams.GetOrGenerate(simstate.Cdc, OpReflectContractPath, &wasmContractPath, nil,
// func(_ *rand.Rand) {
// wasmContractPath = ""
// },
// )
//
// var wasmBz []byte
// if wasmContractPath == "" {
// wasmBz = testdata.ReflectContractWasm()
// } else {
// var err error
// wasmBz, err = ioutil.ReadFile(wasmContractPath)
// if err != nil {
// panic(err)
// }
// }
//
// return simulation.WeightedOperations{
// simulation.NewWeightedOperation(
// weightMsgStoreCode,
// SimulateMsgStoreCode(ak, bk, wasmKeeper, wasmBz, 5_000_000),
// ),
// simulation.NewWeightedOperation(
// weightMsgInstantiateContract,
// SimulateMsgInstantiateContract(ak, bk, wasmKeeper, DefaultSimulationCodeIDSelector),
// ),
// simulation.NewWeightedOperation(
// weightMsgExecuteContract,
// SimulateMsgExecuteContract(
// ak,
// bk,
// wasmKeeper,
// DefaultSimulationExecuteContractSelector,
// DefaultSimulationExecuteSenderSelector,
// DefaultSimulationExecutePayloader,
// ),
// ),
// }
//}
//
//// SimulateMsgStoreCode generates a MsgStoreCode with random values
//func SimulateMsgStoreCode(ak types.AccountKeeper, bk simulation.BankKeeper, wasmKeeper WasmKeeper, wasmBz []byte, gas uint64) simtypes.Operation {
// return func(
// r *rand.Rand,
// app *baseapp.BaseApp,
// ctx sdk.Context,
// accs []simtypes.Account,
// chainID string,
// ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
// if wasmKeeper.GetParams(ctx).CodeUploadAccess.Permission != types.AccessTypeEverybody {
// return simtypes.NoOpMsg(types.ModuleName, types.MsgStoreCode{}.Type(), "no chain permission"), nil, nil
// }
//
// simAccount, _ := simtypes.RandomAcc(r, accs)
//
// permission := wasmKeeper.GetParams(ctx).InstantiateDefaultPermission
// config := permission.With(simAccount.Address)
//
// msg := types.MsgStoreCode{
// Sender: simAccount.Address.String(),
// WASMByteCode: wasmBz,
// InstantiatePermission: &config,
// }
//
// txCtx := simulation.OperationInput{
// R: r,
// App: app,
// TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
// Cdc: nil,
// Msg: &msg,
// MsgType: msg.Type(),
// Context: ctx,
// SimAccount: simAccount,
// AccountKeeper: ak,
// Bankkeeper: bk,
// ModuleName: types.ModuleName,
// }
//
// return GenAndDeliverTxWithRandFees(txCtx, gas)
// }
//}
//
//// CodeIDSelector returns code id to be used in simulations
//type CodeIDSelector = func(ctx sdk.Context, wasmKeeper WasmKeeper) uint64
//
//// DefaultSimulationCodeIDSelector picks the first code id
//func DefaultSimulationCodeIDSelector(ctx sdk.Context, wasmKeeper WasmKeeper) uint64 {
// var codeID uint64
// wasmKeeper.IterateCodeInfos(ctx, func(u uint64, info types.CodeInfo) bool {
// if info.InstantiateConfig.Permission != types.AccessTypeEverybody {
// return false
// }
// codeID = u
// return true
// })
// return codeID
//}
//
//// SimulateMsgInstantiateContract generates a MsgInstantiateContract with random values
//func SimulateMsgInstantiateContract(ak types.AccountKeeper, bk BankKeeper, wasmKeeper WasmKeeper, codeSelector CodeIDSelector) 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)
//
// codeID := codeSelector(ctx, wasmKeeper)
// if codeID == 0 {
// return simtypes.NoOpMsg(types.ModuleName, types.MsgInstantiateContract{}.Type(), "no codes with permission available"), nil, nil
// }
// deposit := sdk.Coins{}
// spendableCoins := bk.SpendableCoins(ctx, simAccount.Address)
// for _, v := range spendableCoins {
// if bk.IsSendEnabledCoin(ctx, v) {
// deposit = deposit.Add(simtypes.RandSubsetCoins(r, sdk.NewCoins(v))...)
// }
// }
//
// msg := types.MsgInstantiateContract{
// Sender: simAccount.Address.String(),
// Admin: simtypes.RandomAccounts(r, 1)[0].Address.String(),
// CodeID: codeID,
// Label: simtypes.RandStringOfLength(r, 10),
// Msg: []byte(`{}`),
// Funds: deposit,
// }
//
// txCtx := simulation.OperationInput{
// R: r,
// App: app,
// TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
// Cdc: nil,
// Msg: &msg,
// MsgType: msg.Type(),
// Context: ctx,
// SimAccount: simAccount,
// AccountKeeper: ak,
// Bankkeeper: bk,
// ModuleName: types.ModuleName,
// CoinsSpentInMsg: deposit,
// }
//
// return simulation.GenAndDeliverTxWithRandFees(txCtx)
// }
//}
//
//// MsgExecuteContractSelector returns contract address to be used in simulations
//type MsgExecuteContractSelector = func(ctx sdk.Context, wasmKeeper WasmKeeper) sdk.AccAddress
//
//// MsgExecutePayloader extension point to modify msg with custom payload
//type MsgExecutePayloader func(msg *types.MsgExecuteContract) error
//
//// MsgExecuteSenderSelector extension point that returns the sender address
//type MsgExecuteSenderSelector func(wasmKeeper WasmKeeper, ctx sdk.Context, contractAddr sdk.AccAddress, accs []simtypes.Account) (simtypes.Account, error)
//
//// SimulateMsgExecuteContract create a execute message a reflect contract instance
//func SimulateMsgExecuteContract(
// ak types.AccountKeeper,
// bk BankKeeper,
// wasmKeeper WasmKeeper,
// contractSelector MsgExecuteContractSelector,
// senderSelector MsgExecuteSenderSelector,
// payloader MsgExecutePayloader,
//) simtypes.Operation {
// return func(
// r *rand.Rand,
// app *baseapp.BaseApp,
// ctx sdk.Context,
// accs []simtypes.Account,
// chainID string,
// ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
// contractAddr := contractSelector(ctx, wasmKeeper)
// if contractAddr == nil {
// return simtypes.NoOpMsg(types.ModuleName, types.MsgExecuteContract{}.Type(), "no contract instance available"), nil, nil
// }
// simAccount, err := senderSelector(wasmKeeper, ctx, contractAddr, accs)
// if err != nil {
// return simtypes.NoOpMsg(types.ModuleName, types.MsgExecuteContract{}.Type(), "query contract owner"), nil, err
// }
//
// deposit := sdk.Coins{}
// spendableCoins := bk.SpendableCoins(ctx, simAccount.Address)
// for _, v := range spendableCoins {
// if bk.IsSendEnabledCoin(ctx, v) {
// deposit = deposit.Add(simtypes.RandSubsetCoins(r, sdk.NewCoins(v))...)
// }
// }
// if deposit.IsZero() {
// return simtypes.NoOpMsg(types.ModuleName, types.MsgExecuteContract{}.Type(), "broke account"), nil, nil
// }
// msg := types.MsgExecuteContract{
// Sender: simAccount.Address.String(),
// Contract: contractAddr.String(),
// Funds: deposit,
// }
// if err := payloader(&msg); err != nil {
// return simtypes.NoOpMsg(types.ModuleName, types.MsgExecuteContract{}.Type(), "contract execute payload"), nil, err
// }
//
// txCtx := simulation.OperationInput{
// R: r,
// App: app,
// TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
// Cdc: nil,
// Msg: &msg,
// MsgType: msg.Type(),
// Context: ctx,
// SimAccount: simAccount,
// AccountKeeper: ak,
// Bankkeeper: bk,
// ModuleName: types.ModuleName,
// CoinsSpentInMsg: deposit,
// }
// return simulation.GenAndDeliverTxWithRandFees(txCtx)
// }
//}
//
//// DefaultSimulationExecuteContractSelector picks the first contract address
//func DefaultSimulationExecuteContractSelector(ctx sdk.Context, wasmKeeper WasmKeeper) sdk.AccAddress {
// var r sdk.AccAddress
// wasmKeeper.IterateContractInfo(ctx, func(address sdk.AccAddress, info types.ContractInfo) bool {
// r = address
// return true
// })
// return r
//}
//
//// DefaultSimulationExecuteSenderSelector queries reflect contract for owner address and selects accounts
//func DefaultSimulationExecuteSenderSelector(wasmKeeper WasmKeeper, ctx sdk.Context, contractAddr sdk.AccAddress, accs []simtypes.Account) (simtypes.Account, error) {
// var none simtypes.Account
// bz, err := json.Marshal(testdata.ReflectQueryMsg{Owner: &struct{}{}})
// if err != nil {
// return none, sdkerrors.Wrap(err, "build smart query")
// }
// got, err := wasmKeeper.QuerySmart(ctx, contractAddr, bz)
// if err != nil {
// return none, sdkerrors.Wrap(err, "exec smart query")
// }
// var ownerRes testdata.OwnerResponse
// if err := json.Unmarshal(got, &ownerRes); err != nil || ownerRes.Owner == "" {
// return none, sdkerrors.Wrap(err, "parse smart query response")
// }
// ownerAddr, err := sdk.AccAddressFromBech32(ownerRes.Owner)
// if err != nil {
// return none, sdkerrors.Wrap(err, "parse contract owner address")
// }
// simAccount, ok := simtypes.FindAccount(accs, ownerAddr)
// if !ok {
// return none, sdkerrors.Wrap(err, "unknown contract owner address")
// }
// return simAccount, nil
//}
//
//// DefaultSimulationExecutePayloader implements a bank msg to send the
//// tokens from contract account back to original sender
//func DefaultSimulationExecutePayloader(msg *types.MsgExecuteContract) error {
// reflectSend := testdata.ReflectHandleMsg{
// Reflect: &testdata.ReflectPayload{
// Msgs: []wasmvmtypes.CosmosMsg{{
// Bank: &wasmvmtypes.BankMsg{
// Send: &wasmvmtypes.SendMsg{
// ToAddress: msg.Sender, //
// Amount: wasmkeeper.ConvertSdkCoinsToWasmCoins(msg.Funds),
// },
// },
// }},
// },
// }
// reflectSendBz, err := json.Marshal(reflectSend)
// if err != nil {
// return err
// }
// msg.Msg = reflectSendBz
// return nil
//}