diff --git a/x/uibc/uics20/ibc_module.go b/x/uibc/uics20/ibc_module.go index e5859e57ca..124e956dd3 100644 --- a/x/uibc/uics20/ibc_module.go +++ b/x/uibc/uics20/ibc_module.go @@ -7,7 +7,6 @@ import ( "github.com/cometbft/cometbft/libs/log" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/iavl/internal/logger" ics20types "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types" channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" porttypes "github.com/cosmos/ibc-go/v7/modules/core/05-port/types" @@ -63,9 +62,8 @@ func (im ICS20Module) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, return ackResp } - var events []string mh := MemoHandler{im.cdc, im.leverage} - msgs, overwriteReceiver, err := mh.onRecvPacketPre(&ctx, packet, ftData) + msgs, overwriteReceiver, events, err := mh.onRecvPacketPre(&ctx, packet, ftData) if err != nil { return channeltypes.NewErrorAcknowledgement(err) } @@ -84,12 +82,10 @@ func (im ICS20Module) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, } if err := mh.dispatchMemoMsgs(&ctx, msgs); err != nil { - err := err.Error() - logger.Error("can't handle ICS20 memo", "err", err) - events = append(events, "can't handle ICS20 memo err = "+err) + events = append(events, "can't handle ICS20 memo err = "+err.Error()) } - // TODO handle errors + im.emitEvents(ctx.EventManager(), recvPacketLogger(&ctx), "ics20-memo-hook", events) return ack } @@ -121,12 +117,28 @@ func (im ICS20Module) onAckErr(ctx *sdk.Context, packet channeltypes.Packet) { ftData, err := deserializeFTData(im.cdc, packet) if err != nil { // we only log error, because we want to propagate the ack to other layers. - ctx.Logger().Error("can't revert quota update", "err", err) + ctx.Logger().With("scope", "ics20-OnAckErr").Error("can't revert quota update", "err", err) } qk := im.kb.Keeper(ctx) qk.IBCRevertQuotaUpdate(ftData.Amount, ftData.Denom) } +func (im ICS20Module) emitEvents(em *sdk.EventManager, logger log.Logger, topic string, events []string) { + attributes := make([]sdk.Attribute, len(events)) + key := topic + "-context" + for i, s := range events { + attributes[i] = sdk.NewAttribute(key, s) + } + logger.Debug("Handle ICS20 memo", "events", events) + + em.EmitEvents(sdk.Events{ + sdk.NewEvent( + topic, + attributes..., + ), + }) +} + func deserializeFTData(cdc codec.JSONCodec, packet channeltypes.Packet, ) (d ics20types.FungibleTokenPacketData, err error) { if err = cdc.UnmarshalJSON(packet.GetData(), &d); err != nil { diff --git a/x/uibc/uics20/memo_handler.go b/x/uibc/uics20/memo_handler.go index 457761f547..1daee291cd 100644 --- a/x/uibc/uics20/memo_handler.go +++ b/x/uibc/uics20/memo_handler.go @@ -22,45 +22,45 @@ type MemoHandler struct { // See ICS20Module.OnRecvPacket for the flow func (mh MemoHandler) onRecvPacketPre( ctx *sdk.Context, packet ibcexported.PacketI, ftData ics20types.FungibleTokenPacketData, -) ([]sdk.Msg, sdk.AccAddress, error) { - logger := recvPacketLogger(ctx) +) ([]sdk.Msg, sdk.AccAddress, []string, error) { + var events []string memo, err := deserializeMemo(mh.cdc, []byte(ftData.Memo)) if err != nil { - logger.Debug("Not recognized ICS20 memo, ignoring hook execution", "err", err) - return nil, nil, nil + recvPacketLogger(ctx).Debug("Not recognized ICS20 memo, ignoring hook execution", "err", err) + return nil, nil, nil, nil } var msgs []sdk.Msg var fallbackReceiver sdk.AccAddress if memo.FallbackAddr != "" { if fallbackReceiver, err = sdk.AccAddressFromBech32(memo.FallbackAddr); err != nil { - return nil, nil, + return nil, nil, nil, sdkerrors.Wrap(err, "ICS20 memo fallback_addr defined, but not formatted correctly") } } msgs, err = memo.GetMsgs() if err != nil { - logger.Debug("Can't unpack ICS20 memo messages", - "err", err, "overwrite receiver to fallback_addr", fallbackReceiver != nil) - return nil, fallbackReceiver, nil + e := "ICS20 memo recognized, but can't unpack memo.messages: " + err.Error() + events = append(events, e) + return nil, fallbackReceiver, events, nil } receiver, err := sdk.AccAddressFromBech32(ftData.Receiver) if err != nil { // must not happen - return nil, nil, sdkerrors.Wrap(err, "can't parse ftData.Receiver bech32 address") + return nil, nil, nil, sdkerrors.Wrap(err, "can't parse ftData.Receiver bech32 address") } amount, ok := sdk.NewIntFromString(ftData.Amount) if !ok { // must not happen - return nil, nil, fmt.Errorf("can't parse transfer amount: %s [%w]", ftData.Amount, err) + return nil, nil, nil, fmt.Errorf("can't parse transfer amount: %s [%w]", ftData.Amount, err) } ibcDenom := uibc.ExtractDenomFromPacketOnRecv(packet, ftData.Denom) sentCoin := sdk.NewCoin(ibcDenom, amount) if err := mh.validateMemoMsg(receiver, sentCoin, msgs); err != nil { - logger.Debug("memo.messages are not valid", "err", err) - return nil, fallbackReceiver, nil + events = append(events, "memo.messages are not valid, err: "+err.Error()) + return nil, fallbackReceiver, events, nil } - return msgs, fallbackReceiver, nil + return msgs, fallbackReceiver, events, nil } // runs messages encoded in the ICS20 memo.