From 06e7b801624a6c6e2588907fc36eaf814e4221a2 Mon Sep 17 00:00:00 2001 From: Aditya Sripal Date: Fri, 27 Mar 2020 16:12:07 -0700 Subject: [PATCH] remove capabilities from msgs and return them on channel handlers --- x/ibc/04-channel/handler.go | 37 +++++++++++++++--------------- x/ibc/04-channel/types/msgs.go | 41 ++++++---------------------------- x/ibc/handler.go | 2 +- 3 files changed, 27 insertions(+), 53 deletions(-) diff --git a/x/ibc/04-channel/handler.go b/x/ibc/04-channel/handler.go index af453c8972ed..a55b302ad2ee 100644 --- a/x/ibc/04-channel/handler.go +++ b/x/ibc/04-channel/handler.go @@ -2,18 +2,19 @@ package channel import ( sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/capability" "github.com/cosmos/cosmos-sdk/x/ibc/04-channel/keeper" "github.com/cosmos/cosmos-sdk/x/ibc/04-channel/types" ) // HandleMsgChannelOpenInit defines the sdk.Handler for MsgChannelOpenInit -func HandleMsgChannelOpenInit(ctx sdk.Context, k keeper.Keeper, msg types.MsgChannelOpenInit) (*sdk.Result, error) { - _, err := k.ChanOpenInit( +func HandleMsgChannelOpenInit(ctx sdk.Context, k keeper.Keeper, portCap capability.Capability, msg types.MsgChannelOpenInit) (*sdk.Result, capability.Capability, error) { + capKey, err := k.ChanOpenInit( ctx, msg.Channel.Ordering, msg.Channel.ConnectionHops, msg.PortID, msg.ChannelID, - msg.PortCap, msg.Channel.Counterparty, msg.Channel.Version, + portCap, msg.Channel.Counterparty, msg.Channel.Version, ) if err != nil { - return nil, err + return nil, nil, err } ctx.EventManager().EmitEvents(sdk.Events{ @@ -34,16 +35,16 @@ func HandleMsgChannelOpenInit(ctx sdk.Context, k keeper.Keeper, msg types.MsgCha return &sdk.Result{ Events: ctx.EventManager().Events(), - }, nil + }, capKey, nil } // HandleMsgChannelOpenTry defines the sdk.Handler for MsgChannelOpenTry -func HandleMsgChannelOpenTry(ctx sdk.Context, k keeper.Keeper, msg types.MsgChannelOpenTry) (*sdk.Result, error) { - _, err := k.ChanOpenTry(ctx, msg.Channel.Ordering, msg.Channel.ConnectionHops, msg.PortID, msg.ChannelID, - msg.PortCap, msg.Channel.Counterparty, msg.Channel.Version, msg.CounterpartyVersion, msg.ProofInit, msg.ProofHeight, +func HandleMsgChannelOpenTry(ctx sdk.Context, k keeper.Keeper, portCap capability.Capability, msg types.MsgChannelOpenTry) (*sdk.Result, capability.Capability, error) { + capKey, err := k.ChanOpenTry(ctx, msg.Channel.Ordering, msg.Channel.ConnectionHops, msg.PortID, msg.ChannelID, + portCap, msg.Channel.Counterparty, msg.Channel.Version, msg.CounterpartyVersion, msg.ProofInit, msg.ProofHeight, ) if err != nil { - return nil, err + return nil, nil, err } ctx.EventManager().EmitEvents(sdk.Events{ @@ -64,13 +65,13 @@ func HandleMsgChannelOpenTry(ctx sdk.Context, k keeper.Keeper, msg types.MsgChan return &sdk.Result{ Events: ctx.EventManager().Events(), - }, nil + }, capKey, nil } // HandleMsgChannelOpenAck defines the sdk.Handler for MsgChannelOpenAck -func HandleMsgChannelOpenAck(ctx sdk.Context, k keeper.Keeper, msg types.MsgChannelOpenAck) (*sdk.Result, error) { +func HandleMsgChannelOpenAck(ctx sdk.Context, k keeper.Keeper, channelCap capability.Capability, msg types.MsgChannelOpenAck) (*sdk.Result, error) { err := k.ChanOpenAck( - ctx, msg.PortID, msg.ChannelID, msg.ChannelCap, msg.CounterpartyVersion, msg.ProofTry, msg.ProofHeight, + ctx, msg.PortID, msg.ChannelID, channelCap, msg.CounterpartyVersion, msg.ProofTry, msg.ProofHeight, ) if err != nil { return nil, err @@ -95,8 +96,8 @@ func HandleMsgChannelOpenAck(ctx sdk.Context, k keeper.Keeper, msg types.MsgChan } // HandleMsgChannelOpenConfirm defines the sdk.Handler for MsgChannelOpenConfirm -func HandleMsgChannelOpenConfirm(ctx sdk.Context, k keeper.Keeper, msg types.MsgChannelOpenConfirm) (*sdk.Result, error) { - err := k.ChanOpenConfirm(ctx, msg.PortID, msg.ChannelID, msg.ChannelCap, msg.ProofAck, msg.ProofHeight) +func HandleMsgChannelOpenConfirm(ctx sdk.Context, k keeper.Keeper, channelCap capability.Capability, msg types.MsgChannelOpenConfirm) (*sdk.Result, error) { + err := k.ChanOpenConfirm(ctx, msg.PortID, msg.ChannelID, channelCap, msg.ProofAck, msg.ProofHeight) if err != nil { return nil, err } @@ -120,8 +121,8 @@ func HandleMsgChannelOpenConfirm(ctx sdk.Context, k keeper.Keeper, msg types.Msg } // HandleMsgChannelCloseInit defines the sdk.Handler for MsgChannelCloseInit -func HandleMsgChannelCloseInit(ctx sdk.Context, k keeper.Keeper, msg types.MsgChannelCloseInit) (*sdk.Result, error) { - err := k.ChanCloseInit(ctx, msg.PortID, msg.ChannelID, msg.ChannelCap) +func HandleMsgChannelCloseInit(ctx sdk.Context, k keeper.Keeper, channelCap capability.Capability, msg types.MsgChannelCloseInit) (*sdk.Result, error) { + err := k.ChanCloseInit(ctx, msg.PortID, msg.ChannelID, channelCap) if err != nil { return nil, err } @@ -145,8 +146,8 @@ func HandleMsgChannelCloseInit(ctx sdk.Context, k keeper.Keeper, msg types.MsgCh } // HandleMsgChannelCloseConfirm defines the sdk.Handler for MsgChannelCloseConfirm -func HandleMsgChannelCloseConfirm(ctx sdk.Context, k keeper.Keeper, msg types.MsgChannelCloseConfirm) (*sdk.Result, error) { - err := k.ChanCloseConfirm(ctx, msg.PortID, msg.ChannelID, msg.ChannelCap, msg.ProofInit, msg.ProofHeight) +func HandleMsgChannelCloseConfirm(ctx sdk.Context, k keeper.Keeper, channelCap capability.Capability, msg types.MsgChannelCloseConfirm) (*sdk.Result, error) { + err := k.ChanCloseConfirm(ctx, msg.PortID, msg.ChannelID, channelCap, msg.ProofInit, msg.ProofHeight) if err != nil { return nil, err } diff --git a/x/ibc/04-channel/types/msgs.go b/x/ibc/04-channel/types/msgs.go index aba51f95170c..6797e942f57b 100644 --- a/x/ibc/04-channel/types/msgs.go +++ b/x/ibc/04-channel/types/msgs.go @@ -5,9 +5,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/cosmos/cosmos-sdk/x/capability" "github.com/cosmos/cosmos-sdk/x/ibc/04-channel/exported" - porttypes "github.com/cosmos/cosmos-sdk/x/ibc/05-port/types" commitmentexported "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/exported" commitmenttypes "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/types" host "github.com/cosmos/cosmos-sdk/x/ibc/24-host" @@ -17,11 +15,10 @@ import ( var _ sdk.Msg = MsgChannelOpenInit{} type MsgChannelOpenInit struct { - PortID string `json:"port_id"` - ChannelID string `json:"channel_id"` - PortCap capability.Capability `json:"port_capability"` - Channel Channel `json:"channel"` - Signer sdk.AccAddress `json:"signer"` + PortID string `json:"port_id"` + ChannelID string `json:"channel_id"` + Channel Channel `json:"channel"` + Signer sdk.AccAddress `json:"signer"` } // NewMsgChannelOpenInit creates a new MsgChannelCloseInit MsgChannelOpenInit @@ -57,9 +54,6 @@ func (msg MsgChannelOpenInit) ValidateBasic() error { if err := host.DefaultChannelIdentifierValidator(msg.ChannelID); err != nil { return sdkerrors.Wrap(err, "invalid channel ID") } - if msg.PortCap == nil { - return sdkerrors.Wrap(porttypes.ErrInvalidPort, "port capability is nil") - } // Signer can be empty return msg.Channel.ValidateBasic() } @@ -79,7 +73,6 @@ var _ sdk.Msg = MsgChannelOpenTry{} type MsgChannelOpenTry struct { PortID string `json:"port_id"` ChannelID string `json:"channel_id"` - PortCap capability.Capability `json:"port_capability"` Channel Channel `json:"channel"` CounterpartyVersion string `json:"counterparty_version"` ProofInit commitmentexported.Proof `json:"proof_init"` @@ -127,9 +120,6 @@ func (msg MsgChannelOpenTry) ValidateBasic() error { if strings.TrimSpace(msg.CounterpartyVersion) == "" { return sdkerrors.Wrap(ErrInvalidCounterparty, "counterparty version cannot be blank") } - if msg.PortCap == nil { - return sdkerrors.Wrap(porttypes.ErrInvalidPort, "port capability is nil") - } if msg.ProofInit == nil { return sdkerrors.Wrap(commitmenttypes.ErrInvalidProof, "cannot submit an empty proof") } @@ -158,7 +148,6 @@ var _ sdk.Msg = MsgChannelOpenAck{} type MsgChannelOpenAck struct { PortID string `json:"port_id"` ChannelID string `json:"channel_id"` - ChannelCap capability.Capability `json:"channel_capability"` CounterpartyVersion string `json:"counterparty_version"` ProofTry commitmentexported.Proof `json:"proof_try"` ProofHeight uint64 `json:"proof_height"` @@ -201,9 +190,6 @@ func (msg MsgChannelOpenAck) ValidateBasic() error { if strings.TrimSpace(msg.CounterpartyVersion) == "" { return sdkerrors.Wrap(ErrInvalidCounterparty, "counterparty version cannot be blank") } - if msg.ChannelCap == nil { - return sdkerrors.Wrap(ErrInvalidChannelCapability, "channel capability is nil") - } if msg.ProofTry == nil { return sdkerrors.Wrap(commitmenttypes.ErrInvalidProof, "cannot submit an empty proof") } @@ -232,7 +218,6 @@ var _ sdk.Msg = MsgChannelOpenConfirm{} type MsgChannelOpenConfirm struct { PortID string `json:"port_id"` ChannelID string `json:"channel_id"` - ChannelCap capability.Capability `json:"channel_capability"` ProofAck commitmentexported.Proof `json:"proof_ack"` ProofHeight uint64 `json:"proof_height"` Signer sdk.AccAddress `json:"signer"` @@ -270,9 +255,6 @@ func (msg MsgChannelOpenConfirm) ValidateBasic() error { if err := host.DefaultChannelIdentifierValidator(msg.ChannelID); err != nil { return sdkerrors.Wrap(err, "invalid channel ID") } - if msg.ChannelCap == nil { - return sdkerrors.Wrap(ErrInvalidChannelCapability, "channel capability is nil") - } if msg.ProofAck == nil { return sdkerrors.Wrap(commitmenttypes.ErrInvalidProof, "cannot submit an empty proof") } @@ -299,10 +281,9 @@ func (msg MsgChannelOpenConfirm) GetSigners() []sdk.AccAddress { var _ sdk.Msg = MsgChannelCloseInit{} type MsgChannelCloseInit struct { - PortID string `json:"port_id"` - ChannelID string `json:"channel_id"` - ChannelCap capability.Capability `json:"channel_capability"` - Signer sdk.AccAddress `json:"signer"` + PortID string `json:"port_id"` + ChannelID string `json:"channel_id"` + Signer sdk.AccAddress `json:"signer"` } // NewMsgChannelCloseInit creates a new MsgChannelCloseInit instance @@ -332,10 +313,6 @@ func (msg MsgChannelCloseInit) ValidateBasic() error { if err := host.DefaultChannelIdentifierValidator(msg.ChannelID); err != nil { return sdkerrors.Wrap(err, "invalid channel ID") } - if msg.ChannelCap == nil { - return sdkerrors.Wrap(ErrInvalidChannelCapability, "channel capability is nil") - } - // Signer can be empty return nil } @@ -355,7 +332,6 @@ var _ sdk.Msg = MsgChannelCloseConfirm{} type MsgChannelCloseConfirm struct { PortID string `json:"port_id"` ChannelID string `json:"channel_id"` - ChannelCap capability.Capability `json:"channel_capability"` ProofInit commitmentexported.Proof `json:"proof_init"` ProofHeight uint64 `json:"proof_height"` Signer sdk.AccAddress `json:"signer"` @@ -393,9 +369,6 @@ func (msg MsgChannelCloseConfirm) ValidateBasic() error { if err := host.DefaultChannelIdentifierValidator(msg.ChannelID); err != nil { return sdkerrors.Wrap(err, "invalid channel ID") } - if msg.ChannelCap == nil { - return sdkerrors.Wrap(ErrInvalidChannelCapability, "channel capability is nil") - } if msg.ProofInit == nil { return sdkerrors.Wrap(commitmenttypes.ErrInvalidProof, "cannot submit an empty proof") } diff --git a/x/ibc/handler.go b/x/ibc/handler.go index 1a3f5843f01f..2494937d6489 100644 --- a/x/ibc/handler.go +++ b/x/ibc/handler.go @@ -37,7 +37,7 @@ func NewHandler(k Keeper) sdk.Handler { // IBC channel msgs case channel.MsgChannelOpenInit: - return channel.HandleMsgChannelOpenInit(ctx, k.ChannelKeeper, msg) + res, cap, err := channel.HandleMsgChannelOpenInit(ctx, k.ChannelKeeper, msg) case channel.MsgChannelOpenTry: return channel.HandleMsgChannelOpenTry(ctx, k.ChannelKeeper, msg)