Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add params for ics27 simulation (backport #2160) #2214

Merged
merged 3 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions modules/apps/27-interchain-accounts/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"math/rand"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -29,8 +30,9 @@ import (
)

var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.AppModuleSimulation = AppModule{}

_ porttypes.IBCModule = host.IBCModule{}
)
Expand Down Expand Up @@ -224,6 +226,11 @@ func (am AppModule) WeightedOperations(_ module.SimulationState) []simtypes.Weig
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)
}

// RegisterStoreDecoder registers a decoder for interchain accounts module's types
func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
sdr[types.StoreKey] = simulation.NewDecodeStore()
Expand Down
41 changes: 41 additions & 0 deletions modules/apps/27-interchain-accounts/simulation/params.go
Original file line number Diff line number Diff line change
@@ -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
}
64 changes: 64 additions & 0 deletions modules/apps/27-interchain-accounts/simulation/params_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
}