From 4d8fbbff6899556f81937d18f6ee7394e3411f10 Mon Sep 17 00:00:00 2001 From: poorphd Date: Tue, 2 Jul 2024 17:35:33 +0900 Subject: [PATCH 1/2] refactor: Remove redundant EventTypeMessage emission in custom modules EventTypeMessage events with sdk.AttributeKeyModule and sdk.AttributeKeySender are now emitted directly at message execution (in baseapp). This change removes the redundant boilerplate event emission from all custom modules. The module name is assumed by baseapp to be the second element of the message route, e.g., "cosmos.bank.v1beta1.MsgSend" -> "bank". For modules that do not follow the standard message path (e.g., IBC), it is advised to continue emitting the module name event. Baseapp only emits the EventTypeMessage event if the module has not already done so. This refactor ensures cleaner code and leverages the automatic event emission provided by baseapp. --- x/coinswap/keeper/keeper_test.go | 1 + x/coinswap/keeper/msg_server.go | 8 ----- x/coinswap/keeper/msg_server_test.go | 44 ++++++++++++++++++++++++++-- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/x/coinswap/keeper/keeper_test.go b/x/coinswap/keeper/keeper_test.go index 495e5d6ae..06a6c45f4 100644 --- a/x/coinswap/keeper/keeper_test.go +++ b/x/coinswap/keeper/keeper_test.go @@ -93,6 +93,7 @@ func (suite *TestSuite) SetupTest() { suite.keeper = suite.app.CoinswapKeeper suite.queryClient = queryClient + suite.msgServer = keeper.NewMsgServerImpl(suite.keeper) sdk.SetCoinDenomRegex(func() string { return `[a-zA-Z][a-zA-Z0-9/\-]{2,127}` diff --git a/x/coinswap/keeper/msg_server.go b/x/coinswap/keeper/msg_server.go index 8844501af..1b11bec3a 100644 --- a/x/coinswap/keeper/msg_server.go +++ b/x/coinswap/keeper/msg_server.go @@ -56,14 +56,6 @@ func (m msgServer) AddLiquidity(goCtx context.Context, msg *types.MsgAddLiquidit return nil, err } - ctx.EventManager().EmitEvent( - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender), - ), - ) - return &types.MsgAddLiquidityResponse{ MintToken: &mintToken, }, nil diff --git a/x/coinswap/keeper/msg_server_test.go b/x/coinswap/keeper/msg_server_test.go index f23aba363..a3bbddda1 100644 --- a/x/coinswap/keeper/msg_server_test.go +++ b/x/coinswap/keeper/msg_server_test.go @@ -4,6 +4,8 @@ import ( "math" sdkmath "cosmossdk.io/math" + "github.com/Canto-Network/Canto/v7/testutil" + "github.com/Canto-Network/Canto/v7/x/coinswap/types" "github.com/cometbft/cometbft/crypto/tmhash" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" @@ -11,15 +13,42 @@ import ( govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/evmos/ethermint/crypto/ethsecp256k1" - - "github.com/Canto-Network/Canto/v7/testutil" - "github.com/Canto-Network/Canto/v7/x/coinswap/types" ) var ( sender = sdk.AccAddress(tmhash.SumTruncated([]byte("sender"))).String() ) +func (suite *TestSuite) TestEventEmit() { + params := types.Params{ + Fee: sdkmath.LegacyNewDec(0), + PoolCreationFee: sdk.Coin{sdk.DefaultBondDenom, sdkmath.ZeroInt()}, + TaxRate: sdkmath.LegacyNewDec(0), + MaxStandardCoinPerPool: sdkmath.NewInt(10_000_000_000), + MaxSwapAmount: sdk.NewCoins( + sdk.NewInt64Coin("btc", 10_000_000), + ), + } + suite.app.CoinswapKeeper.SetParams(suite.ctx, params) + + msg := types.MsgAddLiquidity{ + MaxToken: sdk.NewCoin("btc", sdkmath.NewInt(1000)), + ExactStandardAmt: sdkmath.NewInt(100), + MinLiquidity: sdkmath.NewInt(100), + Deadline: 2611213344, + Sender: addrSender1.String(), + } + _, err := suite.msgServer.AddLiquidity(suite.ctx, &msg) + suite.NoError(err) + + events := suite.ctx.EventManager().Events() + found, index := findEventTypeIndex(events, sdk.EventTypeMessage) + suite.Require().True(found) + suite.Require().Equal("sender", events[index].Attributes[0].Key) + suite.Require().Equal(addrSender1.String(), events[index].Attributes[0].Value) + +} + func (suite *TestSuite) TestMsgSwapOrder_ValidateBasic() { msg := types.MsgSwapOrder{} suite.Require().Equal("/canto.coinswap.v1.MsgSwapOrder", sdk.MsgTypeURL(&msg)) @@ -362,3 +391,12 @@ func buildCoin(denom string, amt int64) sdk.Coin { Amount: sdkmath.NewInt(amt), } } + +func findEventTypeIndex(events sdk.Events, eventType string) (bool, int) { + for index, event := range events { + if event.Type == eventType { + return true, index + } + } + return false, -1 +} From 7517098bbae2ddf82764c52e0efdebb29153fade Mon Sep 17 00:00:00 2001 From: poorphd Date: Tue, 2 Jul 2024 20:55:01 +0900 Subject: [PATCH 2/2] refactor: Remove redundant EventTypeMessage emission in custom modules --- x/coinswap/keeper/msg_server.go | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/x/coinswap/keeper/msg_server.go b/x/coinswap/keeper/msg_server.go index 1b11bec3a..cc5e885cb 100644 --- a/x/coinswap/keeper/msg_server.go +++ b/x/coinswap/keeper/msg_server.go @@ -92,14 +92,6 @@ func (m msgServer) RemoveLiquidity(goCtx context.Context, msg *types.MsgRemoveLi return nil, err } - ctx.EventManager().EmitEvent( - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender), - ), - ) - var coins = make([]*sdk.Coin, 0, withdrawCoins.Len()) for _, coin := range withdrawCoins { coins = append(coins, &coin) @@ -141,13 +133,6 @@ func (m msgServer) SwapCoin(goCtx context.Context, msg *types.MsgSwapOrder) (*ty return nil, err } - ctx.EventManager().EmitEvent( - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - sdk.NewAttribute(sdk.AttributeKeySender, msg.Input.Address), - ), - ) return &types.MsgSwapCoinResponse{}, nil }