Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: rename apis to align with spec #7511

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/core/04-channel/v2/client/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewTxCmd() *cobra.Command {

txCmd.AddCommand(
newCreateChannelTxCmd(),
newProvideCounterpartyTxCmd(),
newRegisterCounterpartyTxCmd(),
)

return txCmd
Expand Down
14 changes: 7 additions & 7 deletions modules/core/04-channel/v2/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ func newCreateChannelTxCmd() *cobra.Command {
return cmd
}

// newProvideCounterpartyCmd defines the command to provide the counterparty channel identifier to an IBC channel.
func newProvideCounterpartyTxCmd() *cobra.Command {
// newRegisterCounterpartyCmd defines the command to provide the counterparty channel identifier to an IBC channel.
func newRegisterCounterpartyTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "provide-counterparty [channel-identifier] [counterparty-channel-identifier]",
Use: "register-counterparty [channel-identifier] [counterparty-channel-identifier]",
Args: cobra.ExactArgs(2),
Short: "provide the counterparty channel id to an IBC channel",
Long: `Provide the counterparty channel id to an IBC channel specified by its channel ID.`,
Example: fmt.Sprintf("%s tx %s %s provide-counterparty channel-0 channel-1", version.AppName, exported.ModuleName, types.SubModuleName),
Short: "Register the counterparty channel identifier for an IBC channel",
Long: `Register the counterparty channel identifier for an IBC channel specified by its channel ID.`,
Example: fmt.Sprintf("%s tx %s %s register-counterparty channel-0 channel-1", version.AppName, exported.ModuleName, types.SubModuleName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
Expand All @@ -64,7 +64,7 @@ func newProvideCounterpartyTxCmd() *cobra.Command {
channelID := args[0]
counterpartyChannelID := args[1]

msg := types.MsgProvideCounterparty{
msg := types.MsgRegisterCounterparty{
ChannelId: channelID,
CounterpartyChannelId: counterpartyChannelID,
Signer: clientCtx.GetFromAddress().String(),
Expand Down
171 changes: 86 additions & 85 deletions modules/core/04-channel/v2/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,50 @@ import (

var _ channeltypesv2.MsgServer = &Keeper{}

// SendPacket implements the PacketMsgServer SendPacket method.
// CreateChannel defines a rpc handler method for MsgCreateChannel.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved CreateChannel and RegisterCounterparty to top of file so things read logically wrt exec flow

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also wanted to move ack after recv 😬 (both here and in packet.go iirc)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

say no more!

func (k *Keeper) CreateChannel(goCtx context.Context, msg *channeltypesv2.MsgCreateChannel) (*channeltypesv2.MsgCreateChannelResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

channelID := k.channelKeeperV1.GenerateChannelIdentifier(ctx)

// Initialize channel with empty counterparty channel identifier.
channel := channeltypesv2.NewChannel(msg.ClientId, "", msg.MerklePathPrefix)
k.SetChannel(ctx, channelID, channel)
k.SetCreator(ctx, channelID, msg.Signer)
k.SetNextSequenceSend(ctx, channelID, 1)

k.EmitCreateChannelEvent(goCtx, channelID)

return &channeltypesv2.MsgCreateChannelResponse{ChannelId: channelID}, nil
}

// RegisterCounterparty defines a rpc handler method for MsgRegisterCounterparty.
func (k *Keeper) RegisterCounterparty(goCtx context.Context, msg *channeltypesv2.MsgRegisterCounterparty) (*channeltypesv2.MsgRegisterCounterpartyResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

creator, found := k.GetCreator(ctx, msg.ChannelId)
if !found {
return nil, errorsmod.Wrap(ibcerrors.ErrUnauthorized, "channel creator must be set")
}

if creator != msg.Signer {
return nil, errorsmod.Wrapf(ibcerrors.ErrUnauthorized, "channel creator (%s) must match signer (%s)", creator, msg.Signer)
}

channel, ok := k.GetChannel(ctx, msg.ChannelId)
if !ok {
return nil, errorsmod.Wrapf(channeltypesv2.ErrInvalidChannel, "channel must exist for channel id %s", msg.ChannelId)
}

channel.CounterpartyChannelId = msg.CounterpartyChannelId
k.SetChannel(ctx, msg.ChannelId, channel)
// Delete client creator from state as it is not needed after this point.
k.DeleteCreator(ctx, msg.ChannelId)

return &channeltypesv2.MsgRegisterCounterpartyResponse{}, nil
}

// SendPacket defines a rpc handler method for MsgSendPacket.
func (k *Keeper) SendPacket(ctx context.Context, msg *channeltypesv2.MsgSendPacket) (*channeltypesv2.MsgSendPacketResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
sequence, destChannel, err := k.sendPacket(ctx, msg.SourceChannel, msg.TimeoutTimestamp, msg.Payloads)
Expand All @@ -43,46 +86,7 @@ func (k *Keeper) SendPacket(ctx context.Context, msg *channeltypesv2.MsgSendPack
return &channeltypesv2.MsgSendPacketResponse{Sequence: sequence}, nil
}

func (k *Keeper) Acknowledgement(ctx context.Context, msg *channeltypesv2.MsgAcknowledgement) (*channeltypesv2.MsgAcknowledgementResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
relayer, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
sdkCtx.Logger().Error("acknowledgement failed", "error", errorsmod.Wrap(err, "Invalid address for msg Signer"))
return nil, errorsmod.Wrap(err, "Invalid address for msg Signer")
}

cacheCtx, writeFn := sdkCtx.CacheContext()
err = k.acknowledgePacket(cacheCtx, msg.Packet, msg.Acknowledgement, msg.ProofAcked, msg.ProofHeight)

switch err {
case nil:
writeFn()
case channeltypesv1.ErrNoOpMsg:
// no-ops do not need event emission as they will be ignored
sdkCtx.Logger().Debug("no-op on redundant relay", "source-channel", msg.Packet.SourceChannel)
return &channeltypesv2.MsgAcknowledgementResponse{Result: channeltypesv1.NOOP}, nil
default:
sdkCtx.Logger().Error("acknowledgement failed", "source-channel", msg.Packet.SourceChannel, "error", errorsmod.Wrap(err, "acknowledge packet verification failed"))
return nil, errorsmod.Wrap(err, "acknowledge packet verification failed")
}

recvResults := make(map[string]channeltypesv2.RecvPacketResult)
for _, r := range msg.Acknowledgement.AcknowledgementResults {
recvResults[r.AppName] = r.RecvPacketResult
}

for _, pd := range msg.Packet.Payloads {
cbs := k.Router.Route(pd.SourcePort)
err := cbs.OnAcknowledgementPacket(ctx, msg.Packet.SourceChannel, msg.Packet.DestinationChannel, pd, recvResults[pd.DestinationPort].Acknowledgement, relayer)
if err != nil {
return nil, errorsmod.Wrapf(err, "failed OnAcknowledgementPacket for source port %s, source channel %s, destination channel %s", pd.SourcePort, msg.Packet.SourceChannel, msg.Packet.DestinationChannel)
}
}

return &channeltypesv2.MsgAcknowledgementResponse{Result: channeltypesv1.SUCCESS}, nil
}

// RecvPacket implements the PacketMsgServer RecvPacket method.
// RecvPacket defines a rpc handler method for MsgRecvPacket.
func (k *Keeper) RecvPacket(ctx context.Context, msg *channeltypesv2.MsgRecvPacket) (*channeltypesv2.MsgRecvPacketResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)

Expand Down Expand Up @@ -162,7 +166,47 @@ func (k *Keeper) RecvPacket(ctx context.Context, msg *channeltypesv2.MsgRecvPack
return &channeltypesv2.MsgRecvPacketResponse{Result: channeltypesv1.SUCCESS}, nil
}

// Timeout implements the PacketMsgServer Timeout method.
// Acknowledgement defines an rpc handler method for MsgAcknowledgement.
func (k *Keeper) Acknowledgement(ctx context.Context, msg *channeltypesv2.MsgAcknowledgement) (*channeltypesv2.MsgAcknowledgementResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
relayer, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
sdkCtx.Logger().Error("acknowledgement failed", "error", errorsmod.Wrap(err, "Invalid address for msg Signer"))
return nil, errorsmod.Wrap(err, "Invalid address for msg Signer")
}

cacheCtx, writeFn := sdkCtx.CacheContext()
err = k.acknowledgePacket(cacheCtx, msg.Packet, msg.Acknowledgement, msg.ProofAcked, msg.ProofHeight)

switch err {
case nil:
writeFn()
case channeltypesv1.ErrNoOpMsg:
// no-ops do not need event emission as they will be ignored
sdkCtx.Logger().Debug("no-op on redundant relay", "source-channel", msg.Packet.SourceChannel)
return &channeltypesv2.MsgAcknowledgementResponse{Result: channeltypesv1.NOOP}, nil
default:
sdkCtx.Logger().Error("acknowledgement failed", "source-channel", msg.Packet.SourceChannel, "error", errorsmod.Wrap(err, "acknowledge packet verification failed"))
return nil, errorsmod.Wrap(err, "acknowledge packet verification failed")
}

recvResults := make(map[string]channeltypesv2.RecvPacketResult)
for _, r := range msg.Acknowledgement.AcknowledgementResults {
recvResults[r.AppName] = r.RecvPacketResult
}

for _, pd := range msg.Packet.Payloads {
cbs := k.Router.Route(pd.SourcePort)
err := cbs.OnAcknowledgementPacket(ctx, msg.Packet.SourceChannel, msg.Packet.DestinationChannel, pd, recvResults[pd.DestinationPort].Acknowledgement, relayer)
if err != nil {
return nil, errorsmod.Wrapf(err, "failed OnAcknowledgementPacket for source port %s, source channel %s, destination channel %s", pd.SourcePort, msg.Packet.SourceChannel, msg.Packet.DestinationChannel)
}
}

return &channeltypesv2.MsgAcknowledgementResponse{Result: channeltypesv1.SUCCESS}, nil
}

// Timeout defines a rpc handler method for MsgTimeout.
func (k *Keeper) Timeout(ctx context.Context, timeout *channeltypesv2.MsgTimeout) (*channeltypesv2.MsgTimeoutResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)

Expand Down Expand Up @@ -200,46 +244,3 @@ func (k *Keeper) Timeout(ctx context.Context, timeout *channeltypesv2.MsgTimeout

return &channeltypesv2.MsgTimeoutResponse{Result: channeltypesv1.SUCCESS}, nil
}

// CreateChannel defines a rpc handler method for MsgCreateChannel
func (k *Keeper) CreateChannel(goCtx context.Context, msg *channeltypesv2.MsgCreateChannel) (*channeltypesv2.MsgCreateChannelResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

channelID := k.channelKeeperV1.GenerateChannelIdentifier(ctx)

// Initialize channel with empty counterparty channel identifier.
channel := channeltypesv2.NewChannel(msg.ClientId, "", msg.MerklePathPrefix)
k.SetChannel(ctx, channelID, channel)
k.SetCreator(ctx, channelID, msg.Signer)
k.SetNextSequenceSend(ctx, channelID, 1)

k.EmitCreateChannelEvent(goCtx, channelID)

return &channeltypesv2.MsgCreateChannelResponse{ChannelId: channelID}, nil
}

// ProvideCounterparty defines a rpc handler method for MsgProvideCounterparty.
func (k *Keeper) ProvideCounterparty(goCtx context.Context, msg *channeltypesv2.MsgProvideCounterparty) (*channeltypesv2.MsgProvideCounterpartyResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

creator, found := k.GetCreator(ctx, msg.ChannelId)
if !found {
return nil, errorsmod.Wrap(ibcerrors.ErrUnauthorized, "channel creator must be set")
}

if creator != msg.Signer {
return nil, errorsmod.Wrapf(ibcerrors.ErrUnauthorized, "channel creator (%s) must match signer (%s)", creator, msg.Signer)
}

channel, ok := k.GetChannel(ctx, msg.ChannelId)
if !ok {
return nil, errorsmod.Wrapf(channeltypesv2.ErrInvalidChannel, "channel must exist for channel id %s", msg.ChannelId)
}

channel.CounterpartyChannelId = msg.CounterpartyChannelId
k.SetChannel(ctx, msg.ChannelId, channel)
// Delete client creator from state as it is not needed after this point.
k.DeleteCreator(ctx, msg.ChannelId)

return &channeltypesv2.MsgProvideCounterpartyResponse{}, nil
}
Loading
Loading