From 70d89c7cd43f19d5966c5f282b051579a2a423f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?colin=20axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Wed, 7 Sep 2022 13:15:49 +0200 Subject: [PATCH 1/2] feat: add params for ics27 simulation (#2160) * add param changes for ics27 simulation * account for unadded submodules * fix tests * Update modules/apps/27-interchain-accounts/module.go * add app simulation declaration (cherry picked from commit cd913b8fab4017e7dd7953904faba23aaba13837) # Conflicts: # modules/apps/27-interchain-accounts/module.go --- modules/apps/27-interchain-accounts/module.go | 29 ++++++++- .../simulation/params.go | 41 ++++++++++++ .../simulation/params_test.go | 64 +++++++++++++++++++ 3 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 modules/apps/27-interchain-accounts/simulation/params.go create mode 100644 modules/apps/27-interchain-accounts/simulation/params_test.go diff --git a/modules/apps/27-interchain-accounts/module.go b/modules/apps/27-interchain-accounts/module.go index fb33eccd500..7cfd371a886 100644 --- a/modules/apps/27-interchain-accounts/module.go +++ b/modules/apps/27-interchain-accounts/module.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "math/rand" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" @@ -28,8 +29,9 @@ import ( ) var ( - _ module.AppModule = AppModule{} - _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModuleSimulation = AppModule{} _ porttypes.IBCModule = host.IBCModule{} ) @@ -203,6 +205,29 @@ func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.V // AppModuleSimulation functions +<<<<<<< HEAD +======= +// GenerateGenesisState creates a randomized GenState of the ics27 module. +func (AppModule) GenerateGenesisState(simState *module.SimulationState) { + simulation.RandomizedGenState(simState) +} + +// ProposalContents doesn't return any content functions for governance proposals. +func (AppModule) ProposalContents(_ module.SimulationState) []simtypes.WeightedProposalContent { + return nil +} + +// WeightedOperations is unimplemented. +func (am AppModule) WeightedOperations(_ module.SimulationState) []simtypes.WeightedOperation { + return nil +} + +// RandomizedParams creates randomized ibc-transfer param changes for the simulator. +func (am AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange { + return simulation.ParamChanges(r, am.controllerKeeper, am.hostKeeper) +} + +>>>>>>> cd913b8 (feat: add params for ics27 simulation (#2160)) // RegisterStoreDecoder registers a decoder for interchain accounts module's types func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { sdr[types.StoreKey] = simulation.NewDecodeStore() diff --git a/modules/apps/27-interchain-accounts/simulation/params.go b/modules/apps/27-interchain-accounts/simulation/params.go new file mode 100644 index 00000000000..7edbbf749a2 --- /dev/null +++ b/modules/apps/27-interchain-accounts/simulation/params.go @@ -0,0 +1,41 @@ +package simulation + +import ( + "fmt" + "math/rand" + + simtypes "github.com/cosmos/cosmos-sdk/types/simulation" + "github.com/cosmos/cosmos-sdk/x/simulation" + gogotypes "github.com/gogo/protobuf/types" + + controllerkeeper "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/controller/keeper" + controllertypes "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/controller/types" + hostkeeper "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/host/keeper" + hosttypes "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/host/types" + "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/types" +) + +// ParamChanges defines the parameters that can be modified by param change proposals +// on the simulation +func ParamChanges(r *rand.Rand, controllerKeeper *controllerkeeper.Keeper, hostKeeper *hostkeeper.Keeper) []simtypes.ParamChange { + var paramChanges []simtypes.ParamChange + if controllerKeeper != nil { + paramChanges = append(paramChanges, simulation.NewSimParamChange(controllertypes.SubModuleName, string(controllertypes.KeyControllerEnabled), + func(r *rand.Rand) string { + controllerEnabled := RandomEnabled(r) + return fmt.Sprintf("%s", types.ModuleCdc.MustMarshalJSON(&gogotypes.BoolValue{Value: controllerEnabled})) //nolint:gosimple + }, + )) + } + + if hostKeeper != nil { + paramChanges = append(paramChanges, simulation.NewSimParamChange(hosttypes.SubModuleName, string(hosttypes.KeyHostEnabled), + func(r *rand.Rand) string { + receiveEnabled := RandomEnabled(r) + return fmt.Sprintf("%s", types.ModuleCdc.MustMarshalJSON(&gogotypes.BoolValue{Value: receiveEnabled})) //nolint:gosimple + }, + )) + } + + return paramChanges +} diff --git a/modules/apps/27-interchain-accounts/simulation/params_test.go b/modules/apps/27-interchain-accounts/simulation/params_test.go new file mode 100644 index 00000000000..c9cc5e2a356 --- /dev/null +++ b/modules/apps/27-interchain-accounts/simulation/params_test.go @@ -0,0 +1,64 @@ +package simulation_test + +import ( + "fmt" + "math/rand" + "testing" + + "github.com/stretchr/testify/require" + + controllertypes "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/controller/types" + hosttypes "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/host/types" + "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/simulation" + "github.com/cosmos/ibc-go/v5/testing/simapp" +) + +func TestParamChanges(t *testing.T) { + app := simapp.Setup(false) + + s := rand.NewSource(1) + r := rand.New(s) + + expected := []struct { + composedKey string + key string + simValue string + subspace string + }{ + {fmt.Sprintf("%s/%s", controllertypes.SubModuleName, controllertypes.KeyControllerEnabled), string(controllertypes.KeyControllerEnabled), "false", controllertypes.SubModuleName}, + {fmt.Sprintf("%s/%s", hosttypes.SubModuleName, hosttypes.KeyHostEnabled), string(hosttypes.KeyHostEnabled), "true", hosttypes.SubModuleName}, + } + + paramChanges := simulation.ParamChanges(r, &app.ICAControllerKeeper, &app.ICAHostKeeper) + require.Len(t, paramChanges, 2) + + for i, p := range paramChanges { + require.Equal(t, expected[i].composedKey, p.ComposedKey()) + require.Equal(t, expected[i].key, p.Key()) + require.Equal(t, expected[i].simValue, p.SimValue()(r), p.Key()) + require.Equal(t, expected[i].subspace, p.Subspace()) + } + + paramChanges = simulation.ParamChanges(r, &app.ICAControllerKeeper, nil) + require.Len(t, paramChanges, 1) + + // the second call to paramChanges causing the controller enabled to be changed to true + expected[0].simValue = "true" + + for _, p := range paramChanges { + require.Equal(t, expected[0].composedKey, p.ComposedKey()) + require.Equal(t, expected[0].key, p.Key()) + require.Equal(t, expected[0].simValue, p.SimValue()(r), p.Key()) + require.Equal(t, expected[0].subspace, p.Subspace()) + } + + paramChanges = simulation.ParamChanges(r, nil, &app.ICAHostKeeper) + require.Len(t, paramChanges, 1) + + for _, p := range paramChanges { + require.Equal(t, expected[1].composedKey, p.ComposedKey()) + require.Equal(t, expected[1].key, p.Key()) + require.Equal(t, expected[1].simValue, p.SimValue()(r), p.Key()) + require.Equal(t, expected[1].subspace, p.Subspace()) + } +} From 72939e6ff7318f3a2ee16eea9428c7c66465339c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Colin=20Axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Thu, 8 Sep 2022 16:25:37 +0200 Subject: [PATCH 2/2] fix conflicts --- modules/apps/27-interchain-accounts/module.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/modules/apps/27-interchain-accounts/module.go b/modules/apps/27-interchain-accounts/module.go index 7cfd371a886..612cc7ea935 100644 --- a/modules/apps/27-interchain-accounts/module.go +++ b/modules/apps/27-interchain-accounts/module.go @@ -205,8 +205,6 @@ func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.V // AppModuleSimulation functions -<<<<<<< HEAD -======= // GenerateGenesisState creates a randomized GenState of the ics27 module. func (AppModule) GenerateGenesisState(simState *module.SimulationState) { simulation.RandomizedGenState(simState) @@ -227,7 +225,6 @@ func (am AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange { return simulation.ParamChanges(r, am.controllerKeeper, am.hostKeeper) } ->>>>>>> cd913b8 (feat: add params for ics27 simulation (#2160)) // RegisterStoreDecoder registers a decoder for interchain accounts module's types func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { sdr[types.StoreKey] = simulation.NewDecodeStore()