diff --git a/modules/apps/27-interchain-accounts/client/cli/query.go b/modules/apps/27-interchain-accounts/client/cli/query.go index 8b26d77dbff..55149f2b0fd 100644 --- a/modules/apps/27-interchain-accounts/client/cli/query.go +++ b/modules/apps/27-interchain-accounts/client/cli/query.go @@ -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, diff --git a/modules/apps/27-interchain-accounts/genesis.go b/modules/apps/27-interchain-accounts/genesis.go index 5a9569b54e6..44b15e0065b 100644 --- a/modules/apps/27-interchain-accounts/genesis.go +++ b/modules/apps/27-interchain-accounts/genesis.go @@ -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)) } } diff --git a/modules/apps/27-interchain-accounts/keeper/account.go b/modules/apps/27-interchain-accounts/keeper/account.go index 98883cd0dba..43e3bc4b143 100644 --- a/modules/apps/27-interchain-accounts/keeper/account.go +++ b/modules/apps/27-interchain-accounts/keeper/account.go @@ -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 @@ -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) { diff --git a/modules/apps/27-interchain-accounts/keeper/keeper.go b/modules/apps/27-interchain-accounts/keeper/keeper.go index 17dd769295e..d45fd9037aa 100644 --- a/modules/apps/27-interchain-accounts/keeper/keeper.go +++ b/modules/apps/27-interchain-accounts/keeper/keeper.go @@ -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 @@ -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) { @@ -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