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

fix: grants by granter pagination total #11813

Merged
merged 8 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,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

Expand Down
17 changes: 9 additions & 8 deletions x/feegrant/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 9 additions & 5 deletions x/feegrant/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
},
},
}
Expand All @@ -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,
})
Expand Down