Skip to content

Commit

Permalink
Modify ICS-20 transfer module to use Protobuf encoding for packet data
Browse files Browse the repository at this point in the history
  • Loading branch information
romac committed Jan 17, 2024
1 parent 9258dd9 commit f9090a2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion modules/apps/callbacks/ibc_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ func (s *CallbacksTestSuite) TestOnTimeoutPacket() {
s.Require().NoError(err)
s.Require().NotNil(packet)

err = transfertypes.ModuleCdc.UnmarshalJSON(packet.Data, &packetData)
err = transfertypes.ModuleCdc.Unmarshal(packet.Data, &packetData)
s.Require().NoError(err)

ctx = s.chainA.GetContext()
Expand Down
8 changes: 4 additions & 4 deletions modules/apps/transfer/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (im IBCModule) OnRecvPacket(

var data types.FungibleTokenPacketData
var ackErr error
if err := types.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil {
if err := types.ModuleCdc.Unmarshal(packet.GetData(), &data); err != nil {
ackErr = errorsmod.Wrapf(ibcerrors.ErrInvalidType, "cannot unmarshal ICS-20 transfer packet data")
logger.Error(fmt.Sprintf("%s sequence %d", ackErr.Error(), packet.Sequence))
ack = channeltypes.NewErrorAcknowledgement(ackErr)
Expand Down Expand Up @@ -237,7 +237,7 @@ func (im IBCModule) OnAcknowledgementPacket(
return errorsmod.Wrapf(ibcerrors.ErrUnknownRequest, "cannot unmarshal ICS-20 transfer packet acknowledgement: %v", err)
}
var data types.FungibleTokenPacketData
if err := types.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil {
if err := types.ModuleCdc.Unmarshal(packet.GetData(), &data); err != nil {
return errorsmod.Wrapf(ibcerrors.ErrUnknownRequest, "cannot unmarshal ICS-20 transfer packet data: %s", err.Error())
}

Expand Down Expand Up @@ -285,7 +285,7 @@ func (im IBCModule) OnTimeoutPacket(
relayer sdk.AccAddress,
) error {
var data types.FungibleTokenPacketData
if err := types.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil {
if err := types.ModuleCdc.Unmarshal(packet.GetData(), &data); err != nil {
return errorsmod.Wrapf(ibcerrors.ErrUnknownRequest, "cannot unmarshal ICS-20 transfer packet data: %s", err.Error())
}
// refund tokens
Expand All @@ -312,7 +312,7 @@ func (im IBCModule) OnTimeoutPacket(
// PacketDataUnmarshaler interface required for ADR 008 support.
func (IBCModule) UnmarshalPacketData(bz []byte) (interface{}, error) {
var packetData types.FungibleTokenPacketData
if err := types.ModuleCdc.UnmarshalJSON(bz, &packetData); err != nil {
if err := types.ModuleCdc.Unmarshal(bz, &packetData); err != nil {
return nil, err
}

Expand Down
9 changes: 9 additions & 0 deletions modules/apps/transfer/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,12 @@ func mustProtoMarshalJSON(msg proto.Message) []byte {

return buf.Bytes()
}

func mustProtoMarshal(msg proto.Message) []byte {
buf, err := proto.Marshal(msg)
if err != nil {
panic(err)
}

return buf
}
5 changes: 2 additions & 3 deletions modules/apps/transfer/types/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"

ibcerrors "github.com/cosmos/ibc-go/v8/modules/core/errors"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
)
Expand Down Expand Up @@ -69,7 +67,8 @@ func (ftpd FungibleTokenPacketData) ValidateBasic() error {

// GetBytes is a helper for serialising
func (ftpd FungibleTokenPacketData) GetBytes() []byte {
return sdk.MustSortJSON(mustProtoMarshalJSON(&ftpd))
// return sdk.MustSortJSON(mustProtoMarshalJSON(&ftpd))
return mustProtoMarshal(&ftpd)
}

// GetPacketSender returns the sender address embedded in the packet data.
Expand Down

0 comments on commit f9090a2

Please sign in to comment.