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: pagination issue in collection paginate #16661

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
25 changes: 23 additions & 2 deletions types/query/collections_pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ func collFilteredPaginateNoKey[K, V any, C Collection[K, V]](
// if no predicate function is specified then we just include the result
if predicateFunc == nil {
results = append(results, kv)
count++

// if predicate function is defined we check if the result matches the filtering criteria
} else {
include, err := predicateFunc(kv.Key, kv.Value)
Expand All @@ -147,9 +149,9 @@ func collFilteredPaginateNoKey[K, V any, C Collection[K, V]](
}
if include {
results = append(results, kv)
count++
}
}
count++
// second case, we found all the objects specified within the limit
case count == limit:
key, err := iterator.Key()
Expand All @@ -172,12 +174,31 @@ func collFilteredPaginateNoKey[K, V any, C Collection[K, V]](
// but we need to count how many possible results exist in total.
// so we keep increasing the count until the iterator is fully consumed.
case count > limit:
count++
if predicateFunc == nil {
count++

// if predicate function is defined we check if the result matches the filtering criteria
} else {
kv, err := iterator.KeyValue()
if err != nil {
return nil, nil, err
}

include, err := predicateFunc(kv.Key, kv.Value)
if err != nil {
return nil, nil, err
}
if include {
count++
}
}
}
}

resp := &PageResponse{
NextKey: nextKey,
}

if countTotal {
resp.Total = count + offset
}
Expand Down
12 changes: 9 additions & 3 deletions x/gov/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ func (q queryServer) Proposal(ctx context.Context, req *v1.QueryProposalRequest)

// Proposals implements the Query/Proposals gRPC method
func (q queryServer) Proposals(ctx context.Context, req *v1.QueryProposalsRequest) (*v1.QueryProposalsResponse, error) {
var filteredProposals []*v1.Proposal
_, pageRes, err := query.CollectionFilteredPaginate(ctx, q.k.Proposals, req.Pagination, func(key uint64, p v1.Proposal) (bool, error) {
res, pageRes, err := query.CollectionFilteredPaginate(ctx, q.k.Proposals, req.Pagination, func(key uint64, p v1.Proposal) (include bool, err error) {
matchVoter, matchDepositor, matchStatus := true, true, true

// match status (if supplied/valid)
Expand Down Expand Up @@ -90,11 +89,18 @@ func (q queryServer) Proposals(ctx context.Context, req *v1.QueryProposalsReques

// if all match, append to results
if matchVoter && matchDepositor && matchStatus {
filteredProposals = append(filteredProposals, &p)
return true, nil
}
// continue to next item, do not include because we're appending results above.
return false, nil
})

filteredProposals := make([]*v1.Proposal, len(res))

for i := 0; i < len(res); i++ {
filteredProposals[i] = &res[i].Value
}

if err != nil && !errors.IsOf(err, collections.ErrInvalidIterator) {
return nil, status.Error(codes.Internal, err.Error())
}
Expand Down
66 changes: 65 additions & 1 deletion x/gov/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (suite *KeeperTestSuite) TestGRPCQueryProposals() {
true,
},
{
"request 2nd page with limit 4",
"request 2nd page with limit 3",
func() {
req = &v1.QueryProposalsRequest{
Pagination: &query.PageRequest{Offset: 3, Limit: 3},
Expand Down Expand Up @@ -294,6 +294,70 @@ func (suite *KeeperTestSuite) TestGRPCQueryProposals() {
},
true,
},
{
"request with filter of status voting period",
func() {
req = &v1.QueryProposalsRequest{
ProposalStatus: v1.StatusVotingPeriod,
}

var proposals []*v1.Proposal
for i := 0; i < len(testProposals); i++ {
if testProposals[i].GetStatus() == v1.StatusVotingPeriod {
proposals = append(proposals, testProposals[i])
}
}

expRes = &v1.QueryProposalsResponse{
Proposals: proposals,
}
},
true,
},
{
"request with filter of status deposit period",
func() {
req = &v1.QueryProposalsRequest{
ProposalStatus: v1.StatusDepositPeriod,
}

var proposals []*v1.Proposal
for i := 0; i < len(testProposals); i++ {
if testProposals[i].GetStatus() == v1.StatusDepositPeriod {
proposals = append(proposals, testProposals[i])
}
}

expRes = &v1.QueryProposalsResponse{
Proposals: proposals,
}
},
true,
},
{
"request with filter of status deposit period with limit 2",
func() {
req = &v1.QueryProposalsRequest{
ProposalStatus: v1.StatusDepositPeriod,
Pagination: &query.PageRequest{
Limit: 2,
CountTotal: true,
},
}

var proposals []*v1.Proposal
for i := 0; i < len(testProposals) && len(proposals) < 2; i++ {
if testProposals[i].GetStatus() == v1.StatusDepositPeriod {
proposals = append(proposals, testProposals[i])
}
}

expRes = &v1.QueryProposalsResponse{
Proposals: proposals,
}
},
true,
},
}

for _, testCase := range testCases {
Expand Down