Skip to content

Commit

Permalink
Move cast to Upgradable module after checking app is set. (#5465)
Browse files Browse the repository at this point in the history
* Move cast to Upgradable module _after_ checking app is set.

* Add test to check nil apps are allowed.
  • Loading branch information
DimitrisJim committed Dec 20, 2023
1 parent 5bad8f2 commit ef9a479
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
20 changes: 10 additions & 10 deletions modules/apps/27-interchain-accounts/controller/ibc_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,6 @@ func (im IBCMiddleware) OnTimeoutPacket(

// OnChanUpgradeInit implements the IBCModule interface
func (im IBCMiddleware) OnChanUpgradeInit(ctx sdk.Context, portID, channelID string, order channeltypes.Order, connectionHops []string, version string) (string, error) {
cbs, ok := im.app.(porttypes.UpgradableModule)
if !ok {
return "", errorsmod.Wrap(porttypes.ErrInvalidRoute, "upgrade route not found to module in application callstack")
}

if !im.keeper.GetParams(ctx).ControllerEnabled {
return "", types.ErrControllerSubModuleDisabled
}
Expand All @@ -254,6 +249,11 @@ func (im IBCMiddleware) OnChanUpgradeInit(ctx sdk.Context, portID, channelID str
}

if im.app != nil && im.keeper.IsMiddlewareEnabled(ctx, portID, connectionID) {
// Only cast to UpgradableModule if the application is set.
cbs, ok := im.app.(porttypes.UpgradableModule)
if !ok {
return "", errorsmod.Wrap(porttypes.ErrInvalidRoute, "upgrade route not found to module in application callstack")
}
return cbs.OnChanUpgradeInit(ctx, portID, channelID, order, connectionHops, version)
}

Expand All @@ -267,11 +267,6 @@ func (IBCMiddleware) OnChanUpgradeTry(ctx sdk.Context, portID, channelID string,

// OnChanUpgradeAck implements the IBCModule interface
func (im IBCMiddleware) OnChanUpgradeAck(ctx sdk.Context, portID, channelID, counterpartyVersion string) error {
cbs, ok := im.app.(porttypes.UpgradableModule)
if !ok {
return errorsmod.Wrap(porttypes.ErrInvalidRoute, "upgrade route not found to module in application callstack")
}

if !im.keeper.GetParams(ctx).ControllerEnabled {
return types.ErrControllerSubModuleDisabled
}
Expand All @@ -286,6 +281,11 @@ func (im IBCMiddleware) OnChanUpgradeAck(ctx sdk.Context, portID, channelID, cou
}

if im.app != nil && im.keeper.IsMiddlewareEnabled(ctx, portID, connectionID) {
// Only cast to UpgradableModule if the application is set.
cbs, ok := im.app.(porttypes.UpgradableModule)
if !ok {
return errorsmod.Wrap(porttypes.ErrInvalidRoute, "upgrade route not found to module in application callstack")
}
return cbs.OnChanUpgradeAck(ctx, portID, channelID, counterpartyVersion)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,13 @@ func (suite *InterchainAccountsTestSuite) TestOnChanUpgradeInit() {
{
"success", func() {}, nil,
},
{
"success: nil underlying app",
func() {
isNilApp = true
},
nil,
},
{
"controller submodule disabled", func() {
suite.chainA.GetSimApp().ICAControllerKeeper.SetParams(suite.chainA.GetContext(), types.NewParams(false))
Expand All @@ -797,11 +804,6 @@ func (suite *InterchainAccountsTestSuite) TestOnChanUpgradeInit() {
}
}, ibcmock.MockApplicationCallbackError,
},
{
"nil underlying app", func() {
isNilApp = true
}, porttypes.ErrInvalidRoute,
},
{
"middleware disabled", func() {
suite.chainA.GetSimApp().ICAControllerKeeper.DeleteMiddlewareEnabled(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ConnectionID)
Expand Down Expand Up @@ -876,6 +878,13 @@ func (suite *InterchainAccountsTestSuite) TestOnChanUpgradeAck() {
{
"success", func() {}, nil,
},
{
"success: nil underlying app",
func() {
isNilApp = true
},
nil,
},
{
"controller submodule disabled", func() {
suite.chainA.GetSimApp().ICAControllerKeeper.SetParams(suite.chainA.GetContext(), types.NewParams(false))
Expand All @@ -893,11 +902,6 @@ func (suite *InterchainAccountsTestSuite) TestOnChanUpgradeAck() {
}
}, ibcmock.MockApplicationCallbackError,
},
{
"nil underlying app", func() {
isNilApp = true
}, porttypes.ErrInvalidRoute,
},
{
"middleware disabled", func() {
suite.chainA.GetSimApp().ICAControllerKeeper.DeleteMiddlewareEnabled(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ConnectionID)
Expand Down

0 comments on commit ef9a479

Please sign in to comment.