Skip to content

Commit

Permalink
upstream more changes from v2
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle committed May 14, 2024
1 parent 493d486 commit d530e80
Show file tree
Hide file tree
Showing 38 changed files with 6,247 additions and 0 deletions.
34 changes: 34 additions & 0 deletions server/v2/api/grpc/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package grpc

import "math"

func DefaultConfig() *Config {
return &Config{
Enable: true,
// DefaultGRPCAddress defines the default address to bind the gRPC server to.
Address: "localhost:9090",
// DefaultGRPCMaxRecvMsgSize defines the default gRPC max message size in
// bytes the server can receive.
MaxRecvMsgSize: 1024 * 1024 * 10,
// DefaultGRPCMaxSendMsgSize defines the default gRPC max message size in
// bytes the server can send.
MaxSendMsgSize: math.MaxInt32,
}
}

// GRPCConfig defines configuration for the gRPC server.
type Config struct {
// Enable defines if the gRPC server should be enabled.
Enable bool `mapstructure:"enable" toml:"enable" comment:"Enable defines if the gRPC server should be enabled."`

// Address defines the API server to listen on
Address string `mapstructure:"address" toml:"address" comment:"Address defines the gRPC server address to bind to."`

// MaxRecvMsgSize defines the max message size in bytes the server can receive.
// The default value is 10MB.
MaxRecvMsgSize int `mapstructure:"max-recv-msg-size" toml:"max-recv-msg-size" comment:"MaxRecvMsgSize defines the max message size in bytes the server can receive.\nThe default value is 10MB."`

// MaxSendMsgSize defines the max message size in bytes the server can send.
// The default value is math.MaxInt32.
MaxSendMsgSize int `mapstructure:"max-send-msg-size" toml:"max-send-msg-size" comment:"MaxSendMsgSize defines the max message size in bytes the server can send.\nThe default value is math.MaxInt32."`
}
5 changes: 5 additions & 0 deletions server/v2/api/grpc/gogoreflection/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Package gogoreflection implements gRPC reflection for gogoproto consumers
// the normal reflection library does not work as it points to a different
// singleton registry. The API and codebase is taken from the official gRPC
// reflection repository.
package gogoreflection
76 changes: 76 additions & 0 deletions server/v2/api/grpc/gogoreflection/fix_registration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package gogoreflection

import (
"reflect"

_ "github.com/cosmos/gogoproto/gogoproto" // required so it does register the gogoproto file descriptor
gogoproto "github.com/cosmos/gogoproto/proto"

_ "github.com/cosmos/cosmos-proto" // look above
"github.com/golang/protobuf/proto" //nolint:staticcheck // migrate in a future pr
)

func getFileDescriptor(filePath string) []byte {
// Since we got well known descriptors which are not registered into gogoproto
// registry but are instead registered into the proto one, we need to check both.
fd := gogoproto.FileDescriptor(filePath)
if len(fd) != 0 {
return fd
}

return proto.FileDescriptor(filePath) //nolint:staticcheck // keep for backward compatibility
}

func getMessageType(name string) reflect.Type {
typ := gogoproto.MessageType(name)
if typ != nil {
return typ
}

return proto.MessageType(name) //nolint:staticcheck // keep for backward compatibility
}

func getExtension(extID int32, m proto.Message) *gogoproto.ExtensionDesc {
// check first in gogoproto registry
for id, desc := range gogoproto.RegisteredExtensions(m) {
if id == extID {
return desc
}
}

// check into proto registry
for id, desc := range proto.RegisteredExtensions(m) { //nolint:staticcheck // keep for backward compatibility
if id == extID {
return &gogoproto.ExtensionDesc{
ExtendedType: desc.ExtendedType, //nolint:staticcheck // keep for backward compatibility
ExtensionType: desc.ExtensionType, //nolint:staticcheck // keep for backward compatibility
Field: desc.Field, //nolint:staticcheck // keep for backward compatibility
Name: desc.Name, //nolint:staticcheck // keep for backward compatibility
Tag: desc.Tag, //nolint:staticcheck // keep for backward compatibility
Filename: desc.Filename, //nolint:staticcheck // keep for backward compatibility
}
}
}

return nil
}

func getExtensionsNumbers(m proto.Message) []int32 {
gogoProtoExts := gogoproto.RegisteredExtensions(m)

out := make([]int32, 0, len(gogoProtoExts))
for id := range gogoProtoExts {
out = append(out, id)
}
if len(out) != 0 {
return out
}

protoExts := proto.RegisteredExtensions(m) //nolint:staticcheck // kept for backwards compatibility
out = make([]int32, 0, len(protoExts))
for id := range protoExts {
out = append(out, id)
}

return out
}
22 changes: 22 additions & 0 deletions server/v2/api/grpc/gogoreflection/fix_registration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package gogoreflection

import (
"testing"

"google.golang.org/protobuf/runtime/protoimpl"
)

func TestRegistrationFix(t *testing.T) {
res := getFileDescriptor("gogoproto/gogo.proto")
rawDesc, err := decompress(res)
if err != nil {
t.Fatal(err)
}
fd := protoimpl.DescBuilder{
RawDescriptor: rawDesc,
}.Build()

if fd.File.Extensions().Len() == 0 {
t.Fatal("unexpected parsing")
}
}

0 comments on commit d530e80

Please sign in to comment.