Skip to content

Commit e2fe96a

Browse files
mergify[bot]SpicyLemonjulienrbrt
authored
feat(x/authz): Add the GetAuthorization function. (backport #13047) (#13052)
* feat(x/authz): Add the GetAuthorization function. (#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 <[email protected]> Co-authored-by: Julien Robert <[email protected]>
1 parent b9d4ed8 commit e2fe96a

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
3737

3838
## [Unreleased]
3939

40+
### Features
41+
42+
* (x/authz) [#13047](https://github.com/cosmos/cosmos-sdk/pull/13047) Add a GetAuthorization function to the keeper.
43+
4044
### Bug Fixes
4145

4246
* (export) [#13029](https://github.com/cosmos/cosmos-sdk/pull/13029) Fix exporting the blockParams regression.

x/authz/keeper/keeper.go

+19
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,25 @@ func (k Keeper) GetAuthorizations(ctx sdk.Context, grantee sdk.AccAddress, grant
251251
return authorizations, nil
252252
}
253253

254+
// GetAuthorization returns an Authorization and it's expiration time.
255+
// A nil Authorization is returned under the following circumstances:
256+
// - No grant is found.
257+
// - A grant is found, but it is expired.
258+
// - There was an error getting the authorization from the grant.
259+
func (k Keeper) GetAuthorization(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType string) (authz.Authorization, *time.Time) {
260+
grant, found := k.getGrant(ctx, grantStoreKey(grantee, granter, msgType))
261+
if !found || (grant.Expiration != nil && grant.Expiration.Before(ctx.BlockHeader().Time)) {
262+
return nil, nil
263+
}
264+
265+
auth, err := grant.GetAuthorization()
266+
if err != nil {
267+
return nil, nil
268+
}
269+
270+
return auth, grant.Expiration
271+
}
272+
254273
// IterateGrants iterates over all authorization grants
255274
// This function should be used with caution because it can involve significant IO operations.
256275
// It should not be used in query or msg services without charging additional gas.

x/authz/keeper/keeper_test.go

+80-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (s *TestSuite) SetupTest() {
4545
s.app = app
4646
s.ctx = ctx
4747
s.queryClient = queryClient
48-
s.addrs = simapp.AddTestAddrsIncremental(app, ctx, 3, sdk.NewInt(30000000))
48+
s.addrs = simapp.AddTestAddrsIncremental(app, ctx, 7, sdk.NewInt(30000000))
4949
}
5050

5151
func (s *TestSuite) TestKeeper() {
@@ -366,6 +366,85 @@ func (s *TestSuite) TestDequeueAllGrantsQueue() {
366366
require.Len(authzs, 1)
367367
}
368368

369+
func (s *TestSuite) TestGetAuthorization() {
370+
addr1 := s.addrs[3]
371+
addr2 := s.addrs[4]
372+
addr3 := s.addrs[5]
373+
addr4 := s.addrs[6]
374+
375+
genAuthMulti := authz.NewGenericAuthorization(sdk.MsgTypeURL(&banktypes.MsgMultiSend{}))
376+
genAuthSend := authz.NewGenericAuthorization(sdk.MsgTypeURL(&banktypes.MsgSend{}))
377+
sendAuth := banktypes.NewSendAuthorization(coins10)
378+
379+
start := s.ctx.BlockHeader().Time
380+
expired := start.Add(time.Duration(1) * time.Second)
381+
notExpired := start.Add(time.Duration(5) * time.Hour)
382+
383+
s.Require().NoError(s.app.AuthzKeeper.SaveGrant(s.ctx, addr1, addr2, genAuthMulti, nil), "creating grant 1->2")
384+
s.Require().NoError(s.app.AuthzKeeper.SaveGrant(s.ctx, addr1, addr3, genAuthSend, &expired), "creating grant 1->3")
385+
s.Require().NoError(s.app.AuthzKeeper.SaveGrant(s.ctx, addr1, addr4, sendAuth, &notExpired), "creating grant 1->4")
386+
// Without access to private keeper methods, I don't know how to save a grant with an invalid authorization.
387+
newCtx := s.ctx.WithBlockTime(start.Add(time.Duration(1) * time.Minute))
388+
389+
tests := []struct {
390+
name string
391+
grantee sdk.AccAddress
392+
granter sdk.AccAddress
393+
msgType string
394+
expAuth authz.Authorization
395+
expExp *time.Time
396+
}{
397+
{
398+
name: "grant has nil exp and is returned",
399+
grantee: addr1,
400+
granter: addr2,
401+
msgType: genAuthMulti.MsgTypeURL(),
402+
expAuth: genAuthMulti,
403+
expExp: nil,
404+
},
405+
{
406+
name: "grant is expired not returned",
407+
grantee: addr1,
408+
granter: addr3,
409+
msgType: genAuthSend.MsgTypeURL(),
410+
expAuth: nil,
411+
expExp: nil,
412+
},
413+
{
414+
name: "grant is not expired and is returned",
415+
grantee: addr1,
416+
granter: addr4,
417+
msgType: sendAuth.MsgTypeURL(),
418+
expAuth: sendAuth,
419+
expExp: &notExpired,
420+
},
421+
{
422+
name: "grant is not expired but wrong msg type returns nil",
423+
grantee: addr1,
424+
granter: addr4,
425+
msgType: genAuthMulti.MsgTypeURL(),
426+
expAuth: nil,
427+
expExp: nil,
428+
},
429+
{
430+
name: "no grant exists between the two",
431+
grantee: addr2,
432+
granter: addr3,
433+
msgType: genAuthSend.MsgTypeURL(),
434+
expAuth: nil,
435+
expExp: nil,
436+
},
437+
}
438+
439+
for _, tc := range tests {
440+
s.Run(tc.name, func() {
441+
actAuth, actExp := s.app.AuthzKeeper.GetAuthorization(newCtx, tc.grantee, tc.granter, tc.msgType)
442+
s.Assert().Equal(tc.expAuth, actAuth, "authorization")
443+
s.Assert().Equal(tc.expExp, actExp, "expiration")
444+
})
445+
}
446+
}
447+
369448
func TestTestSuite(t *testing.T) {
370449
suite.Run(t, new(TestSuite))
371450
}

0 commit comments

Comments
 (0)