Skip to content

Commit

Permalink
Fix status filter (cosmos#7167)
Browse files Browse the repository at this point in the history
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
anilcse and mergify[bot] authored Aug 26, 2020
1 parent a180d0b commit b5bc864
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
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

0 comments on commit b5bc864

Please sign in to comment.