-
Notifications
You must be signed in to change notification settings - Fork 625
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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/<module>/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 eb508b5) # Conflicts: # CHANGELOG.md # modules/apps/27-interchain-accounts/module.go
- Loading branch information
1 parent
80b4e70
commit 70d734e
Showing
4 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
56 changes: 56 additions & 0 deletions
56
modules/apps/27-interchain-accounts/simulation/genesis_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |