Skip to content

Commit

Permalink
fix: Staking delegations should return empty list instead of rpc erro…
Browse files Browse the repository at this point in the history
…r when no records found (#9423)

* return empty list instead of rpc error when no records found for staking delegations

* fix grpc query DelegatorDelegations tests

* remove response code tests for staking delegations

* fix failing tests

* change staking delegations response code to 200 in grpc test

* add staking delegations response code tests to TestQueryDelegatorDelegationsGRPC

* add address without delegations testcase

* add changes to grpc query tests of delegatorDelegations

* remove getRequest unused function from x/staking/client/rest/grpc_query_test.go

* minor fixes

* add testcases for request with no delegations

* address review comments

* add changelog

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
likhita-809 and mergify[bot] authored Jun 1, 2021
1 parent 98fbdbd commit da87ab0
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 78 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ if input key is empty, or input data contains empty key.

### Improvements

* (x/staking) [\#9423](https://github.com/cosmos/cosmos-sdk/pull/9423) Staking delegations now returns empty list instead of rpc error when no records found.
* (baseapp, types) [#\9390](https://github.com/cosmos/cosmos-sdk/pull/9390) Add current block header hash to `Context`
* (x/bank) [\#8614](https://github.com/cosmos/cosmos-sdk/issues/8614) Add `Name` and `Symbol` fields to denom metadata
* (x/auth) [\#8522](https://github.com/cosmos/cosmos-sdk/pull/8522) Allow to query all stored accounts
Expand Down
45 changes: 16 additions & 29 deletions x/staking/client/rest/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ package rest_test

import (
"fmt"
"io/ioutil"
"net/http"
"testing"

"github.com/gogo/protobuf/proto"
Expand Down Expand Up @@ -414,39 +412,15 @@ func (s *IntegrationTestSuite) TestQueryUnbondingDelegationGRPC() {
}
}

func (s *IntegrationTestSuite) TestQueryDelegationsResponseCode() {
func (s *IntegrationTestSuite) TestQueryDelegatorDelegationsGRPC() {
val := s.network.Validators[0]
baseURL := val.APIAddress

// Create new account in the keyring.
// Create new account in the keyring for address without delegations.
info, _, err := val.ClientCtx.Keyring.NewMnemonic("test", keyring.English, sdk.FullFundraiserPath, keyring.DefaultBIP39Passphrase, hd.Secp256k1)
s.Require().NoError(err)
newAddr := sdk.AccAddress(info.GetPubKey().Address())

s.T().Log("expect 404 error for address without delegations")
res, statusCode, err := getRequest(fmt.Sprintf("%s/cosmos/staking/v1beta1/delegations/%s", val.APIAddress, newAddr.String()))
s.Require().NoError(err)
s.Require().Contains(string(res), "\"code\": 5")
s.Require().Equal(404, statusCode)
}

func getRequest(url string) ([]byte, int, error) {
res, err := http.Get(url) // nolint:gosec
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, res.StatusCode, err
}

if err = res.Body.Close(); err != nil {
return nil, res.StatusCode, err
}

return body, res.StatusCode, nil
}

func (s *IntegrationTestSuite) TestQueryDelegatorDelegationsGRPC() {
val := s.network.Validators[0]
baseURL := val.APIAddress

testCases := []struct {
name string
url string
Expand Down Expand Up @@ -486,6 +460,19 @@ func (s *IntegrationTestSuite) TestQueryDelegatorDelegationsGRPC() {
Pagination: &query.PageResponse{Total: 1},
},
},
{
"address without delegations",
fmt.Sprintf("%s/cosmos/staking/v1beta1/delegations/%s", baseURL, newAddr.String()),
map[string]string{
grpctypes.GRPCBlockHeightHeader: "1",
},
false,
&types.QueryDelegatorDelegationsResponse{},
&types.QueryDelegatorDelegationsResponse{
DelegationResponses: types.DelegationResponses{},
Pagination: &query.PageResponse{Total: 0},
},
},
}

for _, tc := range testCases {
Expand Down
5 changes: 0 additions & 5 deletions x/staking/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,6 @@ func (k Querier) DelegatorDelegations(c context.Context, req *types.QueryDelegat
return nil, status.Error(codes.Internal, err.Error())
}

if delegations == nil {
return nil, status.Errorf(
codes.NotFound,
"unable to find delegations for address %s", req.DelegatorAddr)
}
delegationResps, err := DelegationsToDelegationResponses(ctx, k.Keeper, delegations)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
Expand Down
Loading

0 comments on commit da87ab0

Please sign in to comment.