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

Refactor packet data unmarshalling to use specific version #6354

60 changes: 39 additions & 21 deletions modules/apps/transfer/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,25 +174,23 @@ func (IBCModule) OnChanCloseConfirm(
return nil
}

func (IBCModule) unmarshalPacketDataBytesToICS20V2(bz []byte) (types.FungibleTokenPacketDataV2, error) {
// TODO: remove support for this function parsing v1 packet data
// TODO: explicit check for packet data type against app version

var datav1 types.FungibleTokenPacketData
if err := json.Unmarshal(bz, &datav1); err == nil {
if len(datav1.Denom) != 0 {
return convertinternal.PacketDataV1ToV2(datav1), nil
func (IBCModule) unmarshalPacketDataBytesToICS20V2(bz []byte, ics20Version string) (types.FungibleTokenPacketDataV2, error) {
switch ics20Version {
case types.V1:
var datav1 types.FungibleTokenPacketData
if err := json.Unmarshal(bz, &datav1); err != nil {
return types.FungibleTokenPacketDataV2{}, err
}
}

var data types.FungibleTokenPacketDataV2
if err := json.Unmarshal(bz, &data); err == nil {
if len(data.Tokens) != 0 {
return data, nil
return convertinternal.PacketDataV1ToV2(datav1), nil
case types.V2:
var data types.FungibleTokenPacketDataV2
if err := json.Unmarshal(bz, &data); err != nil {
return types.FungibleTokenPacketDataV2{}, err
}
return data, nil
default:
return types.FungibleTokenPacketDataV2{}, errorsmod.Wrapf(types.ErrInvalidVersion, "invalid ICS-20 version: %s", ics20Version)
Copy link
Contributor

Choose a reason for hiding this comment

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

Since types.ErrInvalidVersion translates into the same text...

Suggested change
return types.FungibleTokenPacketDataV2{}, errorsmod.Wrapf(types.ErrInvalidVersion, "invalid ICS-20 version: %s", ics20Version)
return types.FungibleTokenPacketDataV2{}, errorsmod.Wrap(types.ErrInvalidVersion, ics20Version)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah! Good catch, will update this

}

return types.FungibleTokenPacketDataV2{}, errorsmod.Wrapf(ibcerrors.ErrInvalidType, "cannot unmarshal ICS-20 transfer packet data")
}

// OnRecvPacket implements the IBCModule interface. A successful acknowledgement
Expand All @@ -205,7 +203,12 @@ func (im IBCModule) OnRecvPacket(
) ibcexported.Acknowledgement {
ack := channeltypes.NewResultAcknowledgement([]byte{byte(1)})

data, ackErr := im.unmarshalPacketDataBytesToICS20V2(packet.GetData())
ics20Version, ok := im.keeper.GetICS4Wrapper().GetAppVersion(ctx, packet.SourcePort, packet.SourceChannel)
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
if !ok {
return channeltypes.NewErrorAcknowledgement(fmt.Errorf("could not retrieve app version for channel (%s, %s)", packet.SourcePort, packet.SourceChannel))
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
}

data, ackErr := im.unmarshalPacketDataBytesToICS20V2(packet.GetData(), ics20Version)
if ackErr != nil {
im.keeper.Logger(ctx).Error(fmt.Sprintf("%s sequence %d", ackErr.Error(), packet.Sequence))
ack = channeltypes.NewErrorAcknowledgement(ackErr)
Expand Down Expand Up @@ -261,7 +264,12 @@ func (im IBCModule) OnAcknowledgementPacket(
return errorsmod.Wrapf(ibcerrors.ErrUnknownRequest, "cannot unmarshal ICS-20 transfer packet acknowledgement: %v", err)
}

data, err := im.unmarshalPacketDataBytesToICS20V2(packet.GetData())
ics20Version, ok := im.keeper.GetICS4Wrapper().GetAppVersion(ctx, packet.SourcePort, packet.SourceChannel)
if !ok {
return errorsmod.Wrapf(ibcerrors.ErrInvalidVersion, "could not retrieve app version for channel (%s, %s)", packet.SourcePort, packet.SourceChannel)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return errorsmod.Wrapf(ibcerrors.ErrInvalidVersion, "could not retrieve app version for channel (%s, %s)", packet.SourcePort, packet.SourceChannel)
return errorsmod.Wrapf(ibcerrors.ErrNotFound, "app version not found for port %s and channel %s", packet.SourcePort, packet.SourceChannel)

}

data, err := im.unmarshalPacketDataBytesToICS20V2(packet.GetData(), ics20Version)
if err != nil {
return err
}
Expand Down Expand Up @@ -311,7 +319,12 @@ func (im IBCModule) OnTimeoutPacket(
packet channeltypes.Packet,
relayer sdk.AccAddress,
) error {
data, err := im.unmarshalPacketDataBytesToICS20V2(packet.GetData())
ics20Version, ok := im.keeper.GetICS4Wrapper().GetAppVersion(ctx, packet.SourcePort, packet.SourceChannel)
if !ok {
return errorsmod.Wrapf(ibcerrors.ErrInvalidVersion, "could not retrieve app version for channel (%s, %s)", packet.SourcePort, packet.SourceChannel)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return errorsmod.Wrapf(ibcerrors.ErrInvalidVersion, "could not retrieve app version for channel (%s, %s)", packet.SourcePort, packet.SourceChannel)
return errorsmod.Wrapf(ibcerrors.ErrNotFound, "app version not found for port %s and channel %s", packet.SourcePort, packet.SourceChannel)

}

data, err := im.unmarshalPacketDataBytesToICS20V2(packet.GetData(), ics20Version)
if err != nil {
return err
}
Expand Down Expand Up @@ -380,8 +393,13 @@ func (IBCModule) OnChanUpgradeOpen(ctx sdk.Context, portID, channelID string, pr
// UnmarshalPacketData attempts to unmarshal the provided packet data bytes
// into a FungibleTokenPacketData. This function implements the optional
// PacketDataUnmarshaler interface required for ADR 008 support.
func (im IBCModule) UnmarshalPacketData(_ sdk.Context, _, _ string, bz []byte) (interface{}, error) {
ftpd, err := im.unmarshalPacketDataBytesToICS20V2(bz)
func (im IBCModule) UnmarshalPacketData(ctx sdk.Context, portID, channelID string, bz []byte) (interface{}, error) {
ics20Version, ok := im.keeper.GetICS4Wrapper().GetAppVersion(ctx, portID, channelID)
if !ok {
return nil, errorsmod.Wrapf(ibcerrors.ErrInvalidVersion, "could not retrieve app version for channel (%s, %s)", portID, channelID)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return nil, errorsmod.Wrapf(ibcerrors.ErrInvalidVersion, "could not retrieve app version for channel (%s, %s)", portID, channelID)
return nil, errorsmod.Wrapf(ibcerrors.ErrNotFound, "app version not found for port %s and channel %s", portID, channelID)

}

ftpd, err := im.unmarshalPacketDataBytesToICS20V2(bz, ics20Version)
if err != nil {
return nil, err
}
Expand Down
Loading