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: validators query to use power index at bonded validator query #194

Merged
merged 1 commit into from
Jun 1, 2024
Merged
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
21 changes: 16 additions & 5 deletions x/mstaking/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,22 @@
return nil, status.Errorf(codes.InvalidArgument, "invalid validator status %s", req.Status)
}

validators, pageRes, err := query.CollectionFilteredPaginate(ctx, q.Keeper.Validators, req.Pagination, func(valAddr []byte, val types.Validator) (include bool, err error) {
return (req.Status == "" || strings.EqualFold(val.GetStatus().String(), req.Status)), nil
}, func(valAddr []byte, val types.Validator) (types.Validator, error) {
return val, nil
})
var validators []types.Validator
var pageRes *query.PageResponse
var err error

if req.Status == types.Bonded.String() {
validators, pageRes, err = query.CollectionPaginate(ctx, q.Keeper.ValidatorsByConsPowerIndex, req.Pagination, func(key collections.Pair[int64, []byte], _ bool) (types.Validator, error) {
valAddr := key.K2()
return q.Keeper.Validators.Get(ctx, valAddr)
})
} else {
validators, pageRes, err = query.CollectionFilteredPaginate(ctx, q.Keeper.Validators, req.Pagination, func(valAddr []byte, val types.Validator) (include bool, err error) {
return (req.Status == "" || strings.EqualFold(val.GetStatus().String(), req.Status)), nil
}, func(valAddr []byte, val types.Validator) (types.Validator, error) {
return val, nil
})

Check warning on line 50 in x/mstaking/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/mstaking/keeper/grpc_query.go#L45-L50

Added lines #L45 - L50 were not covered by tests
}
Comment on lines +36 to +51
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor the handling of bonded validators to optimize query performance.

The separation of the 'Bonded' status handling is a good approach to optimize the performance for specific queries. However, consider abstracting the querying logic into separate methods to improve readability and maintainability. Additionally, ensure that the new querying logic is covered by unit tests to prevent regressions.

if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
Expand Down
Loading