diff --git a/x/staking/client/rest/grpc_query_test.go b/x/staking/client/rest/grpc_query_test.go index ce9aeca03..0338092ae 100644 --- a/x/staking/client/rest/grpc_query_test.go +++ b/x/staking/client/rest/grpc_query_test.go @@ -62,7 +62,7 @@ func (s *IntegrationTestSuite) TestQueryValidatorsGRPCHandler() { map[string]string{ grpctypes.GRPCBlockHeightHeader: "1", }, - true, + false, }, { "test query validators gRPC route with valid status", diff --git a/x/staking/keeper/grpc_query.go b/x/staking/keeper/grpc_query.go index 2c06293fb..777c1e877 100644 --- a/x/staking/keeper/grpc_query.go +++ b/x/staking/keeper/grpc_query.go @@ -26,12 +26,11 @@ func (k Querier) Validators(c context.Context, req *types.QueryValidatorsRequest return nil, status.Error(codes.InvalidArgument, "empty request") } - if req.Status == "" { - return nil, status.Error(codes.InvalidArgument, "status cannot be empty") - } - if !(req.Status == sdk.Bonded.String() || req.Status == sdk.Unbonded.String() || req.Status == sdk.Unbonding.String()) { + // validate the provided status, return all the validators if the status is empty + if req.Status != "" && !(req.Status == sdk.Bonded.String() || req.Status == sdk.Unbonded.String() || req.Status == sdk.Unbonding.String()) { return nil, status.Errorf(codes.InvalidArgument, "invalid validator status %s", req.Status) } + var validators types.Validators ctx := sdk.UnwrapSDKContext(c) @@ -51,6 +50,7 @@ func (k Querier) Validators(c context.Context, req *types.QueryValidatorsRequest if accumulate { validators = append(validators, val) } + return true, nil }) diff --git a/x/staking/keeper/grpc_query_test.go b/x/staking/keeper/grpc_query_test.go index 474c8ea39..f2c4c9ab3 100644 --- a/x/staking/keeper/grpc_query_test.go +++ b/x/staking/keeper/grpc_query_test.go @@ -18,18 +18,25 @@ func (suite *KeeperTestSuite) TestGRPCQueryValidators() { msg string malleate func() expPass bool + numVals int + hasNext bool }{ { "empty request", func() { req = &types.QueryValidatorsRequest{} }, + true, + + len(vals), false, }, - {"invalid request with empty status", + {"empty status returns all the validators", func() { req = &types.QueryValidatorsRequest{Status: ""} }, + true, + len(vals), false, }, { @@ -38,6 +45,8 @@ func (suite *KeeperTestSuite) TestGRPCQueryValidators() { req = &types.QueryValidatorsRequest{Status: "test"} }, false, + 0, + false, }, {"valid request", func() { @@ -45,6 +54,8 @@ func (suite *KeeperTestSuite) TestGRPCQueryValidators() { Pagination: &query.PageRequest{Limit: 1, CountTotal: true}} }, true, + 1, + true, }, } for _, tc := range testCases { @@ -54,9 +65,14 @@ func (suite *KeeperTestSuite) TestGRPCQueryValidators() { if tc.expPass { suite.NoError(err) suite.NotNil(valsResp) - suite.Equal(1, len(valsResp.Validators)) - suite.NotNil(valsResp.Pagination.NextKey) + suite.Equal(tc.numVals, len(valsResp.Validators)) suite.Equal(uint64(len(vals)), valsResp.Pagination.Total) + + if (tc.hasNext) { + suite.NotNil(valsResp.Pagination.NextKey) + } else { + suite.Nil(valsResp.Pagination.NextKey) + } } else { suite.Require().Error(err) }