From d6be10b9cba171d2678ee2ce7dbcb52c236fceab Mon Sep 17 00:00:00 2001 From: srdtrk <59252793+srdtrk@users.noreply.github.com> Date: Mon, 31 Jul 2023 16:30:18 +0200 Subject: [PATCH] imp(apps): added 'WithICS4Wrapper' function to keepers (#4187) * imp(ica, transfer): added WithICS4Wrapper api function * docs(ica, transfer): updated 'WithICS4Wrapper's godocs * docs(adr8): updated godocs for withics4wrapper * imp(ica/host): removed withics4wrapper from icahost * style(ica, tranfer): ran golanci-lint * imp(ica/controller_test): added WithICS4Wrapper test * imp(transfer_test): added WithICS4Wrapper test * style(transfer_test, ica/controller_test): added spacing to TestWithICS4Wrapper * feat(ica/host): implemented 'WithICS4Wrapper' * feat(fee): implemented 'WithICS4Wrapper' (cherry picked from commit 561eb36f1f41044036859b5911758a5403a85820) # Conflicts: # modules/apps/27-interchain-accounts/controller/keeper/keeper_test.go # modules/apps/27-interchain-accounts/host/keeper/keeper_test.go # modules/apps/transfer/keeper/keeper.go # modules/apps/transfer/keeper/keeper_test.go --- .../controller/keeper/export_test.go | 12 +++ .../controller/keeper/keeper.go | 7 ++ .../controller/keeper/keeper_test.go | 76 +++++++++++++++++++ .../host/keeper/export_test.go | 8 +- .../host/keeper/keeper.go | 7 ++ .../host/keeper/keeper_test.go | 75 ++++++++++++++++++ modules/apps/29-fee/keeper/export_test.go | 12 +++ modules/apps/29-fee/keeper/keeper.go | 7 ++ modules/apps/29-fee/keeper/keeper_test.go | 23 ++++++ modules/apps/transfer/keeper/export_test.go | 12 +++ modules/apps/transfer/keeper/keeper.go | 15 ++++ modules/apps/transfer/keeper/keeper_test.go | 71 +++++++++++++++++ 12 files changed, 324 insertions(+), 1 deletion(-) create mode 100644 modules/apps/27-interchain-accounts/controller/keeper/export_test.go create mode 100644 modules/apps/29-fee/keeper/export_test.go create mode 100644 modules/apps/transfer/keeper/export_test.go diff --git a/modules/apps/27-interchain-accounts/controller/keeper/export_test.go b/modules/apps/27-interchain-accounts/controller/keeper/export_test.go new file mode 100644 index 00000000000..3600380f0a8 --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/keeper/export_test.go @@ -0,0 +1,12 @@ +package keeper + +/* + This file is to allow for unexported functions and fields to be accessible to the testing package. +*/ + +import porttypes "github.com/cosmos/ibc-go/v7/modules/core/05-port/types" + +// GetICS4Wrapper is a getter for the keeper's ICS4Wrapper. +func (k *Keeper) GetICS4Wrapper() porttypes.ICS4Wrapper { + return k.ics4Wrapper +} diff --git a/modules/apps/27-interchain-accounts/controller/keeper/keeper.go b/modules/apps/27-interchain-accounts/controller/keeper/keeper.go index 3796cdef123..7ae30269a58 100644 --- a/modules/apps/27-interchain-accounts/controller/keeper/keeper.go +++ b/modules/apps/27-interchain-accounts/controller/keeper/keeper.go @@ -60,6 +60,13 @@ func NewKeeper( } } +// WithICS4Wrapper sets the ICS4Wrapper. This function may be used after +// the keepers creation to set the middleware which is above this module +// in the IBC application stack. +func (k *Keeper) WithICS4Wrapper(wrapper porttypes.ICS4Wrapper) { + k.ics4Wrapper = wrapper +} + // Logger returns the application logger, scoped to the associated module func (k Keeper) Logger(ctx sdk.Context) log.Logger { return ctx.Logger().With("module", fmt.Sprintf("x/%s-%s", exported.ModuleName, icatypes.ModuleName)) diff --git a/modules/apps/27-interchain-accounts/controller/keeper/keeper_test.go b/modules/apps/27-interchain-accounts/controller/keeper/keeper_test.go index 0583912165b..bc74778a949 100644 --- a/modules/apps/27-interchain-accounts/controller/keeper/keeper_test.go +++ b/modules/apps/27-interchain-accounts/controller/keeper/keeper_test.go @@ -7,6 +7,8 @@ import ( genesistypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/genesis/types" icatypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/types" + ibcfeekeeper "github.com/cosmos/ibc-go/v7/modules/apps/29-fee/keeper" + channelkeeper "github.com/cosmos/ibc-go/v7/modules/core/04-channel/keeper" channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" ibctesting "github.com/cosmos/ibc-go/v7/testing" ) @@ -255,3 +257,77 @@ func (suite *KeeperTestSuite) TestSetInterchainAccountAddress() { suite.Require().True(found) suite.Require().Equal(expectedAccAddr, retrievedAddr) } +<<<<<<< HEAD +======= + +func (suite *KeeperTestSuite) TestSetAndGetParams() { + testCases := []struct { + name string + input types.Params + expPass bool + }{ + // it is not possible to set invalid booleans + {"success: set params false", types.NewParams(false), true}, + {"success: set params true", types.NewParams(true), true}, + } + + for _, tc := range testCases { + tc := tc + suite.Run(tc.name, func() { + suite.SetupTest() // reset + ctx := suite.chainA.GetContext() + if tc.expPass { + suite.chainA.GetSimApp().ICAControllerKeeper.SetParams(ctx, tc.input) + expected := tc.input + p := suite.chainA.GetSimApp().ICAControllerKeeper.GetParams(ctx) + suite.Require().Equal(expected, p) + } else { // currently not possible to set invalid params + suite.Require().Panics(func() { + suite.chainA.GetSimApp().ICAControllerKeeper.SetParams(ctx, tc.input) + }) + } + }) + } +} + +func (suite *KeeperTestSuite) TestUnsetParams() { + suite.SetupTest() + + ctx := suite.chainA.GetContext() + store := suite.chainA.GetContext().KVStore(suite.chainA.GetSimApp().GetKey(types.SubModuleName)) + store.Delete([]byte(types.ParamsKey)) + + suite.Require().Panics(func() { + suite.chainA.GetSimApp().ICAControllerKeeper.GetParams(ctx) + }) +} + +func (suite *KeeperTestSuite) TestGetAuthority() { + suite.SetupTest() + + authority := suite.chainA.GetSimApp().ICAControllerKeeper.GetAuthority() + expectedAuth := authtypes.NewModuleAddress(govtypes.ModuleName).String() + suite.Require().Equal(expectedAuth, authority) +} + +func (suite *KeeperTestSuite) TestWithICS4Wrapper() { + suite.SetupTest() + + // test if the ics4 wrapper is the fee keeper initially + ics4Wrapper := suite.chainA.GetSimApp().ICAControllerKeeper.GetICS4Wrapper() + + _, isFeeKeeper := ics4Wrapper.(ibcfeekeeper.Keeper) + suite.Require().True(isFeeKeeper) + _, isChannelKeeper := ics4Wrapper.(channelkeeper.Keeper) + suite.Require().False(isChannelKeeper) + + // set the ics4 wrapper to the channel keeper + suite.chainA.GetSimApp().ICAControllerKeeper.WithICS4Wrapper(suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper) + ics4Wrapper = suite.chainA.GetSimApp().ICAControllerKeeper.GetICS4Wrapper() + + _, isChannelKeeper = ics4Wrapper.(channelkeeper.Keeper) + suite.Require().True(isChannelKeeper) + _, isFeeKeeper = ics4Wrapper.(ibcfeekeeper.Keeper) + suite.Require().False(isFeeKeeper) +} +>>>>>>> 561eb36f (imp(apps): added 'WithICS4Wrapper' function to keepers (#4187)) diff --git a/modules/apps/27-interchain-accounts/host/keeper/export_test.go b/modules/apps/27-interchain-accounts/host/keeper/export_test.go index 03c13e894d5..100e5203076 100644 --- a/modules/apps/27-interchain-accounts/host/keeper/export_test.go +++ b/modules/apps/27-interchain-accounts/host/keeper/export_test.go @@ -1,15 +1,21 @@ package keeper /* - This file is to allow for unexported functions to be accessible to the testing package. + This file is to allow for unexported functions and fields to be accessible to the testing package. */ import ( sdk "github.com/cosmos/cosmos-sdk/types" icatypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/types" + porttypes "github.com/cosmos/ibc-go/v7/modules/core/05-port/types" ) +// GetICS4Wrapper is a getter for the keeper's ICS4Wrapper. +func (k *Keeper) GetICS4Wrapper() porttypes.ICS4Wrapper { + return k.ics4Wrapper +} + // GetAppMetadata is a wrapper around getAppMetadata to allow the function to be directly called in tests. func (k Keeper) GetAppMetadata(ctx sdk.Context, portID, channelID string) (icatypes.Metadata, error) { return k.getAppMetadata(ctx, portID, channelID) diff --git a/modules/apps/27-interchain-accounts/host/keeper/keeper.go b/modules/apps/27-interchain-accounts/host/keeper/keeper.go index 3277676abdd..56e1f74c82c 100644 --- a/modules/apps/27-interchain-accounts/host/keeper/keeper.go +++ b/modules/apps/27-interchain-accounts/host/keeper/keeper.go @@ -68,6 +68,13 @@ func NewKeeper( } } +// WithICS4Wrapper sets the ICS4Wrapper. This function may be used after +// the keepers creation to set the middleware which is above this module +// in the IBC application stack. +func (k *Keeper) WithICS4Wrapper(wrapper porttypes.ICS4Wrapper) { + k.ics4Wrapper = wrapper +} + // Logger returns the application logger, scoped to the associated module func (k Keeper) Logger(ctx sdk.Context) log.Logger { return ctx.Logger().With("module", fmt.Sprintf("x/%s-%s", exported.ModuleName, icatypes.ModuleName)) diff --git a/modules/apps/27-interchain-accounts/host/keeper/keeper_test.go b/modules/apps/27-interchain-accounts/host/keeper/keeper_test.go index 4072878e935..3d9f2d57bf1 100644 --- a/modules/apps/27-interchain-accounts/host/keeper/keeper_test.go +++ b/modules/apps/27-interchain-accounts/host/keeper/keeper_test.go @@ -8,6 +8,8 @@ import ( genesistypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/genesis/types" icatypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/types" + ibcfeekeeper "github.com/cosmos/ibc-go/v7/modules/apps/29-fee/keeper" + channelkeeper "github.com/cosmos/ibc-go/v7/modules/core/04-channel/keeper" channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" ibcerrors "github.com/cosmos/ibc-go/v7/modules/core/errors" ibctesting "github.com/cosmos/ibc-go/v7/testing" @@ -268,3 +270,76 @@ func (suite *KeeperTestSuite) TestMetadataNotFound() { suite.Require().ErrorIs(err, ibcerrors.ErrNotFound) suite.Require().Contains(err.Error(), fmt.Sprintf("app version not found for port %s and channel %s", invalidPortID, invalidChannelID)) } +<<<<<<< HEAD +======= + +func (suite *KeeperTestSuite) TestParams() { + expParams := types.DefaultParams() + + params := suite.chainA.GetSimApp().ICAHostKeeper.GetParams(suite.chainA.GetContext()) + suite.Require().Equal(expParams, params) + + testCases := []struct { + name string + input types.Params + expPass bool + }{ + {"success: set default params", types.DefaultParams(), true}, + {"success: non-default params", types.NewParams(!types.DefaultHostEnabled, []string{"/cosmos.staking.v1beta1.MsgDelegate"}), true}, + {"success: set empty byte for allow messages", types.NewParams(true, nil), true}, + {"failure: set empty string for allow messages", types.NewParams(true, []string{""}), false}, + {"failure: set space string for allow messages", types.NewParams(true, []string{" "}), false}, + } + + for _, tc := range testCases { + tc := tc + + suite.Run(tc.name, func() { + suite.SetupTest() // reset + ctx := suite.chainA.GetContext() + err := tc.input.Validate() + suite.chainA.GetSimApp().ICAHostKeeper.SetParams(ctx, tc.input) + if tc.expPass { + suite.Require().NoError(err) + expected := tc.input + p := suite.chainA.GetSimApp().ICAHostKeeper.GetParams(ctx) + suite.Require().Equal(expected, p) + } else { + suite.Require().Error(err) + } + }) + } +} + +func (suite *KeeperTestSuite) TestUnsetParams() { + suite.SetupTest() + ctx := suite.chainA.GetContext() + store := suite.chainA.GetContext().KVStore(suite.chainA.GetSimApp().GetKey(types.SubModuleName)) + store.Delete([]byte(types.ParamsKey)) + + suite.Require().Panics(func() { + suite.chainA.GetSimApp().ICAHostKeeper.GetParams(ctx) + }) +} + +func (suite *KeeperTestSuite) TestWithICS4Wrapper() { + suite.SetupTest() + + // test if the ics4 wrapper is the fee keeper initially + ics4Wrapper := suite.chainA.GetSimApp().ICAHostKeeper.GetICS4Wrapper() + + _, isFeeKeeper := ics4Wrapper.(ibcfeekeeper.Keeper) + suite.Require().True(isFeeKeeper) + _, isChannelKeeper := ics4Wrapper.(channelkeeper.Keeper) + suite.Require().False(isChannelKeeper) + + // set the ics4 wrapper to the channel keeper + suite.chainA.GetSimApp().ICAHostKeeper.WithICS4Wrapper(suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper) + ics4Wrapper = suite.chainA.GetSimApp().ICAHostKeeper.GetICS4Wrapper() + + _, isChannelKeeper = ics4Wrapper.(channelkeeper.Keeper) + suite.Require().True(isChannelKeeper) + _, isFeeKeeper = ics4Wrapper.(ibcfeekeeper.Keeper) + suite.Require().False(isFeeKeeper) +} +>>>>>>> 561eb36f (imp(apps): added 'WithICS4Wrapper' function to keepers (#4187)) diff --git a/modules/apps/29-fee/keeper/export_test.go b/modules/apps/29-fee/keeper/export_test.go new file mode 100644 index 00000000000..3600380f0a8 --- /dev/null +++ b/modules/apps/29-fee/keeper/export_test.go @@ -0,0 +1,12 @@ +package keeper + +/* + This file is to allow for unexported functions and fields to be accessible to the testing package. +*/ + +import porttypes "github.com/cosmos/ibc-go/v7/modules/core/05-port/types" + +// GetICS4Wrapper is a getter for the keeper's ICS4Wrapper. +func (k *Keeper) GetICS4Wrapper() porttypes.ICS4Wrapper { + return k.ics4Wrapper +} diff --git a/modules/apps/29-fee/keeper/keeper.go b/modules/apps/29-fee/keeper/keeper.go index c88bbbe492a..97814b5af82 100644 --- a/modules/apps/29-fee/keeper/keeper.go +++ b/modules/apps/29-fee/keeper/keeper.go @@ -49,6 +49,13 @@ func NewKeeper( } } +// WithICS4Wrapper sets the ICS4Wrapper. This function may be used after +// the keepers creation to set the middleware which is above this module +// in the IBC application stack. +func (k *Keeper) WithICS4Wrapper(wrapper porttypes.ICS4Wrapper) { + k.ics4Wrapper = wrapper +} + // Logger returns a module-specific logger. func (k Keeper) Logger(ctx sdk.Context) log.Logger { return ctx.Logger().With("module", "x/"+ibcexported.ModuleName+"-"+types.ModuleName) diff --git a/modules/apps/29-fee/keeper/keeper_test.go b/modules/apps/29-fee/keeper/keeper_test.go index 89dfcd7a30e..b67a1e56c8c 100644 --- a/modules/apps/29-fee/keeper/keeper_test.go +++ b/modules/apps/29-fee/keeper/keeper_test.go @@ -8,7 +8,9 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/suite" + "github.com/cosmos/ibc-go/v7/modules/apps/29-fee/keeper" "github.com/cosmos/ibc-go/v7/modules/apps/29-fee/types" + channelkeeper "github.com/cosmos/ibc-go/v7/modules/core/04-channel/keeper" channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" ibctesting "github.com/cosmos/ibc-go/v7/testing" ibcmock "github.com/cosmos/ibc-go/v7/testing/mock" @@ -301,3 +303,24 @@ func (suite *KeeperTestSuite) TestGetAllCounterpartyPayees() { suite.Require().Len(counterpartyPayeeAddr, len(expectedCounterpartyPayee)) suite.Require().Equal(counterpartyPayeeAddr, expectedCounterpartyPayee) } + +func (suite *KeeperTestSuite) TestWithICS4Wrapper() { + suite.SetupTest() + + // test if the ics4 wrapper is the channel keeper initially + ics4Wrapper := suite.chainA.GetSimApp().IBCFeeKeeper.GetICS4Wrapper() + + _, isChannelKeeper := ics4Wrapper.(channelkeeper.Keeper) + suite.Require().True(isChannelKeeper) + _, isFeeKeeper := ics4Wrapper.(keeper.Keeper) + suite.Require().False(isFeeKeeper) + + // set the ics4 wrapper to itself (don't do this in production) + suite.chainA.GetSimApp().IBCFeeKeeper.WithICS4Wrapper(suite.chainA.GetSimApp().IBCFeeKeeper) + ics4Wrapper = suite.chainA.GetSimApp().IBCFeeKeeper.GetICS4Wrapper() + + _, isFeeKeeper = ics4Wrapper.(keeper.Keeper) + suite.Require().True(isFeeKeeper) + _, isChannelKeeper = ics4Wrapper.(channelkeeper.Keeper) + suite.Require().False(isChannelKeeper) +} diff --git a/modules/apps/transfer/keeper/export_test.go b/modules/apps/transfer/keeper/export_test.go new file mode 100644 index 00000000000..3600380f0a8 --- /dev/null +++ b/modules/apps/transfer/keeper/export_test.go @@ -0,0 +1,12 @@ +package keeper + +/* + This file is to allow for unexported functions and fields to be accessible to the testing package. +*/ + +import porttypes "github.com/cosmos/ibc-go/v7/modules/core/05-port/types" + +// GetICS4Wrapper is a getter for the keeper's ICS4Wrapper. +func (k *Keeper) GetICS4Wrapper() porttypes.ICS4Wrapper { + return k.ics4Wrapper +} diff --git a/modules/apps/transfer/keeper/keeper.go b/modules/apps/transfer/keeper/keeper.go index 736bb11298a..8d02a44bd8a 100644 --- a/modules/apps/transfer/keeper/keeper.go +++ b/modules/apps/transfer/keeper/keeper.go @@ -62,6 +62,21 @@ func NewKeeper( } } +<<<<<<< HEAD +======= +// WithICS4Wrapper sets the ICS4Wrapper. This function may be used after +// the keepers creation to set the middleware which is above this module +// in the IBC application stack. +func (k *Keeper) WithICS4Wrapper(wrapper porttypes.ICS4Wrapper) { + k.ics4Wrapper = wrapper +} + +// GetAuthority returns the transfer module's authority. +func (k Keeper) GetAuthority() string { + return k.authority +} + +>>>>>>> 561eb36f (imp(apps): added 'WithICS4Wrapper' function to keepers (#4187)) // Logger returns a module-specific logger. func (k Keeper) Logger(ctx sdk.Context) log.Logger { return ctx.Logger().With("module", "x/"+exported.ModuleName+"-"+types.ModuleName) diff --git a/modules/apps/transfer/keeper/keeper_test.go b/modules/apps/transfer/keeper/keeper_test.go index f0a746108cc..b4340c219e4 100644 --- a/modules/apps/transfer/keeper/keeper_test.go +++ b/modules/apps/transfer/keeper/keeper_test.go @@ -11,7 +11,9 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/suite" + ibcfeekeeper "github.com/cosmos/ibc-go/v7/modules/apps/29-fee/keeper" "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types" + channelkeeper "github.com/cosmos/ibc-go/v7/modules/core/04-channel/keeper" ibctesting "github.com/cosmos/ibc-go/v7/testing" ) @@ -229,3 +231,72 @@ func (suite *KeeperTestSuite) TestGetAllDenomEscrows() { }) } } +<<<<<<< HEAD +======= + +func (suite *KeeperTestSuite) TestParams() { + testCases := []struct { + name string + input types.Params + expPass bool + }{ + // it is not possible to set invalid booleans + {"success: set params false-false", types.NewParams(false, false), true}, + {"success: set params false-true", types.NewParams(false, true), true}, + {"success: set params true-false", types.NewParams(true, false), true}, + {"success: set params true-true", types.NewParams(true, true), true}, + } + + for _, tc := range testCases { + tc := tc + + suite.Run(tc.name, func() { + suite.SetupTest() // reset + ctx := suite.chainA.GetContext() + if tc.expPass { + suite.chainA.GetSimApp().TransferKeeper.SetParams(ctx, tc.input) + expected := tc.input + p := suite.chainA.GetSimApp().TransferKeeper.GetParams(ctx) + suite.Require().Equal(expected, p) + } else { + suite.Require().Panics(func() { + suite.chainA.GetSimApp().TransferKeeper.SetParams(ctx, tc.input) + }) + } + }) + } +} + +func (suite *KeeperTestSuite) TestUnsetParams() { + suite.SetupTest() + + ctx := suite.chainA.GetContext() + store := suite.chainA.GetContext().KVStore(suite.chainA.GetSimApp().GetKey(types.ModuleName)) + store.Delete([]byte(types.ParamsKey)) + + suite.Require().Panics(func() { + suite.chainA.GetSimApp().TransferKeeper.GetParams(ctx) + }) +} + +func (suite *KeeperTestSuite) TestWithICS4Wrapper() { + suite.SetupTest() + + // test if the ics4 wrapper is the fee keeper initially + ics4Wrapper := suite.chainA.GetSimApp().TransferKeeper.GetICS4Wrapper() + _, isFeeKeeper := ics4Wrapper.(ibcfeekeeper.Keeper) + + suite.Require().True(isFeeKeeper) + _, isChannelKeeper := ics4Wrapper.(channelkeeper.Keeper) + suite.Require().False(isChannelKeeper) + + // set the ics4 wrapper to the channel keeper + suite.chainA.GetSimApp().TransferKeeper.WithICS4Wrapper(suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper) + ics4Wrapper = suite.chainA.GetSimApp().TransferKeeper.GetICS4Wrapper() + + _, isChannelKeeper = ics4Wrapper.(channelkeeper.Keeper) + suite.Require().True(isChannelKeeper) + _, isFeeKeeper = ics4Wrapper.(ibcfeekeeper.Keeper) + suite.Require().False(isFeeKeeper) +} +>>>>>>> 561eb36f (imp(apps): added 'WithICS4Wrapper' function to keepers (#4187))