Skip to content

Commit 01bb9bf

Browse files
authored
chore: interchain accounts cleanup, cli alias (#362)
* adding ica alias for interchain-accounts queries * refactoring bind port and claim capability functionality, code cleanup and godocs
1 parent 0174855 commit 01bb9bf

File tree

4 files changed

+47
-50
lines changed

4 files changed

+47
-50
lines changed

modules/apps/27-interchain-accounts/client/cli/query.go

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
func GetQueryCmd() *cobra.Command {
1414
cmd := &cobra.Command{
1515
Use: "interchain-accounts",
16+
Aliases: []string{"ica"},
1617
Short: "Querying commands for the interchain accounts module",
1718
DisableFlagParsing: true,
1819
SuggestionsMinimumDistance: 2,

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ import (
66
sdk "github.com/cosmos/cosmos-sdk/types"
77
"github.com/cosmos/ibc-go/modules/apps/27-interchain-accounts/keeper"
88
"github.com/cosmos/ibc-go/modules/apps/27-interchain-accounts/types"
9+
10+
host "github.com/cosmos/ibc-go/modules/core/24-host"
911
)
1012

13+
// InitGenesis initializes the interchain accounts application state from a provided genesis state
1114
func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, state types.GenesisState) {
1215
if !keeper.IsBound(ctx, state.PortId) {
13-
err := keeper.BindPort(ctx, state.PortId)
14-
if err != nil {
16+
cap := keeper.BindPort(ctx, state.PortId)
17+
if err := keeper.ClaimCapability(ctx, cap, host.PortPath(state.PortId)); err != nil {
1518
panic(fmt.Sprintf("could not claim port capability: %v", err))
1619
}
1720
}

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

+10-13
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,21 @@ import (
1717
// already in use. Gaining access to interchain accounts whose channels have closed
1818
// cannot be done with this function. A regular MsgChanOpenInit must be used.
1919
func (k Keeper) InitInterchainAccount(ctx sdk.Context, connectionID, counterpartyConnectionID, owner string) error {
20-
portId, err := types.GeneratePortID(owner, connectionID, counterpartyConnectionID)
20+
portID, err := types.GeneratePortID(owner, connectionID, counterpartyConnectionID)
2121
if err != nil {
2222
return err
2323
}
2424

25-
// check if the port is already bound
26-
if k.IsBound(ctx, portId) {
27-
return sdkerrors.Wrap(types.ErrPortAlreadyBound, portId)
25+
if k.IsBound(ctx, portID) {
26+
return sdkerrors.Wrap(types.ErrPortAlreadyBound, portID)
2827
}
2928

30-
portCap := k.portKeeper.BindPort(ctx, portId)
31-
err = k.ClaimCapability(ctx, portCap, host.PortPath(portId))
32-
if err != nil {
29+
cap := k.BindPort(ctx, portID)
30+
if err := k.ClaimCapability(ctx, cap, host.PortPath(portID)); err != nil {
3331
return sdkerrors.Wrap(err, "unable to bind to newly generated portID")
3432
}
3533

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

4543
// Register interchain account if it has not already been created
46-
func (k Keeper) RegisterInterchainAccount(ctx sdk.Context, portId string) {
47-
address := types.GenerateAddress(portId)
44+
func (k Keeper) RegisterInterchainAccount(ctx sdk.Context, portID string) {
45+
address := types.GenerateAddress(portID)
4846

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

5552
interchainAccount := types.NewInterchainAccount(
5653
authtypes.NewBaseAccountWithAddress(address),
57-
portId,
54+
portID,
5855
)
5956

6057
k.accountKeeper.NewAccount(ctx, interchainAccount)
6158
k.accountKeeper.SetAccount(ctx, interchainAccount)
62-
k.SetInterchainAccountAddress(ctx, portId, interchainAccount.Address)
59+
k.SetInterchainAccountAddress(ctx, portID, interchainAccount.Address)
6360
}
6461

6562
func (k Keeper) GetInterchainAccount(ctx sdk.Context, addr sdk.AccAddress) (types.InterchainAccount, error) {

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

+31-35
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99
sdk "github.com/cosmos/cosmos-sdk/types"
1010
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
1111
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
12-
host "github.com/cosmos/ibc-go/modules/core/24-host"
1312
"github.com/tendermint/tendermint/libs/log"
1413

1514
"github.com/cosmos/ibc-go/modules/apps/27-interchain-accounts/types"
15+
host "github.com/cosmos/ibc-go/modules/core/24-host"
1616
)
1717

1818
// Keeper defines the IBC transfer keeper
@@ -49,6 +49,7 @@ func NewKeeper(
4949
}
5050
}
5151

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

90+
// Logger returns the application logger, scoped to the associated module
8991
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
9092
return ctx.Logger().With("module", fmt.Sprintf("x/%s-%s", host.ModuleName, types.ModuleName))
9193
}
9294

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

99-
// BindPort defines a wrapper function for the port Keeper's BindPort function in
100-
// order to expose it to module's InitGenesis function
101-
func (k Keeper) BindPort(ctx sdk.Context, portID string) error {
102-
// Set the portID into our store so we can retrieve it later
101+
// BindPort stores the provided portID and binds to it, returning the associated capability
102+
func (k Keeper) BindPort(ctx sdk.Context, portID string) *capabilitytypes.Capability {
103103
store := ctx.KVStore(k.storeKey)
104104
store.Set([]byte(types.PortKey), []byte(portID))
105105

106-
cap := k.portKeeper.BindPort(ctx, portID)
107-
return k.ClaimCapability(ctx, cap, host.PortPath(portID))
106+
return k.portKeeper.BindPort(ctx, portID)
108107
}
109108

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

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

122-
func (k Keeper) SetActiveChannel(ctx sdk.Context, portId, channelId string) error {
123-
store := ctx.KVStore(k.storeKey)
124-
125-
key := types.KeyActiveChannel(portId)
126-
store.Set(key, []byte(channelId))
127-
return nil
120+
// ClaimCapability wraps the scopedKeeper's ClaimCapability function
121+
func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error {
122+
return k.scopedKeeper.ClaimCapability(ctx, cap, name)
128123
}
129124

125+
// GetActiveChannel retrieves the active channelID from the store keyed by the provided portID
130126
func (k Keeper) GetActiveChannel(ctx sdk.Context, portId string) (string, bool) {
131127
store := ctx.KVStore(k.storeKey)
132128
key := types.KeyActiveChannel(portId)
129+
133130
if !store.Has(key) {
134131
return "", false
135132
}
136133

137-
activeChannel := string(store.Get(key))
138-
return activeChannel, true
134+
return string(store.Get(key)), true
139135
}
140136

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

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

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

0 commit comments

Comments
 (0)