Skip to content

Commit

Permalink
Add Weighted Operations to simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
pinosu committed Apr 28, 2022
1 parent 8ca55b7 commit e11bfe7
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ func NewWasmApp(
distr.NewAppModule(appCodec, app.distrKeeper, app.accountKeeper, app.bankKeeper, app.stakingKeeper),
staking.NewAppModule(appCodec, app.stakingKeeper, app.accountKeeper, app.bankKeeper),
upgrade.NewAppModule(app.upgradeKeeper),
wasm.NewAppModule(appCodec, &app.wasmKeeper, app.stakingKeeper),
wasm.NewAppModule(appCodec, &app.wasmKeeper, app.stakingKeeper, app.accountKeeper),
evidence.NewAppModule(app.evidenceKeeper),
feegrantmodule.NewAppModule(appCodec, app.accountKeeper, app.bankKeeper, app.FeeGrantKeeper, app.interfaceRegistry),
authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.accountKeeper, app.bankKeeper, app.interfaceRegistry),
Expand Down Expand Up @@ -632,7 +632,7 @@ func NewWasmApp(
slashing.NewAppModule(appCodec, app.slashingKeeper, app.accountKeeper, app.bankKeeper, app.stakingKeeper),
params.NewAppModule(app.paramsKeeper),
evidence.NewAppModule(app.evidenceKeeper),
wasm.NewAppModule(appCodec, &app.wasmKeeper, app.stakingKeeper),
wasm.NewAppModule(appCodec, &app.wasmKeeper, app.stakingKeeper, app.accountKeeper),
ibc.NewAppModule(app.ibcKeeper),
transferModule,
)
Expand Down
1 change: 1 addition & 0 deletions app/params/weights.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ const (
DefaultWeightCommunitySpendProposal int = 5
DefaultWeightTextProposal int = 5
DefaultWeightParamChangeProposal int = 5
DefaultWeightMsgStoreCode int = 100
)
6 changes: 4 additions & 2 deletions x/wasm/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ type AppModule struct {
cdc codec.Codec
keeper *Keeper
validatorSetSource keeper.ValidatorSetSource
accountKeeper types.AccountKeeper // for simulation
}

// ConsensusVersion is a sequence number for state-breaking change of the
Expand All @@ -112,12 +113,13 @@ type AppModule struct {
func (AppModule) ConsensusVersion() uint64 { return 1 }

// NewAppModule creates a new AppModule object
func NewAppModule(cdc codec.Codec, keeper *Keeper, validatorSetSource keeper.ValidatorSetSource) AppModule {
func NewAppModule(cdc codec.Codec, keeper *Keeper, validatorSetSource keeper.ValidatorSetSource, ak types.AccountKeeper) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{},
cdc: cdc,
keeper: keeper,
validatorSetSource: validatorSetSource,
accountKeeper: ak,
}
}

Expand Down Expand Up @@ -196,7 +198,7 @@ func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {

// WeightedOperations returns the all the gov module operations with their respective weights.
func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
return nil
return simulation.WeightedOperations(&simState, am.accountKeeper)
}

// ____________________________________________________________________________
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func setupTest(t *testing.T) testData {
ctx, keepers := CreateTestInput(t, false, "iterator,staking,stargate")
cdc := keeper.MakeTestCodec(t)
data := testData{
module: NewAppModule(cdc, keepers.WasmKeeper, keepers.StakingKeeper),
module: NewAppModule(cdc, keepers.WasmKeeper, keepers.StakingKeeper, nil),
ctx: ctx,
acctKeeper: keepers.AccountKeeper,
keeper: *keepers.WasmKeeper,
Expand Down
82 changes: 82 additions & 0 deletions x/wasm/simulation/operations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package simulation

import (
_ "embed"
"math/rand"

"github.com/CosmWasm/wasmd/app/params"
"github.com/cosmos/cosmos-sdk/types/module"

"github.com/CosmWasm/wasmd/x/wasm/keeper"
"github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/cosmos/cosmos-sdk/baseapp"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
)

//go:embed testdata/reflect.wasm
var reflectContract []byte

// Simulation operation weights constants
//nolint:gosec
const (
OpWeightMsgStoreCode = "op_weight_msg_store_code"
)

// WeightedOperations returns all the operations from the module with their respective weights
func WeightedOperations(
simstate *module.SimulationState, ak types.AccountKeeper) simulation.WeightedOperations {
var (
weightMsgStoreCode int
)

simstate.AppParams.GetOrGenerate(simstate.Cdc, OpWeightMsgStoreCode, &weightMsgStoreCode, nil,
func(_ *rand.Rand) {
weightMsgStoreCode = params.DefaultWeightMsgStoreCode
},
)

return simulation.WeightedOperations{
simulation.NewWeightedOperation(
weightMsgStoreCode,
SimulateMsgStoreCode(ak),
),
}
}

// SimulateMsgStoreCode generates a MsgStoreCode with random values
func SimulateMsgStoreCode(ak types.AccountKeeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {

simAccount, _ := simtypes.RandomAcc(r, accs)

config := &types.AccessConfig{
Permission: 3,
Address: keeper.RandomBech32AccountAddress(nil),
}

msg := types.MsgStoreCode{
Sender: keeper.RandomBech32AccountAddress(nil),
WASMByteCode: reflectContract,
InstantiatePermission: config,
}

txCtx := simulation.OperationInput{
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Cdc: nil,
Msg: &msg,
MsgType: msg.Type(),
Context: ctx,
SimAccount: simAccount,
AccountKeeper: ak,
ModuleName: types.ModuleName,
}

return simulation.GenAndDeliverTxWithRandFees(txCtx)
}
}
Binary file added x/wasm/simulation/testdata/reflect.wasm
Binary file not shown.

0 comments on commit e11bfe7

Please sign in to comment.