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 GetValidators status param #7167

Merged
merged 3 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion x/staking/client/rest/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (s *IntegrationTestSuite) TestQueryValidatorsGRPCHandler() {
map[string]string{
grpctypes.GRPCBlockHeightHeader: "1",
},
true,
false,
},
{
"test query validators gRPC route with valid status",
Expand Down
8 changes: 4 additions & 4 deletions x/staking/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -51,6 +50,7 @@ func (k Querier) Validators(c context.Context, req *types.QueryValidatorsRequest
if accumulate {
validators = append(validators, val)
}

return true, nil
})

Expand Down
22 changes: 19 additions & 3 deletions x/staking/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
{
Expand All @@ -38,13 +45,17 @@ func (suite *KeeperTestSuite) TestGRPCQueryValidators() {
req = &types.QueryValidatorsRequest{Status: "test"}
},
false,
0,
false,
},
{"valid request",
func() {
req = &types.QueryValidatorsRequest{Status: sdk.Bonded.String(),
Pagination: &query.PageRequest{Limit: 1, CountTotal: true}}
},
true,
1,
true,
},
}
for _, tc := range testCases {
Expand All @@ -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)
}
Expand Down