diff --git a/app/app.go b/app/app.go index b6ba2ddab9..f64ac4d34c 100644 --- a/app/app.go +++ b/app/app.go @@ -515,7 +515,7 @@ func NewWasmApp( distr.NewAppModule(appCodec, app.distrKeeper, app.accountKeeper, app.bankKeeper, app.stakingKeeper), staking.NewAppModule(appCodec, app.stakingKeeper, app.accountKeeper, app.bankKeeper), upgrade.NewAppModule(app.upgradeKeeper), - wasm.NewAppModule(appCodec, &app.wasmKeeper, app.stakingKeeper), + wasm.NewAppModule(appCodec, &app.wasmKeeper, app.stakingKeeper, app.accountKeeper), evidence.NewAppModule(app.evidenceKeeper), feegrantmodule.NewAppModule(appCodec, app.accountKeeper, app.bankKeeper, app.FeeGrantKeeper, app.interfaceRegistry), authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.accountKeeper, app.bankKeeper, app.interfaceRegistry), @@ -632,7 +632,7 @@ func NewWasmApp( slashing.NewAppModule(appCodec, app.slashingKeeper, app.accountKeeper, app.bankKeeper, app.stakingKeeper), params.NewAppModule(app.paramsKeeper), evidence.NewAppModule(app.evidenceKeeper), - wasm.NewAppModule(appCodec, &app.wasmKeeper, app.stakingKeeper), + wasm.NewAppModule(appCodec, &app.wasmKeeper, app.stakingKeeper, app.accountKeeper), ibc.NewAppModule(app.ibcKeeper), transferModule, ) diff --git a/app/params/weights.go b/app/params/weights.go index 0ba377b009..907071732b 100644 --- a/app/params/weights.go +++ b/app/params/weights.go @@ -20,4 +20,5 @@ const ( DefaultWeightCommunitySpendProposal int = 5 DefaultWeightTextProposal int = 5 DefaultWeightParamChangeProposal int = 5 + DefaultWeightMsgStoreCode int = 100 ) diff --git a/x/wasm/module.go b/x/wasm/module.go index 9453cf09a0..b054288845 100644 --- a/x/wasm/module.go +++ b/x/wasm/module.go @@ -103,6 +103,7 @@ type AppModule struct { cdc codec.Codec keeper *Keeper validatorSetSource keeper.ValidatorSetSource + accountKeeper types.AccountKeeper // for simulation } // ConsensusVersion is a sequence number for state-breaking change of the @@ -112,12 +113,13 @@ type AppModule struct { func (AppModule) ConsensusVersion() uint64 { return 1 } // NewAppModule creates a new AppModule object -func NewAppModule(cdc codec.Codec, keeper *Keeper, validatorSetSource keeper.ValidatorSetSource) AppModule { +func NewAppModule(cdc codec.Codec, keeper *Keeper, validatorSetSource keeper.ValidatorSetSource, ak types.AccountKeeper) AppModule { return AppModule{ AppModuleBasic: AppModuleBasic{}, cdc: cdc, keeper: keeper, validatorSetSource: validatorSetSource, + accountKeeper: ak, } } @@ -196,7 +198,7 @@ func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { // WeightedOperations returns the all the gov module operations with their respective weights. func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { - return nil + return simulation.WeightedOperations(&simState, am.accountKeeper) } // ____________________________________________________________________________ diff --git a/x/wasm/module_test.go b/x/wasm/module_test.go index 360743a125..e2bb62f7fd 100644 --- a/x/wasm/module_test.go +++ b/x/wasm/module_test.go @@ -36,7 +36,7 @@ func setupTest(t *testing.T) testData { ctx, keepers := CreateTestInput(t, false, "iterator,staking,stargate") cdc := keeper.MakeTestCodec(t) data := testData{ - module: NewAppModule(cdc, keepers.WasmKeeper, keepers.StakingKeeper), + module: NewAppModule(cdc, keepers.WasmKeeper, keepers.StakingKeeper, nil), ctx: ctx, acctKeeper: keepers.AccountKeeper, keeper: *keepers.WasmKeeper, diff --git a/x/wasm/simulation/operations.go b/x/wasm/simulation/operations.go new file mode 100644 index 0000000000..7c7a6002ac --- /dev/null +++ b/x/wasm/simulation/operations.go @@ -0,0 +1,82 @@ +package simulation + +import ( + _ "embed" + "math/rand" + + "github.com/CosmWasm/wasmd/app/params" + "github.com/cosmos/cosmos-sdk/types/module" + + "github.com/CosmWasm/wasmd/x/wasm/keeper" + "github.com/CosmWasm/wasmd/x/wasm/types" + "github.com/cosmos/cosmos-sdk/baseapp" + 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" +) + +//go:embed testdata/reflect.wasm +var reflectContract []byte + +// Simulation operation weights constants +//nolint:gosec +const ( + OpWeightMsgStoreCode = "op_weight_msg_store_code" +) + +// WeightedOperations returns all the operations from the module with their respective weights +func WeightedOperations( + simstate *module.SimulationState, ak types.AccountKeeper) simulation.WeightedOperations { + var ( + weightMsgStoreCode int + ) + + simstate.AppParams.GetOrGenerate(simstate.Cdc, OpWeightMsgStoreCode, &weightMsgStoreCode, nil, + func(_ *rand.Rand) { + weightMsgStoreCode = params.DefaultWeightMsgStoreCode + }, + ) + + return simulation.WeightedOperations{ + simulation.NewWeightedOperation( + weightMsgStoreCode, + SimulateMsgStoreCode(ak), + ), + } +} + +// SimulateMsgStoreCode generates a MsgStoreCode with random values +func SimulateMsgStoreCode(ak types.AccountKeeper) 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) + + config := &types.AccessConfig{ + Permission: 3, + Address: keeper.RandomBech32AccountAddress(nil), + } + + msg := types.MsgStoreCode{ + Sender: keeper.RandomBech32AccountAddress(nil), + WASMByteCode: reflectContract, + InstantiatePermission: config, + } + + txCtx := simulation.OperationInput{ + App: app, + TxGen: simappparams.MakeTestEncodingConfig().TxConfig, + Cdc: nil, + Msg: &msg, + MsgType: msg.Type(), + Context: ctx, + SimAccount: simAccount, + AccountKeeper: ak, + ModuleName: types.ModuleName, + } + + return simulation.GenAndDeliverTxWithRandFees(txCtx) + } +} diff --git a/x/wasm/simulation/testdata/reflect.wasm b/x/wasm/simulation/testdata/reflect.wasm new file mode 100644 index 0000000000..312f45768d Binary files /dev/null and b/x/wasm/simulation/testdata/reflect.wasm differ