Skip to content

Commit

Permalink
Disable usage of controller and host submodules based on on-chain par…
Browse files Browse the repository at this point in the history
…ams (#575)

* add usage of enabling/disabling controller and host submodules

Adds if statement checks in controller/host ibc_module.go. Adds tests for each added if statements.
Tests not added for controller ack/timeout since tests do not exist for those functions yet.

* Update modules/apps/27-interchain-accounts/controller/ibc_module_test.go
  • Loading branch information
colin-axner authored Dec 1, 2021
1 parent d681c0d commit eedb0cb
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 9 deletions.
17 changes: 17 additions & 0 deletions modules/apps/27-interchain-accounts/controller/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"

"github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/keeper"
"github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/types"
icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types"
channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types"
porttypes "github.com/cosmos/ibc-go/v2/modules/core/05-port/types"
Expand Down Expand Up @@ -42,6 +43,10 @@ func (im IBCModule) OnChanOpenInit(
counterparty channeltypes.Counterparty,
version string,
) error {
if !im.keeper.IsControllerEnabled(ctx) {
return types.ErrControllerSubModuleDisabled
}

if err := im.keeper.OnChanOpenInit(ctx, order, connectionHops, portID, channelID, chanCap, counterparty, version); err != nil {
return err
}
Expand Down Expand Up @@ -78,6 +83,10 @@ func (im IBCModule) OnChanOpenAck(
channelID string,
counterpartyVersion string,
) error {
if !im.keeper.IsControllerEnabled(ctx) {
return types.ErrControllerSubModuleDisabled
}

if err := im.keeper.OnChanOpenAck(ctx, portID, channelID, counterpartyVersion); err != nil {
return err
}
Expand Down Expand Up @@ -130,6 +139,10 @@ func (im IBCModule) OnAcknowledgementPacket(
acknowledgement []byte,
relayer sdk.AccAddress,
) error {
if !im.keeper.IsControllerEnabled(ctx) {
return types.ErrControllerSubModuleDisabled
}

// call underlying app's OnAcknowledgementPacket callback.
return im.app.OnAcknowledgementPacket(ctx, packet, acknowledgement, relayer)
}
Expand All @@ -140,6 +153,10 @@ func (im IBCModule) OnTimeoutPacket(
packet channeltypes.Packet,
relayer sdk.AccAddress,
) error {
if !im.keeper.IsControllerEnabled(ctx) {
return types.ErrControllerSubModuleDisabled
}

if err := im.keeper.OnTimeoutPacket(ctx, packet); err != nil {
return err
}
Expand Down
17 changes: 14 additions & 3 deletions modules/apps/27-interchain-accounts/controller/ibc_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/suite"
"github.com/tendermint/tendermint/crypto"

"github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/types"
icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types"
clienttypes "github.com/cosmos/ibc-go/v2/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types"
Expand Down Expand Up @@ -117,6 +118,11 @@ func (suite *InterchainAccountsTestSuite) TestOnChanOpenInit() {
{
"success", func() {}, true,
},
{
"controller submodule disabled", func() {
suite.chainA.GetSimApp().ICAControllerKeeper.SetParams(suite.chainA.GetContext(), types.NewParams(false))
}, false,
},
{
"ICA OnChanOpenInit fails - UNORDERED channel", func() {
channel.Ordering = channeltypes.UNORDERED
Expand Down Expand Up @@ -251,6 +257,11 @@ func (suite *InterchainAccountsTestSuite) TestOnChanOpenAck() {
{
"success", func() {}, true,
},
{
"controller submodule disabled", func() {
suite.chainA.GetSimApp().ICAControllerKeeper.SetParams(suite.chainA.GetContext(), types.NewParams(false))
}, false,
},
{
"ICA OnChanOpenACK fails - invalid version", func() {
path.EndpointB.ChannelConfig.Version = "invalid|version"
Expand Down Expand Up @@ -460,15 +471,15 @@ func (suite *InterchainAccountsTestSuite) TestOnRecvPacket() {
packet := channeltypes.NewPacket(
[]byte("empty packet data"),
suite.chainA.SenderAccount.GetSequence(),
path.EndpointB.ChannelConfig.PortID,
path.EndpointB.ChannelID,
path.EndpointA.ChannelConfig.PortID,
path.EndpointA.ChannelID,
path.EndpointB.ChannelConfig.PortID,
path.EndpointB.ChannelID,
clienttypes.NewHeight(0, 100),
0,
)

ack := cbs.OnRecvPacket(suite.chainA.GetContext(), packet, TestAccAddress)
ack := cbs.OnRecvPacket(suite.chainB.GetContext(), packet, TestAccAddress)
suite.Require().Equal(tc.expPass, ack.Success())
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import (
"github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/types"
)

// GetControllerEnabled retrieves the host enabled boolean from the paramstore
func (k Keeper) GetControllerEnabled(ctx sdk.Context) bool {
// IsControllerEnabled retrieves the host enabled boolean from the paramstore.
// True is returned if the controller submodule is enabled.
func (k Keeper) IsControllerEnabled(ctx sdk.Context) bool {
var res bool
k.paramSpace.Get(ctx, types.KeyControllerEnabled, &res)
return res
}

// GetParams returns the total set of the host submodule parameters.
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
return types.NewParams(k.GetControllerEnabled(ctx))
return types.NewParams(k.IsControllerEnabled(ctx))
}

// SetParams sets the total set of the host submodule parameters.
Expand Down
10 changes: 10 additions & 0 deletions modules/apps/27-interchain-accounts/controller/types/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package types

import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// ICA Controller sentinel errors
var (
ErrControllerSubModuleDisabled = sdkerrors.Register(ModuleName, 2, "controller submodule is disabled")
)
13 changes: 13 additions & 0 deletions modules/apps/27-interchain-accounts/host/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"

"github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/keeper"
"github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/types"
icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types"
channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types"
ibcexported "github.com/cosmos/ibc-go/v2/modules/core/exported"
Expand Down Expand Up @@ -49,6 +50,10 @@ func (im IBCModule) OnChanOpenTry(
version,
counterpartyVersion string,
) error {
if !im.keeper.IsHostEnabled(ctx) {
return types.ErrHostSubModuleDisabled
}

return im.keeper.OnChanOpenTry(ctx, order, connectionHops, portID, channelID, chanCap, counterparty, version, counterpartyVersion)
}

Expand All @@ -68,6 +73,10 @@ func (im IBCModule) OnChanOpenConfirm(
portID,
channelID string,
) error {
if !im.keeper.IsHostEnabled(ctx) {
return types.ErrHostSubModuleDisabled
}

return im.keeper.OnChanOpenConfirm(ctx, portID, channelID)
}

Expand Down Expand Up @@ -96,6 +105,10 @@ func (im IBCModule) OnRecvPacket(
packet channeltypes.Packet,
_ sdk.AccAddress,
) ibcexported.Acknowledgement {
if !im.keeper.IsHostEnabled(ctx) {
return channeltypes.NewErrorAcknowledgement(types.ErrHostSubModuleDisabled.Error())
}

ack := channeltypes.NewResultAcknowledgement([]byte{byte(1)})

if err := im.keeper.OnRecvPacket(ctx, packet); err != nil {
Expand Down
16 changes: 16 additions & 0 deletions modules/apps/27-interchain-accounts/host/ibc_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/suite"
"github.com/tendermint/tendermint/crypto"

"github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/types"
icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types"
clienttypes "github.com/cosmos/ibc-go/v2/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types"
Expand Down Expand Up @@ -136,6 +137,11 @@ func (suite *InterchainAccountsTestSuite) TestOnChanOpenTry() {
{
"success", func() {}, true,
},
{
"host submodule disabled", func() {
suite.chainB.GetSimApp().ICAHostKeeper.SetParams(suite.chainB.GetContext(), types.NewParams(false))
}, false,
},
{
"success: ICA auth module callback returns error", func() {
// mock module callback should not be called on host side
Expand Down Expand Up @@ -252,6 +258,11 @@ func (suite *InterchainAccountsTestSuite) TestOnChanOpenConfirm() {
{
"success", func() {}, true,
},
{
"host submodule disabled", func() {
suite.chainB.GetSimApp().ICAHostKeeper.SetParams(suite.chainB.GetContext(), types.NewParams(false))
}, false,
},
{
"success: ICA auth module callback returns error", func() {
// mock module callback should not be called on host side
Expand Down Expand Up @@ -386,6 +397,11 @@ func (suite *InterchainAccountsTestSuite) TestOnRecvPacket() {
{
"success", func() {}, true,
},
{
"host submodule disabled", func() {
suite.chainB.GetSimApp().ICAHostKeeper.SetParams(suite.chainB.GetContext(), types.NewParams(false))
}, false,
},
{
"success with ICA auth module callback failure", func() {
suite.chainB.GetSimApp().ICAAuthModule.IBCApp.OnRecvPacket = func(
Expand Down
7 changes: 4 additions & 3 deletions modules/apps/27-interchain-accounts/host/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import (
"github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/types"
)

// GetHostEnabled retrieves the host enabled boolean from the paramstore
func (k Keeper) GetHostEnabled(ctx sdk.Context) bool {
// IsHostEnabled retrieves the host enabled boolean from the paramstore.
// True is returned if the host submodule is enabled.
func (k Keeper) IsHostEnabled(ctx sdk.Context) bool {
var res bool
k.paramSpace.Get(ctx, types.KeyHostEnabled, &res)
return res
}

// GetParams returns the total set of the host submodule parameters.
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
return types.NewParams(k.GetHostEnabled(ctx))
return types.NewParams(k.IsHostEnabled(ctx))
}

// SetParams sets the total set of the host submodule parameters.
Expand Down
10 changes: 10 additions & 0 deletions modules/apps/27-interchain-accounts/host/types/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package types

import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// ICA Host sentinel errors
var (
ErrHostSubModuleDisabled = sdkerrors.Register(ModuleName, 2, "host submodule is disabled")
)

0 comments on commit eedb0cb

Please sign in to comment.