Skip to content

Commit

Permalink
refactor: Use GenericFilteredPaginate wherever possible (#12386) (#12402
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mergify[bot] authored Jun 30, 2022
1 parent 85bf43b commit 1d9f966
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 94 deletions.
9 changes: 5 additions & 4 deletions types/query/filtered_pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -141,7 +142,7 @@ func (s *paginationTestSuite) TestReverseFilteredPaginations() {
s.Require().NotNil(res)
s.Require().Equal(2, len(balns))
s.Require().NotNil(res.NextKey)
s.Require().Equal(string(res.NextKey), fmt.Sprintf("test7denom"))
s.Require().Equal(string(res.NextKey), "test7denom")
s.Require().Equal(uint64(10), res.Total)

s.T().Log("verify both key and offset can't be given")
Expand Down Expand Up @@ -200,7 +201,7 @@ func ExampleFilteredPaginate(t *testing.T) {

var balResult sdk.Coins
pageRes, err := query.FilteredPaginate(accountStore, pageReq, func(key []byte, value []byte, accumulate bool) (bool, error) {
var amount sdk.Int
var amount math.Int
err := amount.Unmarshal(value)
if err != nil {
return false, err
Expand Down Expand Up @@ -231,7 +232,7 @@ func execFilterPaginate(store sdk.KVStore, pageReq *query.PageRequest, appCodec

var balResult sdk.Coins
res, err = query.FilteredPaginate(accountStore, pageReq, func(key []byte, value []byte, accumulate bool) (bool, error) {
var amount sdk.Int
var amount math.Int
err := amount.Unmarshal(value)
if err != nil {
return false, err
Expand Down Expand Up @@ -275,7 +276,7 @@ func (s *paginationTestSuite) TestFilteredPaginationsNextKey() {

var balResult sdk.Coins
res, err = query.FilteredPaginate(accountStore, pageReq, func(key []byte, value []byte, accumulate bool) (bool, error) {
var amount sdk.Int
var amount math.Int
err := amount.Unmarshal(value)
if err != nil {
return false, err
Expand Down
2 changes: 1 addition & 1 deletion x/bank/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (k BaseKeeper) DenomOwners(
pageRes, err := query.FilteredPaginate(
denomPrefixStore,
req.Pagination,
func(key []byte, value []byte, accumulate bool) (bool, error) {
func(key []byte, _ []byte, accumulate bool) (bool, error) {
if accumulate {
address, _, err := types.AddressAndDenomFromBalancesStore(key)
if err != nil {
Expand Down
25 changes: 11 additions & 14 deletions x/distribution/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,35 +82,32 @@ func (k Keeper) ValidatorSlashes(c context.Context, req *types.QueryValidatorSla
}

ctx := sdk.UnwrapSDKContext(c)
events := make([]types.ValidatorSlashEvent, 0)
store := ctx.KVStore(k.storeKey)
valAddr, err := sdk.ValAddressFromBech32(req.ValidatorAddress)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid validator address")
}
slashesStore := prefix.NewStore(store, types.GetValidatorSlashEventPrefix(valAddr))

pageRes, err := query.FilteredPaginate(slashesStore, req.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) {
var result types.ValidatorSlashEvent
err := k.cdc.Unmarshal(value, &result)
if err != nil {
return false, err
}

events, pageRes, err := query.GenericFilteredPaginate(k.cdc, slashesStore, req.Pagination, func(key []byte, result *types.ValidatorSlashEvent) (*types.ValidatorSlashEvent, error) {
if result.ValidatorPeriod < req.StartingHeight || result.ValidatorPeriod > req.EndingHeight {
return false, nil
return nil, nil
}

if accumulate {
events = append(events, result)
}
return true, nil
return result, nil
}, func() *types.ValidatorSlashEvent {
return &types.ValidatorSlashEvent{}
})
if err != nil {
return nil, err
}

return &types.QueryValidatorSlashesResponse{Slashes: events, Pagination: pageRes}, nil
slashes := []types.ValidatorSlashEvent{}
for _, event := range events {
slashes = append(slashes, *event)
}

return &types.QueryValidatorSlashesResponse{Slashes: slashes, Pagination: pageRes}, nil
}

// DelegationRewards the total rewards accrued by a delegation
Expand Down
18 changes: 5 additions & 13 deletions x/feegrant/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,18 @@ func (q Keeper) AllowancesByGranter(c context.Context, req *feegrant.QueryAllowa

ctx := sdk.UnwrapSDKContext(c)

var grants []*feegrant.Grant

store := ctx.KVStore(q.storeKey)
prefixStore := prefix.NewStore(store, feegrant.FeeAllowanceKeyPrefix)
pageRes, err := query.FilteredPaginate(prefixStore, req.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) {
grants, pageRes, err := query.GenericFilteredPaginate(q.cdc, prefixStore, req.Pagination, func(key []byte, grant *feegrant.Grant) (*feegrant.Grant, error) {
// ParseAddressesFromFeeAllowanceKey expects the full key including the prefix.
granter, _ := feegrant.ParseAddressesFromFeeAllowanceKey(append(feegrant.FeeAllowanceKeyPrefix, key...))
if !granter.Equals(granterAddr) {
return false, nil
}

if accumulate {
var grant feegrant.Grant
if err := q.cdc.Unmarshal(value, &grant); err != nil {
return false, err
}
grants = append(grants, &grant)
return nil, nil
}

return true, nil
return grant, nil
}, func() *feegrant.Grant {
return &feegrant.Grant{}
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
Expand Down
64 changes: 30 additions & 34 deletions x/gov/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,54 +39,50 @@ func (q Keeper) Proposal(c context.Context, req *v1.QueryProposalRequest) (*v1.Q

// Proposals implements the Query/Proposals gRPC method
func (q Keeper) Proposals(c context.Context, req *v1.QueryProposalsRequest) (*v1.QueryProposalsResponse, error) {
var filteredProposals []*v1.Proposal
ctx := sdk.UnwrapSDKContext(c)

store := ctx.KVStore(q.storeKey)
proposalStore := prefix.NewStore(store, types.ProposalsKeyPrefix)

pageRes, err := query.FilteredPaginate(proposalStore, req.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) {
var p v1.Proposal
if err := q.cdc.Unmarshal(value, &p); err != nil {
return false, status.Error(codes.Internal, err.Error())
}

matchVoter, matchDepositor, matchStatus := true, true, true
filteredProposals, pageRes, err := query.GenericFilteredPaginate(
q.cdc,
proposalStore,
req.Pagination,
func(key []byte, p *v1.Proposal) (*v1.Proposal, error) {
matchVoter, matchDepositor, matchStatus := true, true, true

// match status (if supplied/valid)
if v1.ValidProposalStatus(req.ProposalStatus) {
matchStatus = p.Status == req.ProposalStatus
}

// match voter address (if supplied)
if len(req.Voter) > 0 {
voter, err := sdk.AccAddressFromBech32(req.Voter)
if err != nil {
return false, err
// match status (if supplied/valid)
if v1.ValidProposalStatus(req.ProposalStatus) {
matchStatus = p.Status == req.ProposalStatus
}

_, matchVoter = q.GetVote(ctx, p.Id, voter)
}
// match voter address (if supplied)
if len(req.Voter) > 0 {
voter, err := sdk.AccAddressFromBech32(req.Voter)
if err != nil {
return nil, err
}

// match depositor (if supplied)
if len(req.Depositor) > 0 {
depositor, err := sdk.AccAddressFromBech32(req.Depositor)
if err != nil {
return false, err
_, matchVoter = q.GetVote(ctx, p.Id, voter)
}
_, matchDepositor = q.GetDeposit(ctx, p.Id, depositor)
}

if matchVoter && matchDepositor && matchStatus {
if accumulate {
filteredProposals = append(filteredProposals, &p)
// match depositor (if supplied)
if len(req.Depositor) > 0 {
depositor, err := sdk.AccAddressFromBech32(req.Depositor)
if err != nil {
return nil, err
}
_, matchDepositor = q.GetDeposit(ctx, p.Id, depositor)
}

return true, nil
}
if matchVoter && matchDepositor && matchStatus {
return p, nil
}

return false, nil
})
return nil, nil
}, func() *v1.Proposal {
return &v1.Proposal{}
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
Expand Down
51 changes: 23 additions & 28 deletions x/staking/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,30 @@ func (k Querier) Validators(c context.Context, req *types.QueryValidatorsRequest
return nil, status.Errorf(codes.InvalidArgument, "invalid validator status %s", req.Status)
}

var validators types.Validators
ctx := sdk.UnwrapSDKContext(c)

store := ctx.KVStore(k.storeKey)
valStore := prefix.NewStore(store, types.ValidatorsKey)

pageRes, err := query.FilteredPaginate(valStore, req.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) {
val, err := types.UnmarshalValidator(k.cdc, value)
if err != nil {
return false, err
}

validators, pageRes, err := query.GenericFilteredPaginate(k.cdc, valStore, req.Pagination, func(key []byte, val *types.Validator) (*types.Validator, error) {
if req.Status != "" && !strings.EqualFold(val.GetStatus().String(), req.Status) {
return false, nil
return nil, nil
}

if accumulate {
validators = append(validators, val)
}

return true, nil
return val, nil
}, func() *types.Validator {
return &types.Validator{}
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &types.QueryValidatorsResponse{Validators: validators, Pagination: pageRes}, nil
vals := types.Validators{}
for _, val := range validators {
vals = append(vals, *val)
}

return &types.QueryValidatorsResponse{Validators: vals, Pagination: pageRes}, nil
}

// Validator queries validator info for given validator address
Expand Down Expand Up @@ -93,36 +90,34 @@ func (k Querier) ValidatorDelegations(c context.Context, req *types.QueryValidat
if req.ValidatorAddr == "" {
return nil, status.Error(codes.InvalidArgument, "validator address cannot be empty")
}
var delegations []types.Delegation
ctx := sdk.UnwrapSDKContext(c)

store := ctx.KVStore(k.storeKey)
valStore := prefix.NewStore(store, types.DelegationKey)
pageRes, err := query.FilteredPaginate(valStore, req.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) {
delegation, err := types.UnmarshalDelegation(k.cdc, value)
if err != nil {
return false, err
}

delegations, pageRes, err := query.GenericFilteredPaginate(k.cdc, valStore, req.Pagination, func(key []byte, delegation *types.Delegation) (*types.Delegation, error) {
valAddr, err := sdk.ValAddressFromBech32(req.ValidatorAddr)
if err != nil {
return false, err
return nil, err
}

if !delegation.GetValidatorAddr().Equals(valAddr) {
return false, nil
return nil, nil
}

if accumulate {
delegations = append(delegations, delegation)
}
return true, nil
return delegation, nil
}, func() *types.Delegation {
return &types.Delegation{}
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

delResponses, err := DelegationsToDelegationResponses(ctx, k.Keeper, delegations)
dels := types.Delegations{}
for _, d := range delegations {
dels = append(dels, *d)
}

delResponses, err := DelegationsToDelegationResponses(ctx, k.Keeper, dels)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
Expand Down

0 comments on commit 1d9f966

Please sign in to comment.