Skip to content

Commit

Permalink
feat: add message index event attribute to authz message execution (b…
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored and randy75828 committed Aug 10, 2022
1 parent 5f8393c commit 47a27b3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Features

* (upgrade) [#12603](https://github.com/cosmos/cosmos-sdk/pull/12603) feat: Move AppModule.BeginBlock and AppModule.EndBlock to extension interfaces

### 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`.

### Bug Fixes

* (x/mint) [#12384](https://github.com/cosmos/cosmos-sdk/pull/12384) Ensure `GoalBonded` must be positive when performing `x/mint` parameter validation.
Expand Down
20 changes: 16 additions & 4 deletions x/authz/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -74,13 +75,17 @@ func (k Keeper) update(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccA
// grants from the message signer to the grantee.
func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []sdk.Msg) ([][]byte, error) {
results := make([][]byte, len(msgs))

for i, msg := range msgs {
signers := msg.GetSigners()
if len(signers) != 1 {
return nil, sdkerrors.ErrInvalidRequest.Wrap("authorization can be given to msg with only one signer")
}

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) {
authorization, _ := k.GetCleanAuthorization(ctx, grantee, granter, sdk.MsgTypeURL(msg))
if authorization == nil {
Expand All @@ -90,6 +95,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 {
Expand All @@ -98,6 +104,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
}
Expand All @@ -112,14 +119,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: []byte("authz_msg_index"), Value: []byte(strconv.Itoa(i))})

sdkEvents = append(sdkEvents, sdk.Event(e))
}

ctx.EventManager().EmitEvents(sdkEvents)
}

Expand Down
3 changes: 3 additions & 0 deletions x/authz/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,12 @@ func (s *TestSuite) TestDispatchedEvents() {
result, err := app.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,
Expand Down
5 changes: 5 additions & 0 deletions x/authz/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func (k Keeper) Grant(goCtx context.Context, msg *authz.MsgGrant) (*authz.MsgGra
if err != nil {
return nil, err
}

granter, err := sdk.AccAddressFromBech32(msg.Granter)
if err != nil {
return nil, err
Expand All @@ -26,6 +27,7 @@ func (k Keeper) Grant(goCtx context.Context, msg *authz.MsgGrant) (*authz.MsgGra
if authorization == nil {
return nil, sdkerrors.ErrUnpackAny.Wrap("Authorization is not present in the msg")
}

t := authorization.MsgTypeURL()
if k.router.HandlerByTypeURL(t) == nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "%s doesn't exist.", t)
Expand Down Expand Up @@ -66,13 +68,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
}

0 comments on commit 47a27b3

Please sign in to comment.