Skip to content

Commit

Permalink
Add implementation for message server handling of ChanUpgradeOpen.
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitrisJim committed Jun 19, 2023
1 parent b1c81fa commit 429f2dc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
5 changes: 0 additions & 5 deletions modules/core/04-channel/keeper/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ func (k Keeper) ValidateUpgradeFields(ctx sdk.Context, proposedUpgrade types.Upg
return k.validateUpgradeFields(ctx, proposedUpgrade, currentChannel)
}

// WriteUpgradeOpenChannel is a wrapper around writeUpgradeOpenChannel to allow the function to be directly called in tests.
func (k Keeper) WriteUpgradeOpenChannel(ctx sdk.Context, portID, channelID string) {
k.writeUpgradeOpenChannel(ctx, portID, channelID)
}

// WriteUpgradeTimeoutChannel is a wrapper around writeUpgradeTimeoutChannel to allow the function to be directly called in tests.
func (k Keeper) WriteUpgradeTimeoutChannel(ctx sdk.Context, portID, channelID string) error {
return k.writeUpgradeTimeoutChannel(ctx, portID, channelID)
Expand Down
2 changes: 1 addition & 1 deletion modules/core/04-channel/keeper/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ func (k Keeper) ChanUpgradeOpen(
// WriteUpgradeOpenChannel writes the agreed upon upgrade fields to the channel, sets the channel flush status to NOTINFLUSH and sets the channel state back to OPEN. This can be called in one of two cases:
// - In the UpgradeAck step of the handshake if both sides have already flushed all in-flight packets.
// - In the UpgradeOpen step of the handshake.
func (k Keeper) writeUpgradeOpenChannel(ctx sdk.Context, portID, channelID string) {
func (k Keeper) WriteUpgradeOpenChannel(ctx sdk.Context, portID, channelID string) {
channel, found := k.GetChannel(ctx, portID, channelID)
if !found {
panic(fmt.Sprintf("could not find existing channel when updating channel state, channelID: %s, portID: %s", channelID, portID))
Expand Down
30 changes: 29 additions & 1 deletion modules/core/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,35 @@ func (k Keeper) ChannelUpgradeAck(goCtx context.Context, msg *channeltypes.MsgCh

// ChannelUpgradeOpen defines a rpc handler method for MsgChannelUpgradeOpen.
func (k Keeper) ChannelUpgradeOpen(goCtx context.Context, msg *channeltypes.MsgChannelUpgradeOpen) (*channeltypes.MsgChannelUpgradeOpenResponse, error) {
return nil, nil
ctx := sdk.UnwrapSDKContext(goCtx)

module, _, err := k.ChannelKeeper.LookupModuleByChannel(ctx, msg.PortId, msg.ChannelId)
if err != nil {
ctx.Logger().Error("channel upgrade open failed", "port-id", msg.PortId, "error", errorsmod.Wrap(err, "could not retrieve module from port-id"))
return nil, errorsmod.Wrap(err, "could not retrieve module from port-id")
}

cbs, ok := k.Router.GetRoute(module)
if !ok {
ctx.Logger().Error("channel upgrade open failed", "port-id", msg.PortId, "error", errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to module: %s", module))
return nil, errorsmod.Wrapf(porttypes.ErrInvalidRoute, "route not found to module: %s", module)
}

if err = k.ChannelKeeper.ChanUpgradeOpen(ctx, msg.PortId, msg.ChannelId, msg.CounterpartyChannelState, msg.ProofChannel, msg.ProofHeight); err != nil {
ctx.Logger().Error("channel upgrade open failed", "error", errorsmod.Wrap(err, "channel handshake upgrade open failed"))
return nil, errorsmod.Wrap(err, "channel handshake upgrade open failed")
}

k.ChannelKeeper.WriteUpgradeOpenChannel(ctx, msg.PortId, msg.ChannelId)

if err := cbs.OnChanUpgradeOpen(ctx, msg.PortId, msg.ChannelId); err != nil {
ctx.Logger().Error("channel handshake upgrade open callback failed", "port-id", msg.PortId, "channel-id", msg.ChannelId, "error", err.Error())
return nil, errorsmod.Wrapf(err, "channel upgrade open callback failed for port ID: %s, channel ID: %s", msg.PortId, msg.ChannelId)
}

ctx.Logger().Info("channel upgrade open callback succeeded", "channel-id", msg.ChannelId, "port-id", msg.PortId)

return &channeltypes.MsgChannelUpgradeOpenResponse{}, nil
}

// ChannelUpgradeTimeout defines a rpc handler method for MsgChannelUpgradeTimeout.
Expand Down

0 comments on commit 429f2dc

Please sign in to comment.