From c1b975eecbf1c9aa989f47d77f1655dc77af629c Mon Sep 17 00:00:00 2001 From: atheeshp <59333759+atheeshp@users.noreply.github.com> Date: Fri, 4 Sep 2020 20:37:02 +0530 Subject: [PATCH] Register grpc routes for x/{slashing, params} (#7223) * Register grpc routes for x/{slashing, params} * added todos * review changes Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> --- x/params/client/rest/grpc_query_test.go | 129 ++++++++++++++++++++ x/params/module.go | 5 +- x/slashing/client/rest/grpc_query_test.go | 136 ++++++++++++++++++++++ x/slashing/module.go | 4 +- 4 files changed, 272 insertions(+), 2 deletions(-) create mode 100644 x/params/client/rest/grpc_query_test.go create mode 100644 x/slashing/client/rest/grpc_query_test.go diff --git a/x/params/client/rest/grpc_query_test.go b/x/params/client/rest/grpc_query_test.go new file mode 100644 index 0000000000..8997eec890 --- /dev/null +++ b/x/params/client/rest/grpc_query_test.go @@ -0,0 +1,129 @@ +package rest_test + +import ( + "fmt" + "testing" + + "github.com/cosmos/cosmos-sdk/testutil" + "github.com/cosmos/cosmos-sdk/testutil/network" + "github.com/cosmos/cosmos-sdk/x/params/types/proposal" + "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) TearDownSuite() { + s.T().Log("tearing down integration test suite") + s.network.Cleanup() +} + +func (s *IntegrationTestSuite) TestQueryParamsGRPC() { + val := s.network.Validators[0] + baseURL := val.APIAddress + + testCases := []struct { + name string + url string + headers map[string]string + expErr bool + respType proto.Message + expected proto.Message + }{ + { + "with no subspace, key", + fmt.Sprintf("%s/cosmos/params/v1beta1/params?subspace=%s&key=%s", baseURL, "", ""), + map[string]string{}, + true, + &proposal.QueryParamsResponse{}, + &proposal.QueryParamsResponse{ + Param: proposal.ParamChange{ + Subspace: "staking", + Key: "MaxValidators", + Value: "100", + }, + }, + }, + { + "with wrong subspace", + fmt.Sprintf("%s/cosmos/params/v1beta1/params?subspace=%s&key=%s", baseURL, "wrongSubspace", "MaxValidators"), + map[string]string{}, + true, + &proposal.QueryParamsResponse{}, + &proposal.QueryParamsResponse{ + Param: proposal.ParamChange{ + Subspace: "staking", + Key: "MaxValidators", + Value: "100", + }, + }, + }, + { + "with wrong key", + fmt.Sprintf("%s/cosmos/params/v1beta1/params?subspace=%s&key=%s", baseURL, "staking", "wrongKey"), + map[string]string{}, + false, + &proposal.QueryParamsResponse{}, + &proposal.QueryParamsResponse{ + Param: proposal.ParamChange{ + Subspace: "staking", + Key: "wrongKey", + Value: "", + }, + }, + }, + { + "params", + fmt.Sprintf("%s/cosmos/params/v1beta1/params?subspace=%s&key=%s", baseURL, "staking", "MaxValidators"), + map[string]string{}, + false, + &proposal.QueryParamsResponse{}, + &proposal.QueryParamsResponse{ + Param: proposal.ParamChange{ + Subspace: "staking", + Key: "MaxValidators", + Value: "100", + }, + }, + }, + } + + for _, tc := range testCases { + tc := tc + s.Run(tc.name, func() { + resp, err := testutil.GetRequestWithHeaders(tc.url, tc.headers) + s.Require().NoError(err) + + err = val.ClientCtx.JSONMarshaler.UnmarshalJSON(resp, tc.respType) + + if tc.expErr { + s.Require().Error(err) + } else { + s.Require().NoError(err) + s.Require().Equal(tc.expected.String(), tc.respType.String()) + } + }) + } +} + +func TestIntegrationTestSuite(t *testing.T) { + suite.Run(t, new(IntegrationTestSuite)) +} diff --git a/x/params/module.go b/x/params/module.go index 56eb8afc3a..89a76facd9 100644 --- a/x/params/module.go +++ b/x/params/module.go @@ -1,6 +1,7 @@ package params import ( + "context" "encoding/json" "math/rand" @@ -56,7 +57,9 @@ func (AppModuleBasic) ValidateGenesis(_ codec.JSONMarshaler, config client.TxEnc func (AppModuleBasic) RegisterRESTRoutes(_ client.Context, _ *mux.Router) {} // RegisterGRPCRoutes registers the gRPC Gateway routes for the params module. -func (AppModuleBasic) RegisterGRPCRoutes(_ client.Context, _ *runtime.ServeMux) {} +func (AppModuleBasic) RegisterGRPCRoutes(clientCtx client.Context, mux *runtime.ServeMux) { + proposal.RegisterQueryHandlerClient(context.Background(), mux, proposal.NewQueryClient(clientCtx)) +} // GetTxCmd returns no root tx command for the params module. func (AppModuleBasic) GetTxCmd() *cobra.Command { return nil } diff --git a/x/slashing/client/rest/grpc_query_test.go b/x/slashing/client/rest/grpc_query_test.go new file mode 100644 index 0000000000..8e89f80934 --- /dev/null +++ b/x/slashing/client/rest/grpc_query_test.go @@ -0,0 +1,136 @@ +package rest_test + +import ( + "encoding/base64" + "fmt" + "testing" + "time" + + "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/query" + "github.com/cosmos/cosmos-sdk/x/slashing/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) TearDownSuite() { + s.T().Log("tearing down integration test suite") + s.network.Cleanup() +} + +func (s *IntegrationTestSuite) TestGRPCQueries() { + 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 + consAddrBase64 := base64.URLEncoding.EncodeToString(sdk.ConsAddress(val.PubKey.Address())) + + testCases := []struct { + name string + url string + headers map[string]string + expErr bool + respType proto.Message + expected proto.Message + }{ + { + "get signing infos (height specific)", + fmt.Sprintf("%s/cosmos/slashing/v1beta1/signing_infos", baseURL), + map[string]string{ + grpctypes.GRPCBlockHeightHeader: "1", + }, + false, + &types.QuerySigningInfosResponse{}, + &types.QuerySigningInfosResponse{ + Info: []types.ValidatorSigningInfo{ + types.ValidatorSigningInfo{ + Address: sdk.ConsAddress(val.PubKey.Address()), + JailedUntil: time.Unix(0, 0), + }, + }, + Pagination: &query.PageResponse{ + Total: uint64(1), + }, + }, + }, + { + "get signing info (height specific)", + fmt.Sprintf("%s/cosmos/slashing/v1beta1/signing_infos/%s", baseURL, consAddrBase64), + map[string]string{ + grpctypes.GRPCBlockHeightHeader: "1", + }, + false, + &types.QuerySigningInfoResponse{}, + &types.QuerySigningInfoResponse{ + ValSigningInfo: types.ValidatorSigningInfo{ + Address: sdk.ConsAddress(val.PubKey.Address()), + JailedUntil: time.Unix(0, 0), + }, + }, + }, + { + "get signing info wrong address", + fmt.Sprintf("%s/cosmos/slashing/v1beta1/signing_infos/%s", baseURL, "wrongAddress"), + map[string]string{}, + true, + &types.QuerySigningInfoResponse{}, + nil, + }, + { + "params", + fmt.Sprintf("%s/cosmos/slashing/v1beta1/params", baseURL), + map[string]string{}, + false, + &types.QueryParamsResponse{}, + &types.QueryParamsResponse{ + Params: types.DefaultParams(), + }, + }, + } + + for _, tc := range testCases { + tc := tc + + s.Run(tc.name, func() { + resp, err := testutil.GetRequestWithHeaders(tc.url, tc.headers) + s.Require().NoError(err) + + err = val.ClientCtx.JSONMarshaler.UnmarshalJSON(resp, tc.respType) + + if tc.expErr { + s.Require().Error(err) + } else { + s.Require().NoError(err) + s.Require().Equal(tc.expected.String(), tc.respType.String()) + } + }) + } +} + +func TestIntegrationTestSuite(t *testing.T) { + suite.Run(t, new(IntegrationTestSuite)) +} diff --git a/x/slashing/module.go b/x/slashing/module.go index 6a0a2b29ff..b653e38dec 100644 --- a/x/slashing/module.go +++ b/x/slashing/module.go @@ -1,6 +1,7 @@ package slashing import ( + "context" "encoding/json" "fmt" "math/rand" @@ -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.