Skip to content

Commit

Permalink
feat(baseapp): support pulsar gRPC query servers (#11192)
Browse files Browse the repository at this point in the history
## Description

This adds support for registering gRPC query server implementations that were generated using pulsar. It should make integration with the ORM easier.

This should be backportable.

This does not enable support for pulsar msg servers or tx's.



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
aaronc authored Feb 17, 2022
1 parent f2fe1d6 commit 9ce0844
Show file tree
Hide file tree
Showing 20 changed files with 37,156 additions and 214 deletions.
7 changes: 4 additions & 3 deletions api/cosmos/group/v1beta1/types.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

250 changes: 125 additions & 125 deletions api/cosmos/tx/v1beta1/service.pulsar.go

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
"testing"
"time"

"google.golang.org/protobuf/proto"

"github.com/cosmos/cosmos-sdk/testutil/testdata_pulsar"

"github.com/gogo/protobuf/jsonpb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -1817,6 +1821,41 @@ func TestGRPCQuery(t *testing.T) {
require.Equal(t, "Hello foo!", res.Greeting)
}

func TestGRPCQueryPulsar(t *testing.T) {
grpcQueryOpt := func(bapp *baseapp.BaseApp) {
testdata_pulsar.RegisterQueryServer(
bapp.GRPCQueryRouter(),
testdata_pulsar.QueryImpl{},
)
}

app := setupBaseApp(t, grpcQueryOpt)
app.GRPCQueryRouter().SetInterfaceRegistry(codectypes.NewInterfaceRegistry())

app.InitChain(abci.RequestInitChain{})
header := tmproto.Header{Height: app.LastBlockHeight() + 1}
app.BeginBlock(abci.RequestBeginBlock{Header: header})
app.Commit()

req := &testdata_pulsar.SayHelloRequest{Name: "foo"}
reqBz, err := proto.Marshal(req)
require.NoError(t, err)

reqQuery := abci.RequestQuery{
Data: reqBz,
Path: "/testdata.Query/SayHello",
}

resQuery := app.Query(reqQuery)

require.Equal(t, abci.CodeTypeOK, resQuery.Code, resQuery)

var res testdata_pulsar.SayHelloResponse
err = proto.Unmarshal(resQuery.Value, &res)
require.NoError(t, err)
require.Equal(t, "Hello foo!", res.Greeting)
}

// Test p2p filter queries
func TestP2PQuery(t *testing.T) {
addrPeerFilterOpt := func(bapp *baseapp.BaseApp) {
Expand Down
13 changes: 6 additions & 7 deletions baseapp/grpcrouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package baseapp

import (
"fmt"
"github.com/cosmos/cosmos-sdk/codec"

"google.golang.org/grpc/encoding"

"github.com/cosmos/cosmos-sdk/codec"

"github.com/cosmos/cosmos-sdk/client/grpc/reflection"

gogogrpc "github.com/gogo/protobuf/grpc"
Expand Down Expand Up @@ -81,18 +83,15 @@ func (qrt *GRPCQueryRouter) RegisterService(sd *grpc.ServiceDesc, handler interf
// call the method handler from the service description with the handler object,
// a wrapped sdk.Context with proto-unmarshaled data from the ABCI request data
res, err := methodHandler(handler, sdk.WrapSDKContext(ctx), func(i interface{}) error {
err := qrt.cdc.Unmarshal(req.Data, i)
if err != nil {
return err
}
return nil
return qrt.cdc.Unmarshal(req.Data, i)
}, nil)
if err != nil {
return abci.ResponseQuery{}, err
}

// proto marshal the result bytes
resBytes, err := qrt.cdc.Marshal(res)
var resBytes []byte
resBytes, err = qrt.cdc.Marshal(res)
if err != nil {
return abci.ResponseQuery{}, err
}
Expand Down
6 changes: 4 additions & 2 deletions baseapp/grpcrouter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"sync"
"testing"

"github.com/cosmos/cosmos-sdk/testutil/testdata_pulsar"

"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"
Expand All @@ -16,11 +18,11 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

func TestGRPCGatewayRouter(t *testing.T) {
func TestGRPCQueryRouter(t *testing.T) {
qr := baseapp.NewGRPCQueryRouter()
interfaceRegistry := testdata.NewTestInterfaceRegistry()
qr.SetInterfaceRegistry(interfaceRegistry)
testdata.RegisterQueryServer(qr, testdata.QueryImpl{})
testdata_pulsar.RegisterQueryServer(qr, testdata_pulsar.QueryImpl{})
helper := &baseapp.QueryServiceTestHelper{
GRPCQueryRouter: qr,
Ctx: sdk.Context{}.WithContext(context.Background()),
Expand Down
13 changes: 9 additions & 4 deletions codec/proto_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"encoding/binary"
"errors"
"fmt"
"strings"

legacyproto "github.com/golang/protobuf/proto"
"google.golang.org/grpc/encoding"
"strings"
"google.golang.org/protobuf/proto"

"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/gogo/protobuf/jsonpb"
gogoproto "github.com/gogo/protobuf/proto"

"github.com/cosmos/cosmos-sdk/codec/types"
)

// ProtoCodecMarshaler defines an interface for codecs that utilize Protobuf for both
Expand Down Expand Up @@ -264,8 +267,9 @@ type grpcProtoCodec struct {
}

func (g grpcProtoCodec) Marshal(v interface{}) ([]byte, error) {
// TODO(fdymylja): maybe this is the correct place to support protov2 types too for gRPC
switch m := v.(type) {
case proto.Message:
return proto.Marshal(m)
case ProtoMarshaler:
return g.cdc.Marshal(m)
case legacyproto.Message:
Expand All @@ -276,8 +280,9 @@ func (g grpcProtoCodec) Marshal(v interface{}) ([]byte, error) {
}

func (g grpcProtoCodec) Unmarshal(data []byte, v interface{}) error {
// TODO(fdymylja): maybe this is the correct place to support protov2 types too for gRPC
switch m := v.(type) {
case proto.Message:
return proto.Unmarshal(data, m)
case ProtoMarshaler:
return g.cdc.Unmarshal(data, m)
case legacyproto.Message:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/confio/ics23/go v0.6.6
github.com/cosmos/btcutil v1.0.4
github.com/cosmos/cosmos-proto v1.0.0-alpha7
github.com/cosmos/cosmos-sdk/api v0.1.0-alpha4
github.com/cosmos/cosmos-sdk/db v1.0.0-beta.1
github.com/cosmos/cosmos-sdk/errors v1.0.0-beta.2
github.com/cosmos/go-bip39 v1.0.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ github.com/cosmos/btcutil v1.0.4 h1:n7C2ngKXo7UC9gNyMNLbzqz7Asuf+7Qv4gnX/rOdQ44=
github.com/cosmos/btcutil v1.0.4/go.mod h1:Ffqc8Hn6TJUdDgHBwIZLtrLQC1KdJ9jGJl/TvgUaxbU=
github.com/cosmos/cosmos-proto v1.0.0-alpha7 h1:yqYUOHF2jopwZh4dVQp3xgqwftE5/2hkrwIV6vkUbO0=
github.com/cosmos/cosmos-proto v1.0.0-alpha7/go.mod h1:dosO4pSAbJF8zWCzCoTWP7nNsjcvSUBQmniFxDg5daw=
github.com/cosmos/cosmos-sdk/api v0.1.0-alpha4 h1:z2si9sQNUTj2jw+24SujuUxcoNS3TVga/fdYsS4rJII=
github.com/cosmos/cosmos-sdk/api v0.1.0-alpha4/go.mod h1:gZu6sOu2vl4Fd7I+BjDSx2bxndwPgFLGfOegek3SQQo=
github.com/cosmos/cosmos-sdk/errors v1.0.0-beta.2 h1:bBglNlra8ZHb4dmbEE8V85ihLA+DkriSm7tcx6x/JWo=
github.com/cosmos/cosmos-sdk/errors v1.0.0-beta.2/go.mod h1:Gi7pzVRnvZ1N16JAXpLADzng0ePoE7YeEHaULSFB2Ts=
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d/go.mod h1:tSxLoYXyBmiFeKpvmq4dzayMdCjCnu8uqmCysIGBT2Y=
Expand Down Expand Up @@ -1926,6 +1928,7 @@ google.golang.org/genproto v0.0.0-20211129164237-f09f9a12af12/go.mod h1:5CzLGKJ6
google.golang.org/genproto v0.0.0-20211203200212-54befc351ae9/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/genproto v0.0.0-20211206160659-862468c7d6e0/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/genproto v0.0.0-20211223182754-3ac035c7e7cb/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/genproto v0.0.0-20220118154757-00ab72f36ad5 h1:zzNejm+EgrbLfDZ6lu9Uud2IVvHySPl8vQzf04laR5Q=
google.golang.org/genproto v0.0.0-20220118154757-00ab72f36ad5/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/grpc v1.8.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw=
Expand Down
2 changes: 0 additions & 2 deletions proto/cosmos/tx/v1beta1/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ package cosmos.tx.v1beta1;
import "google/api/annotations.proto";
import "cosmos/base/abci/v1beta1/abci.proto";
import "cosmos/tx/v1beta1/tx.proto";
import "gogoproto/gogo.proto";
import "cosmos/base/query/v1beta1/pagination.proto";

option (gogoproto.goproto_registration) = true;
option go_package = "github.com/cosmos/cosmos-sdk/types/tx";

// Service defines a gRPC service for interacting with transactions.
Expand Down
3 changes: 3 additions & 0 deletions scripts/protocgen2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ protoc_install_gopulsar

echo "Generating API module"
(cd proto; buf generate --template buf.gen.pulsar.yaml)

echo "Generate Pulsar Test Data"
(cd testutil/testdata; buf generate --template buf.gen.pulsar.yaml)
5 changes: 3 additions & 2 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"path/filepath"

"github.com/cosmos/cosmos-sdk/testutil/testdata_pulsar"

"github.com/gorilla/mux"
"github.com/rakyll/statik/fs"
"github.com/spf13/cast"
Expand All @@ -27,7 +29,6 @@ import (
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
"github.com/cosmos/cosmos-sdk/store/streaming"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/version"
Expand Down Expand Up @@ -407,7 +408,7 @@ func NewSimApp(
app.mm.RegisterServices(app.configurator)

// add test gRPC service for testing gRPC queries in isolation
testdata.RegisterQueryServer(app.GRPCQueryRouter(), testdata.QueryImpl{})
testdata_pulsar.RegisterQueryServer(app.GRPCQueryRouter(), testdata_pulsar.QueryImpl{})

// create the simulation manager and define the order of the modules for deterministic simulations
//
Expand Down
18 changes: 18 additions & 0 deletions testutil/testdata/buf.gen.pulsar.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: v1
managed:
enabled: true
go_package_prefix:
default: github.com/cosmos/cosmos-sdk/testutil/testdata_pulsar
except:
- buf.build/googleapis/googleapis
- buf.build/cosmos/gogo-proto
- buf.build/cosmos/cosmos-proto
override:
buf.build/cosmos/cosmos-sdk: github.com/cosmos/cosmos-sdk/api
plugins:
- name: go-pulsar
out: ../testdata_pulsar
opt: paths=source_relative
- name: go-grpc
out: ../testdata_pulsar
opt: paths=source_relative
28 changes: 28 additions & 0 deletions testutil/testdata_pulsar/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package testdata_pulsar

import (
"context"
"fmt"
)

type QueryImpl struct {
UnimplementedQueryServer
}

func (q QueryImpl) Echo(_ context.Context, request *EchoRequest) (*EchoResponse, error) {
return &EchoResponse{Message: request.Message}, nil
}

func (q QueryImpl) SayHello(_ context.Context, request *SayHelloRequest) (*SayHelloResponse, error) {
greeting := fmt.Sprintf("Hello %s!", request.Name)
return &SayHelloResponse{Greeting: greeting}, nil
}

func (q QueryImpl) TestAny(_ context.Context, request *TestAnyRequest) (*TestAnyResponse, error) {
return &TestAnyResponse{HasAnimal: &HasAnimal{
Animal: request.AnyAnimal,
X: 10,
}}, nil
}

var _ QueryServer = QueryImpl{}
Loading

0 comments on commit 9ce0844

Please sign in to comment.