diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index 6d42972c1..36afc93ce 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -1555,7 +1555,7 @@ func TestGRPCQuery(t *testing.T) { grpcQueryOpt := func(bapp *BaseApp) { testdata.RegisterTestServiceServer( bapp.GRPCQueryRouter(), - testServer{}, + testdata.TestServiceImpl{}, ) } diff --git a/baseapp/grpcrouter_test.go b/baseapp/grpcrouter_test.go index b36d6edb8..428b3127d 100644 --- a/baseapp/grpcrouter_test.go +++ b/baseapp/grpcrouter_test.go @@ -2,7 +2,6 @@ package baseapp import ( "context" - "fmt" "testing" "github.com/stretchr/testify/require" @@ -11,22 +10,9 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -type testServer struct{} - -func (e testServer) Echo(_ context.Context, req *testdata.EchoRequest) (*testdata.EchoResponse, error) { - return &testdata.EchoResponse{Message: req.Message}, nil -} - -func (e testServer) SayHello(_ context.Context, request *testdata.SayHelloRequest) (*testdata.SayHelloResponse, error) { - greeting := fmt.Sprintf("Hello %s!", request.Name) - return &testdata.SayHelloResponse{Greeting: greeting}, nil -} - -var _ testdata.TestServiceServer = testServer{} - func TestGRPCRouter(t *testing.T) { qr := NewGRPCQueryRouter() - testdata.RegisterTestServiceServer(qr, testServer{}) + testdata.RegisterTestServiceServer(qr, testdata.TestServiceImpl{}) helper := &QueryServiceTestHelper{ GRPCQueryRouter: qr, ctx: sdk.Context{}, diff --git a/client/grpc_query.go b/client/grpc_query.go new file mode 100644 index 000000000..efd6c335f --- /dev/null +++ b/client/grpc_query.go @@ -0,0 +1,33 @@ +package client + +import ( + gocontext "context" + "fmt" + + gogogrpc "github.com/gogo/protobuf/grpc" + "google.golang.org/grpc" + "google.golang.org/grpc/encoding" + "google.golang.org/grpc/encoding/proto" +) + +var _ gogogrpc.ClientConn = Context{} + +var protoCodec = encoding.GetCodec(proto.Name) + +// Invoke implements the grpc ClientConn.Invoke method +func (ctx Context) Invoke(_ gocontext.Context, method string, args, reply interface{}, _ ...grpc.CallOption) error { + reqBz, err := protoCodec.Marshal(args) + if err != nil { + return err + } + resBz, _, err := ctx.QueryWithData(method, reqBz) + if err != nil { + return err + } + return protoCodec.Unmarshal(resBz, reply) +} + +// NewStream implements the grpc ClientConn.NewStream method +func (Context) NewStream(gocontext.Context, *grpc.StreamDesc, string, ...grpc.CallOption) (grpc.ClientStream, error) { + return nil, fmt.Errorf("streaming rpc not supported") +} diff --git a/codec/testdata/test_service.go b/codec/testdata/test_service.go new file mode 100644 index 000000000..3463be872 --- /dev/null +++ b/codec/testdata/test_service.go @@ -0,0 +1,19 @@ +package testdata + +import ( + "context" + "fmt" +) + +type TestServiceImpl struct{} + +func (e TestServiceImpl) Echo(_ context.Context, req *EchoRequest) (*EchoResponse, error) { + return &EchoResponse{Message: req.Message}, nil +} + +func (e TestServiceImpl) SayHello(_ context.Context, request *SayHelloRequest) (*SayHelloResponse, error) { + greeting := fmt.Sprintf("Hello %s!", request.Name) + return &SayHelloResponse{Greeting: greeting}, nil +} + +var _ TestServiceServer = TestServiceImpl{} diff --git a/simapp/app.go b/simapp/app.go index b7bf89cb7..7835fa284 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -4,8 +4,6 @@ import ( "io" "os" - "github.com/cosmos/cosmos-sdk/codec/types" - abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/libs/log" tmos "github.com/tendermint/tendermint/libs/os" @@ -13,6 +11,8 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec/testdata" + "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/std" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" @@ -306,6 +306,9 @@ func NewSimApp( app.mm.RegisterRoutes(app.Router(), app.QueryRouter()) app.mm.RegisterQueryServices(app.GRPCQueryRouter()) + // add test gRPC service for testing gRPC queries in isolation + testdata.RegisterTestServiceServer(app.GRPCQueryRouter(), testdata.TestServiceImpl{}) + // create the simulation manager and define the order of the modules for deterministic simulations // // NOTE: this is not required apps that don't use the simulator for fuzz testing diff --git a/tests/cli/grpc_query_test.go b/tests/cli/grpc_query_test.go new file mode 100644 index 000000000..a6286d678 --- /dev/null +++ b/tests/cli/grpc_query_test.go @@ -0,0 +1,28 @@ +// +build cli_test + +package cli + +import ( + "context" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/codec/testdata" +) + +func TestCliQueryConn(t *testing.T) { + t.Parallel() + f := NewFixtures(t) + + // start simd server + proc := f.SDStart() + t.Cleanup(func() { proc.Stop(false) }) + + ctx := client.NewContext() + testClient := testdata.NewTestServiceClient(ctx) + res, err := testClient.Echo(context.Background(), &testdata.EchoRequest{Message: "hello"}) + require.NoError(t, err) + require.Equal(t, "hello", res.Message) +}