feat: add message index event attribute to authz message execution (#12668)
## Description It's impossible currently for operators or clients to understand what events emitted from messages executed via `MsgExec` correspond to exactly what messages. This PR adds a message index attribute to each event emitted by a message via `MsgExec`. --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
This commit is contained in:
parent
0b69859809
commit
afab2f348a
@ -47,6 +47,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
|
||||
|
||||
### Improvements
|
||||
|
||||
* [#12668](https://github.com/cosmos/cosmos-sdk/pull/12668) Add `authz_msg_index` event attribute to message events emitted when executing via `MsgExec` through `x/authz`.
|
||||
* [#12634](https://github.com/cosmos/cosmos-sdk/pull/12634) Move `sdk.Dec` to math package.
|
||||
* [#12596](https://github.com/cosmos/cosmos-sdk/pull/12596) Remove all imports of the non-existent gogo/protobuf v1.3.3 to ease downstream use and go workspaces.
|
||||
* [#12589](https://github.com/cosmos/cosmos-sdk/pull/12589) Allow zero gas in simulation mode.
|
||||
|
||||
@ -2,10 +2,11 @@ package keeper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gogo/protobuf/proto"
|
||||
|
||||
abci "github.com/tendermint/tendermint/abci/types"
|
||||
"github.com/tendermint/tendermint/libs/log"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/baseapp"
|
||||
@ -84,14 +85,17 @@ func (k Keeper) update(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccA
|
||||
func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []sdk.Msg) ([][]byte, error) {
|
||||
results := make([][]byte, len(msgs))
|
||||
now := ctx.BlockTime()
|
||||
|
||||
for i, msg := range msgs {
|
||||
signers := msg.GetSigners()
|
||||
if len(signers) != 1 {
|
||||
return nil, authz.ErrAuthorizationNumOfSigners
|
||||
}
|
||||
|
||||
granter := signers[0]
|
||||
|
||||
// if granter != grantee then check authorization.Accept, otherwise we implicitly accept.
|
||||
// If granter != grantee then check authorization.Accept, otherwise we
|
||||
// implicitly accept.
|
||||
if !granter.Equals(grantee) {
|
||||
skey := grantStoreKey(grantee, granter, sdk.MsgTypeURL(msg))
|
||||
|
||||
@ -113,6 +117,7 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.Delete {
|
||||
err = k.DeleteGrant(ctx, grantee, granter, sdk.MsgTypeURL(msg))
|
||||
} else if resp.Updated != nil {
|
||||
@ -121,6 +126,7 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !resp.Accept {
|
||||
return nil, sdkerrors.ErrUnauthorized
|
||||
}
|
||||
@ -135,14 +141,19 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []
|
||||
if err != nil {
|
||||
return nil, sdkerrors.Wrapf(err, "failed to execute message; message %v", msg)
|
||||
}
|
||||
|
||||
results[i] = msgResp.Data
|
||||
|
||||
// emit the events from the dispatched actions
|
||||
events := msgResp.Events
|
||||
sdkEvents := make([]sdk.Event, 0, len(events))
|
||||
for i := 0; i < len(events); i++ {
|
||||
sdkEvents = append(sdkEvents, sdk.Event(events[i]))
|
||||
for _, event := range events {
|
||||
e := event
|
||||
e.Attributes = append(e.Attributes, abci.EventAttribute{Key: "authz_msg_index", Value: strconv.Itoa(i)})
|
||||
|
||||
sdkEvents = append(sdkEvents, sdk.Event(e))
|
||||
}
|
||||
|
||||
ctx.EventManager().EmitEvents(sdkEvents)
|
||||
}
|
||||
|
||||
|
||||
@ -316,9 +316,12 @@ func (s *TestSuite) TestDispatchedEvents() {
|
||||
result, err := s.authzKeeper.DispatchActions(s.ctx, granteeAddr, executeMsgs)
|
||||
require.NoError(err)
|
||||
require.NotNil(result)
|
||||
|
||||
events := s.ctx.EventManager().Events()
|
||||
|
||||
// get last 5 events (events that occur *after* the grant)
|
||||
events = events[len(events)-5:]
|
||||
|
||||
requiredEvents := map[string]bool{
|
||||
"coin_spent": false,
|
||||
"coin_received": false,
|
||||
|
||||
@ -17,6 +17,7 @@ func (k Keeper) Grant(goCtx context.Context, msg *authz.MsgGrant) (*authz.MsgGra
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// create the account if it is not in account state
|
||||
granteeAcc := k.authKeeper.GetAccount(ctx, grantee)
|
||||
if granteeAcc == nil {
|
||||
@ -33,6 +34,7 @@ func (k Keeper) Grant(goCtx context.Context, msg *authz.MsgGrant) (*authz.MsgGra
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
t := authorization.MsgTypeURL()
|
||||
if k.router.HandlerByTypeURL(t) == nil {
|
||||
return nil, sdkerrors.ErrInvalidType.Wrapf("%s doesn't exist.", t)
|
||||
@ -73,13 +75,16 @@ func (k Keeper) Exec(goCtx context.Context, msg *authz.MsgExec) (*authz.MsgExecR
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
msgs, err := msg.GetMessages()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
results, err := k.DispatchActions(ctx, grantee, msgs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &authz.MsgExecResponse{Results: results}, nil
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user