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: Staking delegations should return empty list instead of rpc error when no records found #9423

Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
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