Skip to content

Commit

Permalink
fix build and address first review
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaSripal committed Mar 26, 2020
1 parent 1ed4923 commit 8ef8225
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 83 deletions.
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func NewSimApp(
app.CapabilityKeeper = capability.NewKeeper(
app.cdc, keys[capability.StoreKey], memKeys[capability.MemStoreKey],
)
scopedIBCKeeper := app.CapabilityKeeper.ScopeToModule("ibc")
scopedIBCKeeper := app.CapabilityKeeper.ScopeToModule(ibc.ModuleName)

// add keepers
app.AccountKeeper = auth.NewAccountKeeper(
Expand Down
16 changes: 8 additions & 8 deletions x/ibc/04-channel/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (

// HandleMsgChannelOpenInit defines the sdk.Handler for MsgChannelOpenInit
func HandleMsgChannelOpenInit(ctx sdk.Context, k keeper.Keeper, msg types.MsgChannelOpenInit) (*sdk.Result, error) {
err := k.ChanOpenInit(
_, err := k.ChanOpenInit(
ctx, msg.Channel.Ordering, msg.Channel.ConnectionHops, msg.PortID, msg.ChannelID,
msg.Channel.Counterparty, msg.Channel.Version,
msg.PortCap, msg.Channel.Counterparty, msg.Channel.Version,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -39,8 +39,8 @@ func HandleMsgChannelOpenInit(ctx sdk.Context, k keeper.Keeper, msg types.MsgCha

// 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.Channel.Counterparty, msg.Channel.Version, msg.CounterpartyVersion, msg.ProofInit, msg.ProofHeight,
_, 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,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -70,7 +70,7 @@ func HandleMsgChannelOpenTry(ctx sdk.Context, k keeper.Keeper, msg types.MsgChan
// HandleMsgChannelOpenAck defines the sdk.Handler for MsgChannelOpenAck
func HandleMsgChannelOpenAck(ctx sdk.Context, k keeper.Keeper, msg types.MsgChannelOpenAck) (*sdk.Result, error) {
err := k.ChanOpenAck(
ctx, msg.PortID, msg.ChannelID, msg.CounterpartyVersion, msg.ProofTry, msg.ProofHeight,
ctx, msg.PortID, msg.ChannelID, msg.ChannelCap, msg.CounterpartyVersion, msg.ProofTry, msg.ProofHeight,
)
if err != nil {
return nil, err
Expand All @@ -96,7 +96,7 @@ 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.ProofAck, msg.ProofHeight)
err := k.ChanOpenConfirm(ctx, msg.PortID, msg.ChannelID, msg.ChannelCap, msg.ProofAck, msg.ProofHeight)
if err != nil {
return nil, err
}
Expand All @@ -121,7 +121,7 @@ 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)
err := k.ChanCloseInit(ctx, msg.PortID, msg.ChannelID, msg.ChannelCap)
if err != nil {
return nil, err
}
Expand All @@ -146,7 +146,7 @@ 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.ProofInit, msg.ProofHeight)
err := k.ChanCloseConfirm(ctx, msg.PortID, msg.ChannelID, msg.ChannelCap, msg.ProofInit, msg.ProofHeight)
if err != nil {
return nil, err
}
Expand Down
47 changes: 15 additions & 32 deletions x/ibc/04-channel/keeper/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/ibc/04-channel/exported"
"github.com/cosmos/cosmos-sdk/x/ibc/04-channel/types"
commitmentexported "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/exported"
ibctypes "github.com/cosmos/cosmos-sdk/x/ibc/types"
)

// CounterpartyHops returns the connection hops of the counterparty channel.
Expand Down Expand Up @@ -65,11 +66,10 @@ func (k Keeper) ChanOpenInit(
channel := types.NewChannel(exported.INIT, order, counterparty, connectionHops, version)
k.SetChannel(ctx, portID, channelID, channel)

capKey, err := k.scopedKeeper.NewCapability(ctx, channelID)
capKey, err := k.scopedKeeper.NewCapability(ctx, ibctypes.ChannelCapabilityPath(portID, channelID))
if err != nil {
return nil, sdkerrors.Wrap(types.ErrInvalidChannelCapability, err.Error())
}
k.SetChannelCapability(ctx, portID, channelID, capKey)
k.SetNextSequenceSend(ctx, portID, channelID, 1)
k.SetNextSequenceRecv(ctx, portID, channelID, 1)

Expand Down Expand Up @@ -146,11 +146,10 @@ func (k Keeper) ChanOpenTry(

k.SetChannel(ctx, portID, channelID, channel)

capKey, err := k.scopedKeeper.NewCapability(ctx, channelID)
capKey, err := k.scopedKeeper.NewCapability(ctx, ibctypes.ChannelCapabilityPath(portID, channelID))
if err != nil {
return nil, sdkerrors.Wrap(types.ErrInvalidChannelCapability, err.Error())
}
k.SetChannelCapability(ctx, portID, channelID, capKey)
k.SetNextSequenceSend(ctx, portID, channelID, 1)
k.SetNextSequenceRecv(ctx, portID, channelID, 1)

Expand All @@ -163,7 +162,7 @@ func (k Keeper) ChanOpenAck(
ctx sdk.Context,
portID,
channelID string,
portCap capability.Capability,
chanCap capability.Capability,
counterpartyVersion string,
proofTry commitmentexported.Proof,
proofHeight uint64,
Expand All @@ -180,12 +179,8 @@ func (k Keeper) ChanOpenAck(
)
}

if _, ok := k.GetChannelCapability(ctx, portID, channelID); !ok {
return sdkerrors.Wrap(types.ErrChannelCapabilityNotFound, "channel capability not found")
}

if !k.portKeeper.Authenticate(ctx, portCap, portID) {
return sdkerrors.Wrap(porttypes.ErrInvalidPort, "caller does not own port capability")
if !k.scopedKeeper.AuthenticateCapability(ctx, chanCap, ibctypes.ChannelCapabilityPath(portID, channelID)) {
return sdkerrors.Wrap(types.ErrChannelCapabilityNotFound, "caller does not own capability for channel")
}

connectionEnd, found := k.connectionKeeper.GetConnection(ctx, channel.ConnectionHops[0])
Expand Down Expand Up @@ -234,7 +229,7 @@ func (k Keeper) ChanOpenConfirm(
ctx sdk.Context,
portID,
channelID string,
portCap capability.Capability,
chanCap capability.Capability,
proofAck commitmentexported.Proof,
proofHeight uint64,
) error {
Expand All @@ -250,12 +245,8 @@ func (k Keeper) ChanOpenConfirm(
)
}

if _, ok := k.GetChannelCapability(ctx, portID, channelID); !ok {
return sdkerrors.Wrap(types.ErrChannelCapabilityNotFound, "channel capability not found")
}

if !k.portKeeper.Authenticate(ctx, portCap, portID) {
return sdkerrors.Wrap(porttypes.ErrInvalidPort, "caller does not own port capability")
if !k.scopedKeeper.AuthenticateCapability(ctx, chanCap, ibctypes.ChannelCapabilityPath(portID, channelID)) {
return sdkerrors.Wrap(types.ErrChannelCapabilityNotFound, "caller does not own capability for channel")
}

connectionEnd, found := k.connectionKeeper.GetConnection(ctx, channel.ConnectionHops[0])
Expand Down Expand Up @@ -307,14 +298,10 @@ func (k Keeper) ChanCloseInit(
ctx sdk.Context,
portID,
channelID string,
portCap capability.Capability,
chanCap capability.Capability,
) error {
if _, ok := k.GetChannelCapability(ctx, portID, channelID); !ok {
return sdkerrors.Wrap(types.ErrChannelCapabilityNotFound, "channel capability not found")
}

if !k.portKeeper.Authenticate(ctx, portCap, portID) {
return sdkerrors.Wrap(porttypes.ErrInvalidPort, "caller does not own port capability")
if !k.scopedKeeper.AuthenticateCapability(ctx, chanCap, ibctypes.ChannelCapabilityPath(portID, channelID)) {
return sdkerrors.Wrap(types.ErrChannelCapabilityNotFound, "caller does not own capability for channel")
}

channel, found := k.GetChannel(ctx, portID, channelID)
Expand Down Expand Up @@ -350,16 +337,12 @@ func (k Keeper) ChanCloseConfirm(
ctx sdk.Context,
portID,
channelID string,
portCap capability.Capability,
chanCap capability.Capability,
proofInit commitmentexported.Proof,
proofHeight uint64,
) error {
if _, ok := k.GetChannelCapability(ctx, portID, channelID); !ok {
return sdkerrors.Wrap(types.ErrChannelCapabilityNotFound, "channel capability not found")
}

if !k.portKeeper.Authenticate(ctx, portCap, portID) {
return sdkerrors.Wrap(porttypes.ErrInvalidPort, "caller does not own port capability")
if !k.scopedKeeper.AuthenticateCapability(ctx, chanCap, ibctypes.ChannelCapabilityPath(portID, channelID)) {
return sdkerrors.Wrap(types.ErrChannelCapabilityNotFound, "caller does not own capability for channel")
}

channel, found := k.GetChannel(ctx, portID, channelID)
Expand Down
21 changes: 0 additions & 21 deletions x/ibc/04-channel/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,6 @@ func (k Keeper) SetChannel(ctx sdk.Context, portID, channelID string, channel ty
store.Set(ibctypes.KeyChannel(portID, channelID), bz)
}

// GetChannelCapability gets a channel's capability key from the store
func (k Keeper) GetChannelCapability(ctx sdk.Context, portID, channelID string) (capability.Capability, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(ibctypes.KeyChannelCapabilityPath(portID, channelID))
if bz == nil {
return nil, false
}

var capKey capability.Capability
k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &capKey)

return capKey, true
}

// SetChannelCapability sets a channel's capability key to the store
func (k Keeper) SetChannelCapability(ctx sdk.Context, portID, channelID string, key capability.Capability) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryLengthPrefixed(key)
store.Set(ibctypes.KeyChannelCapabilityPath(portID, channelID), bz)
}

// GetNextSequenceSend gets a channel's next send sequence from the store
func (k Keeper) GetNextSequenceSend(ctx sdk.Context, portID, channelID string) (uint64, bool) {
store := ctx.KVStore(k.storeKey)
Expand Down
2 changes: 1 addition & 1 deletion x/ibc/04-channel/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ type ConnectionKeeper interface {

// PortKeeper expected account IBC port keeper
type PortKeeper interface {
Authenticate(ctx, key capability.Capability, portID string) bool
Authenticate(ctx sdk.Context, key capability.Capability, portID string) bool
}
41 changes: 34 additions & 7 deletions x/ibc/04-channel/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ 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 @@ -15,10 +17,11 @@ import (
var _ sdk.Msg = MsgChannelOpenInit{}

type MsgChannelOpenInit struct {
PortID string `json:"port_id"`
ChannelID string `json:"channel_id"`
Channel Channel `json:"channel"`
Signer sdk.AccAddress `json:"signer"`
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"`
}

// NewMsgChannelOpenInit creates a new MsgChannelCloseInit MsgChannelOpenInit
Expand Down Expand Up @@ -54,6 +57,9 @@ 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 @@ -73,6 +79,7 @@ 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 @@ -120,6 +127,9 @@ 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 @@ -148,6 +158,7 @@ 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 @@ -190,6 +201,9 @@ 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 @@ -218,6 +232,7 @@ 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 @@ -255,6 +270,9 @@ 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 @@ -281,9 +299,10 @@ func (msg MsgChannelOpenConfirm) GetSigners() []sdk.AccAddress {
var _ sdk.Msg = MsgChannelCloseInit{}

type MsgChannelCloseInit struct {
PortID string `json:"port_id"`
ChannelID string `json:"channel_id"`
Signer sdk.AccAddress `json:"signer"`
PortID string `json:"port_id"`
ChannelID string `json:"channel_id"`
ChannelCap capability.Capability `json:"channel_capability"`
Signer sdk.AccAddress `json:"signer"`
}

// NewMsgChannelCloseInit creates a new MsgChannelCloseInit instance
Expand Down Expand Up @@ -313,6 +332,10 @@ 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 @@ -332,6 +355,7 @@ 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 @@ -369,6 +393,9 @@ 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
Loading

0 comments on commit 8ef8225

Please sign in to comment.