Skip to content

Commit

Permalink
handle events
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-zaremba committed Feb 28, 2024
1 parent 9a48b30 commit 027ea07
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
28 changes: 20 additions & 8 deletions x/uibc/uics20/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Check warning on line 68 in x/uibc/uics20/ibc_module.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/uics20/ibc_module.go#L65-L68

Added lines #L65 - L68 were not covered by tests
}
Expand All @@ -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())

Check warning on line 85 in x/uibc/uics20/ibc_module.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/uics20/ibc_module.go#L84-L85

Added lines #L84 - L85 were not covered by tests
}

// TODO handle errors
im.emitEvents(ctx.EventManager(), recvPacketLogger(&ctx), "ics20-memo-hook", events)

Check warning on line 88 in x/uibc/uics20/ibc_module.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/uics20/ibc_module.go#L88

Added line #L88 was not covered by tests

return ack
}
Expand Down Expand Up @@ -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)

Check warning on line 120 in x/uibc/uics20/ibc_module.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/uics20/ibc_module.go#L120

Added line #L120 was not covered by tests
}
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)

Check warning on line 130 in x/uibc/uics20/ibc_module.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/uics20/ibc_module.go#L126-L130

Added lines #L126 - L130 were not covered by tests
}
logger.Debug("Handle ICS20 memo", "events", events)

Check warning on line 132 in x/uibc/uics20/ibc_module.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/uics20/ibc_module.go#L132

Added line #L132 was not covered by tests

em.EmitEvents(sdk.Events{
sdk.NewEvent(
topic,
attributes...,
),
})

Check warning on line 139 in x/uibc/uics20/ibc_module.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/uics20/ibc_module.go#L134-L139

Added lines #L134 - L139 were not covered by tests
}

func deserializeFTData(cdc codec.JSONCodec, packet channeltypes.Packet,
) (d ics20types.FungibleTokenPacketData, err error) {
if err = cdc.UnmarshalJSON(packet.GetData(), &d); err != nil {
Expand Down
26 changes: 13 additions & 13 deletions x/uibc/uics20/memo_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 30 in x/uibc/uics20/memo_handler.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/uics20/memo_handler.go#L25-L30

Added lines #L25 - L30 were not covered by tests
}
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")

Check warning on line 37 in x/uibc/uics20/memo_handler.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/uics20/memo_handler.go#L32-L37

Added lines #L32 - L37 were not covered by tests
}
}

msgs, err = memo.GetMsgs()

Check warning on line 41 in x/uibc/uics20/memo_handler.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/uics20/memo_handler.go#L41

Added line #L41 was not covered by tests
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

Check warning on line 45 in x/uibc/uics20/memo_handler.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/uics20/memo_handler.go#L43-L45

Added lines #L43 - L45 were not covered by tests
}

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")

Check warning on line 50 in x/uibc/uics20/memo_handler.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/uics20/memo_handler.go#L49-L50

Added lines #L49 - L50 were not covered by tests
}
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)

Check warning on line 54 in x/uibc/uics20/memo_handler.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/uics20/memo_handler.go#L53-L54

Added lines #L53 - L54 were not covered by tests
}
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

Check warning on line 60 in x/uibc/uics20/memo_handler.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/uics20/memo_handler.go#L57-L60

Added lines #L57 - L60 were not covered by tests
}

return msgs, fallbackReceiver, nil
return msgs, fallbackReceiver, events, nil

Check warning on line 63 in x/uibc/uics20/memo_handler.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/uics20/memo_handler.go#L63

Added line #L63 was not covered by tests
}

// runs messages encoded in the ICS20 memo.
Expand Down

0 comments on commit 027ea07

Please sign in to comment.