From 8b5ea3f2144d5ff343bf877770022b93f00ec08f Mon Sep 17 00:00:00 2001 From: Facundo Medica <14063057+facundomedica@users.noreply.github.com> Date: Thu, 30 Jun 2022 13:36:20 +0100 Subject: [PATCH] refactor: Use GenericFilteredPaginate wherever possible (#12386) (cherry picked from commit 92d24cc69c13bcfb96563de70f81cdede2a08b46) --- types/query/filtered_pagination_test.go | 9 ++-- x/bank/keeper/grpc_query.go | 2 +- x/distribution/keeper/grpc_query.go | 25 +++++----- x/feegrant/keeper/grpc_query.go | 18 ++----- x/gov/keeper/grpc_query.go | 64 ++++++++++++------------- x/staking/keeper/grpc_query.go | 51 +++++++++----------- 6 files changed, 75 insertions(+), 94 deletions(-) diff --git a/types/query/filtered_pagination_test.go b/types/query/filtered_pagination_test.go index 08ba8513a9d..2da46f83125 100644 --- a/types/query/filtered_pagination_test.go +++ b/types/query/filtered_pagination_test.go @@ -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" @@ -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") @@ -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 @@ -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 @@ -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 diff --git a/x/bank/keeper/grpc_query.go b/x/bank/keeper/grpc_query.go index 2afc4c585f7..3e77e9773d9 100644 --- a/x/bank/keeper/grpc_query.go +++ b/x/bank/keeper/grpc_query.go @@ -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 { diff --git a/x/distribution/keeper/grpc_query.go b/x/distribution/keeper/grpc_query.go index 2779822293f..bbc884276a0 100644 --- a/x/distribution/keeper/grpc_query.go +++ b/x/distribution/keeper/grpc_query.go @@ -82,7 +82,6 @@ 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 { @@ -90,27 +89,25 @@ func (k Keeper) ValidatorSlashes(c context.Context, req *types.QueryValidatorSla } 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 diff --git a/x/feegrant/keeper/grpc_query.go b/x/feegrant/keeper/grpc_query.go index 8d2b4c31145..50089a9c08b 100644 --- a/x/feegrant/keeper/grpc_query.go +++ b/x/feegrant/keeper/grpc_query.go @@ -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()) diff --git a/x/gov/keeper/grpc_query.go b/x/gov/keeper/grpc_query.go index 3ae97a3d96a..a343d53bf30 100644 --- a/x/gov/keeper/grpc_query.go +++ b/x/gov/keeper/grpc_query.go @@ -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()) } diff --git a/x/staking/keeper/grpc_query.go b/x/staking/keeper/grpc_query.go index 79f9542adf2..9bbed760fc9 100644 --- a/x/staking/keeper/grpc_query.go +++ b/x/staking/keeper/grpc_query.go @@ -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 @@ -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()) }