From 33121e1aa4d7c8c9f52912ea5179696f76a97f08 Mon Sep 17 00:00:00 2001 From: Dreamer <745124335@qq.com> Date: Wed, 1 Feb 2023 15:57:05 +0800 Subject: [PATCH 1/2] add changelog --- CHANGELOG.md | 18 +++++++++++++++++- RELEASES.md | 4 +++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 162b1db..bd2b983 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,7 +34,23 @@ Ref: https://keepachangelog.com/en/1.0.0/ # Changelog -## [Unreleased] +## [v1.1.1-beta] + +### Dependencies + +### API Breaking + +### State Machine Breaking + +### Improvements + +* [\#7](https://github.com/bianjieai/nft-transfer/pull/7) modify JSON encoding rules + +### Features + +### Bug Fixes + +## [v1.1.0-beta] ### Dependencies diff --git a/RELEASES.md b/RELEASES.md index c2405f3..dda6b07 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -97,4 +97,6 @@ Versions of Golang, Cosmos SDK and Tendermint used by ibc-go in the currently ac | Go | nft-transfer | ibc-go | Cosmos SDK | Tendermint | | ---- | ------------ | ------ | ---------- | ---------- | -| 1.18 | main | v5.0.1 | v0.46.3 | v0.34.22 | +| 1.18 | v1.1.1-beta | v5.0.1 | v0.46.3 | v0.34.22 | +| 1.18 | v1.1.0-beta | v5.0.1 | v0.46.3 | v0.34.22 | +| 1.18 | v1.0.0-beta | v5.0.1 | v0.46.3 | v0.34.22 | From f60bef6a0d65f1b89835cc9ca44d7584a62c9142 Mon Sep 17 00:00:00 2001 From: Dreamer <745124335@qq.com> Date: Tue, 7 Feb 2023 16:54:36 +0800 Subject: [PATCH 2/2] add error detail in acknowledgement event --- ibc_module.go | 36 ++++++++++++++---------------------- keeper/events.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 22 deletions(-) create mode 100644 keeper/events.go diff --git a/ibc_module.go b/ibc_module.go index 08cce75..192f846 100644 --- a/ibc_module.go +++ b/ibc_module.go @@ -182,36 +182,25 @@ func (im IBCModule) OnRecvPacket( relayer sdk.AccAddress, ) ibcexported.Acknowledgement { var ( - ack = channeltypes.NewResultAcknowledgement([]byte{byte(1)}) - data types.NonFungibleTokenPacketData - ackErr error + ack = channeltypes.NewResultAcknowledgement([]byte{byte(1)}) + data types.NonFungibleTokenPacketData + err error ) - if err := types.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil { - ackErr = sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "cannot unmarshal ICS-721 nft-transfer packet data") - ack = channeltypes.NewErrorAcknowledgement(ackErr) + if err = types.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil { + ack = channeltypes.NewErrorAcknowledgement( + sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "cannot unmarshal ICS-721 nft-transfer packet data"), + ) } // only attempt the application logic if the packet data // was successfully decoded if ack.Success() { - if err := im.keeper.OnRecvPacket(ctx, packet, data); err != nil { + if err = im.keeper.OnRecvPacket(ctx, packet, data); err != nil { ack = channeltypes.NewErrorAcknowledgement(err) } } - - ctx.EventManager().EmitEvent( - sdk.NewEvent( - types.EventTypePacket, - sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName), - sdk.NewAttribute(sdk.AttributeKeySender, data.Sender), - sdk.NewAttribute(types.AttributeKeyReceiver, data.Receiver), - sdk.NewAttribute(types.AttributeKeyClassID, data.ClassId), - sdk.NewAttribute(types.AttributeKeyTokenIDs, strings.Join(data.TokenIds, ",")), - sdk.NewAttribute(types.AttributeKeyAckSuccess, fmt.Sprintf("%t", ack.Success())), - ), - ) - + keeper.EmitAcknowledgementEvent(ctx, data, ack, err) // NOTE: acknowledgement will be written synchronously during IBC handler execution. return ack } @@ -225,11 +214,14 @@ func (im IBCModule) OnAcknowledgementPacket( ) error { var ack channeltypes.Acknowledgement if err := types.ModuleCdc.UnmarshalJSON(acknowledgement, &ack); err != nil { - return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal ICS-721 transfer packet acknowledgement: %v", err) + return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, + "cannot unmarshal ICS-721 transfer packet acknowledgement: %v", err) } + var data types.NonFungibleTokenPacketData if err := types.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil { - return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal ICS-721 transfer packet data: %s", err.Error()) + return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, + "cannot unmarshal ICS-721 transfer packet data: %s", err.Error()) } if err := im.keeper.OnAcknowledgementPacket(ctx, packet, data, ack); err != nil { diff --git a/keeper/events.go b/keeper/events.go new file mode 100644 index 0000000..99ae293 --- /dev/null +++ b/keeper/events.go @@ -0,0 +1,36 @@ +package keeper + +import ( + "fmt" + "strings" + + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/cosmos/ibc-go/v5/modules/core/exported" + + "github.com/bianjieai/nft-transfer/types" +) + +// EmitAcknowledgementEvent emits an event signalling a successful or failed acknowledgement and including the error +// details if any. +func EmitAcknowledgementEvent(ctx sdk.Context, data types.NonFungibleTokenPacketData, ack exported.Acknowledgement, err error) { + attributes := []sdk.Attribute{ + sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName), + sdk.NewAttribute(sdk.AttributeKeySender, data.Sender), + sdk.NewAttribute(types.AttributeKeyReceiver, data.Receiver), + sdk.NewAttribute(types.AttributeKeyClassID, data.ClassId), + sdk.NewAttribute(types.AttributeKeyTokenIDs, strings.Join(data.TokenIds, ",")), + sdk.NewAttribute(types.AttributeKeyAckSuccess, fmt.Sprintf("%t", ack.Success())), + } + + if err != nil { + attributes = append(attributes, sdk.NewAttribute(types.AttributeKeyAckError, err.Error())) + } + + ctx.EventManager().EmitEvent( + sdk.NewEvent( + types.EventTypePacket, + attributes..., + ), + ) +}