From 53003e15a3be51c69fedeaa000703c039725e2a4 Mon Sep 17 00:00:00 2001 From: atheeshp <59333759+atheeshp@users.noreply.github.com> Date: Sun, 1 May 2022 14:20:45 +0530 Subject: [PATCH] refactor: (x/feegrant) parsing keys (#11814) ## Description ref: #11362 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) --- x/feegrant/keeper/keeper.go | 5 ++--- x/feegrant/key.go | 31 ++++++++++++++++++++++--------- x/feegrant/key_test.go | 18 ++++++++++++++++++ 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/x/feegrant/keeper/keeper.go b/x/feegrant/keeper/keeper.go index 408273f91a..14c0e3f4be 100644 --- a/x/feegrant/keeper/keeper.go +++ b/x/feegrant/keeper/keeper.go @@ -322,9 +322,8 @@ func (k Keeper) RemoveExpiredAllowances(ctx sdk.Context) { for ; iterator.Valid(); iterator.Next() { store.Delete(iterator.Key()) - expLen := len(sdk.FormatTimeBytes(ctx.BlockTime())) - // extract the fee allowance key by removing the allowance queue prefix length, expiration length from key. - store.Delete(append(feegrant.FeeAllowanceKeyPrefix, iterator.Key()[1+expLen:]...)) + granter, grantee := feegrant.ParseAddressesFromFeeAllowanceQueueKey(iterator.Key()) + store.Delete(feegrant.FeeAllowanceKey(granter, grantee)) } } diff --git a/x/feegrant/key.go b/x/feegrant/key.go index 3d2fdb5633..a4838cad8a 100644 --- a/x/feegrant/key.go +++ b/x/feegrant/key.go @@ -5,7 +5,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/address" - "github.com/cosmos/cosmos-sdk/types/kv" ) const ( @@ -67,17 +66,31 @@ func AllowanceByExpTimeKey(exp *time.Time) []byte { return append(FeeAllowanceQueueKeyPrefix, sdk.FormatTimeBytes(*exp)...) } -// ParseAddressesFromFeeAllowanceKey exrtacts and returns the granter, grantee from the given key. +// ParseAddressesFromFeeAllowanceKey extracts and returns the granter, grantee from the given key. func ParseAddressesFromFeeAllowanceKey(key []byte) (granter, grantee sdk.AccAddress) { + // key is of format: // 0x00 - kv.AssertKeyAtLeastLength(key, 2) - granteeAddrLen := key[1] // remove prefix key - kv.AssertKeyAtLeastLength(key, 2+int(granteeAddrLen)) - grantee = sdk.AccAddress(key[2 : 2+int(granteeAddrLen)]) - granterAddrLen := int(key[2+granteeAddrLen]) - kv.AssertKeyAtLeastLength(key, 3+int(granteeAddrLen)+int(granterAddrLen)) - granter = sdk.AccAddress(key[3+granterAddrLen : 3+int(granteeAddrLen)+int(granterAddrLen)]) + granterAddrLen, granterAddrLenEndIndex := sdk.ParseLengthPrefixedBytes(key, 1, 1) // ignore key[0] since it is a prefix key + grantee, granterAddrEndIndex := sdk.ParseLengthPrefixedBytes(key, granterAddrLenEndIndex+1, int(granterAddrLen[0])) + + granteeAddrLen, granteeAddrLenEndIndex := sdk.ParseLengthPrefixedBytes(key, granterAddrEndIndex+1, 1) + granter, _ = sdk.ParseLengthPrefixedBytes(key, granteeAddrLenEndIndex+1, int(granteeAddrLen[0])) + + return granter, grantee +} + +// ParseAddressesFromFeeAllowanceQueueKey extracts and returns the granter, grantee from the given key. +func ParseAddressesFromFeeAllowanceQueueKey(key []byte) (granter, grantee sdk.AccAddress) { + var lenTime = len(sdk.FormatTimeBytes(time.Now())) + + // key is of format: + // <0x01> + granterAddrLen, granterAddrLenEndIndex := sdk.ParseLengthPrefixedBytes(key, 1+lenTime, 1) // ignore key[0] since it is a prefix key + grantee, granterAddrEndIndex := sdk.ParseLengthPrefixedBytes(key, granterAddrLenEndIndex+1, int(granterAddrLen[0])) + + granteeAddrLen, granteeAddrLenEndIndex := sdk.ParseLengthPrefixedBytes(key, granterAddrEndIndex+1, 1) + granter, _ = sdk.ParseLengthPrefixedBytes(key, granteeAddrLenEndIndex+1, int(granteeAddrLen[0])) return granter, grantee } diff --git a/x/feegrant/key_test.go b/x/feegrant/key_test.go index 75a7964bec..d08b53bb05 100644 --- a/x/feegrant/key_test.go +++ b/x/feegrant/key_test.go @@ -2,6 +2,7 @@ package feegrant_test import ( "testing" + "time" "github.com/stretchr/testify/require" @@ -23,3 +24,20 @@ func TestMarshalAndUnmarshalFeegrantKey(t *testing.T) { require.Equal(t, granter, g1) require.Equal(t, grantee, g2) } + +func TestMarshalAndUnmarshalFeegrantKeyQueueKey(t *testing.T) { + grantee, err := sdk.AccAddressFromBech32("cosmos1qk93t4j0yyzgqgt6k5qf8deh8fq6smpn3ntu3x") + require.NoError(t, err) + granter, err := sdk.AccAddressFromBech32("cosmos1p9qh4ldfd6n0qehujsal4k7g0e37kel90rc4ts") + require.NoError(t, err) + + exp := time.Now() + expBytes := sdk.FormatTimeBytes(exp) + + key := feegrant.FeeAllowancePrefixQueue(&exp, feegrant.FeeAllowanceKey(granter, grantee)[1:]) + require.Len(t, key, len(grantee.Bytes())+len(granter.Bytes())+3+len(expBytes)) + + granter1, grantee1 := feegrant.ParseAddressesFromFeeAllowanceQueueKey(key) + require.Equal(t, granter, granter1) + require.Equal(t, grantee, grantee1) +}