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 9 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
57 changes: 30 additions & 27 deletions x/staking/client/rest/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,39 +414,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 +462,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 All @@ -506,6 +495,20 @@ func (s *IntegrationTestSuite) TestQueryDelegatorDelegationsGRPC() {
}
}

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) TestQueryDelegatorUnbondingDelegationsGRPC() {
val := s.network.Validators[0]
baseURL := val.APIAddress
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
11 changes: 10 additions & 1 deletion x/staking/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,36 +288,45 @@ func (suite *KeeperTestSuite) TestGRPCQueryDelegatorDelegations() {
msg string
malleate func()
expPass bool
expErr bool
}{
{"empty request",
func() {
req = &types.QueryDelegatorDelegationsRequest{}
},
false,
true,
}, {"invalid request",
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
func() {
req = &types.QueryDelegatorDelegationsRequest{DelegatorAddr: addrs[4].String()}
},
false,
false,
},
{"valid request",
func() {
req = &types.QueryDelegatorDelegationsRequest{DelegatorAddr: addrAcc.String(),
Pagination: &query.PageRequest{Limit: 1, CountTotal: true}}
},
true,
false,
},
}

for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
tc.malleate()
res, err := queryClient.DelegatorDelegations(gocontext.Background(), req)
if tc.expPass {
if tc.expPass && !tc.expErr {
suite.NoError(err)
suite.NotNil(res.Pagination.NextKey)
suite.Equal(uint64(2), res.Pagination.Total)
suite.Len(res.DelegationResponses, 1)
suite.Equal(1, len(res.DelegationResponses))
suite.Equal(sdk.NewCoin(sdk.DefaultBondDenom, delegation.Shares.TruncateInt()), res.DelegationResponses[0].Balance)
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
} else if !tc.expPass && !tc.expErr {
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
suite.NoError(err)
suite.Nil(res.DelegationResponses)
} else {
suite.Error(err)
suite.Nil(res)
Expand Down