Skip to content

Commit dcb5247

Browse files
feat: Add a function to initialize the ICS27 module via an upgrade proposal (#1037) (#1040)
## Description closes: #1034 --- 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. - [ ] Targeted PR against correct branch (see [CONTRIBUTING.md](https://github.com/cosmos/ibc-go/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] 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 147e0f1) Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>
1 parent 4af1e62 commit dcb5247

File tree

5 files changed

+110
-9
lines changed

5 files changed

+110
-9
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
6464

6565
### Improvements
6666

67+
* (interchain-accounts) [\#1037](https://github.com/cosmos/ibc-go/pull/1037) Add a function `InitModule` to the interchain accounts `AppModule`. This function should be called within the upgrade handler when adding the interchain accounts module to a chain. It should be called in place of InitGenesis (set the consensus version in the version map).
6768
* (testing) [\#942](https://github.com/cosmos/ibc-go/pull/942) `NewTestChain` will create 4 validators in validator set by default. A new constructor function `NewTestChainWithValSet` is provided for test writers who want custom control over the validator set of test chains.
6869
* (testing) [\#904](https://github.com/cosmos/ibc-go/pull/904) Add `ParsePacketFromEvents` function to the testing package. Useful when sending/relaying packets via the testing package.
6970
* (testing) [\#893](https://github.com/cosmos/ibc-go/pull/893) Support custom private keys for testing.

docs/migrations/v2-to-v3.md

+16-9
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,24 @@ Please see the [ICS27 documentation](../apps/interchain-accounts/overview.md) fo
3939
If the chain will adopt ICS27, it must set the appropriate params during the execution of the upgrade handler in `app.go`:
4040
```go
4141
app.UpgradeKeeper.SetUpgradeHandler("v3",
42-
func(ctx sdk.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
43-
// set ICS27 Host submodule params
44-
app.ICAHostKeeper.SetParams(ctx, icahosttypes.Params{
45-
HostEnabled: true,
46-
AllowMessages: []string{"/cosmos.bank.v1beta1.MsgSend", ...],
47-
})
42+
func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
43+
// set the ICS27 consensus version so InitGenesis is not run
44+
fromVM[icatypes.ModuleName] = icamodule.ConsensusVersion()
4845

49-
// set ICS27 Controller submodule params
50-
app.ICAControllerKeeper.SetParams(ctx, icacontrollertypes.Params{
46+
47+
// create ICS27 Controller submodule params
48+
controllerParams := icacontrollertypes.Params{
5149
ControllerEnabled: true,
52-
})
50+
}
51+
52+
// create ICS27 Host submodule params
53+
hostParams := icahosttypes.Params{
54+
HostEnabled: true,
55+
AllowMessages: []string{"/cosmos.bank.v1beta1.MsgSend", ...],
56+
}
57+
58+
// initialize ICS27 module
59+
icamodule.InitModule(ctx, controllerParams, hostParams)
5360

5461
...
5562

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

+13
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
hosttypes "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/host/types"
2525
"github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/types"
2626
porttypes "github.com/cosmos/ibc-go/v3/modules/core/05-port/types"
27+
ibchost "github.com/cosmos/ibc-go/v3/modules/core/24-host"
2728
)
2829

2930
var (
@@ -101,6 +102,18 @@ func NewAppModule(controllerKeeper *controllerkeeper.Keeper, hostKeeper *hostkee
101102
}
102103
}
103104

105+
// InitModule will initialize the interchain accounts moudule. It should only be
106+
// called once and as an alternative to InitGenesis.
107+
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)
110+
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))
114+
}
115+
}
116+
104117
// RegisterInvariants implements the AppModule interface
105118
func (AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
106119
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package ica_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/suite"
7+
"github.com/tendermint/tendermint/libs/log"
8+
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
9+
dbm "github.com/tendermint/tm-db"
10+
11+
ica "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts"
12+
controllertypes "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/controller/types"
13+
hosttypes "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/host/types"
14+
"github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/types"
15+
ibctesting "github.com/cosmos/ibc-go/v3/testing"
16+
"github.com/cosmos/ibc-go/v3/testing/simapp"
17+
)
18+
19+
type InterchainAccountsTestSuite struct {
20+
suite.Suite
21+
22+
coordinator *ibctesting.Coordinator
23+
}
24+
25+
func TestICATestSuite(t *testing.T) {
26+
suite.Run(t, new(InterchainAccountsTestSuite))
27+
}
28+
29+
func (suite *InterchainAccountsTestSuite) SetupTest() {
30+
suite.coordinator = ibctesting.NewCoordinator(suite.T(), 2)
31+
}
32+
33+
func (suite *InterchainAccountsTestSuite) TestInitModule() {
34+
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+
suite.Require().True(ok)
37+
38+
header := tmproto.Header{
39+
ChainID: "testchain",
40+
Height: 1,
41+
Time: suite.coordinator.CurrentTime.UTC(),
42+
}
43+
44+
ctx := app.GetBaseApp().NewContext(true, header)
45+
46+
// ensure params are not set
47+
suite.Require().Panics(func() {
48+
app.ICAControllerKeeper.GetParams(ctx)
49+
})
50+
suite.Require().Panics(func() {
51+
app.ICAHostKeeper.GetParams(ctx)
52+
})
53+
54+
controllerParams := controllertypes.DefaultParams()
55+
controllerParams.ControllerEnabled = true
56+
57+
hostParams := hosttypes.DefaultParams()
58+
expAllowMessages := []string{"sdk.Msg"}
59+
hostParams.HostEnabled = true
60+
hostParams.AllowMessages = expAllowMessages
61+
62+
suite.Require().False(app.IBCKeeper.PortKeeper.IsBound(ctx, types.PortID))
63+
64+
icamodule.InitModule(ctx, controllerParams, hostParams)
65+
66+
controllerParams = app.ICAControllerKeeper.GetParams(ctx)
67+
suite.Require().True(controllerParams.ControllerEnabled)
68+
69+
hostParams = app.ICAHostKeeper.GetParams(ctx)
70+
suite.Require().True(hostParams.HostEnabled)
71+
suite.Require().Equal(expAllowMessages, hostParams.AllowMessages)
72+
73+
suite.Require().True(app.IBCKeeper.PortKeeper.IsBound(ctx, types.PortID))
74+
}

testing/simapp/app.go

+6
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,12 @@ func (app *SimApp) ModuleAccountAddrs() map[string]bool {
571571
return modAccAddrs
572572
}
573573

574+
// GetModuleManager returns the app module manager
575+
// NOTE: used for testing purposes
576+
func (app *SimApp) GetModuleManager() *module.Manager {
577+
return app.mm
578+
}
579+
574580
// LegacyAmino returns SimApp's amino codec.
575581
//
576582
// NOTE: This is solely to be used for testing purposes as it may be desirable

0 commit comments

Comments
 (0)