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

Register gRPC Gateway routes #7173

Merged
merged 25 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
4036473
added unimplemeted code
atheeshp Aug 26, 2020
1bc5e1e
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Aug 26, 2020
85dff4f
added a test for bank get balances
atheeshp Aug 26, 2020
97d8cf1
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Aug 26, 2020
db05541
fixed lint
atheeshp Aug 26, 2020
7b19f97
Merge branch 'master' into atheesh/5921-grpc-gateway-x-auth-routes
atheeshp Aug 26, 2020
3fbce47
Fix decode error
anilcse Aug 26, 2020
e831ef5
fixed tests
atheeshp Aug 27, 2020
38f0daf
added missing gRPC tests for x/bank
atheeshp Aug 27, 2020
ad84c4a
added tests for params, rewards in x/distribution
atheeshp Aug 27, 2020
3d3e697
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Aug 27, 2020
c958956
added tests for x/distr grpc tests
atheeshp Aug 28, 2020
3335a54
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/5…
atheeshp Aug 28, 2020
9c0761e
Merge branch 'master' into atheesh/5921-grpc-gateway-x-auth-routes
atheeshp Aug 31, 2020
68cf808
added todos
atheeshp Sep 3, 2020
000c835
removed register grpc routes
atheeshp Sep 3, 2020
2bb13af
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Sep 3, 2020
5bbf22b
Merge branch 'master' into atheesh/5921-grpc-gateway-x-auth-routes
alexanderbez Sep 3, 2020
523cadf
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Sep 4, 2020
35fad50
registered x/ibc client handlers
atheeshp Sep 4, 2020
e91a8d1
updated todos
atheeshp Sep 4, 2020
33cce01
fixed error
atheeshp Sep 4, 2020
dc419e1
fixed tests
atheeshp Sep 4, 2020
7169fc1
review changes
atheeshp Sep 4, 2020
9fee16c
review change
atheeshp Sep 4, 2020
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
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
72 changes: 72 additions & 0 deletions x/bank/client/rest/grpc_query_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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/x/bank/types"
)

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

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

testCases := []struct {
name string
url string
headers map[string]string
respType proto.Message
expected proto.Message
}{
{
"gRPC total account balance",
fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s", baseURL, base64.URLEncoding.EncodeToString(val.Address)),
Copy link
Contributor

@amaury1093 amaury1093 Aug 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the address path in the URL is defined as bytes in the proto files, and grpc-gateway decodes bytes with base64.URLEncoding, we (actually, any HTTP client really) need to encode the address as base64 URLEncoding first.

This has the unfortunate consequence of showing non-human-readable addresses in URLs:

+ /cosmos/bank/v1beta1/balances/u1-UOH-CHOvM2rNadSjtNARKkSk= // with base64
- /cosmos/bank/v1beta1/balances/cosmos1hd0egwrlsgwwhnx6kdd8228dxszy4yffnv2gtu // expected

What do people think here? Are you okay to use base64-encoded addresses in URLs?

If not, it's maybe worth to raise an issue on grpc-gateway to see if we can customize bytes decoding.

map[string]string{
grpctypes.GRPCBlockHeightHeader: "1",
},
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
&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, base64.URLEncoding.EncodeToString(val.Address), s.cfg.BondDenom),
map[string]string{
grpctypes.GRPCBlockHeightHeader: "1",
},
&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, base64.URLEncoding.EncodeToString(val.Address)),
map[string]string{
grpctypes.GRPCBlockHeightHeader: "1",
},
&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 := testutil.GetRequestWithHeaders(tc.url, tc.headers)
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
126 changes: 126 additions & 0 deletions x/distribution/client/rest/grpc_query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package rest_test

import (
"encoding/base64"
"fmt"
"testing"

"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/suite"
)

type IntegrationTestSuite struct {
suite.Suite

cfg network.Config
network *network.Network
}

func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")

cfg := network.DefaultConfig()
cfg.NumValidators = 1

s.cfg = cfg
s.network = network.New(s.T(), cfg)

_, err := s.network.WaitForHeight(1)
s.Require().NoError(err)
}

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

testCases := []struct {
name string
url string
respType proto.Message
expected proto.Message
}{
{
"gRPC request params",
fmt.Sprintf("%s/cosmos/distribution/v1beta1/params", baseURL),
&types.QueryParamsResponse{},
&types.QueryParamsResponse{
Params: types.DefaultParams(),
},
},
}

for _, tc := range testCases {
tc := tc
resp, err := rest.GetRequest(tc.url)
s.Run(tc.name, func() {
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.JSONMarshaler.UnmarshalJSON(resp, tc.respType))
s.Require().Equal(tc.expected, tc.respType)
})
}
}

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

rewards, _ := sdk.ParseDecCoins("19.6stake")

testCases := []struct {
name string
url string
headers map[string]string
expErr bool
respType proto.Message
expected proto.Message
}{
{
"gRPC request params with wrong validator address",
fmt.Sprintf("%s/cosmos/distribution/v1beta1/validators/%s/outstanding_rewards", baseURL, "wrongAddress"),
map[string]string{
grpctypes.GRPCBlockHeightHeader: "1",
},
false,
&types.QueryValidatorOutstandingRewardsResponse{},
&types.QueryValidatorOutstandingRewardsResponse{},
},
{
"gRPC request params valid address",
fmt.Sprintf("%s/cosmos/distribution/v1beta1/validators/%s/outstanding_rewards", baseURL, base64.URLEncoding.EncodeToString(val.ValAddress)),
map[string]string{
grpctypes.GRPCBlockHeightHeader: "2",
},
false,
&types.QueryValidatorOutstandingRewardsResponse{},
&types.QueryValidatorOutstandingRewardsResponse{
Rewards: types.ValidatorOutstandingRewards{
Rewards: rewards,
},
},
},
}

for _, tc := range testCases {
tc := tc
resp, err := testutil.GetRequestWithHeaders(tc.url, tc.headers)
s.Run(tc.name, func() {
if tc.expErr {
s.Require().Error(err)
} else {
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.JSONMarshaler.UnmarshalJSON(resp, tc.respType))
s.Require().Equal(tc.expected.String(), tc.respType.String())
}
})
}
}

func TestIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(IntegrationTestSuite))
}
5 changes: 4 additions & 1 deletion x/distribution/module.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package distribution

import (
"context"
"encoding/json"
"fmt"
"math/rand"
Expand Down Expand Up @@ -69,7 +70,9 @@ func (AppModuleBasic) RegisterRESTRoutes(clientCtx sdkclient.Context, rtr *mux.R
}

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

// GetTxCmd returns the root tx command for the distribution module.
func (AppModuleBasic) GetTxCmd() *cobra.Command {
Expand Down
4 changes: 3 additions & 1 deletion x/evidence/module.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package evidence

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

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

// GetTxCmd returns the evidence module's root tx command.
Expand Down
4 changes: 3 additions & 1 deletion x/gov/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gov
// DONTCOVER

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

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

// GetTxCmd returns the root tx command for the gov module.
Expand Down
4 changes: 3 additions & 1 deletion x/ibc-transfer/module.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package transfer

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

// RegisterGRPCRoutes registers the gRPC Gateway routes for the ibc-transfer 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 implements AppModuleBasic interface
Expand Down
2 changes: 1 addition & 1 deletion x/ibc/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, config client.TxE
func (AppModuleBasic) RegisterRESTRoutes(client.Context, *mux.Router) {}

// RegisterGRPCRoutes registers the gRPC Gateway routes for the ibc module.
func (a AppModuleBasic) RegisterGRPCRoutes(_ client.Context, _ *runtime.ServeMux) {
func (AppModuleBasic) RegisterGRPCRoutes(_ client.Context, _ *runtime.ServeMux) {
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
}

// GetTxCmd returns the root tx command for the ibc module.
Expand Down
4 changes: 3 additions & 1 deletion x/mint/module.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mint

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

// RegisterGRPCRoutes registers the gRPC Gateway routes for the mint module.
func (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 no root tx command for the mint module.
Expand Down
4 changes: 3 additions & 1 deletion x/slashing/module.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slashing

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

// RegisterGRPCRoutes registers the gRPC Gateway routes for the slashig module.
func (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 slashing module.
Expand Down
5 changes: 4 additions & 1 deletion x/upgrade/module.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package upgrade

import (
"context"
"encoding/json"

"github.com/gogo/protobuf/grpc"
Expand Down Expand Up @@ -53,7 +54,9 @@ func (AppModuleBasic) RegisterRESTRoutes(clientCtx client.Context, r *mux.Router
}

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

// GetQueryCmd returns the cli query commands for this module
func (AppModuleBasic) GetQueryCmd() *cobra.Command {
Expand Down