From ef4a6340d91acf13d21038479afba0dd51205109 Mon Sep 17 00:00:00 2001 From: Youngtaek Yoon Date: Thu, 10 Aug 2023 06:33:58 +0000 Subject: [PATCH] refactor: automate EventTypeMessage inclusion in every message execution (backport cosmos/cosmos-sdk#13532) (#1063) * refactor: automate EventTypeMessage inclusion in every message execution (#13532) * Fix the test requirement and leave the hint * Update CHANGELOG.md * Lint * Update CHANGELOG.md --------- Co-authored-by: Julien Robert --- CHANGELOG.md | 1 + baseapp/baseapp.go | 32 ++++++++++++++++------- baseapp/testutil/messages.go | 2 +- x/auth/tx/service_test.go | 11 +++++--- x/auth/vesting/msg_server.go | 7 ----- x/auth/vesting/types/constants.go | 3 --- x/bank/keeper/msg_server.go | 14 ---------- x/bank/types/events.go | 2 -- x/crisis/keeper/msg_server.go | 5 ---- x/crisis/types/events.go | 3 +-- x/distribution/keeper/msg_server.go | 31 ---------------------- x/distribution/types/events.go | 2 -- x/evidence/keeper/msg_server.go | 8 ------ x/evidence/types/events.go | 1 - x/feegrant/events.go | 2 -- x/gov/keeper/msg_server.go | 40 +++-------------------------- x/gov/types/events.go | 1 - x/slashing/keeper/msg_server.go | 8 ------ x/slashing/types/events.go | 1 - x/staking/keeper/msg_server.go | 25 ------------------ x/staking/types/events.go | 1 - 21 files changed, 38 insertions(+), 162 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c1202833b..311cd3b390 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Breaking Changes * (x/foundation) [\#999](https://github.com/Finschia/finschia-sdk/pull/999) migrate x/foundation FoundationTax into x/params +* (refactor) [\#1063](https://github.com/Finschia/finschia-sdk/pull/1063) Automate EventTypeMessage inclusion in every message execution (backport cosmos/cosmos-sdk#13532) ### Build, CI * (build,ci) [\#1043](https://github.com/Finschia/finschia-sdk/pull/1043) Update golang version to 1.20 diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 06f938cd33..299ba77670 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -785,15 +785,13 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg) (*sdk.Result, error // NOTE: GasWanted is determined by the AnteHandler and GasUsed by the GasMeter. for i, msg := range msgs { var ( - msgResult *sdk.Result - eventMsgName string // name to use as value in event `message.action` - err error + msgResult *sdk.Result + err error ) if handler := app.msgServiceRouter.Handler(msg); handler != nil { // ADR 031 request type routing msgResult, err = handler(ctx, msg) - eventMsgName = sdk.MsgTypeURL(msg) } else if legacyMsg, ok := msg.(legacytx.LegacyMsg); ok { // legacy sdk.Msg routing // Assuming that the app developer has migrated all their Msgs to @@ -801,7 +799,6 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg) (*sdk.Result, error // path should never be called, because all those Msgs should be // registered within the `msgServiceRouter` already. msgRoute := legacyMsg.Route() - eventMsgName = legacyMsg.Type() handler := app.router.Route(ctx, msgRoute) if handler == nil { return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized message route: %s; message index: %d", msgRoute, i) @@ -816,10 +813,8 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg) (*sdk.Result, error return nil, sdkerrors.Wrapf(err, "failed to execute message; message index: %d", i) } - msgEvents := sdk.Events{ - sdk.NewEvent(sdk.EventTypeMessage, sdk.NewAttribute(sdk.AttributeKeyAction, eventMsgName)), - } - msgEvents = msgEvents.AppendEvents(msgResult.GetEvents()) + // create message events + msgEvents := createEvents(msg).AppendEvents(msgResult.GetEvents()) // append message events, data and logs // @@ -842,3 +837,22 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg) (*sdk.Result, error Events: events.ToABCIEvents(), }, nil } + +func createEvents(msg sdk.Msg) sdk.Events { + eventMsgName := sdk.MsgTypeURL(msg) + msgEvent := sdk.NewEvent(sdk.EventTypeMessage, sdk.NewAttribute(sdk.AttributeKeyAction, eventMsgName)) + + // we set the signer attribute as the sender + if len(msg.GetSigners()) > 0 && !msg.GetSigners()[0].Empty() { + msgEvent = msgEvent.AppendAttributes(sdk.NewAttribute(sdk.AttributeKeySender, msg.GetSigners()[0].String())) + } + + // here we assume that routes module name is the second element of the route + // e.g. "cosmos.bank.v1beta1.MsgSend" => "bank" + moduleName := strings.Split(eventMsgName, ".") + if len(moduleName) > 1 { + msgEvent = msgEvent.AppendAttributes(sdk.NewAttribute(sdk.AttributeKeyModule, moduleName[1])) + } + + return sdk.Events{msgEvent} +} diff --git a/baseapp/testutil/messages.go b/baseapp/testutil/messages.go index 304401c37b..1b1024e510 100644 --- a/baseapp/testutil/messages.go +++ b/baseapp/testutil/messages.go @@ -42,7 +42,7 @@ func (msg *MsgCounter2) ValidateBasic() error { var _ sdk.Msg = &MsgKeyValue{} func (msg *MsgKeyValue) GetSigners() []sdk.AccAddress { - if msg.Signer == "" { + if len(msg.Signer) == 0 { return []sdk.AccAddress{} } diff --git a/x/auth/tx/service_test.go b/x/auth/tx/service_test.go index 74d4cefec2..68f7583dbc 100644 --- a/x/auth/tx/service_test.go +++ b/x/auth/tx/service_test.go @@ -146,8 +146,13 @@ func (s IntegrationTestSuite) TestSimulateTx_GRPC() { } else { s.Require().NoError(err) // Check the result and gas used are correct. - s.Require().Equal(len(res.GetResult().GetEvents()), 6) // 1 coin recv 1 coin spent, 1 transfer, 3 messages. - s.Require().True(res.GetGasInfo().GetGasUsed() > 0) // Gas used sometimes change, just check it's not empty. + // + // NOTE(0Tech): This comment should be updated after applying #11985. + // Events from the antehandlers would not be included in the simulation. The 5 events are: + // - Sending Amount to recipient: coin_spent, coin_received, transfer and message.sender= + // - Msg events: message.module=bank, message.action=/cosmos.bank.v1beta1.MsgSend and message.sender= (in one message) + s.Require().Equal(5, len(res.GetResult().GetEvents())) + s.Require().True(res.GetGasInfo().GetGasUsed() > 0) // Gas used sometimes change, just check it's not empty. } }) } @@ -187,7 +192,7 @@ func (s IntegrationTestSuite) TestSimulateTx_GRPCGateway() { err = val.ClientCtx.Codec.UnmarshalJSON(res, &result) s.Require().NoError(err) // Check the result and gas used are correct. - s.Require().Equal(len(result.GetResult().GetEvents()), 6) // 1 coin recv, 1 coin spent,1 transfer, 3 messages. + s.Require().Equal(5, len(result.GetResult().GetEvents())) // See TestSimulateTx_GRPC for the 5 events. s.Require().True(result.GetGasInfo().GetGasUsed() > 0) // Gas used sometimes change, just check it's not empty. } }) diff --git a/x/auth/vesting/msg_server.go b/x/auth/vesting/msg_server.go index 704ec547d0..f8fd835db0 100644 --- a/x/auth/vesting/msg_server.go +++ b/x/auth/vesting/msg_server.go @@ -89,12 +89,5 @@ func (s msgServer) CreateVestingAccount(goCtx context.Context, msg *types.MsgCre return nil, err } - ctx.EventManager().EmitEvent( - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - ) - return &types.MsgCreateVestingAccountResponse{}, nil } diff --git a/x/auth/vesting/types/constants.go b/x/auth/vesting/types/constants.go index 1d0b0ebbb3..b385598d6a 100644 --- a/x/auth/vesting/types/constants.go +++ b/x/auth/vesting/types/constants.go @@ -4,9 +4,6 @@ const ( // ModuleName defines the module's name. ModuleName = "vesting" - // AttributeValueCategory is an alias for the message event value. - AttributeValueCategory = ModuleName - // RouterKey defines the module's message routing key RouterKey = ModuleName ) diff --git a/x/bank/keeper/msg_server.go b/x/bank/keeper/msg_server.go index fa03718297..a19186231b 100644 --- a/x/bank/keeper/msg_server.go +++ b/x/bank/keeper/msg_server.go @@ -60,13 +60,6 @@ func (k msgServer) Send(goCtx context.Context, msg *types.MsgSend) (*types.MsgSe } }() - ctx.EventManager().EmitEvent( - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - ) - return &types.MsgSendResponse{}, nil } @@ -93,12 +86,5 @@ func (k msgServer) MultiSend(goCtx context.Context, msg *types.MsgMultiSend) (*t return nil, err } - ctx.EventManager().EmitEvent( - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - ), - ) - return &types.MsgMultiSendResponse{}, nil } diff --git a/x/bank/types/events.go b/x/bank/types/events.go index 531f160b7f..ee1ed02012 100644 --- a/x/bank/types/events.go +++ b/x/bank/types/events.go @@ -11,8 +11,6 @@ const ( AttributeKeyRecipient = "recipient" AttributeKeySender = "sender" - AttributeValueCategory = ModuleName - // supply and balance tracking events name and attributes EventTypeCoinSpent = "coin_spent" EventTypeCoinReceived = "coin_received" diff --git a/x/crisis/keeper/msg_server.go b/x/crisis/keeper/msg_server.go index 96e53a600d..4fcb1c10be 100644 --- a/x/crisis/keeper/msg_server.go +++ b/x/crisis/keeper/msg_server.go @@ -55,11 +55,6 @@ func (k Keeper) VerifyInvariant(goCtx context.Context, msg *types.MsgVerifyInvar types.EventTypeInvariant, sdk.NewAttribute(types.AttributeKeyRoute, msg.InvariantRoute), ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCrisis), - sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender), - ), }) return &types.MsgVerifyInvariantResponse{}, nil diff --git a/x/crisis/types/events.go b/x/crisis/types/events.go index a20a7d7a7e..823051733f 100644 --- a/x/crisis/types/events.go +++ b/x/crisis/types/events.go @@ -4,6 +4,5 @@ package types const ( EventTypeInvariant = "invariant" - AttributeValueCrisis = ModuleName - AttributeKeyRoute = "route" + AttributeKeyRoute = "route" ) diff --git a/x/distribution/keeper/msg_server.go b/x/distribution/keeper/msg_server.go index 596f96f73a..107f8a5d96 100644 --- a/x/distribution/keeper/msg_server.go +++ b/x/distribution/keeper/msg_server.go @@ -38,14 +38,6 @@ func (k msgServer) SetWithdrawAddress(goCtx context.Context, msg *types.MsgSetWi return nil, err } - ctx.EventManager().EmitEvent( - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - sdk.NewAttribute(sdk.AttributeKeySender, msg.DelegatorAddress), - ), - ) - return &types.MsgSetWithdrawAddressResponse{}, nil } @@ -77,13 +69,6 @@ func (k msgServer) WithdrawDelegatorReward(goCtx context.Context, msg *types.Msg } }() - ctx.EventManager().EmitEvent( - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - sdk.NewAttribute(sdk.AttributeKeySender, msg.DelegatorAddress), - ), - ) return &types.MsgWithdrawDelegatorRewardResponse{}, nil } @@ -111,14 +96,6 @@ func (k msgServer) WithdrawValidatorCommission(goCtx context.Context, msg *types } }() - ctx.EventManager().EmitEvent( - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - sdk.NewAttribute(sdk.AttributeKeySender, msg.ValidatorAddress), - ), - ) - return &types.MsgWithdrawValidatorCommissionResponse{}, nil } @@ -133,13 +110,5 @@ func (k msgServer) FundCommunityPool(goCtx context.Context, msg *types.MsgFundCo return nil, err } - ctx.EventManager().EmitEvent( - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - sdk.NewAttribute(sdk.AttributeKeySender, msg.Depositor), - ), - ) - return &types.MsgFundCommunityPoolResponse{}, nil } diff --git a/x/distribution/types/events.go b/x/distribution/types/events.go index ce4c0ef62e..47c6b83b09 100644 --- a/x/distribution/types/events.go +++ b/x/distribution/types/events.go @@ -11,6 +11,4 @@ const ( AttributeKeyWithdrawAddress = "withdraw_address" AttributeKeyValidator = "validator" - - AttributeValueCategory = ModuleName ) diff --git a/x/evidence/keeper/msg_server.go b/x/evidence/keeper/msg_server.go index edce46d159..f85d0db6e3 100644 --- a/x/evidence/keeper/msg_server.go +++ b/x/evidence/keeper/msg_server.go @@ -28,14 +28,6 @@ func (ms msgServer) SubmitEvidence(goCtx context.Context, msg *types.MsgSubmitEv return nil, err } - ctx.EventManager().EmitEvent( - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - sdk.NewAttribute(sdk.AttributeKeySender, msg.GetSubmitter().String()), - ), - ) - return &types.MsgSubmitEvidenceResponse{ Hash: evidence.Hash(), }, nil diff --git a/x/evidence/types/events.go b/x/evidence/types/events.go index fe468c43e5..512b093ade 100644 --- a/x/evidence/types/events.go +++ b/x/evidence/types/events.go @@ -4,6 +4,5 @@ package types const ( EventTypeSubmitEvidence = "submit_evidence" - AttributeValueCategory = "evidence" AttributeKeyEvidenceHash = "evidence_hash" ) diff --git a/x/feegrant/events.go b/x/feegrant/events.go index a4470b8270..d4ffafa2e3 100644 --- a/x/feegrant/events.go +++ b/x/feegrant/events.go @@ -8,6 +8,4 @@ const ( AttributeKeyGranter = "granter" AttributeKeyGrantee = "grantee" - - AttributeValueCategory = ModuleName ) diff --git a/x/gov/keeper/msg_server.go b/x/gov/keeper/msg_server.go index 589ed4a48e..ea3078a41f 100644 --- a/x/gov/keeper/msg_server.go +++ b/x/gov/keeper/msg_server.go @@ -38,22 +38,14 @@ func (k msgServer) SubmitProposal(goCtx context.Context, msg *types.MsgSubmitPro return nil, err } - ctx.EventManager().EmitEvent( - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - sdk.NewAttribute(sdk.AttributeKeySender, msg.GetProposer().String()), - ), - ) - - submitEvent := sdk.NewEvent(types.EventTypeSubmitProposal, sdk.NewAttribute(types.AttributeKeyProposalType, msg.GetContent().ProposalType())) if votingStarted { - submitEvent = submitEvent.AppendAttributes( - sdk.NewAttribute(types.AttributeKeyVotingPeriodStart, fmt.Sprintf("%d", proposal.ProposalId)), + ctx.EventManager().EmitEvent( + sdk.NewEvent(types.EventTypeSubmitProposal, + sdk.NewAttribute(types.AttributeKeyVotingPeriodStart, fmt.Sprintf("%d", proposal.ProposalId)), + ), ) } - ctx.EventManager().EmitEvent(submitEvent) return &types.MsgSubmitProposalResponse{ ProposalId: proposal.ProposalId, }, nil @@ -79,14 +71,6 @@ func (k msgServer) Vote(goCtx context.Context, msg *types.MsgVote) (*types.MsgVo }, ) - ctx.EventManager().EmitEvent( - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - sdk.NewAttribute(sdk.AttributeKeySender, msg.Voter), - ), - ) - return &types.MsgVoteResponse{}, nil } @@ -109,14 +93,6 @@ func (k msgServer) VoteWeighted(goCtx context.Context, msg *types.MsgVoteWeighte }, ) - ctx.EventManager().EmitEvent( - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - sdk.NewAttribute(sdk.AttributeKeySender, msg.Voter), - ), - ) - return &types.MsgVoteWeightedResponse{}, nil } @@ -139,14 +115,6 @@ func (k msgServer) Deposit(goCtx context.Context, msg *types.MsgDeposit) (*types }, ) - ctx.EventManager().EmitEvent( - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - sdk.NewAttribute(sdk.AttributeKeySender, msg.Depositor), - ), - ) - if votingStarted { ctx.EventManager().EmitEvent( sdk.NewEvent( diff --git a/x/gov/types/events.go b/x/gov/types/events.go index 19f8857f3a..2c33a37d08 100644 --- a/x/gov/types/events.go +++ b/x/gov/types/events.go @@ -12,7 +12,6 @@ const ( AttributeKeyOption = "option" AttributeKeyProposalID = "proposal_id" AttributeKeyVotingPeriodStart = "voting_period_start" - AttributeValueCategory = "governance" AttributeValueProposalDropped = "proposal_dropped" // didn't meet min deposit AttributeValueProposalPassed = "proposal_passed" // met vote quorum AttributeValueProposalRejected = "proposal_rejected" // didn't meet vote quorum diff --git a/x/slashing/keeper/msg_server.go b/x/slashing/keeper/msg_server.go index 22d78604f8..2844dd1d0f 100644 --- a/x/slashing/keeper/msg_server.go +++ b/x/slashing/keeper/msg_server.go @@ -34,13 +34,5 @@ func (k msgServer) Unjail(goCtx context.Context, msg *types.MsgUnjail) (*types.M return nil, err } - ctx.EventManager().EmitEvent( - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - sdk.NewAttribute(sdk.AttributeKeySender, msg.ValidatorAddr), - ), - ) - return &types.MsgUnjailResponse{}, nil } diff --git a/x/slashing/types/events.go b/x/slashing/types/events.go index e9fb254545..5486c38042 100644 --- a/x/slashing/types/events.go +++ b/x/slashing/types/events.go @@ -14,5 +14,4 @@ const ( AttributeValueDoubleSign = "double_sign" AttributeValueMissingSignature = "missing_signature" - AttributeValueCategory = ModuleName ) diff --git a/x/staking/keeper/msg_server.go b/x/staking/keeper/msg_server.go index 8c993e14fb..8ffb189a2f 100644 --- a/x/staking/keeper/msg_server.go +++ b/x/staking/keeper/msg_server.go @@ -114,11 +114,6 @@ func (k msgServer) CreateValidator(goCtx context.Context, msg *types.MsgCreateVa sdk.NewAttribute(types.AttributeKeyValidator, msg.ValidatorAddress), sdk.NewAttribute(sdk.AttributeKeyAmount, msg.Value.String()), ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - sdk.NewAttribute(sdk.AttributeKeySender, msg.DelegatorAddress), - ), }) return &types.MsgCreateValidatorResponse{}, nil @@ -177,11 +172,6 @@ func (k msgServer) EditValidator(goCtx context.Context, msg *types.MsgEditValida sdk.NewAttribute(types.AttributeKeyCommissionRate, validator.Commission.String()), sdk.NewAttribute(types.AttributeKeyMinSelfDelegation, validator.MinSelfDelegation.String()), ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - sdk.NewAttribute(sdk.AttributeKeySender, msg.ValidatorAddress), - ), }) return &types.MsgEditValidatorResponse{}, nil @@ -236,11 +226,6 @@ func (k msgServer) Delegate(goCtx context.Context, msg *types.MsgDelegate) (*typ sdk.NewAttribute(sdk.AttributeKeyAmount, msg.Amount.String()), sdk.NewAttribute(types.AttributeKeyNewShares, newShares.String()), ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - sdk.NewAttribute(sdk.AttributeKeySender, msg.DelegatorAddress), - ), }) return &types.MsgDelegateResponse{}, nil @@ -302,11 +287,6 @@ func (k msgServer) BeginRedelegate(goCtx context.Context, msg *types.MsgBeginRed sdk.NewAttribute(sdk.AttributeKeyAmount, msg.Amount.String()), sdk.NewAttribute(types.AttributeKeyCompletionTime, completionTime.Format(time.RFC3339)), ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - sdk.NewAttribute(sdk.AttributeKeySender, msg.DelegatorAddress), - ), }) return &types.MsgBeginRedelegateResponse{ @@ -363,11 +343,6 @@ func (k msgServer) Undelegate(goCtx context.Context, msg *types.MsgUndelegate) ( sdk.NewAttribute(sdk.AttributeKeyAmount, msg.Amount.String()), sdk.NewAttribute(types.AttributeKeyCompletionTime, completionTime.Format(time.RFC3339)), ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - sdk.NewAttribute(sdk.AttributeKeySender, msg.DelegatorAddress), - ), }) return &types.MsgUndelegateResponse{ diff --git a/x/staking/types/events.go b/x/staking/types/events.go index 99a0be1ad6..87801311b0 100644 --- a/x/staking/types/events.go +++ b/x/staking/types/events.go @@ -18,5 +18,4 @@ const ( AttributeKeyDelegator = "delegator" AttributeKeyCompletionTime = "completion_time" AttributeKeyNewShares = "new_shares" - AttributeValueCategory = ModuleName )