From a20ada083a93fc2729ddd693c9949581142ea5a1 Mon Sep 17 00:00:00 2001 From: atheeshp <59333759+atheeshp@users.noreply.github.com> Date: Fri, 29 Apr 2022 16:23:18 +0530 Subject: [PATCH] fix: grants by granter pagination `total` (#11813) ## Description Closes: #11812 --- ### 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) (cherry picked from commit 8dfc205731c7a35964fa1a4e0e8b99bbb6eb5bd1) --- CHANGELOG.md | 1 + x/feegrant/keeper/grpc_query.go | 17 +++++++++-------- x/feegrant/keeper/grpc_query_test.go | 14 +++++++++----- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f7be8b0947e..7d4c0e34de6d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -259,6 +259,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (cli) [\#11337](https://github.com/cosmos/cosmos-sdk/pull/11337) Fixes `show-adress` cli cmd * (crypto) [\#11298](https://github.com/cosmos/cosmos-sdk/pull/11298) Fix cgo secp signature verification and update libscep256k1 library. * (x/authz) [\#11512](https://github.com/cosmos/cosmos-sdk/pull/11512) Fix response of a panic to error, when subtracting balances. +* (x/feegrant) [\#11813](https://github.com/cosmos/cosmos-sdk/pull/11813) Fix pagination total count in `AllowancesByGranter` query. ### State Machine Breaking diff --git a/x/feegrant/keeper/grpc_query.go b/x/feegrant/keeper/grpc_query.go index 027eba648aa0..87278c4c59af 100644 --- a/x/feegrant/keeper/grpc_query.go +++ b/x/feegrant/keeper/grpc_query.go @@ -111,21 +111,22 @@ func (q Keeper) AllowancesByGranter(c context.Context, req *feegrant.QueryAllowa store := ctx.KVStore(q.storeKey) prefixStore := prefix.NewStore(store, feegrant.FeeAllowanceKeyPrefix) - pageRes, err := query.Paginate(prefixStore, req.Pagination, func(key []byte, value []byte) error { - var grant feegrant.Grant - + pageRes, err := query.FilteredPaginate(prefixStore, req.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) { // ParseAddressesFromFeeAllowanceKey expects the full key including the prefix. granter, _ := feegrant.ParseAddressesFromFeeAllowanceKey(append(feegrant.FeeAllowanceKeyPrefix, key...)) if !granter.Equals(granterAddr) { - return nil + return false, nil } - if err := q.cdc.Unmarshal(value, &grant); err != nil { - return err + if accumulate { + var grant feegrant.Grant + if err := q.cdc.Unmarshal(value, &grant); err != nil { + return false, err + } + grants = append(grants, &grant) } - grants = append(grants, &grant) - return nil + return true, nil }) if err != nil { diff --git a/x/feegrant/keeper/grpc_query_test.go b/x/feegrant/keeper/grpc_query_test.go index cf813d1c19f5..9a7b76ff1a5a 100644 --- a/x/feegrant/keeper/grpc_query_test.go +++ b/x/feegrant/keeper/grpc_query_test.go @@ -59,7 +59,7 @@ func (suite *KeeperTestSuite) TestFeeAllowance() { }, false, func() { - grantFeeAllowance(suite) + suite.grantFeeAllowance(suite.addrs[0], suite.addrs[1]) }, func(response *feegrant.QueryAllowanceResponse) { suite.Require().Equal(response.Allowance.Granter, suite.addrs[0].String()) @@ -124,7 +124,7 @@ func (suite *KeeperTestSuite) TestFeeAllowances() { }, false, func() { - grantFeeAllowance(suite) + suite.grantFeeAllowance(suite.addrs[0], suite.addrs[1]) }, func(resp *feegrant.QueryAllowancesResponse) { suite.Require().Equal(len(resp.Allowances), 1) @@ -190,12 +190,16 @@ func (suite *KeeperTestSuite) TestFeeAllowancesByGranter() { }, false, func() { - grantFeeAllowance(suite) + suite.grantFeeAllowance(suite.addrs[0], suite.addrs[1]) + + // adding this allowance to check whether the pagination working fine. + suite.grantFeeAllowance(suite.addrs[1], suite.addrs[2]) }, func(resp *feegrant.QueryAllowancesByGranterResponse) { suite.Require().Equal(len(resp.Allowances), 1) suite.Require().Equal(resp.Allowances[0].Granter, suite.addrs[0].String()) suite.Require().Equal(resp.Allowances[0].Grantee, suite.addrs[1].String()) + suite.Require().Equal(resp.Pagination.Total, uint64(1)) }, }, } @@ -214,9 +218,9 @@ func (suite *KeeperTestSuite) TestFeeAllowancesByGranter() { } } -func grantFeeAllowance(suite *KeeperTestSuite) { +func (suite *KeeperTestSuite) grantFeeAllowance(granter, grantee sdk.AccAddress) { exp := suite.sdkCtx.BlockTime().AddDate(1, 0, 0) - err := suite.app.FeeGrantKeeper.GrantAllowance(suite.sdkCtx, suite.addrs[0], suite.addrs[1], &feegrant.BasicAllowance{ + err := suite.app.FeeGrantKeeper.GrantAllowance(suite.sdkCtx, granter, grantee, &feegrant.BasicAllowance{ SpendLimit: sdk.NewCoins(sdk.NewInt64Coin("atom", 555)), Expiration: &exp, })