Skip to content

Commit 0a81a52

Browse files
authored
fix: adjust InitModule to account for empty controller and host keepers (#1120)
## Description closes: #XXXX --- 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. - [x] Code follows the [module structure standards](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules/structure.md). - [x] Wrote unit and integration [tests](https://github.com/cosmos/ibc-go/blob/master/CONTRIBUTING.md#testing) - [x] Updated relevant documentation (`docs/`) or specification (`x/<module>/spec/`) - [x] Added relevant `godoc` [comments](https://blog.golang.org/godoc-documenting-go-code). - [x] Added a relevant changelog entry to the `Unreleased` section in `CHANGELOG.md` - [x] Re-reviewed `Files changed` in the Github PR explorer - [x] Review `Codecov Report` in the comment section below once CI passes
1 parent e0ae0cd commit 0a81a52

File tree

3 files changed

+82
-16
lines changed

3 files changed

+82
-16
lines changed

docs/migrations/v2-to-v3.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ app.UpgradeKeeper.SetUpgradeHandler("v3",
5858

5959
```
6060

61-
The host and controller submodule params only need to be set if you integrate those submodules.
62-
For example, if a chain chooses not to integrate a controller submodule, it does not need to set the controller params.
61+
The host and controller submodule params only need to be set if the chain integrates those submodules.
62+
For example, if a chain chooses not to integrate a controller submodule, it may pass empty params into `InitModule`.
6363

6464
#### Add `StoreUpgrades` for ICS27 module
6565

@@ -76,6 +76,9 @@ if upgradeInfo.Name == "v3" && !app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Heigh
7676
```
7777

7878
This ensures that the new module's stores are added to the multistore before the migrations begin.
79+
The host and controller submodule keys only need to be added if the chain integrates those submodules.
80+
For example, if a chain chooses not to integrate a controller submodule, it does not need to add the controller key to the `Added` field.
81+
7982

8083
### Genesis migrations
8184

modules/apps/27-interchain-accounts/module.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,17 @@ func NewAppModule(controllerKeeper *controllerkeeper.Keeper, hostKeeper *hostkee
105105
// InitModule will initialize the interchain accounts moudule. It should only be
106106
// called once and as an alternative to InitGenesis.
107107
func (am AppModule) InitModule(ctx sdk.Context, controllerParams controllertypes.Params, hostParams hosttypes.Params) {
108-
am.controllerKeeper.SetParams(ctx, controllerParams)
109-
am.hostKeeper.SetParams(ctx, hostParams)
108+
if am.controllerKeeper != nil {
109+
am.controllerKeeper.SetParams(ctx, controllerParams)
110+
}
111+
112+
if am.hostKeeper != nil {
113+
am.hostKeeper.SetParams(ctx, hostParams)
110114

111-
cap := am.hostKeeper.BindPort(ctx, types.PortID)
112-
if err := am.hostKeeper.ClaimCapability(ctx, cap, ibchost.PortPath(types.PortID)); err != nil {
113-
panic(fmt.Sprintf("could not claim port capability: %v", err))
115+
cap := am.hostKeeper.BindPort(ctx, types.PortID)
116+
if err := am.hostKeeper.ClaimCapability(ctx, cap, ibchost.PortPath(types.PortID)); err != nil {
117+
panic(fmt.Sprintf("could not claim port capability: %v", err))
118+
}
114119
}
115120
}
116121

modules/apps/27-interchain-accounts/module_test.go

+67-9
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ func (suite *InterchainAccountsTestSuite) SetupTest() {
3131
}
3232

3333
func (suite *InterchainAccountsTestSuite) TestInitModule() {
34+
// setup and basic testing
3435
app := simapp.NewSimApp(log.NewNopLogger(), dbm.NewMemDB(), nil, true, map[int64]bool{}, simapp.DefaultNodeHome, 5, simapp.MakeTestEncodingConfig(), simapp.EmptyAppOptions{})
35-
icamodule, ok := app.GetModuleManager().Modules[types.ModuleName].(ica.AppModule)
36+
appModule, ok := app.GetModuleManager().Modules[types.ModuleName].(ica.AppModule)
3637
suite.Require().True(ok)
3738

3839
header := tmproto.Header{
@@ -58,17 +59,74 @@ func (suite *InterchainAccountsTestSuite) TestInitModule() {
5859
expAllowMessages := []string{"sdk.Msg"}
5960
hostParams.HostEnabled = true
6061
hostParams.AllowMessages = expAllowMessages
61-
6262
suite.Require().False(app.IBCKeeper.PortKeeper.IsBound(ctx, types.PortID))
6363

64-
icamodule.InitModule(ctx, controllerParams, hostParams)
64+
testCases := []struct {
65+
name string
66+
malleate func()
67+
expControllerPass bool
68+
expHostPass bool
69+
}{
70+
{
71+
"both controller and host set", func() {
72+
var ok bool
73+
appModule, ok = app.GetModuleManager().Modules[types.ModuleName].(ica.AppModule)
74+
suite.Require().True(ok)
75+
}, true, true,
76+
},
77+
{
78+
"neither controller or host is set", func() {
79+
appModule = ica.NewAppModule(nil, nil)
80+
}, false, false,
81+
},
82+
{
83+
"only controller is set", func() {
84+
appModule = ica.NewAppModule(&app.ICAControllerKeeper, nil)
85+
}, true, false,
86+
},
87+
{
88+
"only host is set", func() {
89+
appModule = ica.NewAppModule(nil, &app.ICAHostKeeper)
90+
}, false, true,
91+
},
92+
}
93+
94+
for _, tc := range testCases {
95+
tc := tc
96+
97+
suite.Run(tc.name, func() {
98+
suite.SetupTest() // reset
99+
100+
// reset app state
101+
app = simapp.NewSimApp(log.NewNopLogger(), dbm.NewMemDB(), nil, true, map[int64]bool{}, simapp.DefaultNodeHome, 5, simapp.MakeTestEncodingConfig(), simapp.EmptyAppOptions{})
102+
header := tmproto.Header{
103+
ChainID: "testchain",
104+
Height: 1,
105+
Time: suite.coordinator.CurrentTime.UTC(),
106+
}
107+
108+
ctx := app.GetBaseApp().NewContext(true, header)
65109

66-
controllerParams = app.ICAControllerKeeper.GetParams(ctx)
67-
suite.Require().True(controllerParams.ControllerEnabled)
110+
tc.malleate()
68111

69-
hostParams = app.ICAHostKeeper.GetParams(ctx)
70-
suite.Require().True(hostParams.HostEnabled)
71-
suite.Require().Equal(expAllowMessages, hostParams.AllowMessages)
112+
suite.Require().NotPanics(func() {
113+
appModule.InitModule(ctx, controllerParams, hostParams)
114+
})
115+
116+
if tc.expControllerPass {
117+
controllerParams = app.ICAControllerKeeper.GetParams(ctx)
118+
suite.Require().True(controllerParams.ControllerEnabled)
119+
}
120+
121+
if tc.expHostPass {
122+
hostParams = app.ICAHostKeeper.GetParams(ctx)
123+
suite.Require().True(hostParams.HostEnabled)
124+
suite.Require().Equal(expAllowMessages, hostParams.AllowMessages)
125+
126+
suite.Require().True(app.IBCKeeper.PortKeeper.IsBound(ctx, types.PortID))
127+
}
128+
129+
})
130+
}
72131

73-
suite.Require().True(app.IBCKeeper.PortKeeper.IsBound(ctx, types.PortID))
74132
}

0 commit comments

Comments
 (0)