diff --git a/x/capability/keeper/keeper.go b/x/capability/keeper/keeper.go index 85d0be4a6..b80efc244 100644 --- a/x/capability/keeper/keeper.go +++ b/x/capability/keeper/keeper.go @@ -369,16 +369,18 @@ func (sk ScopedKeeper) GetOwners(ctx sdk.Context, name string) (*types.Capabilit } // LookupModules returns all the module owners for a given capability -// as a string array, the capability is also returned along with a boolean success flag -func (sk ScopedKeeper) LookupModules(ctx sdk.Context, name string) ([]string, *types.Capability, bool) { +// as a string array and the capability itself. +// The method returns an errors if either the capability or the owners cannot be +// retreived from the memstore. +func (sk ScopedKeeper) LookupModules(ctx sdk.Context, name string) ([]string, *types.Capability, error) { cap, ok := sk.GetCapability(ctx, name) if !ok { - return nil, nil, false + return nil, nil, sdkerrors.Wrap(types.ErrCapabilityNotFound, name) } capOwners, ok := sk.GetOwners(ctx, name) if !ok { - return nil, nil, false + return nil, nil, sdkerrors.Wrap(types.ErrCapabilityOwnersNotFound, name) } mods := make([]string, len(capOwners.Owners)) @@ -386,7 +388,7 @@ func (sk ScopedKeeper) LookupModules(ctx sdk.Context, name string) ([]string, *t mods[i] = co.Module } - return mods, cap, true + return mods, cap, nil } func (sk ScopedKeeper) addOwner(ctx sdk.Context, cap *types.Capability, name string) error { diff --git a/x/capability/keeper/keeper_test.go b/x/capability/keeper/keeper_test.go index c025deb15..d74eb6b32 100644 --- a/x/capability/keeper/keeper_test.go +++ b/x/capability/keeper/keeper_test.go @@ -177,12 +177,12 @@ func (suite *KeeperTestSuite) TestGetOwners() { // Ensure all scoped keepers can get owners for _, sk := range sks { owners, ok := sk.GetOwners(suite.ctx, "transfer") - mods, cap, mok := sk.LookupModules(suite.ctx, "transfer") + mods, cap, err := sk.LookupModules(suite.ctx, "transfer") suite.Require().True(ok, "could not retrieve owners") suite.Require().NotNil(owners, "owners is nil") - suite.Require().True(mok, "could not retrieve modules") + suite.Require().NoError(err, "could not retrieve modules") suite.Require().NotNil(cap, "capability is nil") suite.Require().NotNil(mods, "modules is nil") @@ -205,12 +205,12 @@ func (suite *KeeperTestSuite) TestGetOwners() { // Ensure all scoped keepers can get owners for _, sk := range sks { owners, ok := sk.GetOwners(suite.ctx, "transfer") - mods, cap, mok := sk.LookupModules(suite.ctx, "transfer") + mods, cap, err := sk.LookupModules(suite.ctx, "transfer") suite.Require().True(ok, "could not retrieve owners") suite.Require().NotNil(owners, "owners is nil") - suite.Require().True(mok, "could not retrieve modules") + suite.Require().NoError(err, "could not retrieve modules") suite.Require().NotNil(cap, "capability is nil") suite.Require().NotNil(mods, "modules is nil") diff --git a/x/capability/types/errors.go b/x/capability/types/errors.go index bc3354789..93f185426 100644 --- a/x/capability/types/errors.go +++ b/x/capability/types/errors.go @@ -8,7 +8,9 @@ import ( // x/capability module sentinel errors var ( - ErrCapabilityTaken = sdkerrors.Register(ModuleName, 2, "capability name already taken") - ErrOwnerClaimed = sdkerrors.Register(ModuleName, 3, "given owner already claimed capability") - ErrCapabilityNotOwned = sdkerrors.Register(ModuleName, 4, "capability not owned by module") + ErrCapabilityTaken = sdkerrors.Register(ModuleName, 2, "capability name already taken") + ErrOwnerClaimed = sdkerrors.Register(ModuleName, 3, "given owner already claimed capability") + ErrCapabilityNotOwned = sdkerrors.Register(ModuleName, 4, "capability not owned by module") + ErrCapabilityNotFound = sdkerrors.Register(ModuleName, 5, "capability not found") + ErrCapabilityOwnersNotFound = sdkerrors.Register(ModuleName, 6, "owners not found for capability") ) diff --git a/x/ibc/04-channel/keeper/handshake.go b/x/ibc/04-channel/keeper/handshake.go index 5ab7042c0..6eec946b7 100644 --- a/x/ibc/04-channel/keeper/handshake.go +++ b/x/ibc/04-channel/keeper/handshake.go @@ -1,6 +1,8 @@ package keeper import ( + "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/x/capability" @@ -74,7 +76,7 @@ func (k Keeper) ChanOpenInit( k.SetNextSequenceSend(ctx, portID, channelID, 1) k.SetNextSequenceRecv(ctx, portID, channelID, 1) - k.Logger(ctx).Info("channel (port-id: %s, channel-id: %s) state updated: NONE -> INIT", portID, channelID) + k.Logger(ctx).Info(fmt.Sprintf("channel (port-id: %s, channel-id: %s) state updated: NONE -> INIT", portID, channelID)) return capKey, nil } @@ -155,7 +157,7 @@ func (k Keeper) ChanOpenTry( k.SetNextSequenceSend(ctx, portID, channelID, 1) k.SetNextSequenceRecv(ctx, portID, channelID, 1) - k.Logger(ctx).Info("channel (port-id: %s, channel-id: %s) state updated: NONE -> TRYOPEN", portID, channelID) + k.Logger(ctx).Info(fmt.Sprintf("channel (port-id: %s, channel-id: %s) state updated: NONE -> TRYOPEN", portID, channelID)) return capKey, nil } @@ -223,7 +225,7 @@ func (k Keeper) ChanOpenAck( channel.Version = counterpartyVersion k.SetChannel(ctx, portID, channelID, channel) - k.Logger(ctx).Info("channel (port-id: %s, channel-id: %s) state updated: INIT -> OPEN", portID, channelID) + k.Logger(ctx).Info(fmt.Sprintf("channel (port-id: %s, channel-id: %s) state updated: INIT -> OPEN", portID, channelID)) return nil } @@ -288,7 +290,7 @@ func (k Keeper) ChanOpenConfirm( channel.State = ibctypes.OPEN k.SetChannel(ctx, portID, channelID, channel) - k.Logger(ctx).Info("channel (port-id: %s, channel-id: %s) state updated: TRYOPEN -> OPEN", portID, channelID) + k.Logger(ctx).Info(fmt.Sprintf("channel (port-id: %s, channel-id: %s) state updated: TRYOPEN -> OPEN", portID, channelID)) return nil } @@ -330,7 +332,7 @@ func (k Keeper) ChanCloseInit( ) } - k.Logger(ctx).Info("channel (port-id: %s, channel-id: %s) state updated: %s -> CLOSED", portID, channelID, channel.State) + k.Logger(ctx).Info(fmt.Sprintf("channel (port-id: %s, channel-id: %s) state updated: %s -> CLOSED", portID, channelID, channel.State)) channel.State = ibctypes.CLOSED k.SetChannel(ctx, portID, channelID, channel) @@ -393,7 +395,7 @@ func (k Keeper) ChanCloseConfirm( return err } - k.Logger(ctx).Info("channel (port-id: %s, channel-id: %s) state updated: %s -> CLOSED", portID, channelID, channel.State) + k.Logger(ctx).Info(fmt.Sprintf("channel (port-id: %s, channel-id: %s) state updated: %s -> CLOSED", portID, channelID, channel.State)) channel.State = ibctypes.CLOSED k.SetChannel(ctx, portID, channelID, channel) diff --git a/x/ibc/04-channel/keeper/keeper.go b/x/ibc/04-channel/keeper/keeper.go index 8526bb78f..d44d7ee5a 100644 --- a/x/ibc/04-channel/keeper/keeper.go +++ b/x/ibc/04-channel/keeper/keeper.go @@ -251,13 +251,13 @@ func (k Keeper) GetAllChannels(ctx sdk.Context) (channels []types.IdentifiedChan } // LookupModuleByChannel will return the IBCModule along with the capability associated with a given channel defined by its portID and channelID -func (k Keeper) LookupModuleByChannel(ctx sdk.Context, portID, channelID string) (string, *capability.Capability, bool) { - modules, cap, ok := k.scopedKeeper.LookupModules(ctx, ibctypes.ChannelCapabilityPath(portID, channelID)) - if !ok { - return "", nil, false +func (k Keeper) LookupModuleByChannel(ctx sdk.Context, portID, channelID string) (string, *capability.Capability, error) { + modules, cap, err := k.scopedKeeper.LookupModules(ctx, ibctypes.ChannelCapabilityPath(portID, channelID)) + if err != nil { + return "", nil, err } - return ibctypes.GetModuleOwner(modules), cap, true + return ibctypes.GetModuleOwner(modules), cap, nil } // common functionality for IteratePacketCommitment and IteratePacketAcknowledgemen diff --git a/x/ibc/05-port/keeper/keeper.go b/x/ibc/05-port/keeper/keeper.go index 5882b6048..e50422d34 100644 --- a/x/ibc/05-port/keeper/keeper.go +++ b/x/ibc/05-port/keeper/keeper.go @@ -70,12 +70,12 @@ func (k Keeper) Authenticate(ctx sdk.Context, key *capability.Capability, portID } // LookupModuleByPort will return the IBCModule along with the capability associated with a given portID -func (k Keeper) LookupModuleByPort(ctx sdk.Context, portID string) (string, *capability.Capability, bool) { - modules, cap, ok := k.scopedKeeper.LookupModules(ctx, ibctypes.PortPath(portID)) - if !ok { - return "", nil, false +func (k Keeper) LookupModuleByPort(ctx sdk.Context, portID string) (string, *capability.Capability, error) { + modules, cap, err := k.scopedKeeper.LookupModules(ctx, ibctypes.PortPath(portID)) + if err != nil { + return "", nil, err } - return ibctypes.GetModuleOwner(modules), cap, true + return ibctypes.GetModuleOwner(modules), cap, nil } diff --git a/x/ibc/handler.go b/x/ibc/handler.go index c4b3e4da1..f5f3f4b7d 100644 --- a/x/ibc/handler.go +++ b/x/ibc/handler.go @@ -39,10 +39,11 @@ func NewHandler(k Keeper) sdk.Handler { // IBC channel msgs case channel.MsgChannelOpenInit: // Lookup module by port capability - module, portCap, ok := k.PortKeeper.LookupModuleByPort(ctx, msg.PortID) - if !ok { - return nil, sdkerrors.Wrap(port.ErrInvalidPort, "could not retrieve module from portID") + module, portCap, err := k.PortKeeper.LookupModuleByPort(ctx, msg.PortID) + if err != nil { + return nil, sdkerrors.Wrap(err, "could not retrieve module from port-id") } + res, cap, err := channel.HandleMsgChannelOpenInit(ctx, k.ChannelKeeper, portCap, msg) if err != nil { return nil, err @@ -61,9 +62,9 @@ func NewHandler(k Keeper) sdk.Handler { case channel.MsgChannelOpenTry: // Lookup module by port capability - module, portCap, ok := k.PortKeeper.LookupModuleByPort(ctx, msg.PortID) - if !ok { - return nil, sdkerrors.Wrap(port.ErrInvalidPort, "could not retrieve module from portID") + module, portCap, err := k.PortKeeper.LookupModuleByPort(ctx, msg.PortID) + if err != nil { + return nil, sdkerrors.Wrap(err, "could not retrieve module from port-id") } res, cap, err := channel.HandleMsgChannelOpenTry(ctx, k.ChannelKeeper, portCap, msg) if err != nil { @@ -83,16 +84,17 @@ func NewHandler(k Keeper) sdk.Handler { case channel.MsgChannelOpenAck: // Lookup module by channel capability - module, cap, ok := k.ChannelKeeper.LookupModuleByChannel(ctx, msg.PortID, msg.ChannelID) - if !ok { - return nil, sdkerrors.Wrap(channel.ErrChannelCapabilityNotFound, "could not retrieve module from channel capability") + module, cap, err := k.ChannelKeeper.LookupModuleByChannel(ctx, msg.PortID, msg.ChannelID) + if err != nil { + return nil, sdkerrors.Wrap(err, "could not retrieve module from port-id") } // Retrieve callbacks from router cbs, ok := k.Router.GetRoute(module) if !ok { return nil, sdkerrors.Wrapf(port.ErrInvalidRoute, "route not found to module: %s", module) } - err := cbs.OnChanOpenAck(ctx, msg.PortID, msg.ChannelID, msg.CounterpartyVersion) + + err = cbs.OnChanOpenAck(ctx, msg.PortID, msg.ChannelID, msg.CounterpartyVersion) if err != nil { return nil, err } @@ -100,9 +102,9 @@ func NewHandler(k Keeper) sdk.Handler { case channel.MsgChannelOpenConfirm: // Lookup module by channel capability - module, cap, ok := k.ChannelKeeper.LookupModuleByChannel(ctx, msg.PortID, msg.ChannelID) - if !ok { - return nil, sdkerrors.Wrap(channel.ErrChannelCapabilityNotFound, "could not retrieve module from channel capability") + module, cap, err := k.ChannelKeeper.LookupModuleByChannel(ctx, msg.PortID, msg.ChannelID) + if err != nil { + return nil, sdkerrors.Wrap(err, "could not retrieve module from port-id") } // Retrieve callbacks from router cbs, ok := k.Router.GetRoute(module) @@ -110,7 +112,7 @@ func NewHandler(k Keeper) sdk.Handler { return nil, sdkerrors.Wrapf(port.ErrInvalidRoute, "route not found to module: %s", module) } - err := cbs.OnChanOpenConfirm(ctx, msg.PortID, msg.ChannelID) + err = cbs.OnChanOpenConfirm(ctx, msg.PortID, msg.ChannelID) if err != nil { return nil, err } @@ -118,9 +120,9 @@ func NewHandler(k Keeper) sdk.Handler { case channel.MsgChannelCloseInit: // Lookup module by channel capability - module, cap, ok := k.ChannelKeeper.LookupModuleByChannel(ctx, msg.PortID, msg.ChannelID) - if !ok { - return nil, sdkerrors.Wrap(channel.ErrChannelCapabilityNotFound, "could not retrieve module from channel capability") + module, cap, err := k.ChannelKeeper.LookupModuleByChannel(ctx, msg.PortID, msg.ChannelID) + if err != nil { + return nil, sdkerrors.Wrap(err, "could not retrieve module from port-id") } // Retrieve callbacks from router cbs, ok := k.Router.GetRoute(module) @@ -128,7 +130,7 @@ func NewHandler(k Keeper) sdk.Handler { return nil, sdkerrors.Wrapf(port.ErrInvalidRoute, "route not found to module: %s", module) } - err := cbs.OnChanCloseInit(ctx, msg.PortID, msg.ChannelID) + err = cbs.OnChanCloseInit(ctx, msg.PortID, msg.ChannelID) if err != nil { return nil, err } @@ -136,9 +138,9 @@ func NewHandler(k Keeper) sdk.Handler { case channel.MsgChannelCloseConfirm: // Lookup module by channel capability - module, cap, ok := k.ChannelKeeper.LookupModuleByChannel(ctx, msg.PortID, msg.ChannelID) - if !ok { - return nil, sdkerrors.Wrap(channel.ErrChannelCapabilityNotFound, "could not retrieve module from channel capability") + module, cap, err := k.ChannelKeeper.LookupModuleByChannel(ctx, msg.PortID, msg.ChannelID) + if err != nil { + return nil, sdkerrors.Wrap(err, "could not retrieve module from port-id") } // Retrieve callbacks from router cbs, ok := k.Router.GetRoute(module) @@ -146,7 +148,7 @@ func NewHandler(k Keeper) sdk.Handler { return nil, sdkerrors.Wrapf(port.ErrInvalidRoute, "route not found to module: %s", module) } - err := cbs.OnChanCloseConfirm(ctx, msg.PortID, msg.ChannelID) + err = cbs.OnChanCloseConfirm(ctx, msg.PortID, msg.ChannelID) if err != nil { return nil, err } @@ -155,9 +157,9 @@ func NewHandler(k Keeper) sdk.Handler { // IBC packet msgs get routed to the appropriate module callback case channel.MsgPacket: // Lookup module by channel capability - module, _, ok := k.ChannelKeeper.LookupModuleByChannel(ctx, msg.Packet.DestinationPort, msg.Packet.DestinationChannel) - if !ok { - return nil, sdkerrors.Wrap(channel.ErrChannelCapabilityNotFound, "could not retrieve module from channel capability") + module, _, err := k.ChannelKeeper.LookupModuleByChannel(ctx, msg.Packet.DestinationPort, msg.Packet.DestinationChannel) + if err != nil { + return nil, sdkerrors.Wrap(err, "could not retrieve module from port-id") } // Retrieve callbacks from router @@ -169,9 +171,9 @@ func NewHandler(k Keeper) sdk.Handler { case channel.MsgAcknowledgement: // Lookup module by channel capability - module, _, ok := k.ChannelKeeper.LookupModuleByChannel(ctx, msg.Packet.SourcePort, msg.Packet.SourceChannel) - if !ok { - return nil, sdkerrors.Wrap(channel.ErrChannelCapabilityNotFound, "could not retrieve module from channel capability") + module, _, err := k.ChannelKeeper.LookupModuleByChannel(ctx, msg.Packet.SourcePort, msg.Packet.SourceChannel) + if err != nil { + return nil, sdkerrors.Wrap(err, "could not retrieve module from port-id") } // Retrieve callbacks from router @@ -183,9 +185,9 @@ func NewHandler(k Keeper) sdk.Handler { case channel.MsgTimeout: // Lookup module by channel capability - module, cap, ok := k.ChannelKeeper.LookupModuleByChannel(ctx, msg.Packet.SourcePort, msg.Packet.SourceChannel) - if !ok { - return nil, sdkerrors.Wrap(channel.ErrChannelCapabilityNotFound, "could not retrieve module from channel capability") + module, cap, err := k.ChannelKeeper.LookupModuleByChannel(ctx, msg.Packet.SourcePort, msg.Packet.SourceChannel) + if err != nil { + return nil, sdkerrors.Wrap(err, "could not retrieve module from port-id") } // Retrieve callbacks from router