refactor: Use GenericFilteredPaginate wherever possible (#12386)

This commit is contained in:
Facundo Medica 2022-06-30 13:36:20 +01:00 committed by GitHub
parent a32349d558
commit 92d24cc69c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 77 additions and 96 deletions

View File

@ -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

View File

@ -219,7 +219,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 {

View File

@ -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

View File

@ -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
return nil, nil
}
if accumulate {
var grant feegrant.Grant
if err := q.cdc.Unmarshal(value, &grant); err != nil {
return false, err
}
grants = append(grants, &grant)
}
return true, nil
return grant, nil
}, func() *feegrant.Grant {
return &feegrant.Grant{}
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())

View File

@ -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())
}
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
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
}
_, matchDepositor = q.GetDeposit(ctx, p.Id, depositor)
}
if matchVoter && matchDepositor && matchStatus {
if accumulate {
filteredProposals = append(filteredProposals, &p)
_, matchVoter = q.GetVote(ctx, p.Id, voter)
}
return true, nil
}
// 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 false, nil
})
if matchVoter && matchDepositor && matchStatus {
return p, nil
}
return nil, nil
}, func() *v1.Proposal {
return &v1.Proposal{}
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

View File

@ -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())
}