Skip to content

Commit

Permalink
chore: update portkey to include port ID (#467)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* 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 <[email protected]>
  • Loading branch information
damiannolan and colin-axner authored Oct 11, 2021
1 parent 9450085 commit 0ae4a75
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
5 changes: 3 additions & 2 deletions modules/apps/27-interchain-accounts/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ 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)
// TODO: Using a range query with KVStorePrefixIterator export all port IDs
// See https://github.com/cosmos/ibc-go/issues/448

return &types.GenesisState{
PortId: portID,
PortId: types.PortID,
}
}
19 changes: 15 additions & 4 deletions modules/apps/27-interchain-accounts/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"fmt"
"strings"

baseapp "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -83,16 +84,26 @@ 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) 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([]byte(types.PortKey)))
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
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{0x01})

return k.portKeeper.BindPort(ctx, portID)
}
Expand Down
14 changes: 11 additions & 3 deletions modules/apps/27-interchain-accounts/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
suite.Require().Equal(types.PortID, 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() {
Expand Down
21 changes: 16 additions & 5 deletions modules/apps/27-interchain-accounts/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,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
Expand All @@ -49,12 +55,17 @@ 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
// KeyOwnerAccount creates and returns a new key used for interchain 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))
}

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

0 comments on commit 0ae4a75

Please sign in to comment.