diff --git a/x/uibc/gmp/gmp_middleware.go b/x/uibc/gmp/gmp_middleware.go index 58ec8cc94f..9a8d2cbca1 100644 --- a/x/uibc/gmp/gmp_middleware.go +++ b/x/uibc/gmp/gmp_middleware.go @@ -14,14 +14,14 @@ func NewHandler() *Handler { return &Handler{} } -func (h Handler) OnRecvPacket(ctx sdk.Context, coinReceived sdk.Coin, memoStr string, receiver sdk.AccAddress, -) (Message, error) { - if len(memoStr) == 0 { - return Message{}, nil +func (h Handler) ParseMemo(ctx sdk.Context, coinReceived sdk.Coin, memo string, receiver sdk.AccAddress, +) (GMPMemo, error) { + if len(memo) == 0 { + return GMPMemo{}, nil } - var msg Message - if err := json.Unmarshal([]byte(memoStr), &msg); err != nil { + var msg GMPMemo + if err := json.Unmarshal([]byte(memo), &msg); err != nil { return msg, err } diff --git a/x/uibc/gmp/gmp_middleware_test.go b/x/uibc/gmp/gmp_middleware_test.go index 6dd025e2a4..7b1132a434 100644 --- a/x/uibc/gmp/gmp_middleware_test.go +++ b/x/uibc/gmp/gmp_middleware_test.go @@ -20,6 +20,13 @@ func TestGmpMemoHandler(t *testing.T) { memo func() string errMsg string }{ + { + name: "empty memo", + memo: func() string { + return "" + }, + errMsg: "", + }, { name: "invalid memo", memo: func() string { @@ -28,9 +35,24 @@ func TestGmpMemoHandler(t *testing.T) { errMsg: "invalid character", }, { - name: "not supporting the msg", + name: "unsupport msg", + memo: func() string { + validMemo := GMPMemo{ + SourceChain: "source_chain", + SourceAddress: "source_addr", + Payload: nil, + Type: int64(4), + } + m, err := json.Marshal(validMemo) + assert.NilError(t, err) + return string(m) + }, + errMsg: "unrecognized gmp message type", + }, + { + name: "unsupport msg", memo: func() string { - validMemo := Message{ + validMemo := GMPMemo{ SourceChain: "source_chain", SourceAddress: "source_addr", Payload: nil, @@ -40,12 +62,12 @@ func TestGmpMemoHandler(t *testing.T) { assert.NilError(t, err) return string(m) }, - errMsg: "we are not supporting general message", + errMsg: "only msg.type=2 (TypeGeneralMessageWithToken) is supported", }, { name: "valid memo", memo: func() string { - validMemo := Message{ + validMemo := GMPMemo{ SourceChain: "source_chain", SourceAddress: "source_addr", Payload: nil, @@ -61,7 +83,7 @@ func TestGmpMemoHandler(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - _, err := gmpHandler.OnRecvPacket(ctx, sdk.Coin{}, tc.memo(), nil) + _, err := gmpHandler.ParseMemo(ctx, sdk.Coin{}, tc.memo(), nil) if len(tc.errMsg) != 0 { assert.ErrorContains(t, err, tc.errMsg) } else { diff --git a/x/uibc/gmp/types.go b/x/uibc/gmp/types.go index 358a3a81e1..4da7eb77b9 100644 --- a/x/uibc/gmp/types.go +++ b/x/uibc/gmp/types.go @@ -15,8 +15,8 @@ const ( TypeSendToken ) -// Message is attached in ICS20 packet memo field -type Message struct { +// GMPMemo is attached in ICS20 packet memo field +type GMPMemo struct { SourceChain string `json:"source_chain"` SourceAddress string `json:"source_address"` Payload []byte `json:"payload"` diff --git a/x/uibc/uics20/memo_handler.go b/x/uibc/uics20/memo_handler.go index 7e922a4ee9..e90ce89ac1 100644 --- a/x/uibc/uics20/memo_handler.go +++ b/x/uibc/uics20/memo_handler.go @@ -46,6 +46,7 @@ func (mh *MemoHandler) onRecvPacketPrepare( ) ([]string, error) { var events []string var err error + logger := recvPacketLogger(ctx) mh.memo = ftData.Memo amount, ok := sdk.NewIntFromString(ftData.Amount) if !ok { // must not happen @@ -60,12 +61,13 @@ func (mh *MemoHandler) onRecvPacketPrepare( var memoPayload []byte if strings.EqualFold(ftData.Sender, gmp.DefaultGMPAddress) { - events = append(events, "axelar GMP transaction") + events = append(events, "Axelar GMP") mh.isGMP = true gh := gmp.NewHandler() - gmpMessage, err := gh.OnRecvPacket(*ctx, mh.received, mh.memo, mh.receiver) + gmpMessage, err := gh.ParseMemo(*ctx, mh.received, mh.memo, mh.receiver) if err != nil { - return events, err + logger.Debug("Can't parse the gmp memo", "err", err) + return events, errMemoValidation{err} } memoPayload = gmpMessage.Payload } else { @@ -73,7 +75,7 @@ func (mh *MemoHandler) onRecvPacketPrepare( } memo, err := deserializeMemo(mh.cdc, memoPayload) if err != nil { - recvPacketLogger(ctx).Debug("Not recognized ICS20 memo, ignoring hook execution", "err", err) + logger.Debug("Not recognized ICS20 memo, ignoring hook execution", "err", err) return nil, nil } if memo.FallbackAddr != "" { @@ -111,16 +113,11 @@ func (mh MemoHandler) execute(ctx *sdk.Context) error { return errHooksDisabled } - logger := recvPacketLogger(ctx) - // if mh.isGMP { - // gh := gmp.NewHandler(mh.cdc) - // return gh.OnRecvPacket(ctx, mh.received, mh.memo, mh.receiver) - // } - if len(mh.msgs) == 0 { return nil // quick return - we have nothing to handle } + logger := recvPacketLogger(ctx) for _, m := range mh.msgs { if err := mh.handleMemoMsg(ctx, m); err != nil { return sdkerrors.Wrapf(err, "error dispatching msg: %v", m)