Skip to content

Commit

Permalink
chore: interchain accounts cleanup, cli alias (#362)
Browse files Browse the repository at this point in the history
* adding ica alias for interchain-accounts queries

* refactoring bind port and claim capability functionality, code cleanup and godocs
  • Loading branch information
damiannolan authored Aug 31, 2021
1 parent 0174855 commit 01bb9bf
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 50 deletions.
1 change: 1 addition & 0 deletions modules/apps/27-interchain-accounts/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
func GetQueryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "interchain-accounts",
Aliases: []string{"ica"},
Short: "Querying commands for the interchain accounts module",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
Expand Down
7 changes: 5 additions & 2 deletions modules/apps/27-interchain-accounts/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ibc-go/modules/apps/27-interchain-accounts/keeper"
"github.com/cosmos/ibc-go/modules/apps/27-interchain-accounts/types"

host "github.com/cosmos/ibc-go/modules/core/24-host"
)

// InitGenesis initializes the interchain accounts application state from a provided genesis state
func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, state types.GenesisState) {
if !keeper.IsBound(ctx, state.PortId) {
err := keeper.BindPort(ctx, state.PortId)
if err != nil {
cap := keeper.BindPort(ctx, state.PortId)
if err := keeper.ClaimCapability(ctx, cap, host.PortPath(state.PortId)); err != nil {
panic(fmt.Sprintf("could not claim port capability: %v", err))
}
}
Expand Down
23 changes: 10 additions & 13 deletions modules/apps/27-interchain-accounts/keeper/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,21 @@ import (
// already in use. Gaining access to interchain accounts whose channels have closed
// cannot be done with this function. A regular MsgChanOpenInit must be used.
func (k Keeper) InitInterchainAccount(ctx sdk.Context, connectionID, counterpartyConnectionID, owner string) error {
portId, err := types.GeneratePortID(owner, connectionID, counterpartyConnectionID)
portID, err := types.GeneratePortID(owner, connectionID, counterpartyConnectionID)
if err != nil {
return err
}

// check if the port is already bound
if k.IsBound(ctx, portId) {
return sdkerrors.Wrap(types.ErrPortAlreadyBound, portId)
if k.IsBound(ctx, portID) {
return sdkerrors.Wrap(types.ErrPortAlreadyBound, portID)
}

portCap := k.portKeeper.BindPort(ctx, portId)
err = k.ClaimCapability(ctx, portCap, host.PortPath(portId))
if err != nil {
cap := k.BindPort(ctx, portID)
if err := k.ClaimCapability(ctx, cap, host.PortPath(portID)); err != nil {
return sdkerrors.Wrap(err, "unable to bind to newly generated portID")
}

msg := channeltypes.NewMsgChannelOpenInit(portId, types.Version, channeltypes.ORDERED, []string{connectionID}, types.PortID, types.ModuleName)
msg := channeltypes.NewMsgChannelOpenInit(portID, types.Version, channeltypes.ORDERED, []string{connectionID}, types.PortID, types.ModuleName)
handler := k.msgRouter.Handler(msg)
if _, err := handler(ctx, msg); err != nil {
return err
Expand All @@ -43,23 +41,22 @@ func (k Keeper) InitInterchainAccount(ctx sdk.Context, connectionID, counterpart
}

// Register interchain account if it has not already been created
func (k Keeper) RegisterInterchainAccount(ctx sdk.Context, portId string) {
address := types.GenerateAddress(portId)
func (k Keeper) RegisterInterchainAccount(ctx sdk.Context, portID string) {
address := types.GenerateAddress(portID)

account := k.accountKeeper.GetAccount(ctx, address)
if account != nil {
// account already created, return no-op
return
}

interchainAccount := types.NewInterchainAccount(
authtypes.NewBaseAccountWithAddress(address),
portId,
portID,
)

k.accountKeeper.NewAccount(ctx, interchainAccount)
k.accountKeeper.SetAccount(ctx, interchainAccount)
k.SetInterchainAccountAddress(ctx, portId, interchainAccount.Address)
k.SetInterchainAccountAddress(ctx, portID, interchainAccount.Address)
}

func (k Keeper) GetInterchainAccount(ctx sdk.Context, addr sdk.AccAddress) (types.InterchainAccount, error) {
Expand Down
66 changes: 31 additions & 35 deletions modules/apps/27-interchain-accounts/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
host "github.com/cosmos/ibc-go/modules/core/24-host"
"github.com/tendermint/tendermint/libs/log"

"github.com/cosmos/ibc-go/modules/apps/27-interchain-accounts/types"
host "github.com/cosmos/ibc-go/modules/core/24-host"
)

// Keeper defines the IBC transfer keeper
Expand Down Expand Up @@ -49,6 +49,7 @@ func NewKeeper(
}
}

// SerializeCosmosTx marshals data to bytes using the provided codec
func (k Keeper) SerializeCosmosTx(cdc codec.BinaryCodec, data interface{}) ([]byte, error) {
msgs := make([]sdk.Msg, 0)
switch data := data.(type) {
Expand Down Expand Up @@ -86,68 +87,63 @@ func (k Keeper) SerializeCosmosTx(cdc codec.BinaryCodec, data interface{}) ([]by
return bz, nil
}

// 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", host.ModuleName, types.ModuleName))
}

// IsBound checks if the interchain account module is already bound to the desired port
func (k Keeper) IsBound(ctx sdk.Context, portID string) bool {
_, ok := k.scopedKeeper.GetCapability(ctx, host.PortPath(portID))
return ok
// GetPort returns the portID for the interchain accounts module. Used in ExportGenesis
func (k Keeper) GetPort(ctx sdk.Context) string {
store := ctx.KVStore(k.storeKey)
return string(store.Get([]byte(types.PortKey)))
}

// BindPort defines a wrapper function for the port Keeper's BindPort function in
// order to expose it to module's InitGenesis function
func (k Keeper) BindPort(ctx sdk.Context, portID string) error {
// Set the portID into our store so we can retrieve it later
// 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))

cap := k.portKeeper.BindPort(ctx, portID)
return k.ClaimCapability(ctx, cap, host.PortPath(portID))
return k.portKeeper.BindPort(ctx, portID)
}

// GetPort returns the portID for the interchain accounts module. Used in ExportGenesis
func (k Keeper) GetPort(ctx sdk.Context) string {
store := ctx.KVStore(k.storeKey)
return string(store.Get([]byte(types.PortKey)))
// IsBound checks if the interchain account module is already bound to the desired port
func (k Keeper) IsBound(ctx sdk.Context, portID string) bool {
_, ok := k.scopedKeeper.GetCapability(ctx, host.PortPath(portID))
return ok
}

// ClaimCapability allows the transfer module that can claim a capability that IBC module
// passes to it
func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error {
return k.scopedKeeper.ClaimCapability(ctx, cap, name)
// AuthenticateCapability wraps the scopedKeeper's AuthenticateCapability function
func (k Keeper) AuthenticateCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) bool {
return k.scopedKeeper.AuthenticateCapability(ctx, cap, name)
}

func (k Keeper) SetActiveChannel(ctx sdk.Context, portId, channelId string) error {
store := ctx.KVStore(k.storeKey)

key := types.KeyActiveChannel(portId)
store.Set(key, []byte(channelId))
return nil
// ClaimCapability wraps the scopedKeeper's ClaimCapability function
func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error {
return k.scopedKeeper.ClaimCapability(ctx, cap, name)
}

// GetActiveChannel retrieves the active channelID from the store keyed by the provided portID
func (k Keeper) GetActiveChannel(ctx sdk.Context, portId string) (string, bool) {
store := ctx.KVStore(k.storeKey)
key := types.KeyActiveChannel(portId)

if !store.Has(key) {
return "", false
}

activeChannel := string(store.Get(key))
return activeChannel, true
return string(store.Get(key)), true
}

// IsActiveChannel returns true if there exists an active channel for
// the provided portID and false otherwise.
func (k Keeper) IsActiveChannel(ctx sdk.Context, portId string) bool {
_, found := k.GetActiveChannel(ctx, portId)
return found
// SetActiveChannel stores the active channelID, keyed by the provided portID
func (k Keeper) SetActiveChannel(ctx sdk.Context, portID, channelID string) {
store := ctx.KVStore(k.storeKey)
store.Set(types.KeyActiveChannel(portID), []byte(channelID))
}

// AuthenticateCapability wraps the scopedKeeper's AuthenticateCapability function
func (k Keeper) AuthenticateCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) bool {
return k.scopedKeeper.AuthenticateCapability(ctx, cap, name)
// IsActiveChannel returns true if there exists an active channel for the provided portID, otherwise false
func (k Keeper) IsActiveChannel(ctx sdk.Context, portID string) bool {
_, ok := k.GetActiveChannel(ctx, portID)
return ok
}

// GetInterchainAccountAddress retrieves the InterchainAccount address from the store keyed by the provided portID
Expand Down

0 comments on commit 01bb9bf

Please sign in to comment.