Skip to content

Commit

Permalink
feat(x/authz): Add the GetAuthorization function. (backport cosmos#13047
Browse files Browse the repository at this point in the history
) (cosmos#13052)

* feat(x/authz): Add the GetAuthorization function. (cosmos#13047)

* [13027]: Create the GetAuthorization function (in the authz module).

* [13027]: Add unit tests for the new GetAuthorization function.

* [13027]: Add changelog entry.

(cherry picked from commit 5e4651e)

# Conflicts:
#	CHANGELOG.md
#	x/authz/keeper/keeper_test.go

* fix changelog

* Fix failed merge.

* Fix build issue introduced by merge.

Co-authored-by: Daniel Wedul <github@wedul.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
  • Loading branch information
3 people authored and JeancarloBarrios committed Sep 28, 2024
1 parent 28f6c96 commit 9f12259
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 34 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

### Features

* (x/authz) [#13047](https://github.com/cosmos/cosmos-sdk/pull/13047) Add a GetAuthorization function to the keeper.

### Bug Fixes

* (export) [#13029](https://github.com/cosmos/cosmos-sdk/pull/13029) Fix exporting the blockParams regression.
Expand Down
10 changes: 5 additions & 5 deletions x/authz/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,12 @@ func (k Keeper) GetAuthorizations(ctx context.Context, grantee, granter sdk.AccA

// GetAuthorization returns an Authorization and it's expiration time.
// A nil Authorization is returned under the following circumstances:
// - No grant is found.
// - A grant is found, but it is expired.
// - There was an error getting the authorization from the grant.
func (k Keeper) GetAuthorization(ctx context.Context, grantee, granter sdk.AccAddress, msgType string) (authz.Authorization, *time.Time) {
// - No grant is found.
// - A grant is found, but it is expired.
// - There was an error getting the authorization from the grant.
func (k Keeper) GetAuthorization(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType string) (authz.Authorization, *time.Time) {
grant, found := k.getGrant(ctx, grantStoreKey(grantee, granter, msgType))
if !found || (grant.Expiration != nil && grant.Expiration.Before(k.HeaderService.HeaderInfo(ctx).Time)) {
if !found || (grant.Expiration != nil && grant.Expiration.Before(ctx.BlockHeader().Time)) {
return nil, nil
}

Expand Down
40 changes: 11 additions & 29 deletions x/authz/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ func (s *TestSuite) SetupTest() {
queryClient := authz.NewQueryClient(queryHelper)
s.queryClient = queryClient

s.msgSrvr = s.authzKeeper
s.app = app
s.ctx = ctx
s.queryClient = queryClient
s.addrs = simapp.AddTestAddrsIncremental(app, ctx, 7, sdk.NewInt(30000000))
}

func (s *TestSuite) TestKeeper() {
Expand Down Expand Up @@ -477,17 +480,17 @@ func (s *TestSuite) TestGetAuthorization() {

genAuthMulti := authz.NewGenericAuthorization(sdk.MsgTypeURL(&banktypes.MsgMultiSend{}))
genAuthSend := authz.NewGenericAuthorization(sdk.MsgTypeURL(&banktypes.MsgSend{}))
sendAuth := banktypes.NewSendAuthorization(coins10, nil, s.accountKeeper.AddressCodec())
sendAuth := banktypes.NewSendAuthorization(coins10)

start := s.ctx.HeaderInfo().Time
start := s.ctx.BlockHeader().Time
expired := start.Add(time.Duration(1) * time.Second)
notExpired := start.Add(time.Duration(5) * time.Hour)

s.Require().NoError(s.authzKeeper.SaveGrant(s.ctx, addr1, addr2, genAuthMulti, nil), "creating grant 1->2")
s.Require().NoError(s.authzKeeper.SaveGrant(s.ctx, addr1, addr3, genAuthSend, &expired), "creating grant 1->3")
s.Require().NoError(s.authzKeeper.SaveGrant(s.ctx, addr1, addr4, sendAuth, &notExpired), "creating grant 1->4")
s.Require().NoError(s.app.AuthzKeeper.SaveGrant(s.ctx, addr1, addr2, genAuthMulti, nil), "creating grant 1->2")
s.Require().NoError(s.app.AuthzKeeper.SaveGrant(s.ctx, addr1, addr3, genAuthSend, &expired), "creating grant 1->3")
s.Require().NoError(s.app.AuthzKeeper.SaveGrant(s.ctx, addr1, addr4, sendAuth, &notExpired), "creating grant 1->4")
// Without access to private keeper methods, I don't know how to save a grant with an invalid authorization.
newCtx := s.ctx.WithHeaderInfo(header.Info{Time: start.Add(time.Duration(1) * time.Minute)})
newCtx := s.ctx.WithBlockTime(start.Add(time.Duration(1) * time.Minute))

tests := []struct {
name string
Expand Down Expand Up @@ -541,34 +544,13 @@ func (s *TestSuite) TestGetAuthorization() {

for _, tc := range tests {
s.Run(tc.name, func() {
actAuth, actExp := s.authzKeeper.GetAuthorization(newCtx, tc.grantee, tc.granter, tc.msgType)
actAuth, actExp := s.app.AuthzKeeper.GetAuthorization(newCtx, tc.grantee, tc.granter, tc.msgType)
s.Assert().Equal(tc.expAuth, actAuth, "authorization")
s.Assert().Equal(tc.expExp, actExp, "expiration")
})
}
}

func (s *TestSuite) TestGetAuthorizations() {
require := s.Require()
addr1 := s.addrs[1]
addr2 := s.addrs[2]

genAuthMulti := authz.NewGenericAuthorization(sdk.MsgTypeURL(&banktypes.MsgMultiSend{}))
genAuthSend := authz.NewGenericAuthorization(sdk.MsgTypeURL(&banktypes.MsgSend{}))

start := s.ctx.HeaderInfo().Time
expired := start.Add(time.Duration(1) * time.Second)

s.Require().NoError(s.authzKeeper.SaveGrant(s.ctx, addr1, addr2, genAuthMulti, &expired), "creating multi send grant 1->2")
s.Require().NoError(s.authzKeeper.SaveGrant(s.ctx, addr1, addr2, genAuthSend, &expired), "creating send grant 1->2")

authzs, err := s.authzKeeper.GetAuthorizations(s.ctx, addr1, addr2)
require.NoError(err)
require.Len(authzs, 2)
require.Equal(sdk.MsgTypeURL(&banktypes.MsgMultiSend{}), authzs[0].MsgTypeURL())
require.Equal(sdk.MsgTypeURL(&banktypes.MsgSend{}), authzs[1].MsgTypeURL())
}

func TestTestSuite(t *testing.T) {
suite.Run(t, new(TestSuite))
}

0 comments on commit 9f12259

Please sign in to comment.