Skip to content

Commit

Permalink
Use SimulationState fields for the encoders needed in the simulations. (
Browse files Browse the repository at this point in the history
#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.
  • Loading branch information
SpicyLemon authored Apr 16, 2024
1 parent f6fb2ca commit bca9164
Show file tree
Hide file tree
Showing 27 changed files with 464 additions and 603 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
46 changes: 0 additions & 46 deletions app/params/proto.go

This file was deleted.

64 changes: 31 additions & 33 deletions internal/provwasm/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}
}
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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)
}
Expand Down
5 changes: 2 additions & 3 deletions x/attribute/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
Loading

0 comments on commit bca9164

Please sign in to comment.