Skip to content

Commit

Permalink
fix(x/authz): bring back msg response in DispatchActions (#21044)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Aug 16, 2024
1 parent 6276b01 commit 54df758
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
1 change: 1 addition & 0 deletions x/authz/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### API Breaking Changes

* [#21044](https://github.com/cosmos/cosmos-sdk/pull/21044) `k.DispatchActions` returns a slice of byte slices of proto marshaled anys instead of a slice of byte slices of `sdk.Result.Data`.
* [#20502](https://github.com/cosmos/cosmos-sdk/pull/20502) `Accept` on the `Authorization` interface now expects the authz environment in the `context.Context`. This is already done when `Accept` is called by `k.DispatchActions`, but should be done manually if `Accept` is called directly.
* [#19783](https://github.com/cosmos/cosmos-sdk/pull/19783) Removes the use of Accounts String() method
* `NewMsgExec`, `NewMsgGrant` and `NewMsgRevoke` now takes strings as arguments instead of `sdk.AccAddress`.
Expand Down
13 changes: 12 additions & 1 deletion x/authz/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

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

"cosmossdk.io/core/appmodule"
corecontext "cosmossdk.io/core/context"
Expand Down Expand Up @@ -145,10 +146,20 @@ func (k Keeper) DispatchActions(ctx context.Context, grantee sdk.AccAddress, msg
}

// no need to use the branch service here, as if the transaction fails, the transaction will be reverted
_, err = k.MsgRouterService.InvokeUntyped(ctx, msg)
resp, err := k.MsgRouterService.InvokeUntyped(ctx, msg)
if err != nil {
return nil, fmt.Errorf("failed to execute message %d; message %v: %w", i, msg, err)
}

msgRespAny, err := gogoprotoany.NewAnyWithCacheWithValue(resp)
if err != nil {
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)
}
}

return results, nil
Expand Down
30 changes: 23 additions & 7 deletions x/authz/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"testing"
"time"

gogoproto "github.com/cosmos/gogoproto/proto"
gogoprotoany "github.com/cosmos/gogoproto/types/any"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"

Expand Down Expand Up @@ -245,12 +247,13 @@ func (s *TestSuite) TestDispatchAction() {
a := banktypes.NewSendAuthorization(coins100, nil, s.accountKeeper.AddressCodec())

testCases := []struct {
name string
req authz.MsgExec
expectErr bool
errMsg string
preRun func() sdk.Context
postRun func()
name string
req authz.MsgExec
expectErr bool
errMsg string
expectResp string
preRun func() sdk.Context
postRun func()
}{
{
"expect error authorization not found",
Expand All @@ -263,6 +266,7 @@ func (s *TestSuite) TestDispatchAction() {
}),
true,
"authorization not found",
"",
func() sdk.Context {
// remove any existing authorizations
err := s.authzKeeper.DeleteGrant(s.ctx, granteeAddr, granterAddr, bankSendAuthMsgType)
Expand All @@ -282,6 +286,7 @@ func (s *TestSuite) TestDispatchAction() {
}),
true,
"authorization expired",
"",
func() sdk.Context {
e := now.AddDate(0, 0, 1)
err := s.authzKeeper.SaveGrant(s.ctx, granteeAddr, granterAddr, a, &e)
Expand All @@ -301,6 +306,7 @@ func (s *TestSuite) TestDispatchAction() {
}),
true,
"requested amount is more than spend limit",
"",
func() sdk.Context {
e := now.AddDate(0, 1, 0)
err := s.authzKeeper.SaveGrant(s.ctx, granteeAddr, granterAddr, a, &e)
Expand All @@ -320,6 +326,7 @@ func (s *TestSuite) TestDispatchAction() {
}),
false,
"",
"/cosmos.bank.v1beta1.MsgSendResponse",
func() sdk.Context {
e := now.AddDate(0, 1, 0)
err := s.authzKeeper.SaveGrant(s.ctx, granteeAddr, granterAddr, a, &e)
Expand All @@ -346,6 +353,7 @@ func (s *TestSuite) TestDispatchAction() {
}),
false,
"",
"/cosmos.bank.v1beta1.MsgSendResponse",
func() sdk.Context {
e := now.AddDate(0, 1, 0)
err := s.authzKeeper.SaveGrant(s.ctx, granteeAddr, granterAddr, a, &e)
Expand All @@ -372,7 +380,15 @@ func (s *TestSuite) TestDispatchAction() {
require.Contains(err.Error(), tc.errMsg)
} else {
require.NoError(err)
require.NotNil(result)
require.NotEmpty(result)
// unmarshal the result
for _, res := range result {
var msgRes gogoprotoany.Any
err := gogoproto.Unmarshal(res, &msgRes)
require.NoError(err)
require.NotNil(msgRes)
require.Equal(msgRes.TypeUrl, tc.expectResp)
}
}
tc.postRun()
})
Expand Down

0 comments on commit 54df758

Please sign in to comment.