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 JeancarloBarrios committed Sep 28, 2024
1 parent dd94e57 commit f594924
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,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
39 changes: 21 additions & 18 deletions x/authz/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@ import (
"bytes"
"context"
"fmt"
"strconv"
"time"

gogoproto "github.com/cosmos/gogoproto/proto"
gogoprotoany "github.com/cosmos/gogoproto/types/any"

"cosmossdk.io/core/appmodule"
corecontext "cosmossdk.io/core/context"
errorsmod "cosmossdk.io/errors"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/authz"
"github.com/gogo/protobuf/proto"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -85,6 +81,7 @@ func (k Keeper) updateGrant(ctx context.Context, grantee, granter sdk.AccAddress
// 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, _, err := k.cdc.GetMsgSigners(msg)
if err != nil {
Expand All @@ -99,13 +96,10 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []

// If granter != grantee then check authorization.Accept, otherwise we
// implicitly accept.
if !bytes.Equal(granter, grantee) {
skey := grantStoreKey(grantee, granter, sdk.MsgTypeURL(msg))

grant, found := k.getGrant(ctx, skey)
if !found {
return nil, errorsmod.Wrapf(authz.ErrNoAuthorizationFound,
"failed to get grant with given granter: %s, grantee: %s & msgType: %s ", sdk.AccAddress(granter), grantee, sdk.MsgTypeURL(msg))
if !granter.Equals(grantee) {
authorization, _ := k.GetCleanAuthorization(ctx, grantee, granter, sdk.MsgTypeURL(msg))
if authorization == nil {
return nil, sdkerrors.ErrUnauthorized.Wrap("authorization not found")
}

if grant.Expiration != nil && grant.Expiration.Before(now) {
Expand Down Expand Up @@ -154,10 +148,19 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []
return nil, fmt.Errorf("failed to create any for response %d; message %s: %w", i, gogoproto.MessageName(msg), err)
}

results[i], err = gogoproto.Marshal(msgRespAny)
if err != nil {
return nil, fmt.Errorf("failed to marshal response %d; message %s: %w", i, gogoproto.MessageName(msg), err)
results[i] = msgResp.Data

// emit the events from the dispatched actions
events := msgResp.Events
sdkEvents := make([]sdk.Event, 0, len(events))
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)
}

return results, nil
Expand Down
2 changes: 1 addition & 1 deletion x/authz/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ func (s *TestSuite) TestDispatchedEvents() {
events := s.ctx.EventManager().Events()

// get last 5 events (events that occur *after* the grant)
events = events[len(events)-2:]
events = events[len(events)-5:]

requiredEvents := map[string]bool{
"cosmos.authz.v1beta1.EventGrant": true,
Expand Down
6 changes: 1 addition & 5 deletions x/authz/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (k Keeper) Grant(goCtx context.Context, msg *authz.MsgGrant) (*authz.MsgGra
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid grantee address: %s", err)
}

granter, err := k.authKeeper.AddressCodec().StringToBytes(msg.Granter)
granter, err := sdk.AccAddressFromBech32(msg.Granter)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid granter address: %s", err)
}
Expand Down Expand Up @@ -116,10 +116,6 @@ func (k Keeper) Exec(ctx context.Context, msg *authz.MsgExec) (*authz.MsgExecRes
return nil, err
}

if err := validateMsgs(msgs); err != nil {
return nil, err
}

results, err := k.DispatchActions(ctx, grantee, msgs)
if err != nil {
return nil, err
Expand Down

0 comments on commit f594924

Please sign in to comment.