Skip to content

Commit

Permalink
refactor: automate EventTypeMessage inclusion in every message execut…
Browse files Browse the repository at this point in the history
…ion (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 <julien@rbrt.fr>
  • Loading branch information
0Tech and julienrbrt committed Aug 24, 2023
1 parent 3185422 commit ef4a634
Show file tree
Hide file tree
Showing 21 changed files with 38 additions and 162 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 23 additions & 9 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -785,23 +785,20 @@ 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
// proto messages and has registered all `Msg services`, then this
// 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)
Expand All @@ -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
//
Expand All @@ -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}
}
2 changes: 1 addition & 1 deletion baseapp/testutil/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}

Expand Down
11 changes: 8 additions & 3 deletions x/auth/tx/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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=<val1>
// - Msg events: message.module=bank, message.action=/cosmos.bank.v1beta1.MsgSend and message.sender=<val1> (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.
}
})
}
Expand Down Expand Up @@ -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.
}
})
Expand Down
7 changes: 0 additions & 7 deletions x/auth/vesting/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 0 additions & 3 deletions x/auth/vesting/types/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
14 changes: 0 additions & 14 deletions x/bank/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
2 changes: 0 additions & 2 deletions x/bank/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 0 additions & 5 deletions x/crisis/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions x/crisis/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ package types
const (
EventTypeInvariant = "invariant"

AttributeValueCrisis = ModuleName
AttributeKeyRoute = "route"
AttributeKeyRoute = "route"
)
31 changes: 0 additions & 31 deletions x/distribution/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

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

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

Expand All @@ -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
}
2 changes: 0 additions & 2 deletions x/distribution/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@ const (

AttributeKeyWithdrawAddress = "withdraw_address"
AttributeKeyValidator = "validator"

AttributeValueCategory = ModuleName
)
8 changes: 0 additions & 8 deletions x/evidence/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion x/evidence/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ package types
const (
EventTypeSubmitEvidence = "submit_evidence"

AttributeValueCategory = "evidence"
AttributeKeyEvidenceHash = "evidence_hash"
)
2 changes: 0 additions & 2 deletions x/feegrant/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@ const (

AttributeKeyGranter = "granter"
AttributeKeyGrantee = "grantee"

AttributeValueCategory = ModuleName
)
40 changes: 4 additions & 36 deletions x/gov/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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(
Expand Down
1 change: 0 additions & 1 deletion x/gov/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions x/slashing/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 0 additions & 1 deletion x/slashing/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ const (

AttributeValueDoubleSign = "double_sign"
AttributeValueMissingSignature = "missing_signature"
AttributeValueCategory = ModuleName
)
Loading

0 comments on commit ef4a634

Please sign in to comment.