From d3f4b54d0812b238e97d88cae945e5aeca6c18f9 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 8 Sep 2022 12:37:55 +0200 Subject: [PATCH] feat: add genesis simulation generation for ics27 (backport #2154) (#2169) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add genesis simulation generation for ics27 (#2154) ## Description ref: #1352, #2151 --- Before we can merge this PR, please make sure that all the following items have been checked off. If any of the checklist items are not applicable, please leave them but write a little note why. - [x] Targeted PR against correct branch (see [CONTRIBUTING.md](https://github.com/cosmos/ibc-go/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] Linked to Github issue with discussion and accepted design OR link to spec that describes this work. - [ ] Code follows the [module structure standards](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules/structure.md). - [ ] Wrote unit and integration [tests](https://github.com/cosmos/ibc-go/blob/master/CONTRIBUTING.md#testing) - [ ] Updated relevant documentation (`docs/`) or specification (`x//spec/`) - [ ] Added relevant `godoc` [comments](https://blog.golang.org/godoc-documenting-go-code). - [ ] Added a relevant changelog entry to the `Unreleased` section in `CHANGELOG.md` - [ ] Re-reviewed `Files changed` in the Github PR explorer - [ ] Review `Codecov Report` in the comment section below once CI passes (cherry picked from commit eb508b5c8df7667314ee8a11c7083dff0165c2d7) # Conflicts: # CHANGELOG.md # modules/apps/27-interchain-accounts/module.go * fix conflicts Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> --- CHANGELOG.md | 1 + modules/apps/27-interchain-accounts/module.go | 16 +++++ .../simulation/genesis.go | 70 +++++++++++++++++++ .../simulation/genesis_test.go | 56 +++++++++++++++ 4 files changed, 143 insertions(+) create mode 100644 modules/apps/27-interchain-accounts/simulation/genesis.go create mode 100644 modules/apps/27-interchain-accounts/simulation/genesis_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index e8cb6c58f47..f2437f49510 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements +* (27-interchain-accounts) [\#1352](https://github.com/cosmos/ibc-go/issues/1352) Add support for Cosmos-SDK simulation to ics27 module. * (apps/27-interchain-accounts) [\#2134](https://github.com/cosmos/ibc-go/pull/2134) Adding upgrade handler to ICS27 `controller` submodule for migration of channel capabilities. This upgrade handler migrates ownership of channel capabilities from the underlying application to the ICS27 `controller` submodule. * (apps/27-interchain-accounts) [\#2102](https://github.com/cosmos/ibc-go/pull/2102) ICS27 controller middleware now supports a nil underlying application. This allows chains to make use of interchain accounts with existing auth mechanisms such as x/group and x/gov. * (linting) [\#1418](https://github.com/cosmos/ibc-go/pull/1418) Fix linting errors, resulting compatiblity with go1.18 linting style, golangci-lint 1.46.2 and the revivie linter. This caused breaking changes in core/04-channel, core/ante, and the testing library. diff --git a/modules/apps/27-interchain-accounts/module.go b/modules/apps/27-interchain-accounts/module.go index 029e128add5..e5b221f4d48 100644 --- a/modules/apps/27-interchain-accounts/module.go +++ b/modules/apps/27-interchain-accounts/module.go @@ -10,6 +10,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" "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" abci "github.com/tendermint/tendermint/abci/types" @@ -208,6 +209,21 @@ func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.V // AppModuleSimulation functions +// 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 +} + // 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/genesis.go b/modules/apps/27-interchain-accounts/simulation/genesis.go new file mode 100644 index 00000000000..0670e374d4e --- /dev/null +++ b/modules/apps/27-interchain-accounts/simulation/genesis.go @@ -0,0 +1,70 @@ +package simulation + +import ( + "encoding/json" + "fmt" + "math/rand" + + "github.com/cosmos/cosmos-sdk/types/module" + + controllertypes "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/controller/types" + genesistypes "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/genesis/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/types" +) + +// RandomEnabled randomized controller or host enabled param with 75% prob of being true. +func RandomEnabled(r *rand.Rand) bool { + return r.Int63n(101) <= 75 +} + +// RandomizedGenState generates a random GenesisState for ics27. +// Only the params are non nil +func RandomizedGenState(simState *module.SimulationState) { + var controllerEnabled bool + simState.AppParams.GetOrGenerate( + simState.Cdc, string(controllertypes.KeyControllerEnabled), &controllerEnabled, simState.Rand, + func(r *rand.Rand) { controllerEnabled = RandomEnabled(r) }, + ) + + controllerParams := controllertypes.Params{ + ControllerEnabled: controllerEnabled, + } + + controllerGenesisState := genesistypes.ControllerGenesisState{ + ActiveChannels: nil, + InterchainAccounts: nil, + Ports: []string{}, + Params: controllerParams, + } + + var hostEnabled bool + simState.AppParams.GetOrGenerate( + simState.Cdc, string(hosttypes.KeyHostEnabled), &hostEnabled, simState.Rand, + func(r *rand.Rand) { hostEnabled = RandomEnabled(r) }, + ) + + hostParams := hosttypes.Params{ + HostEnabled: hostEnabled, + AllowMessages: []string{"*"}, // allow all messages + } + + hostGenesisState := genesistypes.HostGenesisState{ + ActiveChannels: nil, + InterchainAccounts: nil, + Port: types.PortID, + Params: hostParams, + } + + icaGenesis := genesistypes.GenesisState{ + ControllerGenesisState: controllerGenesisState, + HostGenesisState: hostGenesisState, + } + + bz, err := json.MarshalIndent(&icaGenesis, "", " ") + if err != nil { + panic(err) + } + fmt.Printf("Selected randomly generated %s parameters:\n%s\n", types.ModuleName, bz) + simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(&icaGenesis) +} diff --git a/modules/apps/27-interchain-accounts/simulation/genesis_test.go b/modules/apps/27-interchain-accounts/simulation/genesis_test.go new file mode 100644 index 00000000000..7382ae71eac --- /dev/null +++ b/modules/apps/27-interchain-accounts/simulation/genesis_test.go @@ -0,0 +1,56 @@ +package simulation_test + +import ( + "encoding/json" + "math/rand" + "testing" + + "cosmossdk.io/math" + "github.com/cosmos/cosmos-sdk/codec" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" + "github.com/cosmos/cosmos-sdk/types/module" + simtypes "github.com/cosmos/cosmos-sdk/types/simulation" + "github.com/stretchr/testify/require" + + genesistypes "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/genesis/types" + "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/simulation" + "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/types" +) + +// TestRandomizedGenState tests the normal scenario of applying RandomizedGenState. +// Abonormal scenarios are not tested here. +func TestRandomizedGenState(t *testing.T) { + interfaceRegistry := codectypes.NewInterfaceRegistry() + cryptocodec.RegisterInterfaces(interfaceRegistry) + cdc := codec.NewProtoCodec(interfaceRegistry) + + s := rand.NewSource(1) // 1 is the seed + r := rand.New(s) + + simState := module.SimulationState{ + AppParams: make(simtypes.AppParams), + Cdc: cdc, + Rand: r, + NumBonded: 3, + Accounts: simtypes.RandomAccounts(r, 3), + InitialStake: math.NewInt(1000), + GenState: make(map[string]json.RawMessage), + } + + simulation.RandomizedGenState(&simState) + + var icaGenesis genesistypes.GenesisState + simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &icaGenesis) + + require.True(t, icaGenesis.ControllerGenesisState.Params.ControllerEnabled) + require.Empty(t, icaGenesis.ControllerGenesisState.ActiveChannels) + require.Empty(t, icaGenesis.ControllerGenesisState.InterchainAccounts) + require.Empty(t, icaGenesis.ControllerGenesisState.Ports) + + require.True(t, icaGenesis.HostGenesisState.Params.HostEnabled) + require.Equal(t, []string{"*"}, icaGenesis.HostGenesisState.Params.AllowMessages) + require.Equal(t, types.PortID, icaGenesis.HostGenesisState.Port) + require.Empty(t, icaGenesis.ControllerGenesisState.ActiveChannels) + require.Empty(t, icaGenesis.ControllerGenesisState.InterchainAccounts) +}