From 51ec4563c5950194ed4675af7ba2d3019d80bfa0 Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Tue, 5 Oct 2021 10:21:11 +0200 Subject: [PATCH 1/6] update port key to use prefix, separate key prefixes to vars --- .../apps/27-interchain-accounts/genesis.go | 2 +- .../27-interchain-accounts/keeper/keeper.go | 6 +++--- .../keeper/keeper_test.go | 2 +- .../apps/27-interchain-accounts/types/keys.go | 19 +++++++++++++++---- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/modules/apps/27-interchain-accounts/genesis.go b/modules/apps/27-interchain-accounts/genesis.go index 0fd975ace00..7515c089e1b 100644 --- a/modules/apps/27-interchain-accounts/genesis.go +++ b/modules/apps/27-interchain-accounts/genesis.go @@ -22,7 +22,7 @@ func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, state types.GenesisState // ExportGenesis exports transfer module's portID into its geneis state func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) *types.GenesisState { - portID := keeper.GetPort(ctx) + portID := keeper.GetPort(ctx, types.PortID) return &types.GenesisState{ PortId: portID, diff --git a/modules/apps/27-interchain-accounts/keeper/keeper.go b/modules/apps/27-interchain-accounts/keeper/keeper.go index a68bdb338f1..3a90d281ea7 100644 --- a/modules/apps/27-interchain-accounts/keeper/keeper.go +++ b/modules/apps/27-interchain-accounts/keeper/keeper.go @@ -97,15 +97,15 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger { } // GetPort returns the portID for the interchain accounts module. Used in ExportGenesis -func (k Keeper) GetPort(ctx sdk.Context) string { +func (k Keeper) GetPort(ctx sdk.Context, portID string) string { store := ctx.KVStore(k.storeKey) - return string(store.Get([]byte(types.PortKey))) + return string(store.Get(types.KeyPort(portID))) } // BindPort stores the provided portID and binds to it, returning the associated capability func (k Keeper) BindPort(ctx sdk.Context, portID string) *capabilitytypes.Capability { store := ctx.KVStore(k.storeKey) - store.Set([]byte(types.PortKey), []byte(portID)) + store.Set(types.KeyPort(portID), []byte(portID)) return k.portKeeper.BindPort(ctx, portID) } diff --git a/modules/apps/27-interchain-accounts/keeper/keeper_test.go b/modules/apps/27-interchain-accounts/keeper/keeper_test.go index 1930f327bd9..ac39911380c 100644 --- a/modules/apps/27-interchain-accounts/keeper/keeper_test.go +++ b/modules/apps/27-interchain-accounts/keeper/keeper_test.go @@ -110,7 +110,7 @@ func (suite *KeeperTestSuite) TestIsBound() { } func (suite *KeeperTestSuite) TestGetPort() { - port := suite.chainA.GetSimApp().ICAKeeper.GetPort(suite.chainA.GetContext()) + port := suite.chainA.GetSimApp().ICAKeeper.GetPort(suite.chainA.GetContext(), types.PortID) suite.Require().Equal(types.PortID, port) } diff --git a/modules/apps/27-interchain-accounts/types/keys.go b/modules/apps/27-interchain-accounts/types/keys.go index 8640226191f..0efb3d64c7a 100644 --- a/modules/apps/27-interchain-accounts/types/keys.go +++ b/modules/apps/27-interchain-accounts/types/keys.go @@ -28,8 +28,14 @@ const ( ) var ( - // PortKey defines the key to store the port ID in store - PortKey = []byte{0x01} + // ActiveChannelKeyPrefix defines the key prefix used to store active channels + ActiveChannelKeyPrefix = "activeChannel" + + // OwnerKeyPrefix defines the key prefix used to store interchain accounts + OwnerKeyPrefix = "owner" + + // PortKeyPrefix defines the key prefix used to store ports + PortKeyPrefix = "port" ) // NewVersion returns a complete version string in the format: VersionPrefix + Delimter + AccAddress @@ -39,10 +45,15 @@ func NewAppVersion(versionPrefix, accAddr string) string { // KeyActiveChannel creates and returns a new key used for active channels store operations func KeyActiveChannel(portID string) []byte { - return []byte(fmt.Sprintf("activeChannel/%s", portID)) + return []byte(fmt.Sprintf("%s/%s", ActiveChannelKeyPrefix, portID)) } // KeyOwnerAccount creates and returns a new key used for owner account store operations func KeyOwnerAccount(portID string) []byte { - return []byte(fmt.Sprintf("owner/%s", portID)) + return []byte(fmt.Sprintf("%s/%s", OwnerKeyPrefix, portID)) +} + +// KeyPort creates and returns a new key used for port store operations +func KeyPort(portID string) []byte { + return []byte(fmt.Sprintf("%s/%s", PortKeyPrefix, portID)) } From ed709588801020879575f1fb0c9df1dd34f73259 Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Tue, 5 Oct 2021 10:25:56 +0200 Subject: [PATCH 2/6] updating godoc --- modules/apps/27-interchain-accounts/types/keys.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/apps/27-interchain-accounts/types/keys.go b/modules/apps/27-interchain-accounts/types/keys.go index 0efb3d64c7a..c25b00e5cda 100644 --- a/modules/apps/27-interchain-accounts/types/keys.go +++ b/modules/apps/27-interchain-accounts/types/keys.go @@ -48,7 +48,7 @@ func KeyActiveChannel(portID string) []byte { return []byte(fmt.Sprintf("%s/%s", ActiveChannelKeyPrefix, portID)) } -// KeyOwnerAccount creates and returns a new key used for owner account store operations +// KeyOwnerAccount creates and returns a new key used for interchain account store operations func KeyOwnerAccount(portID string) []byte { return []byte(fmt.Sprintf("%s/%s", OwnerKeyPrefix, portID)) } From df4b1a9f3360abb9579967e31b911509dc146842 Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Tue, 5 Oct 2021 13:06:51 +0200 Subject: [PATCH 3/6] Update modules/apps/27-interchain-accounts/keeper/keeper.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> --- modules/apps/27-interchain-accounts/keeper/keeper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/apps/27-interchain-accounts/keeper/keeper.go b/modules/apps/27-interchain-accounts/keeper/keeper.go index 3a90d281ea7..62aac01351e 100644 --- a/modules/apps/27-interchain-accounts/keeper/keeper.go +++ b/modules/apps/27-interchain-accounts/keeper/keeper.go @@ -105,7 +105,7 @@ func (k Keeper) GetPort(ctx sdk.Context, portID string) string { // BindPort stores the provided portID and binds to it, returning the associated capability func (k Keeper) BindPort(ctx sdk.Context, portID string) *capabilitytypes.Capability { store := ctx.KVStore(k.storeKey) - store.Set(types.KeyPort(portID), []byte(portID)) + store.Set(types.KeyPort(portID), []byte{0x01}) return k.portKeeper.BindPort(ctx, portID) } From b8ffd5505127e558a0d6c8ca7b65c89e06937379 Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Tue, 5 Oct 2021 13:13:12 +0200 Subject: [PATCH 4/6] adding todo with ica genesis issue ref --- modules/apps/27-interchain-accounts/genesis.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/apps/27-interchain-accounts/genesis.go b/modules/apps/27-interchain-accounts/genesis.go index 7515c089e1b..e92d99386dc 100644 --- a/modules/apps/27-interchain-accounts/genesis.go +++ b/modules/apps/27-interchain-accounts/genesis.go @@ -22,6 +22,8 @@ func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, state types.GenesisState // ExportGenesis exports transfer module's portID into its geneis state func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) *types.GenesisState { + // TODO: Using a range query with KVStorePrefixIterator export all port IDs + // See https://github.com/cosmos/ibc-go/issues/448 portID := keeper.GetPort(ctx, types.PortID) return &types.GenesisState{ From 4e6f64960761fdfedc244ccec65a3b98f4e98f09 Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Tue, 5 Oct 2021 13:29:52 +0200 Subject: [PATCH 5/6] fixing failing test from browser commit --- modules/apps/27-interchain-accounts/keeper/keeper_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/apps/27-interchain-accounts/keeper/keeper_test.go b/modules/apps/27-interchain-accounts/keeper/keeper_test.go index ac39911380c..f6e089e988d 100644 --- a/modules/apps/27-interchain-accounts/keeper/keeper_test.go +++ b/modules/apps/27-interchain-accounts/keeper/keeper_test.go @@ -111,7 +111,7 @@ func (suite *KeeperTestSuite) TestIsBound() { func (suite *KeeperTestSuite) TestGetPort() { port := suite.chainA.GetSimApp().ICAKeeper.GetPort(suite.chainA.GetContext(), types.PortID) - suite.Require().Equal(types.PortID, port) + suite.Require().Equal(string([]byte{0x01}), port) } func (suite *KeeperTestSuite) TestGetInterchainAccountAddress() { From b025dd9aef4a1746ed51feb339e059ce8bc422a4 Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Thu, 7 Oct 2021 12:11:04 +0200 Subject: [PATCH 6/6] removing GetPort in favour of GetAllPorts --- modules/apps/27-interchain-accounts/genesis.go | 3 +-- .../27-interchain-accounts/keeper/keeper.go | 17 ++++++++++++++--- .../keeper/keeper_test.go | 14 +++++++++++--- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/modules/apps/27-interchain-accounts/genesis.go b/modules/apps/27-interchain-accounts/genesis.go index e92d99386dc..e052834b8a5 100644 --- a/modules/apps/27-interchain-accounts/genesis.go +++ b/modules/apps/27-interchain-accounts/genesis.go @@ -24,9 +24,8 @@ func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, state types.GenesisState func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) *types.GenesisState { // TODO: Using a range query with KVStorePrefixIterator export all port IDs // See https://github.com/cosmos/ibc-go/issues/448 - portID := keeper.GetPort(ctx, types.PortID) return &types.GenesisState{ - PortId: portID, + PortId: types.PortID, } } diff --git a/modules/apps/27-interchain-accounts/keeper/keeper.go b/modules/apps/27-interchain-accounts/keeper/keeper.go index 028ab76c1ae..8de1c7181c0 100644 --- a/modules/apps/27-interchain-accounts/keeper/keeper.go +++ b/modules/apps/27-interchain-accounts/keeper/keeper.go @@ -2,6 +2,7 @@ package keeper import ( "fmt" + "strings" baseapp "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec" @@ -86,10 +87,20 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger { return ctx.Logger().With("module", fmt.Sprintf("x/%s-%s", host.ModuleName, types.ModuleName)) } -// GetPort returns the portID for the interchain accounts module. Used in ExportGenesis -func (k Keeper) GetPort(ctx sdk.Context, portID string) string { +// GetAllPorts returns all ports to which the interchain accounts module is bound. Used in ExportGenesis +func (k Keeper) GetAllPorts(ctx sdk.Context) []string { store := ctx.KVStore(k.storeKey) - return string(store.Get(types.KeyPort(portID))) + iterator := sdk.KVStorePrefixIterator(store, []byte(types.PortKeyPrefix)) + defer iterator.Close() + + var ports []string + for ; iterator.Valid(); iterator.Next() { + keySplit := strings.Split(string(iterator.Key()), "/") + + ports = append(ports, keySplit[1]) + } + + return ports } // BindPort stores the provided portID and binds to it, returning the associated capability diff --git a/modules/apps/27-interchain-accounts/keeper/keeper_test.go b/modules/apps/27-interchain-accounts/keeper/keeper_test.go index 2c4d43571c3..307451fd14b 100644 --- a/modules/apps/27-interchain-accounts/keeper/keeper_test.go +++ b/modules/apps/27-interchain-accounts/keeper/keeper_test.go @@ -108,9 +108,17 @@ func (suite *KeeperTestSuite) TestIsBound() { suite.Require().True(isBound) } -func (suite *KeeperTestSuite) TestGetPort() { - port := suite.chainA.GetSimApp().ICAKeeper.GetPort(suite.chainA.GetContext(), types.PortID) - suite.Require().Equal(string([]byte{0x01}), port) +func (suite *KeeperTestSuite) TestGetAllPorts() { + suite.SetupTest() + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + ports := suite.chainA.GetSimApp().ICAKeeper.GetAllPorts(suite.chainA.GetContext()) + suite.Require().Contains(ports, types.PortID) + suite.Require().Contains(ports, TestPortID) } func (suite *KeeperTestSuite) TestGetInterchainAccountAddress() {