From bca91646bc168bbbbd63605883581f491b3a6386 Mon Sep 17 00:00:00 2001 From: Daniel Wedul Date: Tue, 16 Apr 2024 15:38:33 -0600 Subject: [PATCH] Use SimulationState fields for the encoders needed in the simulations. (#1931) * [1760]: Remove use of MakeTestEncodingConfig from the attribute module. * [1760]: Remove use of MakeTestEncodingConfig from the ibcratelimit module. * [1760]: Remove use of MakeTestEncodingConfig from the marker module. * [1760]: Remove use of MakeTestEncodingConfig from the name module. * [1760]: Remove use of MakeTestEncodingConfig from the provwasm simulation stuff. * [1760]: Remove use of MakeTestEncodingConfig from the oracle module. Make the oracle operations tests run. * [1760]: Remove use of MakeTestEncodingConfig from the trigger module. * [1760]: Remove use of MakeTestEncodingConfig from the sanction module. * [1760]: Remove use of MakeTestEncodingConfig from the quarantine module. * [1760]: Delete the now-unwanted MakeTestEncodingConfig func (and the file it was in since that's all that was in there). * [1760]: Add changelog entry. * [1760]: Shrink all the WeightedOperations funcs by removing some newlines inside function calls and stuff. * [1760]: Fix all uses of simtypes.NoOpMsg that were providing the msg-type URL instead of the router key. Move all the test kick-off funcs to the tops of the operations tests so that next time one is copy/pasted, it's harder to accidentally delete it. --- CHANGELOG.md | 1 + app/params/proto.go | 46 ------ internal/provwasm/simulation.go | 64 ++++--- x/attribute/module.go | 5 +- x/attribute/simulation/operations.go | 127 ++++++-------- x/attribute/simulation/operations_test.go | 33 ++-- x/ibcratelimit/module/module.go | 7 +- x/ibcratelimit/simulation/operations.go | 43 +++-- x/ibcratelimit/simulation/operations_test.go | 27 ++- x/marker/module.go | 2 +- x/marker/simulation/operations.go | 165 +++++++------------ x/marker/simulation/operations_test.go | 43 ++--- x/name/module.go | 7 +- x/name/simulation/operations.go | 91 ++++------ x/name/simulation/operations_test.go | 29 ++-- x/oracle/module/module.go | 4 +- x/oracle/simulation/operations.go | 64 +++---- x/oracle/simulation/operations_test.go | 24 ++- x/quarantine/module/module.go | 6 +- x/quarantine/simulation/operations.go | 82 +++++---- x/quarantine/simulation/operations_test.go | 28 ++-- x/sanction/module/module.go | 2 +- x/sanction/simulation/operations.go | 54 +++--- x/sanction/simulation/operations_test.go | 15 +- x/trigger/module/module.go | 4 +- x/trigger/simulation/operations.go | 63 +++---- x/trigger/simulation/operations_test.go | 31 ++-- 27 files changed, 464 insertions(+), 603 deletions(-) delete mode 100644 app/params/proto.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 1dbbce1df5..5e0e143005 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * Restore the hold module [#1930](https://github.com/provenance-io/provenance/pull/1930). * Restore gov-prop cli commands and fix next key decoding [#1930](https://github.com/provenance-io/provenance/pull/1930). * Switch to InputOutputCoinsProv for exchange transfers [#1930](https://github.com/provenance-io/provenance/pull/1930). +* Use fields of the SimulationState for the encoders needed for simulations [#1931](https://github.com/provenance-io/provenance/pull/1931). ### Dependencies diff --git a/app/params/proto.go b/app/params/proto.go deleted file mode 100644 index 983cf7159f..0000000000 --- a/app/params/proto.go +++ /dev/null @@ -1,46 +0,0 @@ -package params - -import ( - "cosmossdk.io/x/tx/signing" - - "github.com/cosmos/cosmos-sdk/codec" - amino "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/codec/address" - "github.com/cosmos/cosmos-sdk/codec/types" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/auth/tx" - "github.com/cosmos/gogoproto/proto" -) - -// MakeTestEncodingConfig creates an EncodingConfig for a non-amino based test configuration. -// This function should be used only internally (in the SDK). -// App user shouldn't create new codecs - use the app.AppCodec instead. -// -// TODO[1760]: Update all the simulation stuff that uses this to instead get what's needed from the SimState. -// That will involve passing the SimSate into many places where we used to provide a codec. Then, delete this file. -// -// Deprecated: Either get this from the app (app.GetEncodingConfig()) or use MakeTestEncodingConfig (from the app package), -// or get what's needed from the SimSate. -func MakeTestEncodingConfig() EncodingConfig { - cdc := amino.NewLegacyAmino() - signingOptions := signing.Options{ - AddressCodec: address.Bech32Codec{ - Bech32Prefix: sdk.GetConfig().GetBech32AccountAddrPrefix(), - }, - ValidatorAddressCodec: address.Bech32Codec{ - Bech32Prefix: sdk.GetConfig().GetBech32ValidatorAddrPrefix(), - }, - } - interfaceRegistry, _ := types.NewInterfaceRegistryWithOptions(types.InterfaceRegistryOptions{ - ProtoFiles: proto.HybridResolver, - SigningOptions: signingOptions, - }) - marshaler := codec.NewProtoCodec(interfaceRegistry) - - return EncodingConfig{ - InterfaceRegistry: interfaceRegistry, - Marshaler: marshaler, - TxConfig: tx.NewTxConfig(marshaler, tx.DefaultSignModes), - Amino: cdc, - } -} diff --git a/internal/provwasm/simulation.go b/internal/provwasm/simulation.go index f7b5037516..447b060b42 100644 --- a/internal/provwasm/simulation.go +++ b/internal/provwasm/simulation.go @@ -11,13 +11,11 @@ import ( "github.com/CosmWasm/wasmd/x/wasm/types" sdkmath "cosmossdk.io/math" - "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" - moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" "github.com/cosmos/cosmos-sdk/x/bank/exported" @@ -99,18 +97,18 @@ func (pw Wrapper) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) { } // WeightedOperations returns the all the provwasm operations with their respective weights. -func (pw Wrapper) WeightedOperations(_ module.SimulationState) []simtypes.WeightedOperation { +func (pw Wrapper) WeightedOperations(simSate module.SimulationState) []simtypes.WeightedOperation { count := 0 return []simtypes.WeightedOperation{ simulation.NewWeightedOperation( 100, - SimulateMsgBindName(pw.ak, pw.bk, pw.nk, &count), + SimulateMsgBindName(simSate, pw.ak, pw.bk, pw.nk, &count), ), } } // SimulateMsgBindName will bind a NAME under an existing name -func SimulateMsgBindName(ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper, nk namekeeper.Keeper, count *int) simtypes.Operation { +func SimulateMsgBindName(simState module.SimulationState, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper, nk namekeeper.Keeper, count *int) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { @@ -149,17 +147,17 @@ func SimulateMsgBindName(ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper, nk false), parent) - op, future, err2 := namesim.Dispatch(r, app, ctx, ak, bk, node, chainID, msg) + op, future, err2 := namesim.Dispatch(r, app, ctx, simState, ak, bk, node, chainID, msg) name := namePrefix + "." + parent.Name - future = append(future, simtypes.FutureOperation{Op: SimulateMsgAddMarker(ak, bk, nk, node, feebucket, merchant, consumer, name), BlockHeight: int(ctx.BlockHeight()) + 1}) + future = append(future, simtypes.FutureOperation{Op: SimulateMsgAddMarker(simState, ak, bk, nk, node, feebucket, merchant, consumer, name), BlockHeight: int(ctx.BlockHeight()) + 1}) return op, future, err2 } } -func SimulateMsgAddMarker(ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper, nk namekeeper.Keeper, node, feebucket, merchant, consumer simtypes.Account, name string) simtypes.Operation { +func SimulateMsgAddMarker(simState module.SimulationState, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper, nk namekeeper.Keeper, node, feebucket, merchant, consumer simtypes.Account, name string) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { @@ -189,58 +187,58 @@ func SimulateMsgAddMarker(ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper, nk return simtypes.NoOpMsg("provwasm", "", "unable to fund account"), nil, nil } - msg2, ops, err := markersim.Dispatch(r, app, ctx, ak, bk, node, chainID, msg, nil) + msg2, ops, err := markersim.Dispatch(r, app, ctx, simState, ak, bk, node, chainID, msg, nil) - ops = append(ops, simtypes.FutureOperation{Op: SimulateMsgAddAccess(ak, bk, nk, node, feebucket, merchant, consumer, name), BlockHeight: int(ctx.BlockHeight()) + 1}) + ops = append(ops, simtypes.FutureOperation{Op: SimulateMsgAddAccess(simState, ak, bk, nk, node, feebucket, merchant, consumer, name), BlockHeight: int(ctx.BlockHeight()) + 1}) return msg2, ops, err } } -func SimulateMsgAddAccess(ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper, nk namekeeper.Keeper, node, feebucket, merchant, consumer simtypes.Account, name string) simtypes.Operation { +func SimulateMsgAddAccess(simState module.SimulationState, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper, nk namekeeper.Keeper, node, feebucket, merchant, consumer simtypes.Account, name string) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { accessTypes := []markertypes.Access{markertypes.AccessByName("withdraw")} grant := *markertypes.NewAccessGrant(node.Address, accessTypes) msg := markertypes.NewMsgAddAccessRequest(denom, node.Address, grant) - msg2, ops, err := markersim.Dispatch(r, app, ctx, ak, bk, node, chainID, msg, nil) + msg2, ops, err := markersim.Dispatch(r, app, ctx, simState, ak, bk, node, chainID, msg, nil) - ops = append(ops, simtypes.FutureOperation{Op: SimulateFinalizeMarker(ak, bk, nk, node, feebucket, merchant, consumer, name), BlockHeight: int(ctx.BlockHeight()) + 1}) + ops = append(ops, simtypes.FutureOperation{Op: SimulateFinalizeMarker(simState, ak, bk, nk, node, feebucket, merchant, consumer, name), BlockHeight: int(ctx.BlockHeight()) + 1}) return msg2, ops, err } } -func SimulateFinalizeMarker(ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper, nk namekeeper.Keeper, node, feebucket, merchant, consumer simtypes.Account, name string) simtypes.Operation { +func SimulateFinalizeMarker(simState module.SimulationState, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper, nk namekeeper.Keeper, node, feebucket, merchant, consumer simtypes.Account, name string) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { msg := markertypes.NewMsgFinalizeRequest(denom, node.Address) - msg2, ops, err := markersim.Dispatch(r, app, ctx, ak, bk, node, chainID, msg, nil) + msg2, ops, err := markersim.Dispatch(r, app, ctx, simState, ak, bk, node, chainID, msg, nil) - ops = append(ops, simtypes.FutureOperation{Op: SimulateActivateMarker(ak, bk, nk, node, feebucket, merchant, consumer, name), BlockHeight: int(ctx.BlockHeight()) + 1}) + ops = append(ops, simtypes.FutureOperation{Op: SimulateActivateMarker(simState, ak, bk, nk, node, feebucket, merchant, consumer, name), BlockHeight: int(ctx.BlockHeight()) + 1}) return msg2, ops, err } } -func SimulateActivateMarker(ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper, nk namekeeper.Keeper, node, feebucket, merchant, consumer simtypes.Account, name string) simtypes.Operation { +func SimulateActivateMarker(simState module.SimulationState, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper, nk namekeeper.Keeper, node, feebucket, merchant, consumer simtypes.Account, name string) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { msg := markertypes.NewMsgActivateRequest(denom, node.Address) - msg2, ops, err := markersim.Dispatch(r, app, ctx, ak, bk, node, chainID, msg, nil) + msg2, ops, err := markersim.Dispatch(r, app, ctx, simState, ak, bk, node, chainID, msg, nil) - ops = append(ops, simtypes.FutureOperation{Op: SimulateMsgWithdrawRequest(ak, bk, nk, node, feebucket, merchant, consumer, name), BlockHeight: int(ctx.BlockHeight()) + 1}) + ops = append(ops, simtypes.FutureOperation{Op: SimulateMsgWithdrawRequest(simState, ak, bk, nk, node, feebucket, merchant, consumer, name), BlockHeight: int(ctx.BlockHeight()) + 1}) return msg2, ops, err } } -func SimulateMsgWithdrawRequest(ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper, nk namekeeper.Keeper, node, feebucket, merchant, consumer simtypes.Account, name string) simtypes.Operation { +func SimulateMsgWithdrawRequest(simState module.SimulationState, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper, nk namekeeper.Keeper, node, feebucket, merchant, consumer simtypes.Account, name string) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { @@ -249,15 +247,15 @@ func SimulateMsgWithdrawRequest(ak authkeeper.AccountKeeperI, bk bankkeeper.Keep Amount: sdkmath.NewIntFromUint64(1_000_000), }} msg := markertypes.NewMsgWithdrawRequest(node.Address, consumer.Address, denom, coins) - msg2, ops, err := markersim.Dispatch(r, app, ctx, ak, bk, node, chainID, msg, nil) + msg2, ops, err := markersim.Dispatch(r, app, ctx, simState, ak, bk, node, chainID, msg, nil) - ops = append(ops, simtypes.FutureOperation{Op: SimulateMsgStoreContract(ak, bk, nk, node, feebucket, merchant, consumer, name), BlockHeight: int(ctx.BlockHeight()) + 1}) + ops = append(ops, simtypes.FutureOperation{Op: SimulateMsgStoreContract(simState, ak, bk, nk, node, feebucket, merchant, consumer, name), BlockHeight: int(ctx.BlockHeight()) + 1}) return msg2, ops, err } } -func SimulateMsgStoreContract(ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper, nk namekeeper.Keeper, node, feebucket, merchant, consumer simtypes.Account, name string) simtypes.Operation { +func SimulateMsgStoreContract(simState module.SimulationState, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper, nk namekeeper.Keeper, node, feebucket, merchant, consumer simtypes.Account, name string) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { @@ -272,15 +270,15 @@ func SimulateMsgStoreContract(ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKe WASMByteCode: code, } - msg2, ops, _, storeErr := Dispatch(r, app, ctx, ak, bk, feebucket, chainID, msg, nil, nil) + msg2, ops, _, storeErr := Dispatch(r, app, ctx, simState, ak, bk, feebucket, chainID, msg, nil, nil) - ops = append(ops, simtypes.FutureOperation{Op: SimulateMsgInstantiateContract(ak, bk, nk, node, feebucket, merchant, consumer, name), BlockHeight: int(ctx.BlockHeight()) + 1}) + ops = append(ops, simtypes.FutureOperation{Op: SimulateMsgInstantiateContract(simState, ak, bk, nk, node, feebucket, merchant, consumer, name), BlockHeight: int(ctx.BlockHeight()) + 1}) return msg2, ops, storeErr } } -func SimulateMsgInstantiateContract(ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper, nk namekeeper.Keeper, node, feebucket, merchant, consumer simtypes.Account, name string) simtypes.Operation { +func SimulateMsgInstantiateContract(simState module.SimulationState, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper, nk namekeeper.Keeper, node, feebucket, merchant, consumer simtypes.Account, name string) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { @@ -310,7 +308,7 @@ func SimulateMsgInstantiateContract(ak authkeeper.AccountKeeperI, bk bankkeeper. Funds: amount, } - msg2, ops, sdkResponse, instantiateErr := Dispatch(r, app, ctx, ak, bk, feebucket, chainID, msg, amount, nil) + msg2, ops, sdkResponse, instantiateErr := Dispatch(r, app, ctx, simState, ak, bk, feebucket, chainID, msg, amount, nil) // get the contract address for use when executing the contract if len(sdkResponse.MsgResponses) == 0 { @@ -340,13 +338,13 @@ func SimulateMsgInstantiateContract(ak authkeeper.AccountKeeperI, bk bankkeeper. contractAddr := pInstResp.Address - ops = append(ops, simtypes.FutureOperation{Op: SimulateMsgExecuteContract(ak, bk, node, consumer, contractAddr), BlockHeight: int(ctx.BlockHeight()) + 1}) + ops = append(ops, simtypes.FutureOperation{Op: SimulateMsgExecuteContract(simState, ak, bk, node, consumer, contractAddr), BlockHeight: int(ctx.BlockHeight()) + 1}) return msg2, ops, instantiateErr } } -func SimulateMsgExecuteContract(ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper, _, consumer simtypes.Account, contractAddr string) simtypes.Operation { +func SimulateMsgExecuteContract(simState module.SimulationState, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper, _, consumer simtypes.Account, contractAddr string) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { @@ -364,7 +362,7 @@ func SimulateMsgExecuteContract(ak authkeeper.AccountKeeperI, bk bankkeeper.View Msg: []byte("{\"purchase\":{\"id\":\"12345\"}}"), } - msg2, ops, _, err2 := Dispatch(r, app, ctx, ak, bk, consumer, chainID, msg, amount, nil) + msg2, ops, _, err2 := Dispatch(r, app, ctx, simState, ak, bk, consumer, chainID, msg, amount, nil) return msg2, ops, err2 } } @@ -376,6 +374,7 @@ func Dispatch( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, + simState module.SimulationState, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper, from simtypes.Account, @@ -401,10 +400,9 @@ func Dispatch( panic("no fees") } - txGen := moduletestutil.MakeTestEncodingConfig().TxConfig tx, err := simtestutil.GenSignedMockTx( r, - txGen, + simState.TxConfig, []sdk.Msg{msg}, fees, simtestutil.DefaultGenTxGas*10, // storing a contract requires more gas than most txs @@ -417,7 +415,7 @@ func Dispatch( panic(err) } - _, sdkResponse, err2 := app.SimDeliver(txGen.TxEncoder(), tx) + _, sdkResponse, err2 := app.SimDeliver(simState.TxConfig.TxEncoder(), tx) if err2 != nil { panic(err2) } diff --git a/x/attribute/module.go b/x/attribute/module.go index d2a5534476..61b43bb8ef 100644 --- a/x/attribute/module.go +++ b/x/attribute/module.go @@ -12,6 +12,7 @@ import ( abci "github.com/cometbft/cometbft/abci/types" "cosmossdk.io/core/appmodule" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" cdctypes "github.com/cosmos/cosmos-sdk/codec/types" @@ -179,9 +180,7 @@ func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) { // WeightedOperations returns the all the attribute module operations with their respective weights. func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { - return simulation.WeightedOperations( - simState.AppParams, simState.Cdc, am.keeper, am.ak, am.bk, am.nk, - ) + return simulation.WeightedOperations(simState, am.keeper, am.ak, am.bk, am.nk) } // ConsensusVersion implements AppModule/ConsensusVersion. diff --git a/x/attribute/simulation/operations.go b/x/attribute/simulation/operations.go index ce5706c54d..5e37a913be 100644 --- a/x/attribute/simulation/operations.go +++ b/x/attribute/simulation/operations.go @@ -6,11 +6,10 @@ import ( "github.com/google/uuid" - simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" - "github.com/cosmos/cosmos-sdk/baseapp" - "github.com/cosmos/cosmos-sdk/codec" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" @@ -39,81 +38,47 @@ const ( // WeightedOperations returns all the operations from the module with their respective weights func WeightedOperations( - appParams simtypes.AppParams, cdc codec.JSONCodec, k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper, nk namekeeper.Keeper, + simState module.SimulationState, k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper, nk namekeeper.Keeper, ) simulation.WeightedOperations { var ( - weightMsgAddAttribute int - weightMsgUpdateAttribute int - weightMsgDeleteAttribute int - weightMsgDeleteDistinctAttribute int - weightMsgSetAccountDataRequest int - ) - - appParams.GetOrGenerate(OpWeightMsgAddAttribute, &weightMsgAddAttribute, nil, - func(_ *rand.Rand) { - weightMsgAddAttribute = simappparams.DefaultWeightMsgAddAttribute - }, - ) - - appParams.GetOrGenerate(OpWeightMsgUpdateAttribute, &weightMsgUpdateAttribute, nil, - func(_ *rand.Rand) { - weightMsgUpdateAttribute = simappparams.DefaultWeightMsgUpdateAttribute - }, - ) - - appParams.GetOrGenerate(OpWeightMsgDeleteAttribute, &weightMsgDeleteAttribute, nil, - func(_ *rand.Rand) { - weightMsgDeleteAttribute = simappparams.DefaultWeightMsgDeleteAttribute - }, + wMsgAddAttribute int + wMsgUpdateAttribute int + wMsgDeleteAttribute int + wMsgDeleteDistinctAttribute int + wMsgSetAccountDataRequest int ) - appParams.GetOrGenerate(OpWeightMsgDeleteDistinctAttribute, &weightMsgDeleteDistinctAttribute, nil, - func(_ *rand.Rand) { - weightMsgDeleteDistinctAttribute = simappparams.DefaultWeightMsgDeleteDistinctAttribute - }, - ) - - appParams.GetOrGenerate(OpWeightMsgSetAccountData, &weightMsgSetAccountDataRequest, nil, - func(_ *rand.Rand) { - weightMsgSetAccountDataRequest = simappparams.DefaultWeightMsgSetAccountData - }, - ) + simState.AppParams.GetOrGenerate(OpWeightMsgAddAttribute, &wMsgAddAttribute, nil, + func(_ *rand.Rand) { wMsgAddAttribute = simappparams.DefaultWeightMsgAddAttribute }) + simState.AppParams.GetOrGenerate(OpWeightMsgUpdateAttribute, &wMsgUpdateAttribute, nil, + func(_ *rand.Rand) { wMsgUpdateAttribute = simappparams.DefaultWeightMsgUpdateAttribute }) + simState.AppParams.GetOrGenerate(OpWeightMsgDeleteAttribute, &wMsgDeleteAttribute, nil, + func(_ *rand.Rand) { wMsgDeleteAttribute = simappparams.DefaultWeightMsgDeleteAttribute }) + simState.AppParams.GetOrGenerate(OpWeightMsgDeleteDistinctAttribute, &wMsgDeleteDistinctAttribute, nil, + func(_ *rand.Rand) { wMsgDeleteDistinctAttribute = simappparams.DefaultWeightMsgDeleteDistinctAttribute }) + simState.AppParams.GetOrGenerate(OpWeightMsgSetAccountData, &wMsgSetAccountDataRequest, nil, + func(_ *rand.Rand) { wMsgSetAccountDataRequest = simappparams.DefaultWeightMsgSetAccountData }) return simulation.WeightedOperations{ - simulation.NewWeightedOperation( - weightMsgAddAttribute, - SimulateMsgAddAttribute(k, ak, bk, nk), - ), - simulation.NewWeightedOperation( - weightMsgUpdateAttribute, - SimulateMsgUpdateAttribute(k, ak, bk, nk), - ), - simulation.NewWeightedOperation( - weightMsgDeleteAttribute, - SimulateMsgDeleteAttribute(k, ak, bk, nk), - ), - simulation.NewWeightedOperation( - weightMsgDeleteDistinctAttribute, - SimulateMsgDeleteDistinctAttribute(k, ak, bk, nk), - ), - simulation.NewWeightedOperation( - weightMsgSetAccountDataRequest, - SimulateMsgSetAccountData(k, ak, bk), - ), + simulation.NewWeightedOperation(wMsgAddAttribute, SimulateMsgAddAttribute(simState, k, ak, bk, nk)), + simulation.NewWeightedOperation(wMsgUpdateAttribute, SimulateMsgUpdateAttribute(simState, k, ak, bk, nk)), + simulation.NewWeightedOperation(wMsgDeleteAttribute, SimulateMsgDeleteAttribute(simState, k, ak, bk, nk)), + simulation.NewWeightedOperation(wMsgDeleteDistinctAttribute, SimulateMsgDeleteDistinctAttribute(simState, k, ak, bk, nk)), + simulation.NewWeightedOperation(wMsgSetAccountDataRequest, SimulateMsgSetAccountData(simState, k, ak, bk)), } } // SimulateMsgAddAttribute will add an attribute under an account with a random type. -func SimulateMsgAddAttribute(_ keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper, nk namekeeper.Keeper) simtypes.Operation { +func SimulateMsgAddAttribute(simState module.SimulationState, _ keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper, nk namekeeper.Keeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { randomRecord, simAccount, found, err := getRandomNameRecord(r, ctx, &nk, accs) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(&types.MsgAddAttributeRequest{}), sdk.MsgTypeURL(&types.MsgAddAttributeRequest{}), "iterator of existing name records failed"), nil, err + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgAddAttributeRequest{}), "iterator of existing name records failed"), nil, err } if !found { - return simtypes.NoOpMsg(sdk.MsgTypeURL(&types.MsgAddAttributeRequest{}), sdk.MsgTypeURL(&types.MsgAddAttributeRequest{}), "no name records available to create under"), nil, nil + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgAddAttributeRequest{}), "no name records available to create under"), nil, nil } t := types.AttributeType(r.Intn(9)) @@ -125,21 +90,21 @@ func SimulateMsgAddAttribute(_ keeper.Keeper, ak authkeeper.AccountKeeperI, bk b getRandomValueOfType(r, t), ) - return Dispatch(r, app, ctx, ak, bk, simAccount, chainID, msg) + return Dispatch(r, app, ctx, simState, ak, bk, simAccount, chainID, msg) } } // SimulateMsgUpdateAttribute will add an attribute under an account with a random type. -func SimulateMsgUpdateAttribute(k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper, nk namekeeper.Keeper) simtypes.Operation { +func SimulateMsgUpdateAttribute(simState module.SimulationState, k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper, nk namekeeper.Keeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { randomAttribute, simAccount, found, err := getRandomAttribute(r, ctx, k, &nk, accs) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(&types.MsgUpdateAttributeRequest{}), sdk.MsgTypeURL(&types.MsgUpdateAttributeRequest{}), "iterator of existing attributes failed"), nil, err + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgUpdateAttributeRequest{}), "iterator of existing attributes failed"), nil, err } if !found { - return simtypes.NoOpMsg(sdk.MsgTypeURL(&types.MsgUpdateAttributeRequest{}), sdk.MsgTypeURL(&types.MsgUpdateAttributeRequest{}), "no attributes available to delete"), nil, nil + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgUpdateAttributeRequest{}), "no attributes available to delete"), nil, nil } t := types.AttributeType(r.Intn(9)) @@ -153,50 +118,50 @@ func SimulateMsgUpdateAttribute(k keeper.Keeper, ak authkeeper.AccountKeeperI, b t, ) - return Dispatch(r, app, ctx, ak, bk, simAccount, chainID, msg) + return Dispatch(r, app, ctx, simState, ak, bk, simAccount, chainID, msg) } } // SimulateMsgDeleteAttribute will dispatch a delete attribute operation against a random record -func SimulateMsgDeleteAttribute(k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper, nk namekeeper.Keeper) simtypes.Operation { +func SimulateMsgDeleteAttribute(simState module.SimulationState, k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper, nk namekeeper.Keeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { randomAttribute, simAccount, found, err := getRandomAttribute(r, ctx, k, &nk, accs) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(&types.MsgDeleteAttributeRequest{}), sdk.MsgTypeURL(&types.MsgDeleteAttributeRequest{}), "iterator of existing attributes failed"), nil, err + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgDeleteAttributeRequest{}), "iterator of existing attributes failed"), nil, err } if !found { - return simtypes.NoOpMsg(sdk.MsgTypeURL(&types.MsgDeleteAttributeRequest{}), sdk.MsgTypeURL(&types.MsgDeleteAttributeRequest{}), "no attributes available to delete"), nil, nil + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgDeleteAttributeRequest{}), "no attributes available to delete"), nil, nil } msg := types.NewMsgDeleteAttributeRequest(randomAttribute.Address, simAccount.Address, randomAttribute.Name) - return Dispatch(r, app, ctx, ak, bk, simAccount, chainID, msg) + return Dispatch(r, app, ctx, simState, ak, bk, simAccount, chainID, msg) } } // SimulateMsgDeleteDistinctAttribute will dispatch a delete attribute operation against a random record -func SimulateMsgDeleteDistinctAttribute(k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper, nk namekeeper.Keeper) simtypes.Operation { +func SimulateMsgDeleteDistinctAttribute(simState module.SimulationState, k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper, nk namekeeper.Keeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { randomAttribute, simAccount, found, err := getRandomAttribute(r, ctx, k, &nk, accs) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(&types.MsgDeleteDistinctAttributeRequest{}), sdk.MsgTypeURL(&types.MsgDeleteDistinctAttributeRequest{}), "iterator of existing attributes failed"), nil, err + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgDeleteDistinctAttributeRequest{}), "iterator of existing attributes failed"), nil, err } if !found { - return simtypes.NoOpMsg(sdk.MsgTypeURL(&types.MsgDeleteDistinctAttributeRequest{}), sdk.MsgTypeURL(&types.MsgDeleteDistinctAttributeRequest{}), "no attributes available to delete distinct"), nil, nil + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgDeleteDistinctAttributeRequest{}), "no attributes available to delete distinct"), nil, nil } msg := types.NewMsgDeleteDistinctAttributeRequest(randomAttribute.Address, simAccount.Address, randomAttribute.Name, randomAttribute.Value) - return Dispatch(r, app, ctx, ak, bk, simAccount, chainID, msg) + return Dispatch(r, app, ctx, simState, ak, bk, simAccount, chainID, msg) } } // SimulateMsgSetAccountData will dispatch a set account data operation for a random account. -func SimulateMsgSetAccountData(k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper) simtypes.Operation { +func SimulateMsgSetAccountData(simState module.SimulationState, k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { @@ -219,7 +184,7 @@ func SimulateMsgSetAccountData(k keeper.Keeper, ak authkeeper.AccountKeeperI, bk Account: acc.Address.String(), } - return Dispatch(r, app, ctx, ak, bk, acc, chainID, msg) + return Dispatch(r, app, ctx, simState, ak, bk, acc, chainID, msg) } } @@ -252,6 +217,7 @@ func Dispatch( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, + simState module.SimulationState, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper, from simtypes.Account, @@ -270,10 +236,9 @@ func Dispatch( return simtypes.NoOpMsg(types.ModuleName, fmt.Sprintf("%T", msg), "unable to generate fees"), nil, err } - txGen := simappparams.MakeTestEncodingConfig().TxConfig tx, err := simtestutil.GenSignedMockTx( r, - txGen, + simState.TxConfig, []sdk.Msg{msg}, fees, simtestutil.DefaultGenTxGas, @@ -283,12 +248,12 @@ func Dispatch( from.PrivKey, ) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), "unable to generate mock tx"), nil, err + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "unable to generate mock tx"), nil, err } - _, _, err = app.SimDeliver(txGen.TxEncoder(), tx) + _, _, err = app.SimDeliver(simState.TxConfig.TxEncoder(), tx) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), err.Error()), nil, nil + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), err.Error()), nil, nil } return simtypes.NewOperationMsg(msg, true, ""), nil, nil diff --git a/x/attribute/simulation/operations_test.go b/x/attribute/simulation/operations_test.go index cde1180dea..f807c6948f 100644 --- a/x/attribute/simulation/operations_test.go +++ b/x/attribute/simulation/operations_test.go @@ -10,6 +10,7 @@ import ( "github.com/stretchr/testify/suite" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" "github.com/cosmos/cosmos-sdk/x/bank/testutil" @@ -26,6 +27,10 @@ type SimTestSuite struct { app *app.App } +func TestSimTestSuite(t *testing.T) { + suite.Run(t, new(SimTestSuite)) +} + func (s *SimTestSuite) SetupTest() { s.app = app.Setup(s.T()) s.ctx = s.app.BaseApp.NewContext(false) @@ -58,11 +63,17 @@ func (s *SimTestSuite) LogIfError(err error, format string, args ...interface{}) } } -func (s *SimTestSuite) TestWeightedOperations() { - cdc := s.app.AppCodec() - appParams := make(simtypes.AppParams) +// MakeTestSimState creates a new module.SimulationState struct with the fields needed by the functions being tested. +func (s *SimTestSuite) MakeTestSimState() module.SimulationState { + return module.SimulationState{ + AppParams: make(simtypes.AppParams), + Cdc: s.app.AppCodec(), + TxConfig: s.app.GetTxConfig(), + } +} - weightedOps := simulation.WeightedOperations(appParams, cdc, s.app.AttributeKeeper, +func (s *SimTestSuite) TestWeightedOperations() { + weightedOps := simulation.WeightedOperations(s.MakeTestSimState(), s.app.AttributeKeeper, s.app.AccountKeeper, s.app.BankKeeper, s.app.NameKeeper, ) @@ -120,7 +131,7 @@ func (s *SimTestSuite) TestSimulateMsgAddAttribute() { s.LogIfError(s.app.NameKeeper.SetNameRecord(s.ctx, name, accounts[0].Address, false), "SetNameRecord(%q) error", name) // execute operation - op := simulation.SimulateMsgAddAttribute(s.app.AttributeKeeper, s.app.AccountKeeper, s.app.BankKeeper, s.app.NameKeeper) + op := simulation.SimulateMsgAddAttribute(s.MakeTestSimState(), s.app.AttributeKeeper, s.app.AccountKeeper, s.app.BankKeeper, s.app.NameKeeper) operationMsg, futureOperations, err := op(r, s.app.BaseApp, s.ctx, accounts, "") s.Require().NoError(err, "SimulateMsgAddAttribute op(...) error") s.LogOperationMsg(operationMsg) @@ -152,7 +163,7 @@ func (s *SimTestSuite) TestSimulateMsgUpdateAttribute() { s.LogIfError(s.app.AttributeKeeper.SetAttribute(s.ctx, attr, accounts[0].Address), "SetAttribute(%q) error", name) // execute operation - op := simulation.SimulateMsgUpdateAttribute(s.app.AttributeKeeper, s.app.AccountKeeper, s.app.BankKeeper, s.app.NameKeeper) + op := simulation.SimulateMsgUpdateAttribute(s.MakeTestSimState(), s.app.AttributeKeeper, s.app.AccountKeeper, s.app.BankKeeper, s.app.NameKeeper) operationMsg, futureOperations, err := op(r, s.app.BaseApp, s.ctx, accounts, "") s.Require().NoError(err, "SimulateMsgUpdateAttribute op(...) error") s.LogOperationMsg(operationMsg) @@ -182,7 +193,7 @@ func (s *SimTestSuite) TestSimulateMsgDeleteAttribute() { s.LogIfError(s.app.AttributeKeeper.SetAttribute(s.ctx, attr, accounts[0].Address), "SetAttribute(%q) error", name) // execute operation - op := simulation.SimulateMsgDeleteAttribute(s.app.AttributeKeeper, s.app.AccountKeeper, s.app.BankKeeper, s.app.NameKeeper) + op := simulation.SimulateMsgDeleteAttribute(s.MakeTestSimState(), s.app.AttributeKeeper, s.app.AccountKeeper, s.app.BankKeeper, s.app.NameKeeper) operationMsg, futureOperations, err := op(r, s.app.BaseApp, s.ctx, accounts, "") s.Require().NoError(err, "SimulateMsgDeleteAttribute op(...) error") s.LogOperationMsg(operationMsg) @@ -212,7 +223,7 @@ func (s *SimTestSuite) TestSimulateMsgDeleteDistinctAttribute() { s.LogIfError(s.app.AttributeKeeper.SetAttribute(s.ctx, attr, accounts[0].Address), "SetAttribute(%q) error", name) // execute operation - op := simulation.SimulateMsgDeleteDistinctAttribute(s.app.AttributeKeeper, s.app.AccountKeeper, s.app.BankKeeper, s.app.NameKeeper) + op := simulation.SimulateMsgDeleteDistinctAttribute(s.MakeTestSimState(), s.app.AttributeKeeper, s.app.AccountKeeper, s.app.BankKeeper, s.app.NameKeeper) operationMsg, futureOperations, err := op(r, s.app.BaseApp, s.ctx, accounts, "") s.Require().NoError(err, "SimulateMsgDeleteDistinctAttribute op(...) error") s.LogOperationMsg(operationMsg) @@ -236,7 +247,7 @@ func (s *SimTestSuite) TestSimulateMsgSetAccountData() { accounts := s.getTestingAccounts(r, 3) // execute operation - op := simulation.SimulateMsgSetAccountData(s.app.AttributeKeeper, s.app.AccountKeeper, s.app.BankKeeper) + op := simulation.SimulateMsgSetAccountData(s.MakeTestSimState(), s.app.AttributeKeeper, s.app.AccountKeeper, s.app.BankKeeper) operationMsg, futureOperations, err := op(r, s.app.BaseApp, s.ctx, accounts, "") s.Require().NoError(err, "SimulateMsgDeleteDistinctAttribute op(...) error") s.LogOperationMsg(operationMsg) @@ -280,7 +291,3 @@ func GenerateRandomTime(minHours int) time.Time { return randomTime } - -func TestSimTestSuite(t *testing.T) { - suite.Run(t, new(SimTestSuite)) -} diff --git a/x/ibcratelimit/module/module.go b/x/ibcratelimit/module/module.go index 01e51d9f42..6e05a17163 100644 --- a/x/ibcratelimit/module/module.go +++ b/x/ibcratelimit/module/module.go @@ -6,13 +6,14 @@ import ( "fmt" "math/rand" - "cosmossdk.io/core/appmodule" "github.com/gorilla/mux" "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" abci "github.com/cometbft/cometbft/abci/types" + "cosmossdk.io/core/appmodule" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" @@ -135,9 +136,7 @@ func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) { // WeightedOperations returns simulation operations (i.e msgs) with their respective weight func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { - return simulation.WeightedOperations( - simState.AppParams, simState.Cdc, am.keeper, am.accountKeeper, am.bankKeeper, - ) + return simulation.WeightedOperations(simState, am.keeper, am.accountKeeper, am.bankKeeper) } // Name returns the ibcratelimit module's name. diff --git a/x/ibcratelimit/simulation/operations.go b/x/ibcratelimit/simulation/operations.go index 7f2f3b7afc..20f1400584 100644 --- a/x/ibcratelimit/simulation/operations.go +++ b/x/ibcratelimit/simulation/operations.go @@ -4,13 +4,12 @@ import ( "fmt" "math/rand" - simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" - sdkmath "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/baseapp" - "github.com/cosmos/cosmos-sdk/codec" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" @@ -31,34 +30,28 @@ const ( // WeightedOperations returns all the operations from the module with their respective weights func WeightedOperations( - appParams simtypes.AppParams, cdc codec.JSONCodec, k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper, + simState module.SimulationState, k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper, ) simulation.WeightedOperations { var ( - weightMsgUpdateParams int + wMsgUpdateParams int ) - appParams.GetOrGenerate(OpWeightMsgUpdateParams, &weightMsgUpdateParams, nil, - func(_ *rand.Rand) { - weightMsgUpdateParams = simappparams.DefaultWeightGovUpdateParams - }, - ) + simState.AppParams.GetOrGenerate(OpWeightMsgUpdateParams, &wMsgUpdateParams, nil, + func(_ *rand.Rand) { wMsgUpdateParams = simappparams.DefaultWeightGovUpdateParams }) return simulation.WeightedOperations{ - simulation.NewWeightedOperation( - weightMsgUpdateParams, - SimulateMsgGovUpdateParams(k, ak, bk), - ), + simulation.NewWeightedOperation(wMsgUpdateParams, SimulateMsgGovUpdateParams(simState, k, ak, bk)), } } // SimulateMsgGovUpdateParams sends a MsgUpdateParams. -func SimulateMsgGovUpdateParams(_ keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper) simtypes.Operation { +func SimulateMsgGovUpdateParams(simState module.SimulationState, _ keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { raccs, err := RandomAccs(r, accs, uint64(len(accs))) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(&ibcratelimit.MsgGovUpdateParamsRequest{}), sdk.MsgTypeURL(&ibcratelimit.MsgGovUpdateParamsRequest{}), err.Error()), nil, nil + return simtypes.NoOpMsg(ibcratelimit.ModuleName, sdk.MsgTypeURL(&ibcratelimit.MsgGovUpdateParamsRequest{}), err.Error()), nil, nil } // 50% chance to be from the module's authority @@ -67,7 +60,8 @@ func SimulateMsgGovUpdateParams(_ keeper.Keeper, ak authkeeper.AccountKeeperI, b msg := ibcratelimit.NewMsgGovUpdateParamsRequest(from.Address.String(), to.Address.String()) - return Dispatch(r, app, ctx, from, chainID, msg, ak, bk, nil) + // TODO[1760]: Refactor this to submit it as a gov prop and return futures for votes. + return Dispatch(r, app, ctx, simState, from, chainID, msg, ak, bk, nil) } } @@ -77,6 +71,7 @@ func Dispatch( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, + simState module.SimulationState, from simtypes.Account, chainID string, msg sdk.Msg, @@ -93,19 +88,19 @@ func Dispatch( fees, err := simtypes.RandomFees(r, ctx, spendable) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), "unable to generate fees"), nil, err + return simtypes.NoOpMsg(ibcratelimit.ModuleName, sdk.MsgTypeURL(msg), "unable to generate fees"), nil, err } err = testutil.FundAccount(ctx, bk, account.GetAddress(), sdk.NewCoins(sdk.Coin{ Denom: pioconfig.GetProvenanceConfig().BondDenom, Amount: sdkmath.NewInt(1_000_000_000_000_000), })) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), "unable to fund account"), nil, err + return simtypes.NoOpMsg(ibcratelimit.ModuleName, sdk.MsgTypeURL(msg), "unable to fund account"), nil, err } - txGen := simappparams.MakeTestEncodingConfig().TxConfig + tx, err := simtestutil.GenSignedMockTx( r, - txGen, + simState.TxConfig, []sdk.Msg{msg}, fees, simtestutil.DefaultGenTxGas, @@ -115,12 +110,12 @@ func Dispatch( from.PrivKey, ) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), "unable to generate mock tx"), nil, err + return simtypes.NoOpMsg(ibcratelimit.ModuleName, sdk.MsgTypeURL(msg), "unable to generate mock tx"), nil, err } - _, _, err = app.SimDeliver(txGen.TxEncoder(), tx) + _, _, err = app.SimDeliver(simState.TxConfig.TxEncoder(), tx) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), err.Error()), nil, nil + return simtypes.NoOpMsg(ibcratelimit.ModuleName, sdk.MsgTypeURL(msg), err.Error()), nil, nil } return simtypes.NewOperationMsg(msg, true, ""), futures, nil diff --git a/x/ibcratelimit/simulation/operations_test.go b/x/ibcratelimit/simulation/operations_test.go index 314754ba95..ccd77955b1 100644 --- a/x/ibcratelimit/simulation/operations_test.go +++ b/x/ibcratelimit/simulation/operations_test.go @@ -5,10 +5,12 @@ import ( "fmt" "math/rand" "strings" + "testing" "github.com/stretchr/testify/suite" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" "github.com/cosmos/cosmos-sdk/x/bank/testutil" @@ -16,7 +18,6 @@ import ( simappparams "github.com/provenance-io/provenance/app/params" "github.com/provenance-io/provenance/x/ibcratelimit" "github.com/provenance-io/provenance/x/ibcratelimit/simulation" - "github.com/provenance-io/provenance/x/trigger/types" ) type SimTestSuite struct { @@ -26,6 +27,10 @@ type SimTestSuite struct { app *app.App } +func TestSimTestSuite(t *testing.T) { + suite.Run(t, new(SimTestSuite)) +} + func (s *SimTestSuite) SetupTest() { s.app = app.Setup(s.T()) s.ctx = s.app.BaseApp.NewContext(false) @@ -50,11 +55,17 @@ func (s *SimTestSuite) LogOperationMsg(operationMsg simtypes.OperationMsg, msg s ) } -func (s *SimTestSuite) TestWeightedOperations() { - cdc := s.app.AppCodec() - appParams := make(simtypes.AppParams) +// MakeTestSimState creates a new module.SimulationState struct with the fields needed by the functions being tested. +func (s *SimTestSuite) MakeTestSimState() module.SimulationState { + return module.SimulationState{ + AppParams: make(simtypes.AppParams), + Cdc: s.app.AppCodec(), + TxConfig: s.app.GetTxConfig(), + } +} - weightedOps := simulation.WeightedOperations(appParams, cdc, *s.app.RateLimitingKeeper, +func (s *SimTestSuite) TestWeightedOperations() { + weightedOps := simulation.WeightedOperations(s.MakeTestSimState(), *s.app.RateLimitingKeeper, s.app.AccountKeeper, s.app.BankKeeper, ) @@ -68,7 +79,7 @@ func (s *SimTestSuite) TestWeightedOperations() { opMsgRoute string opMsgName string }{ - {simappparams.DefaultWeightGovUpdateParams, sdk.MsgTypeURL(&ibcratelimit.MsgGovUpdateParamsRequest{}), sdk.MsgTypeURL(&ibcratelimit.MsgGovUpdateParamsRequest{})}, + {simappparams.DefaultWeightGovUpdateParams, ibcratelimit.ModuleName, sdk.MsgTypeURL(&ibcratelimit.MsgGovUpdateParamsRequest{})}, } expNames := make([]string, len(expected)) @@ -103,7 +114,7 @@ func (s *SimTestSuite) TestSimulateMsgGovUpdateParams() { accounts := s.getTestingAccounts(r, 3) // execute operation - op := simulation.SimulateMsgGovUpdateParams(*s.app.RateLimitingKeeper, s.app.AccountKeeper, s.app.BankKeeper) + op := simulation.SimulateMsgGovUpdateParams(s.MakeTestSimState(), *s.app.RateLimitingKeeper, s.app.AccountKeeper, s.app.BankKeeper) operationMsg, futureOperations, err := op(r, s.app.BaseApp, s.ctx, accounts, "") s.Require().NoError(err, "SimulateMsgGovUpdateParams op(...) error") s.LogOperationMsg(operationMsg, "good") @@ -113,7 +124,7 @@ func (s *SimTestSuite) TestSimulateMsgGovUpdateParams() { s.Assert().True(operationMsg.OK, "operationMsg.OK") s.Assert().Equal(sdk.MsgTypeURL(&msg), operationMsg.Name, "operationMsg.Name") - s.Assert().Equal(types.RouterKey, operationMsg.Route, "operationMsg.Route") + s.Assert().Equal(ibcratelimit.ModuleName, operationMsg.Route, "operationMsg.Route") s.Assert().Len(futureOperations, 0, "futureOperations") } diff --git a/x/marker/module.go b/x/marker/module.go index e36cd4be88..33978d7c43 100644 --- a/x/marker/module.go +++ b/x/marker/module.go @@ -194,7 +194,7 @@ func (am AppModule) RegisterStoreDecoder(_ simtypes.StoreDecoderRegistry) { // WeightedOperations returns the all the marker module operations with their respective weights. func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { return simulation.WeightedOperations( - simState.AppParams, simState.Cdc, codec.NewProtoCodec(am.registry), + simState, codec.NewProtoCodec(am.registry), am.keeper, am.accountKeeper, am.bankKeeper, am.govKeeper, am.attrKeeper, ) } diff --git a/x/marker/simulation/operations.go b/x/marker/simulation/operations.go index 4a2f66af39..c507087e35 100644 --- a/x/marker/simulation/operations.go +++ b/x/marker/simulation/operations.go @@ -14,6 +14,7 @@ import ( codectypes "github.com/cosmos/cosmos-sdk/codec/types" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" @@ -47,12 +48,11 @@ const ( // WeightedOperations returns all the operations from the module with their respective weights func WeightedOperations( - appParams simtypes.AppParams, cdc codec.JSONCodec, protoCodec *codec.ProtoCodec, + simState module.SimulationState, protoCodec *codec.ProtoCodec, k keeper.Keeper, ak authkeeper.AccountKeeper, bk bankkeeper.Keeper, gk govkeeper.Keeper, attrk types.AttrKeeper, ) simulation.WeightedOperations { args := &WeightedOpsArgs{ - AppParams: appParams, - JSONCodec: cdc, + SimState: simState, ProtoCodec: protoCodec, AK: ak, BK: bk, @@ -61,91 +61,43 @@ func WeightedOperations( } var ( - weightMsgAddMarker int - weightMsgChangeStatus int - weightMsgAddAccess int - weightMsgAddFinalizeActivateMarker int - weightMsgAddMarkerProposal int - weightMsgSetAccountData int - weightMsgUpdateSendDenyList int + wMsgAddMarker int + wMsgChangeStatus int + wMsgAddAccess int + wMsgAFAM int + wMsgAddMarkerProposal int + wMsgSetAccountData int + wMsgUpdateSendDenyList int ) - appParams.GetOrGenerate(OpWeightMsgAddMarker, &weightMsgAddMarker, nil, - func(_ *rand.Rand) { - weightMsgAddMarker = simappparams.DefaultWeightMsgAddMarker - }, - ) - - appParams.GetOrGenerate(OpWeightMsgChangeStatus, &weightMsgChangeStatus, nil, - func(_ *rand.Rand) { - weightMsgChangeStatus = simappparams.DefaultWeightMsgChangeStatus - }, - ) - - appParams.GetOrGenerate(OpWeightMsgAddAccess, &weightMsgAddAccess, nil, - func(_ *rand.Rand) { - weightMsgAddAccess = simappparams.DefaultWeightMsgAddAccess - }, - ) - - appParams.GetOrGenerate(OpWeightMsgAddActivateFinalizeMarker, &weightMsgAddFinalizeActivateMarker, nil, - func(_ *rand.Rand) { - weightMsgAddFinalizeActivateMarker = simappparams.DefaultWeightMsgAddFinalizeActivateMarker - }, - ) - - appParams.GetOrGenerate(OpWeightMsgAddMarkerProposal, &weightMsgAddMarkerProposal, nil, - func(_ *rand.Rand) { - weightMsgAddMarkerProposal = simappparams.DefaultWeightMsgAddMarkerProposal - }, - ) - - appParams.GetOrGenerate(OpWeightMsgSetAccountData, &weightMsgSetAccountData, nil, - func(_ *rand.Rand) { - weightMsgSetAccountData = simappparams.DefaultWeightMsgSetAccountData - }, - ) - - appParams.GetOrGenerate(OpWeightMsgUpdateSendDenyList, &weightMsgUpdateSendDenyList, nil, - func(_ *rand.Rand) { - weightMsgUpdateSendDenyList = simappparams.DefaultWeightMsgUpdateDenySendList - }, - ) + simState.AppParams.GetOrGenerate(OpWeightMsgAddMarker, &wMsgAddMarker, nil, + func(_ *rand.Rand) { wMsgAddMarker = simappparams.DefaultWeightMsgAddMarker }) + simState.AppParams.GetOrGenerate(OpWeightMsgChangeStatus, &wMsgChangeStatus, nil, + func(_ *rand.Rand) { wMsgChangeStatus = simappparams.DefaultWeightMsgChangeStatus }) + simState.AppParams.GetOrGenerate(OpWeightMsgAddAccess, &wMsgAddAccess, nil, + func(_ *rand.Rand) { wMsgAddAccess = simappparams.DefaultWeightMsgAddAccess }) + simState.AppParams.GetOrGenerate(OpWeightMsgAddActivateFinalizeMarker, &wMsgAFAM, nil, + func(_ *rand.Rand) { wMsgAFAM = simappparams.DefaultWeightMsgAddFinalizeActivateMarker }) + simState.AppParams.GetOrGenerate(OpWeightMsgAddMarkerProposal, &wMsgAddMarkerProposal, nil, + func(_ *rand.Rand) { wMsgAddMarkerProposal = simappparams.DefaultWeightMsgAddMarkerProposal }) + simState.AppParams.GetOrGenerate(OpWeightMsgSetAccountData, &wMsgSetAccountData, nil, + func(_ *rand.Rand) { wMsgSetAccountData = simappparams.DefaultWeightMsgSetAccountData }) + simState.AppParams.GetOrGenerate(OpWeightMsgUpdateSendDenyList, &wMsgUpdateSendDenyList, nil, + func(_ *rand.Rand) { wMsgUpdateSendDenyList = simappparams.DefaultWeightMsgUpdateDenySendList }) return simulation.WeightedOperations{ - simulation.NewWeightedOperation( - weightMsgAddMarker, - SimulateMsgAddMarker(k, ak, bk), - ), - simulation.NewWeightedOperation( - weightMsgChangeStatus, - SimulateMsgChangeStatus(k, ak, bk), - ), - simulation.NewWeightedOperation( - weightMsgAddAccess, - SimulateMsgAddAccess(k, ak, bk), - ), - simulation.NewWeightedOperation( - weightMsgAddFinalizeActivateMarker, - SimulateMsgAddFinalizeActivateMarker(k, ak, bk), - ), - simulation.NewWeightedOperation( - weightMsgAddMarkerProposal, - SimulateMsgAddMarkerProposal(k, args), - ), - simulation.NewWeightedOperation( - weightMsgSetAccountData, - SimulateMsgSetAccountData(k, args), - ), - simulation.NewWeightedOperation( - weightMsgUpdateSendDenyList, - SimulateMsgUpdateSendDenyList(k, args), - ), + simulation.NewWeightedOperation(wMsgAddMarker, SimulateMsgAddMarker(k, args)), + simulation.NewWeightedOperation(wMsgChangeStatus, SimulateMsgChangeStatus(k, args)), + simulation.NewWeightedOperation(wMsgAddAccess, SimulateMsgAddAccess(k, args)), + simulation.NewWeightedOperation(wMsgAFAM, SimulateMsgAddFinalizeActivateMarker(k, args)), + simulation.NewWeightedOperation(wMsgAddMarkerProposal, SimulateMsgAddMarkerProposal(k, args)), + simulation.NewWeightedOperation(wMsgSetAccountData, SimulateMsgSetAccountData(k, args)), + simulation.NewWeightedOperation(wMsgUpdateSendDenyList, SimulateMsgUpdateSendDenyList(k, args)), } } // SimulateMsgAddMarker will Add a random marker with random configuration. -func SimulateMsgAddMarker(k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper) simtypes.Operation { +func SimulateMsgAddMarker(k keeper.Keeper, args *WeightedOpsArgs) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { @@ -166,12 +118,12 @@ func SimulateMsgAddMarker(k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bank 0, ) - return Dispatch(r, app, ctx, ak, bk, simAccount, chainID, msg, nil) + return Dispatch(r, app, ctx, args.SimState, args.AK, args.BK, simAccount, chainID, msg, nil) } } // SimulateMsgChangeStatus will randomly change the status of the marker depending on it's current state. -func SimulateMsgChangeStatus(k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper) simtypes.Operation { +func SimulateMsgChangeStatus(k keeper.Keeper, args *WeightedOpsArgs) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { @@ -201,46 +153,46 @@ func SimulateMsgChangeStatus(k keeper.Keeper, ak authkeeper.AccountKeeperI, bk b case types.StatusActive: simAccount, found = randomAccWithAccess(r, m, accs, types.Access_Delete) if !found { - return simtypes.NoOpMsg(sdk.MsgTypeURL(&types.MsgCancelRequest{}), sdk.MsgTypeURL(&types.MsgCancelRequest{}), "no account has cancel access"), nil, nil + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgCancelRequest{}), "no account has cancel access"), nil, nil } msg = types.NewMsgCancelRequest(m.GetDenom(), simAccount.Address) case types.StatusCancelled: simAccount, found = randomAccWithAccess(r, m, accs, types.Access_Delete) if !found { - return simtypes.NoOpMsg(sdk.MsgTypeURL(&types.MsgDeleteRequest{}), sdk.MsgTypeURL(&types.MsgDeleteRequest{}), "no account has delete access"), nil, nil + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgDeleteRequest{}), "no account has delete access"), nil, nil } msg = types.NewMsgDeleteRequest(m.GetDenom(), simAccount.Address) case types.StatusDestroyed: return simtypes.NoOpMsg(types.ModuleName, "ChangeStatus", "marker status is destroyed"), nil, nil default: - return simtypes.NoOpMsg("marker", "", "unknown marker status"), nil, fmt.Errorf("unknown marker status: %#v", m) + return simtypes.NoOpMsg(types.ModuleName, "", "unknown marker status"), nil, fmt.Errorf("unknown marker status: %#v", m) } - return Dispatch(r, app, ctx, ak, bk, simAccount, chainID, msg, nil) + return Dispatch(r, app, ctx, args.SimState, args.AK, args.BK, simAccount, chainID, msg, nil) } } // SimulateMsgAddAccess will Add a random access to an account. -func SimulateMsgAddAccess(k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper) simtypes.Operation { +func SimulateMsgAddAccess(k keeper.Keeper, args *WeightedOpsArgs) 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) m := randomMarker(r, ctx, k) if m == nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(&types.MsgAddAccessRequest{}), sdk.MsgTypeURL(&types.MsgAddAccessRequest{}), "unable to get marker for access change"), nil, nil + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgAddAccessRequest{}), "unable to get marker for access change"), nil, nil } if !m.GetManager().Equals(sdk.AccAddress{}) { simAccount, _ = simtypes.FindAccount(accs, m.GetManager()) } grants := randomAccessGrants(r, accs, 100, m.GetMarkerType()) msg := types.NewMsgAddAccessRequest(m.GetDenom(), simAccount.Address, grants[0]) - return Dispatch(r, app, ctx, ak, bk, simAccount, chainID, msg, nil) + return Dispatch(r, app, ctx, args.SimState, args.AK, args.BK, simAccount, chainID, msg, nil) } } // SimulateMsgAddFinalizeActivateMarker will bind a NAME under an existing name using a 40% probability of restricting it. -func SimulateMsgAddFinalizeActivateMarker(k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper) simtypes.Operation { +func SimulateMsgAddFinalizeActivateMarker(k keeper.Keeper, args *WeightedOpsArgs) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { @@ -269,7 +221,7 @@ func SimulateMsgAddFinalizeActivateMarker(k keeper.Keeper, ak authkeeper.Account msg.AllowForcedTransfer = false } - return Dispatch(r, app, ctx, ak, bk, simAccount, chainID, msg, nil) + return Dispatch(r, app, ctx, args.SimState, args.AK, args.BK, simAccount, chainID, msg, nil) } } @@ -362,7 +314,7 @@ func SimulateMsgSetAccountData(k keeper.Keeper, args *WeightedOpsArgs) simtypes. marker, signer := randomMarkerWithAccessSigner(r, ctx, k, accs, types.Access_Deposit) if marker == nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), "unable to find marker with a deposit signer"), nil, nil + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "unable to find marker with a deposit signer"), nil, nil } msg.Denom = marker.GetDenom() @@ -379,7 +331,7 @@ func SimulateMsgSetAccountData(k keeper.Keeper, args *WeightedOpsArgs) simtypes. msg.Value = simtypes.RandStringOfLength(r, strLen) } - return Dispatch(r, app, ctx, args.AK, args.BK, signer, chainID, msg, nil) + return Dispatch(r, app, ctx, args.SimState, args.AK, args.BK, signer, chainID, msg, nil) } } @@ -392,7 +344,7 @@ func SimulateMsgUpdateSendDenyList(k keeper.Keeper, args *WeightedOpsArgs) simty marker, signer := randomMarkerWithAccessSigner(r, ctx, k, accs, types.Access_Transfer) if marker == nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), "unable to find marker with a transfer signer"), nil, nil + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "unable to find marker with a transfer signer"), nil, nil } rDenyAccounts := simtypes.RandomAccounts(r, 10) @@ -405,7 +357,7 @@ func SimulateMsgUpdateSendDenyList(k keeper.Keeper, args *WeightedOpsArgs) simty msg.AddDeniedAddresses = addDenyAddresses msg.Authority = signer.Address.String() - return Dispatch(r, app, ctx, args.AK, args.BK, signer, chainID, msg, nil) + return Dispatch(r, app, ctx, args.SimState, args.AK, args.BK, signer, chainID, msg, nil) } } @@ -415,6 +367,7 @@ func Dispatch( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, + simState module.SimulationState, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper, from simtypes.Account, @@ -431,7 +384,7 @@ func Dispatch( fees, err := simtypes.RandomFees(r, ctx, spendable) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), "unable to generate fees"), nil, err + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "unable to generate fees"), nil, err } // fund account with nhash for additional fees, if the account exists (100m stake) if sdk.MsgTypeURL(msg) == "/provenance.marker.v1.MsgAddMarkerRequest" && ak.GetAccount(ctx, account.GetAddress()) != nil { @@ -440,7 +393,7 @@ func Dispatch( Amount: sdkmath.NewInt(100_000_000_000_000), })) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), "unable to fund account with additional fee"), nil, err + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "unable to fund account with additional fee"), nil, err } fees = fees.Add(sdk.Coin{ Denom: "stake", @@ -448,10 +401,9 @@ func Dispatch( }) } - txGen := simappparams.MakeTestEncodingConfig().TxConfig tx, err := simtestutil.GenSignedMockTx( r, - txGen, + simState.TxConfig, []sdk.Msg{msg}, fees, simtestutil.DefaultGenTxGas, @@ -461,12 +413,12 @@ func Dispatch( from.PrivKey, ) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), "unable to generate mock tx"), nil, err + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "unable to generate mock tx"), nil, err } - _, _, err = app.SimDeliver(txGen.TxEncoder(), tx) + _, _, err = app.SimDeliver(simState.TxConfig.TxEncoder(), tx) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), err.Error()), nil, nil + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), err.Error()), nil, nil } return simtypes.NewOperationMsg(msg, true, ""), futures, nil @@ -585,10 +537,9 @@ func randomInt63(r *rand.Rand, max int64) (result int64) { // WeightedOpsArgs holds all the args provided to WeightedOperations so that they can be passed on later more easily. type WeightedOpsArgs struct { - AppParams simtypes.AppParams - JSONCodec codec.JSONCodec + SimState module.SimulationState ProtoCodec *codec.ProtoCodec - AK authkeeper.AccountKeeper + AK authkeeper.AccountKeeperI BK bankkeeper.Keeper GK govkeeper.Keeper AttrK types.AttrKeeper @@ -640,7 +591,7 @@ func SendGovMsg(args *SendGovMsgArgs) (bool, simtypes.OperationMsg, error) { txCtx := simulation.OperationInput{ R: args.R, App: args.App, - TxGen: simappparams.MakeTestEncodingConfig().TxConfig, + TxGen: args.SimState.TxConfig, Cdc: args.ProtoCodec, Msg: govMsg, CoinsSpentInMsg: govMsg.InitialDeposit, @@ -670,7 +621,7 @@ func OperationMsgVote(args *WeightedOpsArgs, voter simtypes.Account, govPropID u txCtx := simulation.OperationInput{ R: r, App: app, - TxGen: simappparams.MakeTestEncodingConfig().TxConfig, + TxGen: args.SimState.TxConfig, Cdc: args.ProtoCodec, Msg: msg, CoinsSpentInMsg: sdk.Coins{}, diff --git a/x/marker/simulation/operations_test.go b/x/marker/simulation/operations_test.go index 89ef1a4e58..e57fb12088 100644 --- a/x/marker/simulation/operations_test.go +++ b/x/marker/simulation/operations_test.go @@ -10,9 +10,9 @@ import ( "github.com/stretchr/testify/suite" sdkmath "cosmossdk.io/math" - "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" "github.com/cosmos/cosmos-sdk/x/bank/testutil" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1" @@ -31,6 +31,10 @@ type SimTestSuite struct { app *app.App } +func TestSimTestSuite(t *testing.T) { + suite.Run(t, new(SimTestSuite)) +} + func (s *SimTestSuite) SetupTest() { s.app = app.Setup(s.T()) s.ctx = s.app.BaseApp.NewContext(false) @@ -54,11 +58,17 @@ func (s *SimTestSuite) LogOperationMsg(operationMsg simtypes.OperationMsg) { ) } -func (s *SimTestSuite) TestWeightedOperations() { - cdc := s.app.AppCodec() - appParams := make(simtypes.AppParams) +// MakeTestSimState creates a new module.SimulationState struct with the fields needed by the functions being tested. +func (s *SimTestSuite) MakeTestSimState() module.SimulationState { + return module.SimulationState{ + AppParams: make(simtypes.AppParams), + Cdc: s.app.AppCodec(), + TxConfig: s.app.GetTxConfig(), + } +} - weightedOps := simulation.WeightedOperations(appParams, cdc, codec.NewProtoCodec(s.app.InterfaceRegistry()), s.app.MarkerKeeper, +func (s *SimTestSuite) TestWeightedOperations() { + weightedOps := simulation.WeightedOperations(s.MakeTestSimState(), codec.NewProtoCodec(s.app.InterfaceRegistry()), s.app.MarkerKeeper, s.app.AccountKeeper, s.app.BankKeeper, s.app.GovKeeper, s.app.AttributeKeeper, ) @@ -132,7 +142,7 @@ func (s *SimTestSuite) TestSimulateMsgAddMarker() { accounts := s.getTestingAccounts(r, 3) // execute operation - op := simulation.SimulateMsgAddMarker(s.app.MarkerKeeper, s.app.AccountKeeper, s.app.BankKeeper) + op := simulation.SimulateMsgAddMarker(s.app.MarkerKeeper, s.getWeightedOpsArgs()) operationMsg, futureOperations, err := op(r, s.app.BaseApp, s.ctx, accounts, "") s.Require().NoError(err) @@ -154,7 +164,7 @@ func (s *SimTestSuite) TestSimulateMsgAddActivateFinalizeMarker() { accounts := s.getTestingAccounts(r, 3) // execute operation - op := simulation.SimulateMsgAddFinalizeActivateMarker(s.app.MarkerKeeper, s.app.AccountKeeper, s.app.BankKeeper) + op := simulation.SimulateMsgAddFinalizeActivateMarker(s.app.MarkerKeeper, s.getWeightedOpsArgs()) operationMsg, futureOperations, err := op(r, s.app.BaseApp, s.ctx, accounts, "") s.Require().NoError(err) @@ -308,7 +318,7 @@ func (s *SimTestSuite) TestSimulateMsgAddMarkerProposal() { s.Run(tc.name, func() { args := &simulation.SendGovMsgArgs{ - WeightedOpsArgs: s.getWeightedOpsArgs(), + WeightedOpsArgs: *s.getWeightedOpsArgs(), R: rand.New(rand.NewSource(1)), App: s.app.BaseApp, Ctx: s.ctx, @@ -378,9 +388,8 @@ func (s *SimTestSuite) TestSimulateMsgSetAccountData() { _, err := markerMsgServer.AddFinalizeActivateMarker(s.ctx, newMarker) s.Require().NoError(err, "AddFinalizeActivateMarker") - args := s.getWeightedOpsArgs() // execute operation - op := simulation.SimulateMsgSetAccountData(s.app.MarkerKeeper, &args) + op := simulation.SimulateMsgSetAccountData(s.app.MarkerKeeper, s.getWeightedOpsArgs()) operationMsg, futureOperations, err := op(r, s.app.BaseApp, s.ctx, accounts, "") s.Require().NoError(err, "SimulateMsgSetAccountData op(...) error") s.LogOperationMsg(operationMsg) @@ -427,9 +436,8 @@ func (s *SimTestSuite) TestSimulateMsgUpdateSendDenyList() { _, err := markerMsgServer.AddFinalizeActivateMarker(s.ctx, newMarker) s.Require().NoError(err, "AddFinalizeActivateMarker") - args := s.getWeightedOpsArgs() // execute operation - op := simulation.SimulateMsgUpdateSendDenyList(s.app.MarkerKeeper, &args) + op := simulation.SimulateMsgUpdateSendDenyList(s.app.MarkerKeeper, s.getWeightedOpsArgs()) operationMsg, futureOperations, err := op(r, s.app.BaseApp, s.ctx, accounts, "") s.Require().NoError(err, "SimulateMsgUpdateSendDenyList op(...) error") s.LogOperationMsg(operationMsg) @@ -463,15 +471,10 @@ func (s *SimTestSuite) getTestingAccounts(r *rand.Rand, n int) []simtypes.Accoun return accounts } -func TestSimTestSuite(t *testing.T) { - suite.Run(t, new(SimTestSuite)) -} - // getWeightedOpsArgs creates a standard WeightedOpsArgs. -func (s *SimTestSuite) getWeightedOpsArgs() simulation.WeightedOpsArgs { - return simulation.WeightedOpsArgs{ - AppParams: make(simtypes.AppParams), - JSONCodec: s.app.AppCodec(), +func (s *SimTestSuite) getWeightedOpsArgs() *simulation.WeightedOpsArgs { + return &simulation.WeightedOpsArgs{ + SimState: s.MakeTestSimState(), ProtoCodec: codec.NewProtoCodec(s.app.InterfaceRegistry()), AK: s.app.AccountKeeper, BK: s.app.BankKeeper, diff --git a/x/name/module.go b/x/name/module.go index ea37c1656b..05e556519d 100644 --- a/x/name/module.go +++ b/x/name/module.go @@ -6,10 +6,11 @@ import ( "fmt" "math/rand" - "cosmossdk.io/core/appmodule" "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" + "cosmossdk.io/core/appmodule" + abci "github.com/cometbft/cometbft/abci/types" sdkclient "github.com/cosmos/cosmos-sdk/client" @@ -169,9 +170,7 @@ func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) { // WeightedOperations returns the all the gov module operations with their respective weights. func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { - return simulation.WeightedOperations( - simState.AppParams, simState.Cdc, am.keeper, am.ak, am.bk, - ) + return simulation.WeightedOperations(simState, am.keeper, am.ak, am.bk) } // ConsensusVersion implements AppModule/ConsensusVersion. diff --git a/x/name/simulation/operations.go b/x/name/simulation/operations.go index 1f1fb5c2fe..fd39039d60 100644 --- a/x/name/simulation/operations.go +++ b/x/name/simulation/operations.go @@ -4,19 +4,18 @@ import ( "math/rand" "strings" - simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" - "github.com/cosmos/cosmos-sdk/baseapp" - "github.com/cosmos/cosmos-sdk/codec" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" "github.com/cosmos/cosmos-sdk/x/simulation" simappparams "github.com/provenance-io/provenance/app/params" - keeper "github.com/provenance-io/provenance/x/name/keeper" - types "github.com/provenance-io/provenance/x/name/types" + "github.com/provenance-io/provenance/x/name/keeper" + "github.com/provenance-io/provenance/x/name/types" ) // Simulation operation weights constants @@ -31,60 +30,40 @@ const ( // WeightedOperations returns all the operations from the module with their respective weights func WeightedOperations( - appParams simtypes.AppParams, cdc codec.JSONCodec, k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper, + simState module.SimulationState, k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper, ) simulation.WeightedOperations { var ( - weightMsgBindName int - weightMsgDeleteName int - weightMsgModifyName int - ) - - appParams.GetOrGenerate(OpWeightMsgBindName, &weightMsgBindName, nil, - func(_ *rand.Rand) { - weightMsgBindName = simappparams.DefaultWeightMsgBindName - }, - ) - - appParams.GetOrGenerate(OpWeightMsgDeleteName, &weightMsgDeleteName, nil, - func(_ *rand.Rand) { - weightMsgDeleteName = simappparams.DefaultWeightMsgDeleteName - }, + wMsgBindName int + wMsgDeleteName int + wMsgModifyName int ) - appParams.GetOrGenerate(OpWeightMsgModifyName, &weightMsgModifyName, nil, - func(_ *rand.Rand) { - weightMsgModifyName = simappparams.DefaultWeightMsgModifyName - }, - ) + simState.AppParams.GetOrGenerate(OpWeightMsgBindName, &wMsgBindName, nil, + func(_ *rand.Rand) { wMsgBindName = simappparams.DefaultWeightMsgBindName }) + simState.AppParams.GetOrGenerate(OpWeightMsgDeleteName, &wMsgDeleteName, nil, + func(_ *rand.Rand) { wMsgDeleteName = simappparams.DefaultWeightMsgDeleteName }) + simState.AppParams.GetOrGenerate(OpWeightMsgModifyName, &wMsgModifyName, nil, + func(_ *rand.Rand) { wMsgModifyName = simappparams.DefaultWeightMsgModifyName }) return simulation.WeightedOperations{ - simulation.NewWeightedOperation( - weightMsgBindName, - SimulateMsgBindName(k, ak, bk), - ), - simulation.NewWeightedOperation( - weightMsgDeleteName, - SimulateMsgDeleteName(k, ak, bk), - ), - simulation.NewWeightedOperation( - weightMsgModifyName, - SimulateMsgModifyName(k, ak, bk), - ), + simulation.NewWeightedOperation(wMsgBindName, SimulateMsgBindName(simState, k, ak, bk)), + simulation.NewWeightedOperation(wMsgDeleteName, SimulateMsgDeleteName(simState, k, ak, bk)), + simulation.NewWeightedOperation(wMsgModifyName, SimulateMsgModifyName(simState, k, ak, bk)), } } // SimulateMsgBindName will bind a NAME under an existing name using a 40% probability of restricting it. -func SimulateMsgBindName(k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper) simtypes.Operation { +func SimulateMsgBindName(simState module.SimulationState, k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { params := k.GetParams(ctx) parentRecord, parentOwner, found, err := getRandomRecord(r, ctx, k, accs, 1, int(params.MaxNameLevels)-1) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(&types.MsgBindNameRequest{}), sdk.MsgTypeURL(&types.MsgBindNameRequest{}), "iterator of existing records failed"), nil, err + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgBindNameRequest{}), "iterator of existing records failed"), nil, err } if !found { - return simtypes.NoOpMsg(sdk.MsgTypeURL(&types.MsgBindNameRequest{}), sdk.MsgTypeURL(&types.MsgBindNameRequest{}), "no name records available to create under"), nil, nil + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgBindNameRequest{}), "no name records available to create under"), nil, nil } nameLen := randIntBetween(r, int(params.GetMinSegmentLength()), int(params.GetMaxSegmentLength())) @@ -97,12 +76,12 @@ func SimulateMsgBindName(k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankk newRecord := types.NewNameRecord(newRecordName, newRecordOwner.Address, newRecordRestricted) msg := types.NewMsgBindNameRequest(newRecord, parentRecord) - return Dispatch(r, app, ctx, ak, bk, parentOwner, chainID, msg) + return Dispatch(r, app, ctx, simState, ak, bk, parentOwner, chainID, msg) } } // SimulateMsgDeleteName will dispatch a delete name operation against a random name record -func SimulateMsgDeleteName(k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper) simtypes.Operation { +func SimulateMsgDeleteName(simState module.SimulationState, k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { @@ -111,37 +90,37 @@ func SimulateMsgDeleteName(k keeper.Keeper, ak authkeeper.AccountKeeperI, bk ban // Not doing a min/max params lookup because they can change during the sim and don't apply to this operation. randomRecord, simAccount, found, err := getRandomRecord(r, ctx, k, accs, 2, 1_000_000) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(&types.MsgDeleteNameRequest{}), sdk.MsgTypeURL(&types.MsgDeleteNameRequest{}), "iterator of existing records failed"), nil, err + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgDeleteNameRequest{}), "iterator of existing records failed"), nil, err } if !found { - return simtypes.NoOpMsg(sdk.MsgTypeURL(&types.MsgDeleteNameRequest{}), sdk.MsgTypeURL(&types.MsgDeleteNameRequest{}), "no name records available to delete"), nil, nil + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgDeleteNameRequest{}), "no name records available to delete"), nil, nil } msg := types.NewMsgDeleteNameRequest(randomRecord) - return Dispatch(r, app, ctx, ak, bk, simAccount, chainID, msg) + return Dispatch(r, app, ctx, simState, ak, bk, simAccount, chainID, msg) } } // SimulateMsgModifyName will dispatch a modify name operation against a random name record -func SimulateMsgModifyName(k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper) simtypes.Operation { +func SimulateMsgModifyName(simState module.SimulationState, k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { params := k.GetParams(ctx) randomRecord, simAccount, found, err := getRandomRecord(r, ctx, k, accs, 1, int(params.MaxNameLevels)) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(&types.MsgModifyNameRequest{}), sdk.MsgTypeURL(&types.MsgModifyNameRequest{}), "iterator of existing records failed"), nil, err + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgModifyNameRequest{}), "iterator of existing records failed"), nil, err } if !found { - return simtypes.NoOpMsg(sdk.MsgTypeURL(&types.MsgModifyNameRequest{}), sdk.MsgTypeURL(&types.MsgModifyNameRequest{}), "no name records available to modify"), nil, nil + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgModifyNameRequest{}), "no name records available to modify"), nil, nil } newOwner, _ := simtypes.RandomAcc(r, accs) restrict := r.Intn(9) < 4 msg := types.NewMsgModifyNameRequest(simAccount.Address.String(), randomRecord.Name, newOwner.Address, restrict) - return Dispatch(r, app, ctx, ak, bk, simAccount, chainID, msg) + return Dispatch(r, app, ctx, simState, ak, bk, simAccount, chainID, msg) } } @@ -151,6 +130,7 @@ func Dispatch( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, + simState module.SimulationState, ak authkeeper.AccountKeeperI, bk bankkeeper.ViewKeeper, from simtypes.Account, @@ -166,13 +146,12 @@ func Dispatch( fees, err := simtypes.RandomFees(r, ctx, spendable) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), "unable to generate fees"), nil, err + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "unable to generate fees"), nil, err } - txGen := simappparams.MakeTestEncodingConfig().TxConfig tx, err := simtestutil.GenSignedMockTx( r, - txGen, + simState.TxConfig, []sdk.Msg{msg}, fees, simtestutil.DefaultGenTxGas, @@ -182,12 +161,12 @@ func Dispatch( from.PrivKey, ) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), "unable to generate mock tx"), nil, err + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "unable to generate mock tx"), nil, err } - _, _, err = app.SimDeliver(txGen.TxEncoder(), tx) + _, _, err = app.SimDeliver(simState.TxConfig.TxEncoder(), tx) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), err.Error()), nil, nil + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), err.Error()), nil, nil } return simtypes.NewOperationMsg(msg, true, ""), nil, nil diff --git a/x/name/simulation/operations_test.go b/x/name/simulation/operations_test.go index 2e97795fce..a189f1d3c4 100644 --- a/x/name/simulation/operations_test.go +++ b/x/name/simulation/operations_test.go @@ -9,6 +9,7 @@ import ( "github.com/stretchr/testify/suite" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" "github.com/cosmos/cosmos-sdk/x/bank/testutil" @@ -25,6 +26,10 @@ type SimTestSuite struct { app *app.App } +func TestSimTestSuite(t *testing.T) { + suite.Run(t, new(SimTestSuite)) +} + func (s *SimTestSuite) SetupTest() { s.app = app.Setup(s.T()) s.ctx = s.app.BaseApp.NewContext(false) @@ -56,11 +61,17 @@ func (s *SimTestSuite) LogIfError(err error, format string, args ...interface{}) } } -func (s *SimTestSuite) TestWeightedOperations() { - cdc := s.app.AppCodec() - appParams := make(simtypes.AppParams) +// MakeTestSimState creates a new module.SimulationState struct with the fields needed by the functions being tested. +func (s *SimTestSuite) MakeTestSimState() module.SimulationState { + return module.SimulationState{ + AppParams: make(simtypes.AppParams), + Cdc: s.app.AppCodec(), + TxConfig: s.app.GetTxConfig(), + } +} - weightedOps := simulation.WeightedOperations(appParams, cdc, s.app.NameKeeper, +func (s *SimTestSuite) TestWeightedOperations() { + weightedOps := simulation.WeightedOperations(s.MakeTestSimState(), s.app.NameKeeper, s.app.AccountKeeper, s.app.BankKeeper, ) @@ -117,7 +128,7 @@ func (s *SimTestSuite) TestSimulateMsgBindName() { s.LogIfError(s.app.NameKeeper.SetNameRecord(s.ctx, name, accounts[0].Address, false), "SetNameRecord(%q)", name) // execute operation - op := simulation.SimulateMsgBindName(s.app.NameKeeper, s.app.AccountKeeper, s.app.BankKeeper) + op := simulation.SimulateMsgBindName(s.MakeTestSimState(), s.app.NameKeeper, s.app.AccountKeeper, s.app.BankKeeper) operationMsg, futureOperations, err := op(r, s.app.BaseApp, s.ctx, accounts, "") s.Require().NoError(err, "SimulateMsgBindName op(...) error") s.LogOperationMsg(operationMsg) @@ -148,7 +159,7 @@ func (s *SimTestSuite) TestSimulateMsgDeleteName() { s.LogIfError(s.app.NameKeeper.SetNameRecord(s.ctx, name, accounts[0].Address, false), "SetNameRecord(%q)", name) // execute operation - op := simulation.SimulateMsgDeleteName(s.app.NameKeeper, s.app.AccountKeeper, s.app.BankKeeper) + op := simulation.SimulateMsgDeleteName(s.MakeTestSimState(), s.app.NameKeeper, s.app.AccountKeeper, s.app.BankKeeper) operationMsg, futureOperations, err := op(r, s.app.BaseApp, s.ctx, accounts, "") s.Require().NoError(err, "SimulateMsgDeleteName op(...) error") s.LogOperationMsg(operationMsg) @@ -177,7 +188,7 @@ func (s *SimTestSuite) TestSimulateMsgModifyName() { s.LogIfError(s.app.NameKeeper.SetNameRecord(s.ctx, name, accounts[0].Address, false), "SetNameRecord(%q)", name) // execute operation - op := simulation.SimulateMsgModifyName(s.app.NameKeeper, s.app.AccountKeeper, s.app.BankKeeper) + op := simulation.SimulateMsgModifyName(s.MakeTestSimState(), s.app.NameKeeper, s.app.AccountKeeper, s.app.BankKeeper) operationMsg, futureOperations, err := op(r, s.app.BaseApp, s.ctx, accounts, "") s.Require().NoError(err, "SimulateMsgModifyName op(...) error") s.LogOperationMsg(operationMsg) @@ -209,7 +220,3 @@ func (s *SimTestSuite) getTestingAccounts(r *rand.Rand, n int) []simtypes.Accoun return accounts } - -func TestSimTestSuite(t *testing.T) { - suite.Run(t, new(SimTestSuite)) -} diff --git a/x/oracle/module/module.go b/x/oracle/module/module.go index 5b7b3e9543..007fe3b387 100644 --- a/x/oracle/module/module.go +++ b/x/oracle/module/module.go @@ -143,9 +143,7 @@ func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) { // WeightedOperations returns simulation operations (i.e msgs) with their respective weight func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { - return simulation.WeightedOperations( - simState.AppParams, simState.Cdc, am.keeper, am.accountKeeper, am.bankKeeper, am.channelKeeper, - ) + return simulation.WeightedOperations(simState, am.keeper, am.accountKeeper, am.bankKeeper, am.channelKeeper) } // Name returns the oracle module's name. diff --git a/x/oracle/simulation/operations.go b/x/oracle/simulation/operations.go index c3d4f49405..abc357fa2b 100644 --- a/x/oracle/simulation/operations.go +++ b/x/oracle/simulation/operations.go @@ -4,13 +4,12 @@ import ( "fmt" "math/rand" - simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" - sdkmath "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/baseapp" - "github.com/cosmos/cosmos-sdk/codec" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" @@ -34,44 +33,32 @@ const ( // WeightedOperations returns all the operations from the module with their respective weights func WeightedOperations( - appParams simtypes.AppParams, cdc codec.JSONCodec, k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper, ck channelkeeper.Keeper, + simState module.SimulationState, k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper, ck channelkeeper.Keeper, ) simulation.WeightedOperations { var ( - weightMsgUpdateOracle int - weightMsgSendOracleQuery int + wMsgUpdateOracle int + wMsgSendOracleQuery int ) - appParams.GetOrGenerate(OpWeightMsgUpdateOracle, &weightMsgUpdateOracle, nil, - func(_ *rand.Rand) { - weightMsgUpdateOracle = simappparams.DefaultWeightUpdateOracle - }, - ) - appParams.GetOrGenerate(OpWeightMsgSendOracleQuery, &weightMsgSendOracleQuery, nil, - func(_ *rand.Rand) { - weightMsgSendOracleQuery = simappparams.DefaultWeightSendOracleQuery - }, - ) + simState.AppParams.GetOrGenerate(OpWeightMsgUpdateOracle, &wMsgUpdateOracle, nil, + func(_ *rand.Rand) { wMsgUpdateOracle = simappparams.DefaultWeightUpdateOracle }) + simState.AppParams.GetOrGenerate(OpWeightMsgSendOracleQuery, &wMsgSendOracleQuery, nil, + func(_ *rand.Rand) { wMsgSendOracleQuery = simappparams.DefaultWeightSendOracleQuery }) return simulation.WeightedOperations{ - simulation.NewWeightedOperation( - weightMsgUpdateOracle, - SimulateMsgUpdateOracle(k, ak, bk), - ), - simulation.NewWeightedOperation( - weightMsgSendOracleQuery, - SimulateMsgSendQueryOracle(k, ak, bk, ck), - ), + simulation.NewWeightedOperation(wMsgUpdateOracle, SimulateMsgUpdateOracle(simState, k, ak, bk)), + simulation.NewWeightedOperation(wMsgSendOracleQuery, SimulateMsgSendQueryOracle(simState, k, ak, bk, ck)), } } // SimulateMsgUpdateOracle sends a MsgUpdateOracle. -func SimulateMsgUpdateOracle(_ keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper) simtypes.Operation { +func SimulateMsgUpdateOracle(simState module.SimulationState, _ keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { raccs, err := RandomAccs(r, accs, uint64(len(accs))) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(&types.MsgUpdateOracleRequest{}), sdk.MsgTypeURL(&types.MsgUpdateOracleRequest{}), err.Error()), nil, nil + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgUpdateOracleRequest{}), err.Error()), nil, nil } // 50% chance to be from the module's authority @@ -80,30 +67,30 @@ func SimulateMsgUpdateOracle(_ keeper.Keeper, ak authkeeper.AccountKeeperI, bk b msg := types.NewMsgUpdateOracle(from.Address.String(), to.Address.String()) - return Dispatch(r, app, ctx, from, chainID, msg, ak, bk, nil) + return Dispatch(r, app, ctx, simState, from, chainID, msg, ak, bk, nil) } } // SimulateMsgSendQueryOracle sends a MsgSendQueryOracle. -func SimulateMsgSendQueryOracle(_ keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper, ck channelkeeper.Keeper) simtypes.Operation { +func SimulateMsgSendQueryOracle(simState module.SimulationState, _ keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper, ck channelkeeper.Keeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { raccs, err := RandomAccs(r, accs, 1) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(&types.MsgSendQueryOracleRequest{}), sdk.MsgTypeURL(&types.MsgSendQueryOracleRequest{}), err.Error()), nil, nil + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgSendQueryOracleRequest{}), err.Error()), nil, nil } addr := raccs[0] channel, err := randomChannel(r, ctx, ck) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(&types.MsgSendQueryOracleRequest{}), sdk.MsgTypeURL(&types.MsgSendQueryOracleRequest{}), err.Error()), nil, nil + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgSendQueryOracleRequest{}), err.Error()), nil, nil } query := randomQuery(r) msg := types.NewMsgSendQueryOracle(addr.Address.String(), channel, query) - return Dispatch(r, app, ctx, addr, chainID, msg, ak, bk, nil) + return Dispatch(r, app, ctx, simState, addr, chainID, msg, ak, bk, nil) } } @@ -113,6 +100,7 @@ func Dispatch( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, + simState module.SimulationState, from simtypes.Account, chainID string, msg sdk.Msg, @@ -129,19 +117,19 @@ func Dispatch( fees, err := simtypes.RandomFees(r, ctx, spendable) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), "unable to generate fees"), nil, err + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "unable to generate fees"), nil, err } err = testutil.FundAccount(ctx, bk, account.GetAddress(), sdk.NewCoins(sdk.Coin{ Denom: pioconfig.GetProvenanceConfig().BondDenom, Amount: sdkmath.NewInt(1_000_000_000_000_000), })) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), "unable to fund account"), nil, err + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "unable to fund account"), nil, err } - txGen := simappparams.MakeTestEncodingConfig().TxConfig + tx, err := simtestutil.GenSignedMockTx( r, - txGen, + simState.TxConfig, []sdk.Msg{msg}, fees, simtestutil.DefaultGenTxGas, @@ -151,12 +139,12 @@ func Dispatch( from.PrivKey, ) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), "unable to generate mock tx"), nil, err + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "unable to generate mock tx"), nil, err } - _, _, err = app.SimDeliver(txGen.TxEncoder(), tx) + _, _, err = app.SimDeliver(simState.TxConfig.TxEncoder(), tx) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), err.Error()), nil, nil + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), err.Error()), nil, nil } return simtypes.NewOperationMsg(msg, true, ""), futures, nil diff --git a/x/oracle/simulation/operations_test.go b/x/oracle/simulation/operations_test.go index f8ee1f7173..1d8874b2a2 100644 --- a/x/oracle/simulation/operations_test.go +++ b/x/oracle/simulation/operations_test.go @@ -5,10 +5,12 @@ import ( "fmt" "math/rand" "strings" + "testing" "github.com/stretchr/testify/suite" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" "github.com/cosmos/cosmos-sdk/x/bank/testutil" @@ -25,6 +27,10 @@ type SimTestSuite struct { app *app.App } +func TestSimTestSuite(t *testing.T) { + suite.Run(t, new(SimTestSuite)) +} + func (s *SimTestSuite) SetupTest() { s.app = app.Setup(s.T()) s.ctx = s.app.BaseApp.NewContext(false) @@ -49,11 +55,17 @@ func (s *SimTestSuite) LogOperationMsg(operationMsg simtypes.OperationMsg, msg s ) } -func (s *SimTestSuite) TestWeightedOperations() { - cdc := s.app.AppCodec() - appParams := make(simtypes.AppParams) +// MakeTestSimState creates a new module.SimulationState struct with the fields needed by the functions being tested. +func (s *SimTestSuite) MakeTestSimState() module.SimulationState { + return module.SimulationState{ + AppParams: make(simtypes.AppParams), + Cdc: s.app.AppCodec(), + TxConfig: s.app.GetTxConfig(), + } +} - weightedOps := simulation.WeightedOperations(appParams, cdc, s.app.OracleKeeper, +func (s *SimTestSuite) TestWeightedOperations() { + weightedOps := simulation.WeightedOperations(s.MakeTestSimState(), s.app.OracleKeeper, s.app.AccountKeeper, s.app.BankKeeper, s.app.IBCKeeper.ChannelKeeper, ) @@ -103,7 +115,7 @@ func (s *SimTestSuite) TestSimulateMsgUpdateOracle() { accounts := s.getTestingAccounts(r, 3) // execute operation - op := simulation.SimulateMsgUpdateOracle(s.app.OracleKeeper, s.app.AccountKeeper, s.app.BankKeeper) + op := simulation.SimulateMsgUpdateOracle(s.MakeTestSimState(), s.app.OracleKeeper, s.app.AccountKeeper, s.app.BankKeeper) operationMsg, futureOperations, err := op(r, s.app.BaseApp, s.ctx, accounts, "") s.Require().NoError(err, "SimulateMsgUpdateOracle op(...) error") s.LogOperationMsg(operationMsg, "good") @@ -124,7 +136,7 @@ func (s *SimTestSuite) TestSimulateMsgSendQueryOracle() { accounts := s.getTestingAccounts(r, 3) // execute operation - op := simulation.SimulateMsgSendQueryOracle(s.app.OracleKeeper, s.app.AccountKeeper, s.app.BankKeeper, s.app.IBCKeeper.ChannelKeeper) + op := simulation.SimulateMsgSendQueryOracle(s.MakeTestSimState(), s.app.OracleKeeper, s.app.AccountKeeper, s.app.BankKeeper, s.app.IBCKeeper.ChannelKeeper) operationMsg, futureOperations, err := op(r, s.app.BaseApp, s.ctx, accounts, "") s.Require().NoError(err, "SimulateMsgSendQueryOracle op(...) error") s.LogOperationMsg(operationMsg, "good") diff --git a/x/quarantine/module/module.go b/x/quarantine/module/module.go index d1a1f73304..cc0bac33b8 100644 --- a/x/quarantine/module/module.go +++ b/x/quarantine/module/module.go @@ -12,6 +12,7 @@ import ( abci "github.com/cometbft/cometbft/abci/types" "cosmossdk.io/core/appmodule" + sdkclient "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" cdctypes "github.com/cosmos/cosmos-sdk/codec/types" @@ -164,8 +165,5 @@ func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) { // WeightedOperations returns the all the quarantine module operations with their respective weights. func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { - return simulation.WeightedOperations( - simState.AppParams, simState.Cdc, - am.accKeeper, am.bankKeeper, am.keeper, am.cdc, - ) + return simulation.WeightedOperations(simState, am.accKeeper, am.bankKeeper, am.keeper) } diff --git a/x/quarantine/simulation/operations.go b/x/quarantine/simulation/operations.go index 4ee023aee6..30b953ec39 100644 --- a/x/quarantine/simulation/operations.go +++ b/x/quarantine/simulation/operations.go @@ -5,15 +5,13 @@ import ( "math/rand" "github.com/cosmos/cosmos-sdk/baseapp" - "github.com/cosmos/cosmos-sdk/codec" - cdctypes "github.com/cosmos/cosmos-sdk/codec/types" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" 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" - simappparams "github.com/provenance-io/provenance/app/params" "github.com/provenance-io/provenance/x/quarantine" "github.com/provenance-io/provenance/x/quarantine/keeper" ) @@ -47,39 +45,38 @@ const ( // WeightedOperations returns all the operations from the module with their respective weights func WeightedOperations( - appParams simtypes.AppParams, _ codec.JSONCodec, - ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k keeper.Keeper, _ cdctypes.AnyUnpacker, + simState module.SimulationState, ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k keeper.Keeper, ) simulation.WeightedOperations { var ( - weightMsgOptIn int - weightMsgOptOut int - weightMsgAccept int - weightMsgDecline int - weightMsgUpdateAutoResponses int + wMsgOptIn int + wMsgOptOut int + wMsgAccept int + wMsgDecline int + wMsgUpdateAutoResponses int ) - appParams.GetOrGenerate(OpMsgOptIn, &weightMsgOptIn, nil, - func(_ *rand.Rand) { weightMsgOptIn = WeightMsgOptIn }) - appParams.GetOrGenerate(OpMsgOptOut, &weightMsgOptOut, nil, - func(_ *rand.Rand) { weightMsgOptOut = WeightMsgOptOut }) - appParams.GetOrGenerate(OpMsgAccept, &weightMsgAccept, nil, - func(_ *rand.Rand) { weightMsgAccept = WeightMsgAccept }) - appParams.GetOrGenerate(OpMsgDecline, &weightMsgDecline, nil, - func(_ *rand.Rand) { weightMsgDecline = WeightMsgDecline }) - appParams.GetOrGenerate(OpMsgUpdateAutoResponses, &weightMsgUpdateAutoResponses, nil, - func(_ *rand.Rand) { weightMsgUpdateAutoResponses = WeightMsgUpdateAutoResponses }) + simState.AppParams.GetOrGenerate(OpMsgOptIn, &wMsgOptIn, nil, + func(_ *rand.Rand) { wMsgOptIn = WeightMsgOptIn }) + simState.AppParams.GetOrGenerate(OpMsgOptOut, &wMsgOptOut, nil, + func(_ *rand.Rand) { wMsgOptOut = WeightMsgOptOut }) + simState.AppParams.GetOrGenerate(OpMsgAccept, &wMsgAccept, nil, + func(_ *rand.Rand) { wMsgAccept = WeightMsgAccept }) + simState.AppParams.GetOrGenerate(OpMsgDecline, &wMsgDecline, nil, + func(_ *rand.Rand) { wMsgDecline = WeightMsgDecline }) + simState.AppParams.GetOrGenerate(OpMsgUpdateAutoResponses, &wMsgUpdateAutoResponses, nil, + func(_ *rand.Rand) { wMsgUpdateAutoResponses = WeightMsgUpdateAutoResponses }) return simulation.WeightedOperations{ - simulation.NewWeightedOperation(weightMsgOptIn, SimulateMsgOptIn(ak, bk)), - simulation.NewWeightedOperation(weightMsgOptOut, SimulateMsgOptOut(ak, bk, k)), - simulation.NewWeightedOperation(weightMsgAccept, SimulateMsgAccept(ak, bk, k)), - simulation.NewWeightedOperation(weightMsgDecline, SimulateMsgDecline(ak, bk, k)), - simulation.NewWeightedOperation(weightMsgUpdateAutoResponses, SimulateMsgUpdateAutoResponses(ak, bk, k)), + simulation.NewWeightedOperation(wMsgOptIn, SimulateMsgOptIn(simState, ak, bk)), + simulation.NewWeightedOperation(wMsgOptOut, SimulateMsgOptOut(simState, ak, bk, k)), + simulation.NewWeightedOperation(wMsgAccept, SimulateMsgAccept(simState, ak, bk, k)), + simulation.NewWeightedOperation(wMsgDecline, SimulateMsgDecline(simState, ak, bk, k)), + simulation.NewWeightedOperation(wMsgUpdateAutoResponses, SimulateMsgUpdateAutoResponses(simState, ak, bk, k)), } } // SimulateMsgOptIn opts an account into quarantine. -func SimulateMsgOptIn(ak quarantine.AccountKeeper, bk quarantine.BankKeeper) simtypes.Operation { +func SimulateMsgOptIn(simState module.SimulationState, ak quarantine.AccountKeeper, bk quarantine.BankKeeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, @@ -98,10 +95,9 @@ func SimulateMsgOptIn(ak quarantine.AccountKeeper, bk quarantine.BankKeeper) sim account := ak.GetAccount(ctx, acct.Address) - encCfg := simappparams.MakeTestEncodingConfig() tx, err := simtestutil.GenSignedMockTx( r, - encCfg.TxConfig, + simState.TxConfig, []sdk.Msg{msg}, fees, simtestutil.DefaultGenTxGas, @@ -114,7 +110,7 @@ func SimulateMsgOptIn(ak quarantine.AccountKeeper, bk quarantine.BankKeeper) sim return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to generate mock tx"), nil, err } - _, _, err = app.SimDeliver(encCfg.TxConfig.TxEncoder(), tx) + _, _, err = app.SimDeliver(simState.TxConfig.TxEncoder(), tx) if err != nil { return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to deliver tx"), nil, err } @@ -124,7 +120,7 @@ func SimulateMsgOptIn(ak quarantine.AccountKeeper, bk quarantine.BankKeeper) sim } // SimulateMsgOptOut opts an account out of quarantine. -func SimulateMsgOptOut(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k keeper.Keeper) simtypes.Operation { +func SimulateMsgOptOut(simState module.SimulationState, ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k keeper.Keeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, @@ -161,10 +157,9 @@ func SimulateMsgOptOut(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k account := ak.GetAccount(ctx, acct.Address) - encCfg := simappparams.MakeTestEncodingConfig() tx, err := simtestutil.GenSignedMockTx( r, - encCfg.TxConfig, + simState.TxConfig, []sdk.Msg{msg}, fees, simtestutil.DefaultGenTxGas, @@ -177,7 +172,7 @@ func SimulateMsgOptOut(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to generate mock tx"), nil, err } - _, _, err = app.SimDeliver(encCfg.TxConfig.TxEncoder(), tx) + _, _, err = app.SimDeliver(simState.TxConfig.TxEncoder(), tx) if err != nil { return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to deliver tx"), nil, err } @@ -187,7 +182,7 @@ func SimulateMsgOptOut(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k } // SimulateMsgAccept accepts quarantined funds. -func SimulateMsgAccept(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k keeper.Keeper) simtypes.Operation { +func SimulateMsgAccept(simState module.SimulationState, ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k keeper.Keeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, @@ -234,10 +229,9 @@ func SimulateMsgAccept(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k account := ak.GetAccount(ctx, acct.Address) - encCfg := simappparams.MakeTestEncodingConfig() tx, err := simtestutil.GenSignedMockTx( r, - encCfg.TxConfig, + simState.TxConfig, []sdk.Msg{msg}, fees, simtestutil.DefaultGenTxGas, @@ -250,7 +244,7 @@ func SimulateMsgAccept(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to generate mock tx"), nil, err } - _, _, err = app.SimDeliver(encCfg.TxConfig.TxEncoder(), tx) + _, _, err = app.SimDeliver(simState.TxConfig.TxEncoder(), tx) if err != nil { return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to deliver tx"), nil, err } @@ -260,7 +254,7 @@ func SimulateMsgAccept(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k } // SimulateMsgDecline declines quarantined funds. -func SimulateMsgDecline(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k keeper.Keeper) simtypes.Operation { +func SimulateMsgDecline(simState module.SimulationState, ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k keeper.Keeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, @@ -307,10 +301,9 @@ func SimulateMsgDecline(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k account := ak.GetAccount(ctx, acct.Address) - encCfg := simappparams.MakeTestEncodingConfig() tx, err := simtestutil.GenSignedMockTx( r, - encCfg.TxConfig, + simState.TxConfig, []sdk.Msg{msg}, fees, simtestutil.DefaultGenTxGas, @@ -323,7 +316,7 @@ func SimulateMsgDecline(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to generate mock tx"), nil, err } - _, _, err = app.SimDeliver(encCfg.TxConfig.TxEncoder(), tx) + _, _, err = app.SimDeliver(simState.TxConfig.TxEncoder(), tx) if err != nil { return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to deliver tx"), nil, err } @@ -333,7 +326,7 @@ func SimulateMsgDecline(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k } // SimulateMsgUpdateAutoResponses updates an accounts auto-responses -func SimulateMsgUpdateAutoResponses(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k keeper.Keeper) simtypes.Operation { +func SimulateMsgUpdateAutoResponses(simState module.SimulationState, ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k keeper.Keeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, @@ -391,10 +384,9 @@ func SimulateMsgUpdateAutoResponses(ak quarantine.AccountKeeper, bk quarantine.B account := ak.GetAccount(ctx, acct.Address) - encCfg := simappparams.MakeTestEncodingConfig() tx, err := simtestutil.GenSignedMockTx( r, - encCfg.TxConfig, + simState.TxConfig, []sdk.Msg{msg}, fees, simtestutil.DefaultGenTxGas, @@ -407,7 +399,7 @@ func SimulateMsgUpdateAutoResponses(ak quarantine.AccountKeeper, bk quarantine.B return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to generate mock tx"), nil, err } - _, _, err = app.SimDeliver(encCfg.TxConfig.TxEncoder(), tx) + _, _, err = app.SimDeliver(simState.TxConfig.TxEncoder(), tx) if err != nil { return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to deliver tx"), nil, err } diff --git a/x/quarantine/simulation/operations_test.go b/x/quarantine/simulation/operations_test.go index 10a0ab46e6..963a1c1a26 100644 --- a/x/quarantine/simulation/operations_test.go +++ b/x/quarantine/simulation/operations_test.go @@ -8,6 +8,7 @@ import ( "github.com/stretchr/testify/suite" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" "github.com/cosmos/cosmos-sdk/x/bank/testutil" @@ -48,10 +49,16 @@ func (s *SimTestSuite) SetupTest() { s.ctx = s.app.BaseApp.NewContext(false) } -func (s *SimTestSuite) TestWeightedOperations() { - cdc := s.app.AppCodec() - appParams := make(simtypes.AppParams) +// MakeTestSimState creates a new module.SimulationState struct with the fields needed by the functions being tested. +func (s *SimTestSuite) MakeTestSimState() module.SimulationState { + return module.SimulationState{ + AppParams: make(simtypes.AppParams), + Cdc: s.app.AppCodec(), + TxConfig: s.app.GetTxConfig(), + } +} +func (s *SimTestSuite) TestWeightedOperations() { expected := []struct { weight int opsMsgRoute string @@ -64,10 +71,7 @@ func (s *SimTestSuite) TestWeightedOperations() { {simulation.WeightMsgUpdateAutoResponses, quarantine.ModuleName, simulation.TypeMsgUpdateAutoResponses}, } - weightedOps := simulation.WeightedOperations( - appParams, cdc, - s.app.AccountKeeper, s.app.BankKeeper, s.app.QuarantineKeeper, cdc, - ) + weightedOps := simulation.WeightedOperations(s.MakeTestSimState(), s.app.AccountKeeper, s.app.BankKeeper, s.app.QuarantineKeeper) s.Require().Len(weightedOps, len(expected), "weighted ops") @@ -92,7 +96,7 @@ func (s *SimTestSuite) TestSimulateMsgOptIn() { r := rand.New(rand.NewSource(1)) accounts := s.getTestingAccounts(r, 10) - op := simulation.SimulateMsgOptIn(s.app.AccountKeeper, s.app.BankKeeper) + op := simulation.SimulateMsgOptIn(s.MakeTestSimState(), s.app.AccountKeeper, s.app.BankKeeper) opMsg, futureOps, err := op(r, s.app.BaseApp, s.ctx, accounts, "") s.Require().NoError(err, "running SimulateMsgOptIn op") @@ -113,7 +117,7 @@ func (s *SimTestSuite) TestSimulateMsgOptOut() { err := s.app.QuarantineKeeper.SetOptIn(s.ctx, accounts[0].Address) s.Require().NoError(err, "SetOptIn on accounts[0]") - op := simulation.SimulateMsgOptOut(s.app.AccountKeeper, s.app.BankKeeper, s.app.QuarantineKeeper) + op := simulation.SimulateMsgOptOut(s.MakeTestSimState(), s.app.AccountKeeper, s.app.BankKeeper, s.app.QuarantineKeeper) opMsg, futureOps, err := op(r, s.app.BaseApp, s.ctx, accounts, "") s.Require().NoError(err, "running SimulateMsgOptIn op") @@ -139,7 +143,7 @@ func (s *SimTestSuite) TestSimulateMsgAccept() { err = s.app.BankKeeper.SendCoins(s.ctx, accounts[1].Address, accounts[0].Address, toSend) s.Require().NoError(err, "SendCoins") - op := simulation.SimulateMsgAccept(s.app.AccountKeeper, s.app.BankKeeper, s.app.QuarantineKeeper) + op := simulation.SimulateMsgAccept(s.MakeTestSimState(), s.app.AccountKeeper, s.app.BankKeeper, s.app.QuarantineKeeper) opMsg, futureOps, err := op(r, s.app.BaseApp, s.ctx, accounts, "") s.Require().NoError(err, "running SimulateMsgOptIn op") @@ -165,7 +169,7 @@ func (s *SimTestSuite) TestSimulateMsgDecline() { err = s.app.BankKeeper.SendCoins(s.ctx, accounts[1].Address, accounts[0].Address, toSend) s.Require().NoError(err, "SendCoins") - op := simulation.SimulateMsgDecline(s.app.AccountKeeper, s.app.BankKeeper, s.app.QuarantineKeeper) + op := simulation.SimulateMsgDecline(s.MakeTestSimState(), s.app.AccountKeeper, s.app.BankKeeper, s.app.QuarantineKeeper) opMsg, futureOps, err := op(r, s.app.BaseApp, s.ctx, accounts, "") s.Require().NoError(err, "running SimulateMsgOptIn op") @@ -186,7 +190,7 @@ func (s *SimTestSuite) TestSimulateMsgUpdateAutoResponses() { err := s.app.QuarantineKeeper.SetOptIn(s.ctx, accounts[0].Address) s.Require().NoError(err, "SetOptIn on accounts[0]") - op := simulation.SimulateMsgUpdateAutoResponses(s.app.AccountKeeper, s.app.BankKeeper, s.app.QuarantineKeeper) + op := simulation.SimulateMsgUpdateAutoResponses(s.MakeTestSimState(), s.app.AccountKeeper, s.app.BankKeeper, s.app.QuarantineKeeper) opMsg, futureOps, err := op(r, s.app.BaseApp, s.ctx, accounts, "") s.Require().NoError(err, "running SimulateMsgOptIn op") diff --git a/x/sanction/module/module.go b/x/sanction/module/module.go index d071ca0df0..060c1b2df7 100644 --- a/x/sanction/module/module.go +++ b/x/sanction/module/module.go @@ -167,7 +167,7 @@ func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) { // WeightedOperations returns the all the sanction module operations with their respective weights. func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { return simulation.WeightedOperations( - simState.AppParams, simState.Cdc, codec.NewProtoCodec(am.registry), + simState, codec.NewProtoCodec(am.registry), am.accKeeper, am.bankKeeper, am.govKeeper, am.keeper, ) } diff --git a/x/sanction/simulation/operations.go b/x/sanction/simulation/operations.go index 5cee3b49b2..fac142c5da 100644 --- a/x/sanction/simulation/operations.go +++ b/x/sanction/simulation/operations.go @@ -8,12 +8,12 @@ import ( "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper" govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" "github.com/cosmos/cosmos-sdk/x/simulation" - simappparams "github.com/provenance-io/provenance/app/params" "github.com/provenance-io/provenance/x/sanction" "github.com/provenance-io/provenance/x/sanction/keeper" ) @@ -34,8 +34,7 @@ const ( // WeightedOpsArgs holds all the args provided to WeightedOperations so that they can be passed on later more easily. type WeightedOpsArgs struct { - AppParams simtypes.AppParams - JSONCodec codec.JSONCodec + SimState module.SimulationState ProtoCodec *codec.ProtoCodec AK sanction.AccountKeeper BK sanction.BankKeeper @@ -60,12 +59,11 @@ type SendGovMsgArgs struct { } func WeightedOperations( - appParams simtypes.AppParams, jsonCodec codec.JSONCodec, protoCodec *codec.ProtoCodec, + simState module.SimulationState, protoCodec *codec.ProtoCodec, ak sanction.AccountKeeper, bk sanction.BankKeeper, gk govkeeper.Keeper, sk keeper.Keeper, ) simulation.WeightedOperations { args := &WeightedOpsArgs{ - AppParams: appParams, - JSONCodec: jsonCodec, + SimState: simState, ProtoCodec: protoCodec, AK: ak, BK: bk, @@ -74,30 +72,30 @@ func WeightedOperations( } var ( - weightSanction int - weightSanctionImmediate int - weightUnsanction int - weightUnsanctionImmediate int - weightUpdateParams int + wSanction int + wSanctionImmediate int + wUnsanction int + wUnsanctionImmediate int + wUpdateParams int ) - appParams.GetOrGenerate(OpWeightSanction, &weightSanction, nil, - func(_ *rand.Rand) { weightSanction = DefaultWeightSanction }) - appParams.GetOrGenerate(OpWeightSanctionImmediate, &weightSanctionImmediate, nil, - func(_ *rand.Rand) { weightSanctionImmediate = DefaultWeightSanctionImmediate }) - appParams.GetOrGenerate(OpWeightUnsanction, &weightUnsanction, nil, - func(_ *rand.Rand) { weightUnsanction = DefaultWeightUnsanction }) - appParams.GetOrGenerate(OpWeightUnsanctionImmediate, &weightUnsanctionImmediate, nil, - func(_ *rand.Rand) { weightUnsanctionImmediate = DefaultWeightUnsanctionImmediate }) - appParams.GetOrGenerate(OpWeightUpdateParams, &weightUpdateParams, nil, - func(_ *rand.Rand) { weightUpdateParams = DefaultWeightUpdateParams }) + simState.AppParams.GetOrGenerate(OpWeightSanction, &wSanction, nil, + func(_ *rand.Rand) { wSanction = DefaultWeightSanction }) + simState.AppParams.GetOrGenerate(OpWeightSanctionImmediate, &wSanctionImmediate, nil, + func(_ *rand.Rand) { wSanctionImmediate = DefaultWeightSanctionImmediate }) + simState.AppParams.GetOrGenerate(OpWeightUnsanction, &wUnsanction, nil, + func(_ *rand.Rand) { wUnsanction = DefaultWeightUnsanction }) + simState.AppParams.GetOrGenerate(OpWeightUnsanctionImmediate, &wUnsanctionImmediate, nil, + func(_ *rand.Rand) { wUnsanctionImmediate = DefaultWeightUnsanctionImmediate }) + simState.AppParams.GetOrGenerate(OpWeightUpdateParams, &wUpdateParams, nil, + func(_ *rand.Rand) { wUpdateParams = DefaultWeightUpdateParams }) return simulation.WeightedOperations{ - simulation.NewWeightedOperation(weightSanction, SimulateGovMsgSanction(args)), - simulation.NewWeightedOperation(weightSanctionImmediate, SimulateGovMsgSanctionImmediate(args)), - simulation.NewWeightedOperation(weightUnsanction, SimulateGovMsgUnsanction(args)), - simulation.NewWeightedOperation(weightUnsanctionImmediate, SimulateGovMsgUnsanctionImmediate(args)), - simulation.NewWeightedOperation(weightUpdateParams, SimulateGovMsgUpdateParams(args)), + simulation.NewWeightedOperation(wSanction, SimulateGovMsgSanction(args)), + simulation.NewWeightedOperation(wSanctionImmediate, SimulateGovMsgSanctionImmediate(args)), + simulation.NewWeightedOperation(wUnsanction, SimulateGovMsgUnsanction(args)), + simulation.NewWeightedOperation(wUnsanctionImmediate, SimulateGovMsgUnsanctionImmediate(args)), + simulation.NewWeightedOperation(wUpdateParams, SimulateGovMsgUpdateParams(args)), } } @@ -132,7 +130,7 @@ func SendGovMsg(args *SendGovMsgArgs) (bool, simtypes.OperationMsg, error) { txCtx := simulation.OperationInput{ R: args.R, App: args.App, - TxGen: simappparams.MakeTestEncodingConfig().TxConfig, + TxGen: args.SimState.TxConfig, Cdc: args.ProtoCodec, Msg: govMsg, CoinsSpentInMsg: govMsg.InitialDeposit, @@ -162,7 +160,7 @@ func OperationMsgVote(args *WeightedOpsArgs, voter simtypes.Account, govPropID u txCtx := simulation.OperationInput{ R: r, App: app, - TxGen: simappparams.MakeTestEncodingConfig().TxConfig, + TxGen: args.SimState.TxConfig, Cdc: args.ProtoCodec, Msg: msg, CoinsSpentInMsg: sdk.Coins{}, diff --git a/x/sanction/simulation/operations_test.go b/x/sanction/simulation/operations_test.go index 2f5c78e3c2..7f2194e2d9 100644 --- a/x/sanction/simulation/operations_test.go +++ b/x/sanction/simulation/operations_test.go @@ -15,6 +15,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" bankutil "github.com/cosmos/cosmos-sdk/x/bank/testutil" govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" @@ -227,11 +228,19 @@ func (s *SimTestSuite) requireSanctionAddresses(addrs []sdk.AccAddress) { }) } +// MakeTestSimState creates a new module.SimulationState struct with the fields needed by the functions being tested. +func (s *SimTestSuite) MakeTestSimState() module.SimulationState { + return module.SimulationState{ + AppParams: make(simtypes.AppParams), + Cdc: s.app.AppCodec(), + TxConfig: s.app.GetTxConfig(), + } +} + // getWeightedOpsArgs creates a standard WeightedOpsArgs. func (s *SimTestSuite) getWeightedOpsArgs() simulation.WeightedOpsArgs { return simulation.WeightedOpsArgs{ - AppParams: make(simtypes.AppParams), - JSONCodec: s.app.AppCodec(), + SimState: s.MakeTestSimState(), ProtoCodec: codec.NewProtoCodec(s.app.InterfaceRegistry()), AK: s.app.AccountKeeper, BK: s.app.BankKeeper, @@ -259,7 +268,7 @@ func (s *SimTestSuite) TestWeightedOperations() { } weightedOps := simulation.WeightedOperations( - make(simtypes.AppParams), s.app.AppCodec(), codec.NewProtoCodec(s.app.InterfaceRegistry()), + s.MakeTestSimState(), codec.NewProtoCodec(s.app.InterfaceRegistry()), s.app.AccountKeeper, s.app.BankKeeper, s.app.GovKeeper, s.app.SanctionKeeper, ) diff --git a/x/trigger/module/module.go b/x/trigger/module/module.go index fdd8401471..e56c59ed6e 100644 --- a/x/trigger/module/module.go +++ b/x/trigger/module/module.go @@ -143,9 +143,7 @@ func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) { // WeightedOperations returns simulation operations (i.e msgs) with their respective weight func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { - return simulation.WeightedOperations( - simState.AppParams, simState.Cdc, am.keeper, am.accountKeeper, am.bankKeeper, - ) + return simulation.WeightedOperations(simState, am.keeper, am.accountKeeper, am.bankKeeper) } // Name returns the trigger module's name. diff --git a/x/trigger/simulation/operations.go b/x/trigger/simulation/operations.go index 33c745a702..eeff1f7591 100644 --- a/x/trigger/simulation/operations.go +++ b/x/trigger/simulation/operations.go @@ -7,9 +7,9 @@ import ( sdkmath "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/baseapp" - "github.com/cosmos/cosmos-sdk/codec" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" @@ -32,66 +32,54 @@ const ( // WeightedOperations returns all the operations from the module with their respective weights func WeightedOperations( - appParams simtypes.AppParams, cdc codec.JSONCodec, k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper, + simState module.SimulationState, k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper, ) simulation.WeightedOperations { var ( - weightMsgCreateTrigger int - weightMsgDestroyTrigger int + wMsgCreateTrigger int + wMsgDestroyTrigger int ) - appParams.GetOrGenerate(OpWeightMsgCreateTrigger, &weightMsgCreateTrigger, nil, - func(_ *rand.Rand) { - weightMsgCreateTrigger = simappparams.DefaultWeightSubmitCreateTrigger - }, - ) - appParams.GetOrGenerate(OpWeightMsgDestroyTrigger, &weightMsgDestroyTrigger, nil, - func(_ *rand.Rand) { - weightMsgDestroyTrigger = simappparams.DefaultWeightSubmitDestroyTrigger - }, - ) + simState.AppParams.GetOrGenerate(OpWeightMsgCreateTrigger, &wMsgCreateTrigger, nil, + func(_ *rand.Rand) { wMsgCreateTrigger = simappparams.DefaultWeightSubmitCreateTrigger }) + simState.AppParams.GetOrGenerate(OpWeightMsgDestroyTrigger, &wMsgDestroyTrigger, nil, + func(_ *rand.Rand) { wMsgDestroyTrigger = simappparams.DefaultWeightSubmitDestroyTrigger }) return simulation.WeightedOperations{ - simulation.NewWeightedOperation( - weightMsgCreateTrigger, - SimulateMsgCreateTrigger(k, ak, bk), - ), - simulation.NewWeightedOperation( - weightMsgDestroyTrigger, - SimulateMsgDestroyTrigger(k, ak, bk), - ), + simulation.NewWeightedOperation(wMsgCreateTrigger, SimulateMsgCreateTrigger(simState, k, ak, bk)), + simulation.NewWeightedOperation(wMsgDestroyTrigger, SimulateMsgDestroyTrigger(simState, k, ak, bk)), } } // SimulateMsgCreateTrigger sends a MsgCreateTriggerRequest. -func SimulateMsgCreateTrigger(_ keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper) simtypes.Operation { +func SimulateMsgCreateTrigger(simState module.SimulationState, _ keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { now := ctx.BlockTime() raccs, err := RandomAccs(r, accs, 2) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(&types.MsgCreateTriggerRequest{}), sdk.MsgTypeURL(&types.MsgCreateTriggerRequest{}), err.Error()), nil, nil + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgCreateTriggerRequest{}), err.Error()), nil, nil } from := raccs[0] to := raccs[1] msg, err := types.NewCreateTriggerRequest([]string{from.Address.String()}, NewRandomEvent(r, now), []sdk.Msg{NewRandomAction(r, from.Address.String(), to.Address.String())}) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), "error creating message"), nil, err + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "error creating message"), nil, err } - return Dispatch(r, app, ctx, from, chainID, msg, ak, bk, nil) + return Dispatch(r, app, ctx, simState, from, chainID, msg, ak, bk, nil) } } // SimulateMsgDestroyTrigger sends a MsgDestroyTriggerRequest. -func SimulateMsgDestroyTrigger(k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper) simtypes.Operation { +func SimulateMsgDestroyTrigger(simState module.SimulationState, k keeper.Keeper, ak authkeeper.AccountKeeperI, bk bankkeeper.Keeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { trigger := randomTrigger(r, ctx, k) if trigger == nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(&types.MsgDestroyTriggerRequest{}), sdk.MsgTypeURL(&types.MsgDestroyTriggerRequest{}), "unable to find a valid trigger"), nil, nil + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgDestroyTriggerRequest{}), "unable to find a valid trigger"), nil, nil } addr, err := sdk.AccAddressFromBech32(trigger.Owner) if err != nil { @@ -100,10 +88,10 @@ func SimulateMsgDestroyTrigger(k keeper.Keeper, ak authkeeper.AccountKeeperI, bk } simAccount, found := simtypes.FindAccount(accs, addr) if !found { - return simtypes.NoOpMsg(sdk.MsgTypeURL(&types.MsgDestroyTriggerRequest{}), sdk.MsgTypeURL(&types.MsgDestroyTriggerRequest{}), "creator of trigger does not exist"), nil, nil + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgDestroyTriggerRequest{}), "creator of trigger does not exist"), nil, nil } msg := types.NewDestroyTriggerRequest(trigger.GetOwner(), trigger.GetId()) - return Dispatch(r, app, ctx, simAccount, chainID, msg, ak, bk, nil) + return Dispatch(r, app, ctx, simState, simAccount, chainID, msg, ak, bk, nil) } } @@ -113,6 +101,7 @@ func Dispatch( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, + simState module.SimulationState, from simtypes.Account, chainID string, msg sdk.Msg, @@ -129,19 +118,19 @@ func Dispatch( fees, err := simtypes.RandomFees(r, ctx, spendable) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), "unable to generate fees"), nil, err + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "unable to generate fees"), nil, err } err = testutil.FundAccount(ctx, bk, account.GetAddress(), sdk.NewCoins(sdk.Coin{ Denom: pioconfig.GetProvenanceConfig().BondDenom, Amount: sdkmath.NewInt(1_000_000_000_000_000), })) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), "unable to fund account"), nil, err + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "unable to fund account"), nil, err } - txGen := simappparams.MakeTestEncodingConfig().TxConfig + tx, err := simtestutil.GenSignedMockTx( r, - txGen, + simState.TxConfig, []sdk.Msg{msg}, fees, simtestutil.DefaultGenTxGas, @@ -151,12 +140,12 @@ func Dispatch( from.PrivKey, ) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), "unable to generate mock tx"), nil, err + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "unable to generate mock tx"), nil, err } - _, _, err = app.SimDeliver(txGen.TxEncoder(), tx) + _, _, err = app.SimDeliver(simState.TxConfig.TxEncoder(), tx) if err != nil { - return simtypes.NoOpMsg(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), err.Error()), nil, nil + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), err.Error()), nil, nil } return simtypes.NewOperationMsg(msg, true, ""), futures, nil diff --git a/x/trigger/simulation/operations_test.go b/x/trigger/simulation/operations_test.go index b01f90bc81..0c678dc376 100644 --- a/x/trigger/simulation/operations_test.go +++ b/x/trigger/simulation/operations_test.go @@ -11,6 +11,7 @@ import ( codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" sdktx "github.com/cosmos/cosmos-sdk/types/tx" "github.com/cosmos/cosmos-sdk/x/bank/testutil" @@ -28,6 +29,10 @@ type SimTestSuite struct { app *app.App } +func TestSimTestSuite(t *testing.T) { + suite.Run(t, new(SimTestSuite)) +} + func (s *SimTestSuite) SetupTest() { s.app = app.Setup(s.T()) s.ctx = s.app.BaseApp.NewContext(false) @@ -52,11 +57,17 @@ func (s *SimTestSuite) LogOperationMsg(operationMsg simtypes.OperationMsg, msg s ) } -func (s *SimTestSuite) TestWeightedOperations() { - cdc := s.app.AppCodec() - appParams := make(simtypes.AppParams) +// MakeTestSimState creates a new module.SimulationState struct with the fields needed by the functions being tested. +func (s *SimTestSuite) MakeTestSimState() module.SimulationState { + return module.SimulationState{ + AppParams: make(simtypes.AppParams), + Cdc: s.app.AppCodec(), + TxConfig: s.app.GetTxConfig(), + } +} - weightedOps := simulation.WeightedOperations(appParams, cdc, s.app.TriggerKeeper, +func (s *SimTestSuite) TestWeightedOperations() { + weightedOps := simulation.WeightedOperations(s.MakeTestSimState(), s.app.TriggerKeeper, s.app.AccountKeeper, s.app.BankKeeper, ) @@ -106,8 +117,8 @@ func (s *SimTestSuite) TestSimulateMsgCreateTrigger() { accounts := s.getTestingAccounts(r, 3) // bad operation - op := simulation.SimulateMsgCreateTrigger(s.app.TriggerKeeper, s.app.AccountKeeper, s.app.BankKeeper) - expBadOp := simtypes.NoOpMsg(sdk.MsgTypeURL(&types.MsgCreateTriggerRequest{}), sdk.MsgTypeURL(&types.MsgCreateTriggerRequest{}), "cannot choose 2 accounts because there are only 1") + op := simulation.SimulateMsgCreateTrigger(s.MakeTestSimState(), s.app.TriggerKeeper, s.app.AccountKeeper, s.app.BankKeeper) + expBadOp := simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgCreateTriggerRequest{}), "cannot choose 2 accounts because there are only 1") operationMsg, futureOperations, err := op(r, s.app.BaseApp, s.ctx, accounts[0:1], "") s.LogOperationMsg(operationMsg, "bad SimulateMsgCreateTrigger") s.Assert().Equal(expBadOp, operationMsg, "bad operationMsg") @@ -115,7 +126,7 @@ func (s *SimTestSuite) TestSimulateMsgCreateTrigger() { s.Assert().NoError(err, "bad SimulateMsgCreateTrigger op(...) error") // execute operation - op = simulation.SimulateMsgCreateTrigger(s.app.TriggerKeeper, s.app.AccountKeeper, s.app.BankKeeper) + op = simulation.SimulateMsgCreateTrigger(s.MakeTestSimState(), s.app.TriggerKeeper, s.app.AccountKeeper, s.app.BankKeeper) operationMsg, futureOperations, err = op(r, s.app.BaseApp, s.ctx, accounts, "") s.Require().NoError(err, "SimulateMsgCreateTrigger op(...) error") s.LogOperationMsg(operationMsg, "good") @@ -143,7 +154,7 @@ func (s *SimTestSuite) TestSimulateMsgDestroyTrigger() { s.app.TriggerKeeper.SetGasLimit(s.ctx, trigger.GetId(), 1000) // execute operation - op := simulation.SimulateMsgDestroyTrigger(s.app.TriggerKeeper, s.app.AccountKeeper, s.app.BankKeeper) + op := simulation.SimulateMsgDestroyTrigger(s.MakeTestSimState(), s.app.TriggerKeeper, s.app.AccountKeeper, s.app.BankKeeper) operationMsg, futureOperations, err := op(r, s.app.BaseApp, s.ctx, accounts, "") s.Require().NoError(err, "SimulateMsgDestroyTrigger op(...) error") s.LogOperationMsg(operationMsg, "good") @@ -217,10 +228,6 @@ func (s *SimTestSuite) TestRandomAccs() { } } -func TestSimTestSuite(t *testing.T) { - suite.Run(t, new(SimTestSuite)) -} - func (s *SimTestSuite) getTestingAccounts(r *rand.Rand, n int) []simtypes.Account { accounts := simtypes.RandomAccounts(r, n)