Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(authz)!: pruning expired authorizations #10714

Merged
merged 70 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
748822e
chore: remove `GetCleanAuthorization`
aleem1314 Nov 29, 2021
f9e6539
fix: fix keeper tests
aleem1314 Nov 29, 2021
1f5a61f
chore: fix test
aleem1314 Nov 29, 2021
cd07048
chore: add changelog
aleem1314 Nov 30, 2021
88d74e3
Merge branch 'master' into aleem/10611-authz-exec
aleem1314 Nov 30, 2021
c7fe1f8
chore: review changes
aleem1314 Dec 2, 2021
8b5b356
Merge branch 'master' into aleem/10611-authz-exec
aleem1314 Dec 2, 2021
873cf34
feat: add queue key
aleem1314 Dec 9, 2021
e222ff1
feat: add keeper functions
aleem1314 Dec 9, 2021
90fa276
feat: add endblocker
aleem1314 Dec 9, 2021
19c5ad4
chore: cleanup
aleem1314 Dec 10, 2021
c3c66b0
feat: add migration
aleem1314 Dec 10, 2021
6768159
chore: register migrations
aleem1314 Dec 10, 2021
8c626c3
chore: try fixing simulations
aleem1314 Dec 10, 2021
312fd88
Merge branch 'master' into aleem/8311-authz-pruning
aleem1314 Dec 10, 2021
158aba0
chore: simulations
aleem1314 Dec 12, 2021
f9c5e7d
Merge branch 'master' into aleem/8311-authz-pruning
aleem1314 Dec 12, 2021
fa3578c
chore: update delegations.go
aleem1314 Dec 13, 2021
a9e982d
chore: refactor
aleem1314 Dec 13, 2021
91ea07a
chore: add godoc
aleem1314 Dec 15, 2021
e23a4b6
feat: add migration tests
aleem1314 Dec 15, 2021
cd1ce4a
Merge branch 'aleem/authz-pruning-refactor' into aleem/8311-authz-pru…
aleem1314 Dec 15, 2021
bbde75f
fix failing tests
aleem1314 Dec 15, 2021
0f851b0
Merge branch 'master' into aleem/8311-authz-pruning
aleem1314 Dec 15, 2021
1060399
chore: fix simulations
aleem1314 Dec 15, 2021
dccff34
Merge branch 'master' into aleem/8311-authz-pruning
aleem1314 Dec 15, 2021
8f38282
chore: refactors
aleem1314 Dec 16, 2021
06cd3a6
Merge branch 'aleem/8311-authz-pruning' of https://github.com/cosmos/…
aleem1314 Dec 16, 2021
6dc61f9
chore: revert app.go
aleem1314 Dec 16, 2021
012f2e5
Merge branch 'master' into aleem/8311-authz-pruning
aleem1314 Dec 16, 2021
16efe18
Merge branch 'master' into aleem/10611-authz-exec
aleem1314 Dec 17, 2021
2bc1c51
chore: add more tests
aleem1314 Dec 22, 2021
aa1cade
Merge branch 'master' into aleem/8311-authz-pruning
aleem1314 Dec 22, 2021
67aca2e
chore: refactor
aleem1314 Dec 22, 2021
c5bde25
Merge branch 'aleem/8311-authz-pruning' of https://github.com/cosmos/…
aleem1314 Dec 22, 2021
8623dbe
Update simapp/app.go
aleem1314 Dec 23, 2021
2f27b00
Update x/authz/keeper/keeper.go
aleem1314 Dec 23, 2021
6cf3ba8
Update proto/cosmos/authz/v1beta1/authz.proto
aleem1314 Dec 23, 2021
8b2784a
Update x/authz/keeper/keeper_test.go
aleem1314 Dec 23, 2021
514da8f
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into al…
aleem1314 Jan 3, 2022
e7fce47
Merge branch 'aleem/10611-authz-exec' into aleem/8311-authz-pruning
aleem1314 Jan 3, 2022
3402724
Merge branch 'master' into aleem/8311-authz-pruning
aleem1314 Jan 4, 2022
d359412
chore: refactor simulations
aleem1314 Jan 5, 2022
2491cc3
Merge branch 'master' into aleem/8311-authz-pruning
aleem1314 Jan 6, 2022
c5b79da
Merge branch 'master' into aleem/8311-authz-pruning
aleem1314 Jan 6, 2022
fcf1afb
chore: small refactors
aleem1314 Jan 6, 2022
b97d69f
chore: fix failing test
aleem1314 Jan 6, 2022
5934d6a
chore: review changes
aleem1314 Jan 11, 2022
b14a901
Merge branch 'master' into aleem/8311-authz-pruning
aleem1314 Jan 11, 2022
5de96d9
chore: move pruning to BeginBlocker
aleem1314 Jan 13, 2022
96b52a0
feat: refactor grant queue key
aleem1314 Jan 13, 2022
5b1c4f4
chore: fix grpc test
aleem1314 Jan 13, 2022
061a360
Merge branch 'master' into aleem/8311-authz-pruning
aleem1314 Jan 13, 2022
32b47c5
Merge branch 'master' into aleem/8311-authz-pruning
aleem1314 Jan 17, 2022
ae9f324
Merge branch 'master' into aleem/8311-authz-pruning
aleem1314 Feb 7, 2022
93f6ca3
cleanup
aleem1314 Feb 7, 2022
21642e5
Merge branch 'master' into aleem/8311-authz-pruning
aleem1314 Feb 7, 2022
d116a13
Merge branch 'master' into aleem/8311-authz-pruning
aleem1314 Feb 8, 2022
6790488
resolve conflicts
aleem1314 Feb 8, 2022
3ee7fb5
chore: fix failing tests
aleem1314 Feb 8, 2022
3b981dd
Merge branch 'master' into aleem/8311-authz-pruning
aleem1314 Feb 8, 2022
e67a94f
Update x/authz/keeper/grpc_query.go
aleem1314 Feb 8, 2022
063d31d
chore: review changes
aleem1314 Feb 8, 2022
caaacc4
fix grpc test
aleem1314 Feb 8, 2022
9e052ec
Merge branch 'master' into aleem/8311-authz-pruning
aleem1314 Feb 10, 2022
1e98db8
Merge branch 'master' into aleem/8311-authz-pruning
aleem1314 Feb 16, 2022
259b014
run: make proto-gen
aleem1314 Feb 16, 2022
653ca22
chore: fix errors
aleem1314 Feb 16, 2022
de0a641
Merge branch 'master' into aleem/8311-authz-pruning
aleem1314 Feb 16, 2022
432fa61
Merge branch 'master' into aleem/8311-authz-pruning
robert-zaremba Feb 16, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: cleanup
  • Loading branch information
aleem1314 committed Dec 10, 2021
commit 19c5ad4ab1ee57ca56f88121d7c51e5914ce5a5a
42 changes: 17 additions & 25 deletions x/authz/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []
}

// SaveGrant method grants the provided authorization to the grantee on the granter's account
// with the provided expiration time and insert authorization key into the expired grants queue. If there is an existing authorization grant for the
// with the provided expiration time and insert authorization key into the grants queue. If there is an existing authorization grant for the
// same `sdk.Msg` type, this grant overwrites that.
func (k Keeper) SaveGrant(ctx sdk.Context, grantee, granter sdk.AccAddress, authorization authz.Authorization, expiration time.Time) error {
store := ctx.KVStore(k.storeKey)
skey := grantStoreKey(grantee, granter, authorization.MsgTypeURL())

grant, found := k.getGrant(ctx, skey)
if found {
aleem1314 marked this conversation as resolved.
Show resolved Hide resolved
k.removeFromExpiredGrantQueue(ctx, grantee, granter, authorization.MsgTypeURL(), grant.Expiration)
k.removeFromGrantQueue(ctx, skey, grant.Expiration)
aleem1314 marked this conversation as resolved.
Show resolved Hide resolved
}

grant, err := authz.NewGrant(authorization, expiration)
Expand All @@ -147,7 +147,7 @@ func (k Keeper) SaveGrant(ctx sdk.Context, grantee, granter sdk.AccAddress, auth

bz := k.cdc.MustMarshal(&grant)
store.Set(skey, bz)
k.insertIntoExpiredGrantQueue(ctx, grantee, granter, authorization.MsgTypeURL(), expiration)
k.insertIntoGrantQueue(ctx, skey, expiration)

return ctx.EventManager().EmitTypedEvent(&authz.EventGrant{
MsgTypeUrl: authorization.MsgTypeURL(),
Expand All @@ -165,8 +165,9 @@ func (k Keeper) DeleteGrant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk
if !found {
return sdkerrors.ErrNotFound.Wrap("authorization not found")
}

store.Delete(skey)
k.removeFromExpiredGrantQueue(ctx, grantee, granter, msgType, grant.Expiration)
k.removeFromGrantQueue(ctx, skey, grant.Expiration)
aleem1314 marked this conversation as resolved.
Show resolved Hide resolved

return ctx.EventManager().EmitTypedEvent(&authz.EventRevoke{
MsgTypeUrl: msgType,
Expand Down Expand Up @@ -263,36 +264,27 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data *authz.GenesisState) {
}
}

// insertIntoExpiredGrantQueue Inserts a grant key into the expired grant queue
func (keeper Keeper) insertIntoExpiredGrantQueue(ctx sdk.Context, grantee, granter sdk.AccAddress, msgType string, expiration time.Time) {
// insertIntoGrantQueue Inserts a grant key into the grant queue
aleem1314 marked this conversation as resolved.
Show resolved Hide resolved
func (keeper Keeper) insertIntoGrantQueue(ctx sdk.Context, grantKey []byte, expiration time.Time) {
store := ctx.KVStore(keeper.storeKey)
bz := grantStoreKey(grantee, granter, msgType)
store.Set(expiredGrantQueueKey(grantee, granter, msgType, expiration), bz)
store.Set(GrantQueueKey(grantKey, expiration), grantKey)
aleem1314 marked this conversation as resolved.
Show resolved Hide resolved
}

// removeFromExpiredGrantQueue removes a grant key from the expired grant queue
func (keeper Keeper) removeFromExpiredGrantQueue(ctx sdk.Context, grantee, granter sdk.AccAddress, msgType string, expiration time.Time) {
// removeFromGrantQueue removes a grant key from the grant queue
func (keeper Keeper) removeFromGrantQueue(ctx sdk.Context, grantKey []byte, expiration time.Time) {
store := ctx.KVStore(keeper.storeKey)
store.Delete(expiredGrantQueueKey(grantee, granter, msgType, expiration))
store.Delete(GrantQueueKey(grantKey, expiration))
}

// IterateExpiredGrantQueue iterates over the grants in the expired authorization queue
// and performs a callback function
func (keeper Keeper) IterateExpiredGrantQueue(ctx sdk.Context, endTime time.Time, cb func(grantee, granter sdk.AccAddress, msgType string) (stop bool)) {
store := ctx.KVStore(keeper.storeKey)
// DeleteAllMatureGrants deletes expired grants from the queue and deletes grant from the store.
func (k Keeper) DeleteAllMatureGrants(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)

iterator := store.Iterator(ExpiredGrantQueuePrefix, sdk.PrefixEndBytes(expiredGrantByTimeKey(endTime)))
iterator := store.Iterator(GrantQueuePrefix, sdk.PrefixEndBytes(grantByTimeKey(ctx.BlockTime())))
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
_, grantee, granter, msgType := splitExpiredGrantQueueKey(iterator.Key())
_, found := keeper.getGrant(ctx, grantStoreKey(grantee, granter, msgType))
if !found {
panic("grant not found")
}

if cb(grantee, granter, msgType) {
break
}
store.Delete(iterator.Key())
aleem1314 marked this conversation as resolved.
Show resolved Hide resolved
store.Delete(iterator.Value())
}
}
14 changes: 6 additions & 8 deletions x/authz/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (s *TestSuite) TestDispatchedEvents() {
}
}

func (s *TestSuite) TestIterExpiredGrantsQueue() {
func (s *TestSuite) TestPruneAllGrantsQueue() {
require := s.Require()
app, addrs := s.app, s.addrs
granter := addrs[0]
Expand All @@ -269,14 +269,12 @@ func (s *TestSuite) TestIterExpiredGrantsQueue() {
err = app.AuthzKeeper.SaveGrant(s.ctx, granter, grantee, &banktypes.SendAuthorization{SpendLimit: smallCoin}, now.AddDate(2, 0, 0))
require.NoError(err)

futureTime := s.ctx.WithBlockTime(now.AddDate(1, 0, 0)).BlockTime()
app.AuthzKeeper.IterateExpiredGrantQueue(s.ctx, futureTime, func(gte, gtr sdk.AccAddress, msgType string) (stop bool) {
require.Contains([]sdk.AccAddress{grantee, grantee1}, gte)
require.Equal(granter, gtr)
require.Equal(msgType, bankSendAuthMsgType)
ctx := s.ctx.WithBlockTime(now.AddDate(1, 0, 0))
app.AuthzKeeper.DeleteAllMatureGrants(ctx)

return false
})
require.Len(app.AuthzKeeper.GetAuthorizations(ctx, grantee, granter), 0)
require.Len(app.AuthzKeeper.GetAuthorizations(ctx, grantee1, granter), 0)
require.Len(app.AuthzKeeper.GetAuthorizations(ctx, granter, granter), 0)
}

func TestTestSuite(t *testing.T) {
Expand Down
32 changes: 13 additions & 19 deletions x/authz/keeper/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
// - 0x02<grant_expiration_Bytes><grant_Bytes>: grantKey
//
var (
GrantKey = []byte{0x01} // prefix for each key
ExpiredGrantQueuePrefix = []byte{0x02}
GrantKey = []byte{0x01} // prefix for each key
GrantQueuePrefix = []byte{0x02}
)

var lenTime = len(sdk.FormatTimeBytes(time.Now()))
Expand Down Expand Up @@ -61,7 +61,7 @@ func addressesFromGrantStoreKey(key []byte) (granterAddr, granteeAddr sdk.AccAdd
return granterAddr, granteeAddr
}

func splitExpiredGrantQueueKey(key []byte) (expiration time.Time, grantee, granter sdk.AccAddress, msgType string) {
func splitGrantQueueKey(key []byte) (expiration time.Time, grantee, granter sdk.AccAddress, msgType string) {
// key is of format:
// 0x02<grant_expiration_Bytes><granterAddressLen (1 Byte)><granterAddress_Bytes><granteeAddressLen (1 Byte)><granteeAddress_Bytes><msgType_Bytes>

Expand All @@ -83,28 +83,22 @@ func splitExpiredGrantQueueKey(key []byte) (expiration time.Time, grantee, grant
return expiration, grantee, granter, msgType
}

// expiredGrantByTimeKey gets the expired grant queue key by expiration
func expiredGrantByTimeKey(expiration time.Time) []byte {
return append(ExpiredGrantQueuePrefix, sdk.FormatTimeBytes(expiration)...)
// grantByTimeKey gets the grant queue key by expiration
func grantByTimeKey(expiration time.Time) []byte {
return append(GrantQueuePrefix, sdk.FormatTimeBytes(expiration)...)
}

// expiredGrantQueueKey - return expired grant queue store key
// Items are stored with the following key: values
// GrantQueueKey - return grant queue store key
// Key format is
//
// - 0x02<grant_expiration_Bytes><granterAddressLen (1 Byte)><granterAddress_Bytes><granteeAddressLen (1 Byte)><granteeAddress_Bytes><msgType_Bytes>: grantKey
func expiredGrantQueueKey(grantee sdk.AccAddress, granter sdk.AccAddress, msgType string, expiration time.Time) []byte {
expiredGrantKey := expiredGrantByTimeKey(expiration)
expiredGrantKeyLen := len(expiredGrantByTimeKey(expiration))

m := conv.UnsafeStrToBytes(msgType)
granter = address.MustLengthPrefix(granter)
grantee = address.MustLengthPrefix(grantee)
func GrantQueueKey(grantKey []byte, expiration time.Time) []byte {
expiredGrantKey := grantByTimeKey(expiration)
expiredGrantKeyLen := len(expiredGrantKey)

l := expiredGrantKeyLen + len(grantee) + len(granter) + len(m)
l := len(grantKey) - 1 + expiredGrantKeyLen
var key = make([]byte, l)
copy(key, expiredGrantKey)
copy(key[expiredGrantKeyLen:], granter)
copy(key[expiredGrantKeyLen+len(granter):], grantee)
copy(key[l-len(m):], m)
copy(key[expiredGrantKeyLen:], grantKey[1:])
return key
}
7 changes: 4 additions & 3 deletions x/authz/keeper/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ func TestGrantkey(t *testing.T) {
require.Equal(grantee, grantee1)
}

func TestExpiredQueueKey(t *testing.T) {
func TestGrantQueueKey(t *testing.T) {
blockTime := time.Now().UTC()
key := grantStoreKey(grantee, granter, msgType)

key := expiredGrantQueueKey(grantee, granter, msgType, blockTime)
queueKey := GrantQueueKey(key, blockTime)

expiration, grantee1, granter1, authzType := splitExpiredGrantQueueKey(key)
expiration, grantee1, granter1, authzType := splitGrantQueueKey(queueKey)

require.Equal(t, blockTime, expiration)
require.Equal(t, granter, granter1)
Expand Down
9 changes: 3 additions & 6 deletions x/authz/module/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ import (
"github.com/cosmos/cosmos-sdk/x/authz/keeper"
)

// EndBlocker prunes expired authorizations from the state
// EndBlocker is called at the end of every block
func EndBlocker(ctx sdk.Context, keeper keeper.Keeper) {
aleem1314 marked this conversation as resolved.
Show resolved Hide resolved

// delete expired authorizations from store
keeper.IterateExpiredGrantQueue(ctx, ctx.BlockHeader().Time, func(grantee, granter sdk.AccAddress, msgType string) (stop bool) {
keeper.DeleteGrant(ctx, grantee, granter, msgType)
return false
})
// clears all the mature grants
keeper.DeleteAllMatureGrants(ctx)
}
2 changes: 1 addition & 1 deletion x/authz/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 1 }
func (AppModule) ConsensusVersion() uint64 { return 2 }

func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {}

Expand Down