Skip to content

Commit

Permalink
refactor: (x/feegrant) parsing keys (#11814)
Browse files Browse the repository at this point in the history
## 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)
  • Loading branch information
atheeshp authored May 1, 2022
1 parent 05d582c commit 53003e1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
5 changes: 2 additions & 3 deletions x/feegrant/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
31 changes: 22 additions & 9 deletions x/feegrant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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<granteeAddressLen (1 Byte)><granteeAddress_Bytes><granterAddressLen (1 Byte)><granterAddress_Bytes>
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><expiration_bytes(fixed length)><granteeAddressLen (1 Byte)><granteeAddress_Bytes><granterAddressLen (1 Byte)><granterAddress_Bytes>
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
}
18 changes: 18 additions & 0 deletions x/feegrant/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package feegrant_test

import (
"testing"
"time"

"github.com/stretchr/testify/require"

Expand All @@ -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)
}

0 comments on commit 53003e1

Please sign in to comment.