Skip to content

Commit 0ae4a75

Browse files
chore: update portkey to include port ID (#467)
* update port key to use prefix, separate key prefixes to vars * updating godoc * Update modules/apps/27-interchain-accounts/keeper/keeper.go Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * adding todo with ica genesis issue ref * fixing failing test from browser commit * removing GetPort in favour of GetAllPorts Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>
1 parent 9450085 commit 0ae4a75

File tree

4 files changed

+45
-14
lines changed

4 files changed

+45
-14
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, state types.GenesisState
2222

2323
// ExportGenesis exports transfer module's portID into its geneis state
2424
func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) *types.GenesisState {
25-
portID := keeper.GetPort(ctx)
25+
// TODO: Using a range query with KVStorePrefixIterator export all port IDs
26+
// See https://github.com/cosmos/ibc-go/issues/448
2627

2728
return &types.GenesisState{
28-
PortId: portID,
29+
PortId: types.PortID,
2930
}
3031
}

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

+15-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package keeper
22

33
import (
44
"fmt"
5+
"strings"
56

67
baseapp "github.com/cosmos/cosmos-sdk/baseapp"
78
"github.com/cosmos/cosmos-sdk/codec"
@@ -83,16 +84,26 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
8384
return ctx.Logger().With("module", fmt.Sprintf("x/%s-%s", host.ModuleName, types.ModuleName))
8485
}
8586

86-
// GetPort returns the portID for the interchain accounts module. Used in ExportGenesis
87-
func (k Keeper) GetPort(ctx sdk.Context) string {
87+
// GetAllPorts returns all ports to which the interchain accounts module is bound. Used in ExportGenesis
88+
func (k Keeper) GetAllPorts(ctx sdk.Context) []string {
8889
store := ctx.KVStore(k.storeKey)
89-
return string(store.Get([]byte(types.PortKey)))
90+
iterator := sdk.KVStorePrefixIterator(store, []byte(types.PortKeyPrefix))
91+
defer iterator.Close()
92+
93+
var ports []string
94+
for ; iterator.Valid(); iterator.Next() {
95+
keySplit := strings.Split(string(iterator.Key()), "/")
96+
97+
ports = append(ports, keySplit[1])
98+
}
99+
100+
return ports
90101
}
91102

92103
// BindPort stores the provided portID and binds to it, returning the associated capability
93104
func (k Keeper) BindPort(ctx sdk.Context, portID string) *capabilitytypes.Capability {
94105
store := ctx.KVStore(k.storeKey)
95-
store.Set([]byte(types.PortKey), []byte(portID))
106+
store.Set(types.KeyPort(portID), []byte{0x01})
96107

97108
return k.portKeeper.BindPort(ctx, portID)
98109
}

modules/apps/27-interchain-accounts/keeper/keeper_test.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,17 @@ func (suite *KeeperTestSuite) TestIsBound() {
108108
suite.Require().True(isBound)
109109
}
110110

111-
func (suite *KeeperTestSuite) TestGetPort() {
112-
port := suite.chainA.GetSimApp().ICAKeeper.GetPort(suite.chainA.GetContext())
113-
suite.Require().Equal(types.PortID, port)
111+
func (suite *KeeperTestSuite) TestGetAllPorts() {
112+
suite.SetupTest()
113+
path := NewICAPath(suite.chainA, suite.chainB)
114+
suite.coordinator.SetupConnections(path)
115+
116+
err := SetupICAPath(path, TestOwnerAddress)
117+
suite.Require().NoError(err)
118+
119+
ports := suite.chainA.GetSimApp().ICAKeeper.GetAllPorts(suite.chainA.GetContext())
120+
suite.Require().Contains(ports, types.PortID)
121+
suite.Require().Contains(ports, TestPortID)
114122
}
115123

116124
func (suite *KeeperTestSuite) TestGetInterchainAccountAddress() {

modules/apps/27-interchain-accounts/types/keys.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,14 @@ const (
3838
)
3939

4040
var (
41-
// PortKey defines the key to store the port ID in store
42-
PortKey = []byte{0x01}
41+
// ActiveChannelKeyPrefix defines the key prefix used to store active channels
42+
ActiveChannelKeyPrefix = "activeChannel"
43+
44+
// OwnerKeyPrefix defines the key prefix used to store interchain accounts
45+
OwnerKeyPrefix = "owner"
46+
47+
// PortKeyPrefix defines the key prefix used to store ports
48+
PortKeyPrefix = "port"
4349
)
4450

4551
// NewVersion returns a complete version string in the format: VersionPrefix + Delimter + AccAddress
@@ -49,12 +55,17 @@ func NewAppVersion(versionPrefix, accAddr string) string {
4955

5056
// KeyActiveChannel creates and returns a new key used for active channels store operations
5157
func KeyActiveChannel(portID string) []byte {
52-
return []byte(fmt.Sprintf("activeChannel/%s", portID))
58+
return []byte(fmt.Sprintf("%s/%s", ActiveChannelKeyPrefix, portID))
5359
}
5460

55-
// KeyOwnerAccount creates and returns a new key used for owner account store operations
61+
// KeyOwnerAccount creates and returns a new key used for interchain account store operations
5662
func KeyOwnerAccount(portID string) []byte {
57-
return []byte(fmt.Sprintf("owner/%s", portID))
63+
return []byte(fmt.Sprintf("%s/%s", OwnerKeyPrefix, portID))
64+
}
65+
66+
// KeyPort creates and returns a new key used for port store operations
67+
func KeyPort(portID string) []byte {
68+
return []byte(fmt.Sprintf("%s/%s", PortKeyPrefix, portID))
5869
}
5970

6071
// ParseControllerConnSequence attempts to parse the controller connection sequence from the provided port identifier

0 commit comments

Comments
 (0)