From f396b05d6edc623734253c514cd7f7b36e0bc7c8 Mon Sep 17 00:00:00 2001 From: Anmol1696 Date: Mon, 31 Oct 2022 21:17:46 +0530 Subject: [PATCH 01/12] Add more events to on IBC transfer, add tests for ibc transfer event --- modules/apps/transfer/keeper/msg_server.go | 17 ++++++++ .../apps/transfer/keeper/msg_server_test.go | 41 ++++++++++++++++++- modules/apps/transfer/types/events.go | 28 ++++++++----- 3 files changed, 73 insertions(+), 13 deletions(-) diff --git a/modules/apps/transfer/keeper/msg_server.go b/modules/apps/transfer/keeper/msg_server.go index 6fe43b7814f..ed87b3a5e36 100644 --- a/modules/apps/transfer/keeper/msg_server.go +++ b/modules/apps/transfer/keeper/msg_server.go @@ -2,11 +2,13 @@ package keeper import ( "context" + "fmt" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types" + channeltypes "github.com/cosmos/ibc-go/v6/modules/core/04-channel/types" ) var _ types.MsgServer = Keeper{} @@ -37,11 +39,26 @@ func (k Keeper) Transfer(goCtx context.Context, msg *types.MsgTransfer) (*types. k.Logger(ctx).Info("IBC fungible token transfer", "token", msg.Token.Denom, "amount", msg.Token.Amount.String(), "sender", msg.Sender, "receiver", msg.Receiver) + // get destination channel and port for emiting events + channel, found := k.channelKeeper.GetChannel(ctx, msg.SourcePort, msg.SourceChannel) + if !found { + return nil, sdkerrors.Wrapf(channeltypes.ErrChannelNotFound, "port ID (%s) channel ID (%s)", msg.SourcePort, msg.SourceChannel) + } + ctx.EventManager().EmitEvents(sdk.Events{ sdk.NewEvent( types.EventTypeTransfer, sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender), sdk.NewAttribute(types.AttributeKeyReceiver, msg.Receiver), + sdk.NewAttribute(types.AttributeKeyAmount, msg.Token.Amount.String()), + sdk.NewAttribute(types.AttributeKeyDenom, msg.Token.Denom), + sdk.NewAttribute(types.AttributeKeySrcPort, msg.SourcePort), + sdk.NewAttribute(types.AttributeKeySrcChannel, msg.SourceChannel), + sdk.NewAttribute(types.AttributeKeyDstPort, channel.GetCounterparty().GetPortID()), + sdk.NewAttribute(types.AttributeKeyDstChannel, channel.GetCounterparty().GetChannelID()), + sdk.NewAttribute(types.AttributeKeyTimeoutHeight, msg.TimeoutHeight.String()), + sdk.NewAttribute(types.AttributeKeyTimeoutTimestamp, fmt.Sprintf("%d", msg.TimeoutTimestamp)), + sdk.NewAttribute(types.AttributeKeyMemo, msg.Memo), ), sdk.NewEvent( sdk.EventTypeMessage, diff --git a/modules/apps/transfer/keeper/msg_server_test.go b/modules/apps/transfer/keeper/msg_server_test.go index 9380b7fb4f8..7d8a5fabf0b 100644 --- a/modules/apps/transfer/keeper/msg_server_test.go +++ b/modules/apps/transfer/keeper/msg_server_test.go @@ -2,7 +2,6 @@ package keeper_test import ( sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types" ) @@ -71,15 +70,53 @@ func (suite *KeeperTestSuite) TestMsgTransfer() { tc.malleate() - res, err := suite.chainA.GetSimApp().TransferKeeper.Transfer(sdk.WrapSDKContext(suite.chainA.GetContext()), msg) + ctx := suite.chainA.GetContext() + res, err := suite.chainA.GetSimApp().TransferKeeper.Transfer(sdk.WrapSDKContext(ctx), msg) + + expEvent := map[string]string{ + "sender": suite.chainA.SenderAccount.GetAddress().String(), + "receiver": suite.chainB.SenderAccount.GetAddress().String(), + "amount": coin.Amount.String(), + "denom": coin.Denom, + "src_port": path.EndpointA.ChannelConfig.PortID, + "src_channel": path.EndpointA.ChannelID, + "dst_port": path.EndpointB.ChannelConfig.PortID, + "dst_channel": path.EndpointB.ChannelID, + "timeout_height": suite.chainB.GetTimeoutHeight().String(), + "timeout_timestamp": "0", + "memo": "memo", + } + + checkTransferEvent := func(event sdk.Event) { + suite.Require().Equal(event.Type, types.EventTypeTransfer) + suite.Require().Len(event.Attributes, len(expEvent)) + for _, attr := range event.Attributes { + expValue, found := expEvent[string(attr.Key)] + suite.Require().True(found) + suite.Require().Equal(expValue, string(attr.Value)) + } + } if tc.expPass { suite.Require().NoError(err) suite.Require().NotNil(res) suite.Require().NotEqual(res.Sequence, uint64(0)) + + events := ctx.EventManager().Events() + var hasEvent bool + for _, event := range events { + if event.Type == types.EventTypeTransfer { + hasEvent = true + checkTransferEvent(event) + } + } + suite.Require().True(hasEvent) } else { suite.Require().Error(err) suite.Require().Nil(res) + + events := ctx.EventManager().Events() + suite.Require().Len(events, 0) } }) } diff --git a/modules/apps/transfer/types/events.go b/modules/apps/transfer/types/events.go index 89964a8a9a4..6b445961ba0 100644 --- a/modules/apps/transfer/types/events.go +++ b/modules/apps/transfer/types/events.go @@ -8,15 +8,21 @@ const ( EventTypeChannelClose = "channel_closed" EventTypeDenomTrace = "denomination_trace" - AttributeKeyReceiver = "receiver" - AttributeKeyDenom = "denom" - AttributeKeyAmount = "amount" - AttributeKeyRefundReceiver = "refund_receiver" - AttributeKeyRefundDenom = "refund_denom" - AttributeKeyRefundAmount = "refund_amount" - AttributeKeyAckSuccess = "success" - AttributeKeyAck = "acknowledgement" - AttributeKeyAckError = "error" - AttributeKeyTraceHash = "trace_hash" - AttributeKeyMemo = "memo" + AttributeKeyReceiver = "receiver" + AttributeKeyDenom = "denom" + AttributeKeyAmount = "amount" + AttributeKeyRefundReceiver = "refund_receiver" + AttributeKeyRefundDenom = "refund_denom" + AttributeKeyRefundAmount = "refund_amount" + AttributeKeyAckSuccess = "success" + AttributeKeyAck = "acknowledgement" + AttributeKeyAckError = "error" + AttributeKeyTraceHash = "trace_hash" + AttributeKeyMemo = "memo" + AttributeKeySrcPort = "src_port" + AttributeKeySrcChannel = "src_channel" + AttributeKeyDstPort = "dst_port" + AttributeKeyDstChannel = "dst_channel" + AttributeKeyTimeoutHeight = "timeout_height" + AttributeKeyTimeoutTimestamp = "timeout_timestamp" ) From 02348bbe648c7c2e9c87f34be8e9e94af73f5dc4 Mon Sep 17 00:00:00 2001 From: Anmol1696 Date: Mon, 31 Oct 2022 21:29:32 +0530 Subject: [PATCH 02/12] Fix imports --- modules/apps/transfer/keeper/msg_server_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/apps/transfer/keeper/msg_server_test.go b/modules/apps/transfer/keeper/msg_server_test.go index 7d8a5fabf0b..c3d591a2581 100644 --- a/modules/apps/transfer/keeper/msg_server_test.go +++ b/modules/apps/transfer/keeper/msg_server_test.go @@ -2,6 +2,7 @@ package keeper_test import ( sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types" ) From edf464bca65958bf34c3f3a3d4dfaa6da3ad2077 Mon Sep 17 00:00:00 2001 From: Anmol1696 Date: Mon, 31 Oct 2022 21:38:41 +0530 Subject: [PATCH 03/12] Fix spelling of comment --- modules/apps/transfer/keeper/msg_server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/apps/transfer/keeper/msg_server.go b/modules/apps/transfer/keeper/msg_server.go index ed87b3a5e36..490689df55c 100644 --- a/modules/apps/transfer/keeper/msg_server.go +++ b/modules/apps/transfer/keeper/msg_server.go @@ -39,7 +39,7 @@ func (k Keeper) Transfer(goCtx context.Context, msg *types.MsgTransfer) (*types. k.Logger(ctx).Info("IBC fungible token transfer", "token", msg.Token.Denom, "amount", msg.Token.Amount.String(), "sender", msg.Sender, "receiver", msg.Receiver) - // get destination channel and port for emiting events + // Get destination channel and port for emitting events channel, found := k.channelKeeper.GetChannel(ctx, msg.SourcePort, msg.SourceChannel) if !found { return nil, sdkerrors.Wrapf(channeltypes.ErrChannelNotFound, "port ID (%s) channel ID (%s)", msg.SourcePort, msg.SourceChannel) From 982e371726ad932b60e876daf574fc512f4fd2db Mon Sep 17 00:00:00 2001 From: Anmol1696 Date: Tue, 1 Nov 2022 15:59:05 +0530 Subject: [PATCH 04/12] Create checking of expected and all values into a function --- .../apps/transfer/keeper/msg_server_test.go | 72 +++++++++++-------- 1 file changed, 42 insertions(+), 30 deletions(-) diff --git a/modules/apps/transfer/keeper/msg_server_test.go b/modules/apps/transfer/keeper/msg_server_test.go index c3d591a2581..2e83486daa9 100644 --- a/modules/apps/transfer/keeper/msg_server_test.go +++ b/modules/apps/transfer/keeper/msg_server_test.go @@ -6,6 +6,33 @@ import ( "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types" ) +type ExpectedEvents map[string]map[string]string + +func checkEvents(suite *KeeperTestSuite, actualEvents sdk.Events, expEvents ExpectedEvents) { + hasEvents := make(map[string]bool) + for eventType := range expEvents { + hasEvents[eventType] = false + } + + for _, event := range actualEvents { + expEvent, eventFound := expEvents[event.Type] + if eventFound { + hasEvents[event.Type] = true + suite.Require().Len(event.Attributes, len(expEvent)) + for _, attr := range event.Attributes { + expValue, found := expEvent[string(attr.Key)] + suite.Require().True(found) + suite.Require().Equal(expValue, string(attr.Value)) + } + } + } + + for eventName, hasEvent := range hasEvents { + suite.Require().True(hasEvent, "event: %s was not found in events", eventName) + } + +} + func (suite *KeeperTestSuite) TestMsgTransfer() { var msg *types.MsgTransfer @@ -74,28 +101,20 @@ func (suite *KeeperTestSuite) TestMsgTransfer() { ctx := suite.chainA.GetContext() res, err := suite.chainA.GetSimApp().TransferKeeper.Transfer(sdk.WrapSDKContext(ctx), msg) - expEvent := map[string]string{ - "sender": suite.chainA.SenderAccount.GetAddress().String(), - "receiver": suite.chainB.SenderAccount.GetAddress().String(), - "amount": coin.Amount.String(), - "denom": coin.Denom, - "src_port": path.EndpointA.ChannelConfig.PortID, - "src_channel": path.EndpointA.ChannelID, - "dst_port": path.EndpointB.ChannelConfig.PortID, - "dst_channel": path.EndpointB.ChannelID, - "timeout_height": suite.chainB.GetTimeoutHeight().String(), - "timeout_timestamp": "0", - "memo": "memo", - } - - checkTransferEvent := func(event sdk.Event) { - suite.Require().Equal(event.Type, types.EventTypeTransfer) - suite.Require().Len(event.Attributes, len(expEvent)) - for _, attr := range event.Attributes { - expValue, found := expEvent[string(attr.Key)] - suite.Require().True(found) - suite.Require().Equal(expValue, string(attr.Value)) - } + expEvents := ExpectedEvents{ + "ibc_transfer": map[string]string{ + "sender": suite.chainA.SenderAccount.GetAddress().String(), + "receiver": suite.chainB.SenderAccount.GetAddress().String(), + "amount": coin.Amount.String(), + "denom": coin.Denom, + "src_port": path.EndpointA.ChannelConfig.PortID, + "src_channel": path.EndpointA.ChannelID, + "dst_port": path.EndpointB.ChannelConfig.PortID, + "dst_channel": path.EndpointB.ChannelID, + "timeout_height": suite.chainB.GetTimeoutHeight().String(), + "timeout_timestamp": "0", + "memo": "memo", + }, } if tc.expPass { @@ -104,14 +123,7 @@ func (suite *KeeperTestSuite) TestMsgTransfer() { suite.Require().NotEqual(res.Sequence, uint64(0)) events := ctx.EventManager().Events() - var hasEvent bool - for _, event := range events { - if event.Type == types.EventTypeTransfer { - hasEvent = true - checkTransferEvent(event) - } - } - suite.Require().True(hasEvent) + checkEvents(suite, events, expEvents) } else { suite.Require().Error(err) suite.Require().Nil(res) From 2f65684238af5f38e095e7df7778763d4f900659 Mon Sep 17 00:00:00 2001 From: Anmol1696 Date: Tue, 1 Nov 2022 16:01:13 +0530 Subject: [PATCH 05/12] Remove type of expected events and use map of map directly --- modules/apps/transfer/keeper/msg_server_test.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/modules/apps/transfer/keeper/msg_server_test.go b/modules/apps/transfer/keeper/msg_server_test.go index 2e83486daa9..8d78d3110b3 100644 --- a/modules/apps/transfer/keeper/msg_server_test.go +++ b/modules/apps/transfer/keeper/msg_server_test.go @@ -6,9 +6,11 @@ import ( "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types" ) -type ExpectedEvents map[string]map[string]string - -func checkEvents(suite *KeeperTestSuite, actualEvents sdk.Events, expEvents ExpectedEvents) { +func checkEvents( + suite *KeeperTestSuite, + actualEvents sdk.Events, + expEvents map[string]map[string]string, +) { hasEvents := make(map[string]bool) for eventType := range expEvents { hasEvents[eventType] = false @@ -30,7 +32,6 @@ func checkEvents(suite *KeeperTestSuite, actualEvents sdk.Events, expEvents Expe for eventName, hasEvent := range hasEvents { suite.Require().True(hasEvent, "event: %s was not found in events", eventName) } - } func (suite *KeeperTestSuite) TestMsgTransfer() { @@ -101,8 +102,8 @@ func (suite *KeeperTestSuite) TestMsgTransfer() { ctx := suite.chainA.GetContext() res, err := suite.chainA.GetSimApp().TransferKeeper.Transfer(sdk.WrapSDKContext(ctx), msg) - expEvents := ExpectedEvents{ - "ibc_transfer": map[string]string{ + expEvents := map[string]map[string]string{ + "ibc_transfer": { "sender": suite.chainA.SenderAccount.GetAddress().String(), "receiver": suite.chainB.SenderAccount.GetAddress().String(), "amount": coin.Amount.String(), From a67f56f289c3a1667bb438172cba7f2ee5fd4d01 Mon Sep 17 00:00:00 2001 From: Anmol1696 Date: Thu, 3 Nov 2022 10:36:16 +0530 Subject: [PATCH 06/12] Remove all event for ibc-transfer apart from amount and denom --- modules/apps/transfer/keeper/msg_server.go | 16 ----------- .../apps/transfer/keeper/msg_server_test.go | 15 +++------- modules/apps/transfer/types/events.go | 28 ++++++++----------- 3 files changed, 15 insertions(+), 44 deletions(-) diff --git a/modules/apps/transfer/keeper/msg_server.go b/modules/apps/transfer/keeper/msg_server.go index 490689df55c..8aa3446a22e 100644 --- a/modules/apps/transfer/keeper/msg_server.go +++ b/modules/apps/transfer/keeper/msg_server.go @@ -2,13 +2,10 @@ package keeper import ( "context" - "fmt" - sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types" - channeltypes "github.com/cosmos/ibc-go/v6/modules/core/04-channel/types" ) var _ types.MsgServer = Keeper{} @@ -39,12 +36,6 @@ func (k Keeper) Transfer(goCtx context.Context, msg *types.MsgTransfer) (*types. k.Logger(ctx).Info("IBC fungible token transfer", "token", msg.Token.Denom, "amount", msg.Token.Amount.String(), "sender", msg.Sender, "receiver", msg.Receiver) - // Get destination channel and port for emitting events - channel, found := k.channelKeeper.GetChannel(ctx, msg.SourcePort, msg.SourceChannel) - if !found { - return nil, sdkerrors.Wrapf(channeltypes.ErrChannelNotFound, "port ID (%s) channel ID (%s)", msg.SourcePort, msg.SourceChannel) - } - ctx.EventManager().EmitEvents(sdk.Events{ sdk.NewEvent( types.EventTypeTransfer, @@ -52,13 +43,6 @@ func (k Keeper) Transfer(goCtx context.Context, msg *types.MsgTransfer) (*types. sdk.NewAttribute(types.AttributeKeyReceiver, msg.Receiver), sdk.NewAttribute(types.AttributeKeyAmount, msg.Token.Amount.String()), sdk.NewAttribute(types.AttributeKeyDenom, msg.Token.Denom), - sdk.NewAttribute(types.AttributeKeySrcPort, msg.SourcePort), - sdk.NewAttribute(types.AttributeKeySrcChannel, msg.SourceChannel), - sdk.NewAttribute(types.AttributeKeyDstPort, channel.GetCounterparty().GetPortID()), - sdk.NewAttribute(types.AttributeKeyDstChannel, channel.GetCounterparty().GetChannelID()), - sdk.NewAttribute(types.AttributeKeyTimeoutHeight, msg.TimeoutHeight.String()), - sdk.NewAttribute(types.AttributeKeyTimeoutTimestamp, fmt.Sprintf("%d", msg.TimeoutTimestamp)), - sdk.NewAttribute(types.AttributeKeyMemo, msg.Memo), ), sdk.NewEvent( sdk.EventTypeMessage, diff --git a/modules/apps/transfer/keeper/msg_server_test.go b/modules/apps/transfer/keeper/msg_server_test.go index 8d78d3110b3..e20b2bd3be0 100644 --- a/modules/apps/transfer/keeper/msg_server_test.go +++ b/modules/apps/transfer/keeper/msg_server_test.go @@ -104,17 +104,10 @@ func (suite *KeeperTestSuite) TestMsgTransfer() { expEvents := map[string]map[string]string{ "ibc_transfer": { - "sender": suite.chainA.SenderAccount.GetAddress().String(), - "receiver": suite.chainB.SenderAccount.GetAddress().String(), - "amount": coin.Amount.String(), - "denom": coin.Denom, - "src_port": path.EndpointA.ChannelConfig.PortID, - "src_channel": path.EndpointA.ChannelID, - "dst_port": path.EndpointB.ChannelConfig.PortID, - "dst_channel": path.EndpointB.ChannelID, - "timeout_height": suite.chainB.GetTimeoutHeight().String(), - "timeout_timestamp": "0", - "memo": "memo", + "sender": suite.chainA.SenderAccount.GetAddress().String(), + "receiver": suite.chainB.SenderAccount.GetAddress().String(), + "amount": coin.Amount.String(), + "denom": coin.Denom, }, } diff --git a/modules/apps/transfer/types/events.go b/modules/apps/transfer/types/events.go index 6b445961ba0..89964a8a9a4 100644 --- a/modules/apps/transfer/types/events.go +++ b/modules/apps/transfer/types/events.go @@ -8,21 +8,15 @@ const ( EventTypeChannelClose = "channel_closed" EventTypeDenomTrace = "denomination_trace" - AttributeKeyReceiver = "receiver" - AttributeKeyDenom = "denom" - AttributeKeyAmount = "amount" - AttributeKeyRefundReceiver = "refund_receiver" - AttributeKeyRefundDenom = "refund_denom" - AttributeKeyRefundAmount = "refund_amount" - AttributeKeyAckSuccess = "success" - AttributeKeyAck = "acknowledgement" - AttributeKeyAckError = "error" - AttributeKeyTraceHash = "trace_hash" - AttributeKeyMemo = "memo" - AttributeKeySrcPort = "src_port" - AttributeKeySrcChannel = "src_channel" - AttributeKeyDstPort = "dst_port" - AttributeKeyDstChannel = "dst_channel" - AttributeKeyTimeoutHeight = "timeout_height" - AttributeKeyTimeoutTimestamp = "timeout_timestamp" + AttributeKeyReceiver = "receiver" + AttributeKeyDenom = "denom" + AttributeKeyAmount = "amount" + AttributeKeyRefundReceiver = "refund_receiver" + AttributeKeyRefundDenom = "refund_denom" + AttributeKeyRefundAmount = "refund_amount" + AttributeKeyAckSuccess = "success" + AttributeKeyAck = "acknowledgement" + AttributeKeyAckError = "error" + AttributeKeyTraceHash = "trace_hash" + AttributeKeyMemo = "memo" ) From 8d6df4cfcd2ccbfbab98683b8a838d3c5a416674 Mon Sep 17 00:00:00 2001 From: Anmol1696 Date: Thu, 3 Nov 2022 10:45:50 +0530 Subject: [PATCH 07/12] Fix imports --- modules/apps/transfer/keeper/msg_server.go | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/apps/transfer/keeper/msg_server.go b/modules/apps/transfer/keeper/msg_server.go index 8aa3446a22e..2ebebdbe53f 100644 --- a/modules/apps/transfer/keeper/msg_server.go +++ b/modules/apps/transfer/keeper/msg_server.go @@ -2,6 +2,7 @@ package keeper import ( "context" + sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" From c38b139afc774be43425c9a0e3be13fa293d798c Mon Sep 17 00:00:00 2001 From: Anmol1696 Date: Wed, 9 Nov 2022 13:30:42 +0530 Subject: [PATCH 08/12] Add memo, change event checker function --- modules/apps/transfer/keeper/msg_server.go | 1 + .../apps/transfer/keeper/msg_server_test.go | 39 ++++++++----------- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/modules/apps/transfer/keeper/msg_server.go b/modules/apps/transfer/keeper/msg_server.go index bfe697cd8ea..564647b2148 100644 --- a/modules/apps/transfer/keeper/msg_server.go +++ b/modules/apps/transfer/keeper/msg_server.go @@ -48,6 +48,7 @@ func (k Keeper) Transfer(goCtx context.Context, msg *types.MsgTransfer) (*types. sdk.NewAttribute(types.AttributeKeyReceiver, msg.Receiver), sdk.NewAttribute(types.AttributeKeyAmount, msg.Token.Amount.String()), sdk.NewAttribute(types.AttributeKeyDenom, msg.Token.Denom), + sdk.NewAttribute(types.AttributeKeyMemo, msg.Memo), ), sdk.NewEvent( sdk.EventTypeMessage, diff --git a/modules/apps/transfer/keeper/msg_server_test.go b/modules/apps/transfer/keeper/msg_server_test.go index 5112504a6c8..54b0ca090f0 100644 --- a/modules/apps/transfer/keeper/msg_server_test.go +++ b/modules/apps/transfer/keeper/msg_server_test.go @@ -3,24 +3,28 @@ package keeper_test import ( sdk "github.com/cosmos/cosmos-sdk/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" - "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types" ) -func checkEvents( - suite *KeeperTestSuite, +func (suite *KeeperTestSuite) checkTransferEvents( actualEvents sdk.Events, - expEvents map[string]map[string]string, + coin sdk.Coin, + memo string, ) { - hasEvents := make(map[string]bool) - for eventType := range expEvents { - hasEvents[eventType] = false + hasEvent := false + + eventType := "ibc_transfer" + expEvent := map[string]string{ + "sender": suite.chainA.SenderAccount.GetAddress().String(), + "receiver": suite.chainB.SenderAccount.GetAddress().String(), + "amount": coin.Amount.String(), + "denom": coin.Denom, + "memo": memo, } for _, event := range actualEvents { - expEvent, eventFound := expEvents[event.Type] - if eventFound { - hasEvents[event.Type] = true + if event.Type == eventType { + hasEvent = true suite.Require().Len(event.Attributes, len(expEvent)) for _, attr := range event.Attributes { expValue, found := expEvent[string(attr.Key)] @@ -30,9 +34,7 @@ func checkEvents( } } - for eventName, hasEvent := range hasEvents { - suite.Require().True(hasEvent, "event: %s was not found in events", eventName) - } + suite.Require().True(hasEvent, "event: %s was not found in events", eventType) } func (suite *KeeperTestSuite) TestMsgTransfer() { @@ -125,22 +127,13 @@ func (suite *KeeperTestSuite) TestMsgTransfer() { ctx := suite.chainA.GetContext() res, err := suite.chainA.GetSimApp().TransferKeeper.Transfer(sdk.WrapSDKContext(ctx), msg) - expEvents := map[string]map[string]string{ - "ibc_transfer": { - "sender": suite.chainA.SenderAccount.GetAddress().String(), - "receiver": suite.chainB.SenderAccount.GetAddress().String(), - "amount": coin.Amount.String(), - "denom": coin.Denom, - }, - } - if tc.expPass { suite.Require().NoError(err) suite.Require().NotNil(res) suite.Require().NotEqual(res.Sequence, uint64(0)) events := ctx.EventManager().Events() - checkEvents(suite, events, expEvents) + suite.checkTransferEvents(events, coin, "memo") } else { suite.Require().Error(err) suite.Require().Nil(res) From 0b6bbb7102cef52fc6fae62baa4e167fb4104f5e Mon Sep 17 00:00:00 2001 From: Anmol Date: Wed, 9 Nov 2022 16:17:06 +0530 Subject: [PATCH 09/12] Update modules/apps/transfer/keeper/msg_server_test.go --- modules/apps/transfer/keeper/msg_server_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/apps/transfer/keeper/msg_server_test.go b/modules/apps/transfer/keeper/msg_server_test.go index 54b0ca090f0..a2506b3fc69 100644 --- a/modules/apps/transfer/keeper/msg_server_test.go +++ b/modules/apps/transfer/keeper/msg_server_test.go @@ -6,7 +6,7 @@ import ( "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types" ) -func (suite *KeeperTestSuite) checkTransferEvents( +func (suite *KeeperTestSuite) assertTransferEvents( actualEvents sdk.Events, coin sdk.Coin, memo string, From 1894c3f2bacb355154873efb2cd3049f3d1956b8 Mon Sep 17 00:00:00 2001 From: Anmol Date: Wed, 9 Nov 2022 16:17:17 +0530 Subject: [PATCH 10/12] Update modules/apps/transfer/keeper/msg_server_test.go --- modules/apps/transfer/keeper/msg_server_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/apps/transfer/keeper/msg_server_test.go b/modules/apps/transfer/keeper/msg_server_test.go index a2506b3fc69..985fedd88cb 100644 --- a/modules/apps/transfer/keeper/msg_server_test.go +++ b/modules/apps/transfer/keeper/msg_server_test.go @@ -133,7 +133,7 @@ func (suite *KeeperTestSuite) TestMsgTransfer() { suite.Require().NotEqual(res.Sequence, uint64(0)) events := ctx.EventManager().Events() - suite.checkTransferEvents(events, coin, "memo") + suite.assertTransferEvents(events, coin, "memo") } else { suite.Require().Error(err) suite.Require().Nil(res) From 897909f767c24e6cad85da3a111361355c5cab04 Mon Sep 17 00:00:00 2001 From: Anmol1696 Date: Thu, 17 Nov 2022 22:06:40 +0530 Subject: [PATCH 11/12] Use constants instead of string attributes for event verification --- modules/apps/transfer/keeper/msg_server_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/modules/apps/transfer/keeper/msg_server_test.go b/modules/apps/transfer/keeper/msg_server_test.go index 985fedd88cb..0fe65b4ebdc 100644 --- a/modules/apps/transfer/keeper/msg_server_test.go +++ b/modules/apps/transfer/keeper/msg_server_test.go @@ -3,6 +3,7 @@ package keeper_test import ( sdk "github.com/cosmos/cosmos-sdk/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types" ) @@ -13,17 +14,16 @@ func (suite *KeeperTestSuite) assertTransferEvents( ) { hasEvent := false - eventType := "ibc_transfer" expEvent := map[string]string{ - "sender": suite.chainA.SenderAccount.GetAddress().String(), - "receiver": suite.chainB.SenderAccount.GetAddress().String(), - "amount": coin.Amount.String(), - "denom": coin.Denom, - "memo": memo, + sdk.AttributeKeySender: suite.chainA.SenderAccount.GetAddress().String(), + types.AttributeKeyReceiver: suite.chainB.SenderAccount.GetAddress().String(), + types.AttributeKeyAmount: coin.Amount.String(), + types.AttributeKeyDenom: coin.Denom, + types.AttributeKeyMemo: memo, } for _, event := range actualEvents { - if event.Type == eventType { + if event.Type == types.EventTypeTransfer { hasEvent = true suite.Require().Len(event.Attributes, len(expEvent)) for _, attr := range event.Attributes { @@ -34,7 +34,7 @@ func (suite *KeeperTestSuite) assertTransferEvents( } } - suite.Require().True(hasEvent, "event: %s was not found in events", eventType) + suite.Require().True(hasEvent, "event: %s was not found in events", types.EventTypeTransfer) } func (suite *KeeperTestSuite) TestMsgTransfer() { From 91b179f00579d64638fdd442cbd2d165562bab28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Colin=20Axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Mon, 21 Nov 2022 13:08:49 +0100 Subject: [PATCH 12/12] chore: add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f70c712e94f..60ba157940d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -76,6 +76,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements +* (apps/transfer) [\#2643](https://github.com/cosmos/ibc-go/pull/2643) Add amount, denom, and memo to transfer event emission. * (modules/light-clients/07-tendermint) [\#1713](https://github.com/cosmos/ibc-go/pull/1713) Allow client upgrade proposals to update `TrustingPeriod`. See ADR-026 for context. * (modules/core/02-client) [\#1188](https://github.com/cosmos/ibc-go/pull/1188/files) Routing `MsgSubmitMisbehaviour` to `UpdateClient` keeper function. Deprecating `SubmitMisbehaviour` endpoint. * (modules/core/02-client) [\#1208](https://github.com/cosmos/ibc-go/pull/1208) Replace `CheckHeaderAndUpdateState` usage in 02-client with calls to `VerifyClientMessage`, `CheckForMisbehaviour`, `UpdateStateOnMisbehaviour` and `UpdateState`.