Skip to content

Commit

Permalink
Register gRPC Gateway routes (#7173)
Browse files Browse the repository at this point in the history
* added unimplemeted code

* added a test for bank get balances

* fixed lint

* Fix decode error

* fixed tests

* added missing gRPC tests for x/bank

* added tests for params, rewards in x/distribution

* added tests for x/distr grpc tests

* added todos

* removed register grpc routes

* registered x/ibc client handlers

* updated todos

* fixed error

* fixed tests

* review changes

* review change

Co-authored-by: anilCSE <anil@vitwit.com>
Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 4, 2020
1 parent 2bbcdbb commit 5df7dbc
Show file tree
Hide file tree
Showing 9 changed files with 582 additions and 7 deletions.
4 changes: 3 additions & 1 deletion x/auth/module.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package auth

import (
"context"
"encoding/json"
"fmt"
"math/rand"
Expand Down Expand Up @@ -66,7 +67,8 @@ func (AppModuleBasic) RegisterRESTRoutes(clientCtx client.Context, rtr *mux.Rout
}

// RegisterGRPCRoutes registers the gRPC Gateway routes for the auth module.
func (a AppModuleBasic) RegisterGRPCRoutes(_ client.Context, _ *runtime.ServeMux) {
func (AppModuleBasic) RegisterGRPCRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx))
}

// GetTxCmd returns the root tx command for the auth module.
Expand Down
67 changes: 67 additions & 0 deletions x/bank/client/rest/grpc_query_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package rest_test

import (
"encoding/base64"
"fmt"

"github.com/gogo/protobuf/proto"

"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)

Expand Down Expand Up @@ -93,3 +96,67 @@ func (s *IntegrationTestSuite) TestTotalSupplyGRPCHandler() {
})
}
}

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

// TODO: need to pass bech32 string instead of base64 encoding string.
// ref: https://github.com/cosmos/cosmos-sdk/issues/7195
accAddrBase64 := base64.URLEncoding.EncodeToString(val.Address)

testCases := []struct {
name string
url string
respType proto.Message
expected proto.Message
}{
{
"gRPC total account balance",
fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s", baseURL, accAddrBase64),
&types.QueryAllBalancesResponse{},
&types.QueryAllBalancesResponse{
Balances: sdk.NewCoins(
sdk.NewCoin(fmt.Sprintf("%stoken", val.Moniker), s.cfg.AccountTokens),
sdk.NewCoin(s.cfg.BondDenom, s.cfg.StakingTokens.Sub(s.cfg.BondedTokens)),
),
Pagination: &query.PageResponse{
Total: 2,
},
},
},
{
"gPRC account balance of a denom",
fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s/%s", baseURL, accAddrBase64, s.cfg.BondDenom),
&types.QueryBalanceResponse{},
&types.QueryBalanceResponse{
Balance: &sdk.Coin{
Denom: s.cfg.BondDenom,
Amount: s.cfg.StakingTokens.Sub(s.cfg.BondedTokens),
},
},
},
{
"gPRC account balance of a bogus denom",
fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s/foobar", baseURL, accAddrBase64),
&types.QueryBalanceResponse{},
&types.QueryBalanceResponse{
Balance: &sdk.Coin{
Denom: "foobar",
Amount: sdk.NewInt(0),
},
},
},
}

for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
resp, err := rest.GetRequest(tc.url)
s.Require().NoError(err)

s.Require().NoError(val.ClientCtx.JSONMarshaler.UnmarshalJSON(resp, tc.respType))
s.Require().Equal(tc.expected.String(), tc.respType.String())
})
}
}
2 changes: 1 addition & 1 deletion x/bank/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (AppModuleBasic) RegisterRESTRoutes(clientCtx client.Context, rtr *mux.Rout
}

// RegisterGRPCRoutes registers the gRPC Gateway routes for the bank module.
func (a AppModuleBasic) RegisterGRPCRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
func (AppModuleBasic) RegisterGRPCRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx))
}

Expand Down
Loading

0 comments on commit 5df7dbc

Please sign in to comment.