Skip to content

Commit

Permalink
remove capabilities from msgs and return them on channel handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaSripal committed Mar 27, 2020
1 parent 8ef8225 commit 06e7b80
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 53 deletions.
37 changes: 19 additions & 18 deletions x/ibc/04-channel/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
41 changes: 7 additions & 34 deletions x/ibc/04-channel/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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()
}
Expand All @@ -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"`
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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")
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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"`
Expand Down Expand Up @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion x/ibc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 06e7b80

Please sign in to comment.