From 3c0ddc0b516fe905d313176d7bf54522e831b32a Mon Sep 17 00:00:00 2001 From: testinginprod Date: Tue, 23 Jul 2024 13:43:06 +0200 Subject: [PATCH 01/11] refactor grpc query handlers --- runtime/v2/app.go | 8 ++++---- runtime/v2/manager.go | 11 +++++------ server/v2/cometbft/abci.go | 12 +++++++----- server/v2/cometbft/server.go | 2 +- server/v2/types.go | 2 +- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/runtime/v2/app.go b/runtime/v2/app.go index 89d6d0be24ba..e45674acf0fd 100644 --- a/runtime/v2/app.go +++ b/runtime/v2/app.go @@ -46,9 +46,9 @@ type App[T transaction.Tx] struct { amino legacy.Amino moduleManager *MM[T] - // GRPCQueryDecoders maps gRPC method name to a function that decodes the request + // GRPCMethodsToMessageMap maps gRPC method name to a function that decodes the request // bytes into a gogoproto.Message, which then can be passed to appmanager. - GRPCQueryDecoders map[string]func(requestBytes []byte) (gogoproto.Message, error) + GRPCMethodsToMessageMap map[string]func() gogoproto.Message } // Name returns the app name. @@ -120,6 +120,6 @@ func (a *App[T]) GetAppManager() *appmanager.AppManager[T] { return a.AppManager } -func (a *App[T]) GetGRPCQueryDecoders() map[string]func(requestBytes []byte) (gogoproto.Message, error) { - return a.GRPCQueryDecoders +func (a *App[T]) GetGPRCMethodsToMessageMap() map[string]func() gogoproto.Message { + return a.GRPCMethodsToMessageMap } diff --git a/runtime/v2/manager.go b/runtime/v2/manager.go index c947e0b96581..fc7d9d783c8e 100644 --- a/runtime/v2/manager.go +++ b/runtime/v2/manager.go @@ -556,7 +556,7 @@ func (m *MM[T]) assertNoForgottenModules( func registerServices[T transaction.Tx](s appmodule.HasServices, app *App[T], registry *protoregistry.Files) error { c := &configurator{ - grpcQueryDecoders: map[string]func([]byte) (gogoproto.Message, error){}, + grpcQueryDecoders: map[string]func() gogoproto.Message{}, stfQueryRouter: app.queryRouterBuilder, stfMsgRouter: app.msgRouterBuilder, registry: registry, @@ -567,7 +567,7 @@ func registerServices[T transaction.Tx](s appmodule.HasServices, app *App[T], re if err != nil { return fmt.Errorf("unable to register services: %w", err) } - app.GRPCQueryDecoders = c.grpcQueryDecoders + app.GRPCMethodsToMessageMap = c.grpcQueryDecoders return nil } @@ -576,7 +576,7 @@ var _ grpc.ServiceRegistrar = (*configurator)(nil) type configurator struct { // grpcQueryDecoders is required because module expose queries through gRPC // this provides a way to route to modules using gRPC. - grpcQueryDecoders map[string]func([]byte) (gogoproto.Message, error) + grpcQueryDecoders map[string]func() gogoproto.Message stfQueryRouter *stf.MsgRouterBuilder stfMsgRouter *stf.MsgRouterBuilder @@ -618,9 +618,8 @@ func (c *configurator) registerQueryHandlers(sd *grpc.ServiceDesc, ss interface{ if typ == nil { return fmt.Errorf("unable to find message in gogotype registry: %w", err) } - decoderFunc := func(bytes []byte) (gogoproto.Message, error) { - msg := reflect.New(typ.Elem()).Interface().(gogoproto.Message) - return msg, gogoproto.Unmarshal(bytes, msg) + decoderFunc := func() gogoproto.Message { + return reflect.New(typ.Elem()).Interface().(gogoproto.Message) } c.grpcQueryDecoders[md.MethodName] = decoderFunc } diff --git a/server/v2/cometbft/abci.go b/server/v2/cometbft/abci.go index b0c1a8452a13..cb1ee9aed226 100644 --- a/server/v2/cometbft/abci.go +++ b/server/v2/cometbft/abci.go @@ -41,7 +41,6 @@ type Consensus[T transaction.Tx] struct { streaming streaming.Manager snapshotManager *snapshots.Manager mempool mempool.Mempool[T] - grpcQueryDecoders map[string]func(requestBytes []byte) (gogoproto.Message, error) // legacy support for gRPC cfg Config indexedEvents map[string]struct{} @@ -60,6 +59,8 @@ type Consensus[T transaction.Tx] struct { addrPeerFilter types.PeerFilter // filter peers by address and port idPeerFilter types.PeerFilter // filter peers by node ID + + grpcMethodsMap map[string]func() gogoproto.Message // maps gRPC method to message creator func } func NewConsensus[T transaction.Tx]( @@ -69,7 +70,7 @@ func NewConsensus[T transaction.Tx]( app *appmanager.AppManager[T], mp mempool.Mempool[T], indexedEvents map[string]struct{}, - grpcQueryDecoders map[string]func(requestBytes []byte) (gogoproto.Message, error), + gRPCMethodsMap map[string]func() gogoproto.Message, store types.Store, cfg Config, txCodec transaction.Codec[T], @@ -78,7 +79,7 @@ func NewConsensus[T transaction.Tx]( appName: appName, version: getCometBFTServerVersion(), consensusAuthority: consensusAuthority, - grpcQueryDecoders: grpcQueryDecoders, + grpcMethodsMap: gRPCMethodsMap, app: app, cfg: cfg, store: store, @@ -173,9 +174,10 @@ func (c *Consensus[T]) Info(ctx context.Context, _ *abciproto.InfoRequest) (*abc // It is called by cometbft to query application state. func (c *Consensus[T]) Query(ctx context.Context, req *abciproto.QueryRequest) (resp *abciproto.QueryResponse, err error) { // check if it's a gRPC method - grpcQueryDecoder, isGRPC := c.grpcQueryDecoders[req.Path] + makeGRPCRequest, isGRPC := c.grpcMethodsMap[req.Path] if isGRPC { - protoRequest, err := grpcQueryDecoder(req.Data) + protoRequest := makeGRPCRequest() + err = gogoproto.Unmarshal(req.Data, protoRequest) // TODO: use codec if err != nil { return nil, fmt.Errorf("unable to decode gRPC request with path %s from ABCI.Query: %w", req.Path, err) } diff --git a/server/v2/cometbft/server.go b/server/v2/cometbft/server.go index 1553a5c9a592..47c8b6bdfdac 100644 --- a/server/v2/cometbft/server.go +++ b/server/v2/cometbft/server.go @@ -79,7 +79,7 @@ func (s *CometBFTServer[T]) Init(appI serverv2.AppI[T], v *viper.Viper, logger l appI.GetAppManager(), s.serverOptions.Mempool, indexEvents, - appI.GetGRPCQueryDecoders(), + appI.GetGPRCMethodsToMessageMap(), appI.GetStore().(types.Store), s.config, s.initTxCodec, diff --git a/server/v2/types.go b/server/v2/types.go index fc6caaaeb735..978b46b78810 100644 --- a/server/v2/types.go +++ b/server/v2/types.go @@ -17,6 +17,6 @@ type AppI[T transaction.Tx] interface { InterfaceRegistry() coreapp.InterfaceRegistry GetAppManager() *appmanager.AppManager[T] GetConsensusAuthority() string - GetGRPCQueryDecoders() map[string]func(requestBytes []byte) (gogoproto.Message, error) + GetGPRCMethodsToMessageMap() map[string]func() gogoproto.Message GetStore() any } From 916206547194bac065d6b5ce342c44a0e059428a Mon Sep 17 00:00:00 2001 From: testinginprod Date: Tue, 23 Jul 2024 16:20:58 +0200 Subject: [PATCH 02/11] add make unknown service handler --- server/v2/api/grpc/server.go | 45 ++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/server/v2/api/grpc/server.go b/server/v2/api/grpc/server.go index c5e31f5dfc71..46cc7a3da362 100644 --- a/server/v2/api/grpc/server.go +++ b/server/v2/api/grpc/server.go @@ -2,11 +2,16 @@ package grpc import ( "context" + "errors" "fmt" + "io" "net" + "github.com/cosmos/gogoproto/proto" "github.com/spf13/viper" "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" "cosmossdk.io/core/transaction" "cosmossdk.io/log" @@ -43,10 +48,9 @@ func (s *GRPCServer[T]) Init(appI serverv2.AppI[T], v *viper.Viper, logger log.L grpc.ForceServerCodec(newProtoCodec(appI.InterfaceRegistry()).GRPCCodec()), grpc.MaxSendMsgSize(cfg.MaxSendMsgSize), grpc.MaxRecvMsgSize(cfg.MaxRecvMsgSize), + grpc.UnknownServiceHandler(makeUnknownServiceHandler(appI.GetGPRCMethodsToMessageMap(), appI.GetAppManager())), ) - // appI.RegisterGRPCServer(grpcSrv) - // Reflection allows external clients to see what services and methods the gRPC server exposes. gogoreflection.Register(grpcSrv) @@ -57,6 +61,43 @@ func (s *GRPCServer[T]) Init(appI serverv2.AppI[T], v *viper.Viper, logger log.L return nil } +func makeUnknownServiceHandler(messageMap map[string]func() proto.Message, querier interface { + Query(ctx context.Context, version uint64, msg proto.Message) (proto.Message, error) +}) grpc.StreamHandler { + return func(srv any, stream grpc.ServerStream) error { + method, ok := grpc.MethodFromServerStream(stream) + if !ok { + return status.Error(codes.InvalidArgument, "unable to get method") + } + makeMsg, exists := messageMap[method] + if !exists { + return status.Errorf(codes.Unimplemented, "gRPC method %s is not handled", method) + } + for { + req := makeMsg() + err := stream.RecvMsg(req) + if err != nil { + if errors.Is(err, io.EOF) { + return nil + } + return err + } + + // extract height header + height := uint64(0) + + resp, err := querier.Query(stream.Context(), height, req) + if err != nil { + return err + } + err = stream.SendMsg(resp) + if err != nil { + return err + } + } + } +} + func (s *GRPCServer[T]) Name() string { return "grpc" } From cdbd164e078593040adbe5b15a3a330972b47a9c Mon Sep 17 00:00:00 2001 From: testinginprod Date: Tue, 23 Jul 2024 16:56:33 +0200 Subject: [PATCH 03/11] refactors left and right --- server/v2/api/grpc/server.go | 51 ++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/server/v2/api/grpc/server.go b/server/v2/api/grpc/server.go index 46cc7a3da362..466a34312bf7 100644 --- a/server/v2/api/grpc/server.go +++ b/server/v2/api/grpc/server.go @@ -6,11 +6,13 @@ import ( "fmt" "io" "net" + "strconv" "github.com/cosmos/gogoproto/proto" "github.com/spf13/viper" "google.golang.org/grpc" "google.golang.org/grpc/codes" + "google.golang.org/grpc/metadata" "google.golang.org/grpc/status" "cosmossdk.io/core/transaction" @@ -19,7 +21,9 @@ import ( "cosmossdk.io/server/v2/api/grpc/gogoreflection" ) -type GRPCServer[T transaction.Tx] struct { +const BlockHeightHeader = "x-cosmos-block-height" + +type Server[T transaction.Tx] struct { logger log.Logger config *Config cfgOptions []CfgOption @@ -28,15 +32,15 @@ type GRPCServer[T transaction.Tx] struct { } // New creates a new grpc server. -func New[T transaction.Tx](cfgOptions ...CfgOption) *GRPCServer[T] { - return &GRPCServer[T]{ +func New[T transaction.Tx](cfgOptions ...CfgOption) *Server[T] { + return &Server[T]{ cfgOptions: cfgOptions, } } // Init returns a correctly configured and initialized gRPC server. // Note, the caller is responsible for starting the server. -func (s *GRPCServer[T]) Init(appI serverv2.AppI[T], v *viper.Viper, logger log.Logger) error { +func (s *Server[T]) Init(appI serverv2.AppI[T], v *viper.Viper, logger log.Logger) error { cfg := s.Config().(*Config) if v != nil { if err := serverv2.UnmarshalSubConfig(v, s.Name(), &cfg); err != nil { @@ -84,9 +88,12 @@ func makeUnknownServiceHandler(messageMap map[string]func() proto.Message, queri } // extract height header - height := uint64(0) - - resp, err := querier.Query(stream.Context(), height, req) + ctx := stream.Context() + height, err := getHeightFromCtx(ctx) + if err != nil { + return status.Errorf(codes.InvalidArgument, "invalid get height from context: %v", err) + } + resp, err := querier.Query(ctx, height, req) if err != nil { return err } @@ -98,11 +105,33 @@ func makeUnknownServiceHandler(messageMap map[string]func() proto.Message, queri } } -func (s *GRPCServer[T]) Name() string { +func getHeightFromCtx(ctx context.Context) (uint64, error) { + md, ok := metadata.FromIncomingContext(ctx) + if !ok { + return 0, nil + } + values := md.Get(BlockHeightHeader) + if len(values) == 0 { + return 0, nil + } + if len(values) != 1 { + return 0, fmt.Errorf("gRPC height metadata must be of lenght 1, got: %d", len(values)) + } + + heightStr := values[0] + height, err := strconv.ParseUint(heightStr, 10, 64) + if err != nil { + return 0, fmt.Errorf("unable to parse height string from gRPC metadata %s: %v", heightStr, err) + } + + return height, nil +} + +func (s *Server[T]) Name() string { return "grpc" } -func (s *GRPCServer[T]) Config() any { +func (s *Server[T]) Config() any { if s.config == nil || s.config == (&Config{}) { cfg := DefaultConfig() // overwrite the default config with the provided options @@ -116,7 +145,7 @@ func (s *GRPCServer[T]) Config() any { return s.config } -func (s *GRPCServer[T]) Start(ctx context.Context) error { +func (s *Server[T]) Start(ctx context.Context) error { if !s.config.Enable { return nil } @@ -143,7 +172,7 @@ func (s *GRPCServer[T]) Start(ctx context.Context) error { return err } -func (s *GRPCServer[T]) Stop(ctx context.Context) error { +func (s *Server[T]) Stop(ctx context.Context) error { if !s.config.Enable { return nil } From 8ab8b26ad90cd0e46a046d0d7a3c5309962624b4 Mon Sep 17 00:00:00 2001 From: testinginprod Date: Tue, 23 Jul 2024 16:56:33 +0200 Subject: [PATCH 04/11] add height header handling --- server/v2/api/grpc/server.go | 51 ++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/server/v2/api/grpc/server.go b/server/v2/api/grpc/server.go index 46cc7a3da362..466a34312bf7 100644 --- a/server/v2/api/grpc/server.go +++ b/server/v2/api/grpc/server.go @@ -6,11 +6,13 @@ import ( "fmt" "io" "net" + "strconv" "github.com/cosmos/gogoproto/proto" "github.com/spf13/viper" "google.golang.org/grpc" "google.golang.org/grpc/codes" + "google.golang.org/grpc/metadata" "google.golang.org/grpc/status" "cosmossdk.io/core/transaction" @@ -19,7 +21,9 @@ import ( "cosmossdk.io/server/v2/api/grpc/gogoreflection" ) -type GRPCServer[T transaction.Tx] struct { +const BlockHeightHeader = "x-cosmos-block-height" + +type Server[T transaction.Tx] struct { logger log.Logger config *Config cfgOptions []CfgOption @@ -28,15 +32,15 @@ type GRPCServer[T transaction.Tx] struct { } // New creates a new grpc server. -func New[T transaction.Tx](cfgOptions ...CfgOption) *GRPCServer[T] { - return &GRPCServer[T]{ +func New[T transaction.Tx](cfgOptions ...CfgOption) *Server[T] { + return &Server[T]{ cfgOptions: cfgOptions, } } // Init returns a correctly configured and initialized gRPC server. // Note, the caller is responsible for starting the server. -func (s *GRPCServer[T]) Init(appI serverv2.AppI[T], v *viper.Viper, logger log.Logger) error { +func (s *Server[T]) Init(appI serverv2.AppI[T], v *viper.Viper, logger log.Logger) error { cfg := s.Config().(*Config) if v != nil { if err := serverv2.UnmarshalSubConfig(v, s.Name(), &cfg); err != nil { @@ -84,9 +88,12 @@ func makeUnknownServiceHandler(messageMap map[string]func() proto.Message, queri } // extract height header - height := uint64(0) - - resp, err := querier.Query(stream.Context(), height, req) + ctx := stream.Context() + height, err := getHeightFromCtx(ctx) + if err != nil { + return status.Errorf(codes.InvalidArgument, "invalid get height from context: %v", err) + } + resp, err := querier.Query(ctx, height, req) if err != nil { return err } @@ -98,11 +105,33 @@ func makeUnknownServiceHandler(messageMap map[string]func() proto.Message, queri } } -func (s *GRPCServer[T]) Name() string { +func getHeightFromCtx(ctx context.Context) (uint64, error) { + md, ok := metadata.FromIncomingContext(ctx) + if !ok { + return 0, nil + } + values := md.Get(BlockHeightHeader) + if len(values) == 0 { + return 0, nil + } + if len(values) != 1 { + return 0, fmt.Errorf("gRPC height metadata must be of lenght 1, got: %d", len(values)) + } + + heightStr := values[0] + height, err := strconv.ParseUint(heightStr, 10, 64) + if err != nil { + return 0, fmt.Errorf("unable to parse height string from gRPC metadata %s: %v", heightStr, err) + } + + return height, nil +} + +func (s *Server[T]) Name() string { return "grpc" } -func (s *GRPCServer[T]) Config() any { +func (s *Server[T]) Config() any { if s.config == nil || s.config == (&Config{}) { cfg := DefaultConfig() // overwrite the default config with the provided options @@ -116,7 +145,7 @@ func (s *GRPCServer[T]) Config() any { return s.config } -func (s *GRPCServer[T]) Start(ctx context.Context) error { +func (s *Server[T]) Start(ctx context.Context) error { if !s.config.Enable { return nil } @@ -143,7 +172,7 @@ func (s *GRPCServer[T]) Start(ctx context.Context) error { return err } -func (s *GRPCServer[T]) Stop(ctx context.Context) error { +func (s *Server[T]) Stop(ctx context.Context) error { if !s.config.Enable { return nil } From 7b3e05174f767c6499c61aa24d2dff6f2f8701fd Mon Sep 17 00:00:00 2001 From: testinginprod Date: Thu, 25 Jul 2024 14:09:33 +0200 Subject: [PATCH 05/11] gRPC finally works --- runtime/v2/manager.go | 8 ++- runtime/v2/module.go | 11 +-- .../grpc/gogoreflection/serverreflection.go | 68 +++++++++++++------ server/v2/api/grpc/server.go | 28 +++++++- 4 files changed, 83 insertions(+), 32 deletions(-) diff --git a/runtime/v2/manager.go b/runtime/v2/manager.go index fc7d9d783c8e..9d69c6744156 100644 --- a/runtime/v2/manager.go +++ b/runtime/v2/manager.go @@ -567,7 +567,10 @@ func registerServices[T transaction.Tx](s appmodule.HasServices, app *App[T], re if err != nil { return fmt.Errorf("unable to register services: %w", err) } - app.GRPCMethodsToMessageMap = c.grpcQueryDecoders + // merge maps + for path, decoder := range c.grpcQueryDecoders { + app.GRPCMethodsToMessageMap[path] = decoder + } return nil } @@ -621,7 +624,8 @@ func (c *configurator) registerQueryHandlers(sd *grpc.ServiceDesc, ss interface{ decoderFunc := func() gogoproto.Message { return reflect.New(typ.Elem()).Interface().(gogoproto.Message) } - c.grpcQueryDecoders[md.MethodName] = decoderFunc + methodName := fmt.Sprintf("/%s/%s", sd.ServiceName, md.MethodName) + c.grpcQueryDecoders[methodName] = decoderFunc } return nil } diff --git a/runtime/v2/module.go b/runtime/v2/module.go index ffc5632f9403..eb294f65d88f 100644 --- a/runtime/v2/module.go +++ b/runtime/v2/module.go @@ -130,11 +130,12 @@ func ProvideAppBuilder[T transaction.Tx]( msgRouterBuilder := stf.NewMsgRouterBuilder() app := &App[T]{ - storeKeys: nil, - interfaceRegistrar: interfaceRegistrar, - amino: amino, - msgRouterBuilder: msgRouterBuilder, - queryRouterBuilder: stf.NewMsgRouterBuilder(), // TODO dedicated query router + storeKeys: nil, + interfaceRegistrar: interfaceRegistrar, + amino: amino, + msgRouterBuilder: msgRouterBuilder, + queryRouterBuilder: stf.NewMsgRouterBuilder(), // TODO dedicated query router + GRPCMethodsToMessageMap: map[string]func() proto.Message{}, } appBuilder := &AppBuilder[T]{app: app} diff --git a/server/v2/api/grpc/gogoreflection/serverreflection.go b/server/v2/api/grpc/gogoreflection/serverreflection.go index 077c15c3321a..ac24d50a45a5 100644 --- a/server/v2/api/grpc/gogoreflection/serverreflection.go +++ b/server/v2/api/grpc/gogoreflection/serverreflection.go @@ -42,13 +42,17 @@ import ( "errors" "fmt" "io" - "log" "reflect" "sort" + "strings" "sync" - //nolint: staticcheck // keep this import for backward compatibility - "github.com/golang/protobuf/proto" + "cosmossdk.io/core/log" + gogoproto "github.com/cosmos/gogoproto/proto" + "google.golang.org/protobuf/reflect/protodesc" + "google.golang.org/protobuf/reflect/protoreflect" + + // nolint: staticcheck // keep this import for backward compatibility dpb "github.com/golang/protobuf/protoc-gen-go/descriptor" "google.golang.org/grpc" "google.golang.org/grpc/codes" @@ -60,15 +64,20 @@ type serverReflectionServer struct { rpb.UnimplementedServerReflectionServer s *grpc.Server + methods []string + initSymbols sync.Once serviceNames []string symbols map[string]*dpb.FileDescriptorProto // map of fully-qualified names to files + log log.Logger } // Register registers the server reflection service on the given gRPC server. -func Register(s *grpc.Server) { +func Register(s *grpc.Server, methods []string, logger log.Logger) { rpb.RegisterServerReflectionServer(s, &serverReflectionServer{ - s: s, + s: s, + methods: methods, + log: logger, }) } @@ -82,21 +91,13 @@ type protoMessage interface { func (s *serverReflectionServer) getSymbols() (svcNames []string, symbolIndex map[string]*dpb.FileDescriptorProto) { s.initSymbols.Do(func() { - serviceInfo := s.s.GetServiceInfo() s.symbols = map[string]*dpb.FileDescriptorProto{} - s.serviceNames = make([]string, 0, len(serviceInfo)) + services, fds := s.getServices(s.methods) + s.serviceNames = services + processed := map[string]struct{}{} - for svc, info := range serviceInfo { - s.serviceNames = append(s.serviceNames, svc) - fdenc, ok := parseMetadata(info.Metadata) - if !ok { - continue - } - fd, err := decodeFileDesc(fdenc) - if err != nil { - continue - } + for _, fd := range fds { s.processFile(fd, processed) } sort.Strings(s.serviceNames) @@ -207,7 +208,7 @@ func decodeFileDesc(enc []byte) (*dpb.FileDescriptorProto, error) { } fd := new(dpb.FileDescriptorProto) - if err := proto.Unmarshal(raw, fd); err != nil { + if err := gogoproto.Unmarshal(raw, fd); err != nil { return nil, fmt.Errorf("bad descriptor: %w", err) } return fd, nil @@ -237,7 +238,7 @@ func typeForName(name string) (reflect.Type, error) { } func fileDescContainingExtension(st reflect.Type, ext int32) (*dpb.FileDescriptorProto, error) { - m, ok := reflect.Zero(reflect.PtrTo(st)).Interface().(proto.Message) + m, ok := reflect.Zero(reflect.PtrTo(st)).Interface().(gogoproto.Message) if !ok { return nil, fmt.Errorf("failed to create message from type: %v", st) } @@ -252,7 +253,7 @@ func fileDescContainingExtension(st reflect.Type, ext int32) (*dpb.FileDescripto } func (s *serverReflectionServer) allExtensionNumbersForType(st reflect.Type) ([]int32, error) { - m, ok := reflect.Zero(reflect.PtrTo(st)).Interface().(proto.Message) + m, ok := reflect.Zero(reflect.PtrTo(st)).Interface().(gogoproto.Message) if !ok { return nil, fmt.Errorf("failed to create message from type: %v", st) } @@ -272,7 +273,7 @@ func fileDescWithDependencies(fd *dpb.FileDescriptorProto, sentFileDescriptors m queue = queue[1:] if sent := sentFileDescriptors[currentfd.GetName()]; len(r) == 0 || !sent { sentFileDescriptors[currentfd.GetName()] = true - currentfdEncoded, err := proto.Marshal(currentfd) + currentfdEncoded, err := gogoproto.Marshal(currentfd) if err != nil { return nil, err } @@ -446,7 +447,6 @@ func (s *serverReflectionServer) ServerReflectionInfo(stream rpb.ServerReflectio ErrorMessage: err.Error(), }, } - log.Printf("OH NO: %s", err) } else { out.MessageResponse = &rpb.ServerReflectionResponse_AllExtensionNumbersResponse{ AllExtensionNumbersResponse: &rpb.ExtensionNumberResponse{ //nolint:staticcheck // SA1019: we want to keep using v1alpha @@ -476,3 +476,27 @@ func (s *serverReflectionServer) ServerReflectionInfo(stream rpb.ServerReflectio } } } + +func (s *serverReflectionServer) getServices(methods []string) (svcs []string, fds []*dpb.FileDescriptorProto) { + registry, err := gogoproto.MergedRegistry() + if err != nil { + s.log.Error("unable to load merged registry", "err", err) + return nil, nil + } + seenSvc := map[protoreflect.FullName]struct{}{} + for _, methodName := range methods { + methodName = strings.Join(strings.Split(methodName[1:], "/"), ".") + md, err := registry.FindDescriptorByName(protoreflect.FullName(methodName)) + if err != nil { + s.log.Error("unable to load method descriptor", "method", methodName, "err", err) + continue + } + svc := md.(protoreflect.MethodDescriptor).Parent() + if _, seen := seenSvc[svc.FullName()]; !seen { + svcs = append(svcs, string(svc.FullName())) + file := svc.ParentFile() + fds = append(fds, protodesc.ToFileDescriptorProto(file)) + } + } + return +} diff --git a/server/v2/api/grpc/server.go b/server/v2/api/grpc/server.go index 466a34312bf7..254d5c543887 100644 --- a/server/v2/api/grpc/server.go +++ b/server/v2/api/grpc/server.go @@ -9,7 +9,9 @@ import ( "strconv" "github.com/cosmos/gogoproto/proto" + "github.com/spf13/pflag" "github.com/spf13/viper" + "golang.org/x/exp/maps" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/metadata" @@ -21,7 +23,10 @@ import ( "cosmossdk.io/server/v2/api/grpc/gogoreflection" ) -const BlockHeightHeader = "x-cosmos-block-height" +const ( + BlockHeightHeader = "x-cosmos-block-height" + FlagAddress = "address" +) type Server[T transaction.Tx] struct { logger log.Logger @@ -47,16 +52,19 @@ func (s *Server[T]) Init(appI serverv2.AppI[T], v *viper.Viper, logger log.Logge return fmt.Errorf("failed to unmarshal config: %w", err) } } + methodsMap := appI.GetGPRCMethodsToMessageMap() grpcSrv := grpc.NewServer( grpc.ForceServerCodec(newProtoCodec(appI.InterfaceRegistry()).GRPCCodec()), grpc.MaxSendMsgSize(cfg.MaxSendMsgSize), grpc.MaxRecvMsgSize(cfg.MaxRecvMsgSize), - grpc.UnknownServiceHandler(makeUnknownServiceHandler(appI.GetGPRCMethodsToMessageMap(), appI.GetAppManager())), + grpc.UnknownServiceHandler( + makeUnknownServiceHandler(methodsMap, appI.GetAppManager()), + ), ) // Reflection allows external clients to see what services and methods the gRPC server exposes. - gogoreflection.Register(grpcSrv) + gogoreflection.Register(grpcSrv, maps.Keys(methodsMap), logger.With("sub-module", "grpc-reflection")) s.grpcSrv = grpcSrv s.config = cfg @@ -65,6 +73,20 @@ func (s *Server[T]) Init(appI serverv2.AppI[T], v *viper.Viper, logger log.Logge return nil } +func (s *Server[T]) StartCmdFlags() *pflag.FlagSet { + flags := pflag.NewFlagSet("grpc", pflag.ExitOnError) + + // start flags are prefixed with the server name + // as the config in prefixed with the server name + // this allows viper to properly bind the flags + prefix := func(f string) string { + return fmt.Sprintf("%s.%s", s.Name(), f) + } + + flags.String(prefix(FlagAddress), "localhost:9090", "Listen address") + return flags +} + func makeUnknownServiceHandler(messageMap map[string]func() proto.Message, querier interface { Query(ctx context.Context, version uint64, msg proto.Message) (proto.Message, error) }) grpc.StreamHandler { From 422ea8dcb5197d71a05150db19662026909a9716 Mon Sep 17 00:00:00 2001 From: testinginprod Date: Mon, 29 Jul 2024 11:29:38 +0200 Subject: [PATCH 06/11] go mod tidy all --- runtime/v2/go.mod | 7 +++++++ runtime/v2/go.sum | 19 +++++++++++++++++++ server/v2/go.mod | 2 +- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/runtime/v2/go.mod b/runtime/v2/go.mod index 205e0e19748f..15e9f8c528f4 100644 --- a/runtime/v2/go.mod +++ b/runtime/v2/go.mod @@ -30,6 +30,7 @@ require ( cosmossdk.io/server/v2/stf v0.0.0-00010101000000-000000000000 cosmossdk.io/store/v2 v2.0.0-00010101000000-000000000000 cosmossdk.io/x/tx v0.13.3 + github.com/cometbft/cometbft v0.38.10 github.com/cosmos/gogoproto v1.5.0 github.com/spf13/viper v1.19.0 golang.org/x/exp v0.0.0-20240531132922-fd00a4e0eefc @@ -44,6 +45,7 @@ require ( cosmossdk.io/errors v1.0.1 // indirect github.com/DataDog/zstd v1.5.5 // indirect github.com/beorn7/perks v1.0.1 // indirect + github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/cockroachdb/errors v1.11.1 // indirect github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect @@ -55,10 +57,12 @@ require ( github.com/cosmos/iavl v1.2.1-0.20240725141113-7adc688cf179 // indirect github.com/cosmos/ics23/go v0.10.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect + github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect github.com/emicklei/dot v1.6.2 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/getsentry/sentry-go v0.27.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect + github.com/golang/protobuf v1.5.4 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/google/btree v1.1.2 // indirect github.com/google/go-cmp v0.6.0 // indirect @@ -76,8 +80,10 @@ require ( github.com/mattn/go-sqlite3 v1.14.22 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect + github.com/oasisprotocol/curve25519-voi v0.0.0-20220708102147-0a8a51822cae // indirect github.com/onsi/gomega v1.28.1 // indirect github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/prometheus/client_golang v1.19.1 // indirect @@ -88,6 +94,7 @@ require ( github.com/rs/zerolog v1.33.0 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect + github.com/sasha-s/go-deadlock v0.3.1 // indirect github.com/sourcegraph/conc v0.3.0 // indirect github.com/spf13/afero v1.11.0 // indirect github.com/spf13/cast v1.6.0 // indirect diff --git a/runtime/v2/go.sum b/runtime/v2/go.sum index 4a496ee484f8..fbff9632b90b 100644 --- a/runtime/v2/go.sum +++ b/runtime/v2/go.sum @@ -19,6 +19,12 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24 github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= +github.com/btcsuite/btcd/btcec/v2 v2.3.2 h1:5n0X6hX0Zk+6omWcihdYvdAlGf2DfasC0GMf7DClJ3U= +github.com/btcsuite/btcd/btcec/v2 v2.3.2/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04= +github.com/btcsuite/btcd/btcutil v1.1.3 h1:xfbtw8lwpp0G6NwSHb+UE67ryTFHJAiNuipusjXSohQ= +github.com/btcsuite/btcd/btcutil v1.1.3/go.mod h1:UR7dsSJzJUfMmFiiLlIrMq1lS9jh9EdCV7FStZSnpi0= +github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U= +github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= @@ -39,6 +45,8 @@ github.com/cockroachdb/redact v1.1.5 h1:u1PMllDkdFfPWaNGMyLD1+so+aq3uUItthCFqzwP github.com/cockroachdb/redact v1.1.5/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZZ2lK+dpvRg= github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 h1:zuQyyAKVxetITBuuhv3BI9cMrmStnpT18zmgmTxunpo= github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06/go.mod h1:7nc4anLGjupUW/PeY5qiNYsdNXj7zopG+eqsS7To5IQ= +github.com/cometbft/cometbft v0.38.10 h1:2ePuglchT+j0Iao+cfmt/nw5U7K2lnGDzXSUPGVdXaU= +github.com/cometbft/cometbft v0.38.10/go.mod h1:jHPx9vQpWzPHEAiYI/7EDKaB1NXhK6o3SArrrY8ExKc= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/cosmos/cosmos-db v1.0.2 h1:hwMjozuY1OlJs/uh6vddqnk9j7VamLv+0DBlbEXbAKs= github.com/cosmos/cosmos-db v1.0.2/go.mod h1:Z8IXcFJ9PqKK6BIsVOB3QXtkKoqUOp1vRvPT39kOXEA= @@ -55,6 +63,10 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/decred/dcrd/crypto/blake256 v1.0.0 h1:/8DMNYp9SGi5f0w7uCm6d6M4OU2rGFK09Y2A4Xv7EE0= +github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc= +github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1m5sE92cU+pd5Mcc= +github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs= github.com/emicklei/dot v1.6.2 h1:08GN+DD79cy/tzN6uLCT84+2Wk9u+wvqP+Hkx/dIR8A= github.com/emicklei/dot v1.6.2/go.mod h1:DeV7GvQtIw4h2u73RKBkkFdvVAz0D9fzeJrgPW6gy/s= github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= @@ -168,6 +180,8 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= +github.com/oasisprotocol/curve25519-voi v0.0.0-20220708102147-0a8a51822cae h1:FatpGJD2jmJfhZiFDElaC0QhZUDQnxUeAwTGkfAHN3I= +github.com/oasisprotocol/curve25519-voi v0.0.0-20220708102147-0a8a51822cae/go.mod h1:hVoHR2EVESiICEMbg137etN/Lx+lSrHPTD39Z/uE+2s= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0= @@ -184,6 +198,8 @@ github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0Mw github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 h1:q2e307iGHPdTGp0hoxKjt1H5pDo6utceo3dQVK3I5XQ= +github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5/go.mod h1:jvVRKCrJTQWu0XVbaOlby/2lO20uSCHEMzzplHXte1o= github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4= github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= @@ -223,6 +239,8 @@ github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6ke github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= +github.com/sasha-s/go-deadlock v0.3.1 h1:sqv7fDNShgjcaxkO0JNcOAlr8B9+cV5Ey/OB71efZx0= +github.com/sasha-s/go-deadlock v0.3.1/go.mod h1:F73l+cr82YSh10GxyRI6qZiCgK64VaZjwesgfQ1/iLM= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= @@ -374,6 +392,7 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU= gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU= sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E= diff --git a/server/v2/go.mod b/server/v2/go.mod index aaf1411f94c2..8f91d1e7301c 100644 --- a/server/v2/go.mod +++ b/server/v2/go.mod @@ -38,6 +38,7 @@ require ( github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.19.0 github.com/stretchr/testify v1.9.0 + golang.org/x/exp v0.0.0-20240531132922-fd00a4e0eefc golang.org/x/sync v0.7.0 google.golang.org/grpc v1.64.1 google.golang.org/protobuf v1.34.2 @@ -99,7 +100,6 @@ require ( github.com/tidwall/btree v1.7.0 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/crypto v0.25.0 // indirect - golang.org/x/exp v0.0.0-20240531132922-fd00a4e0eefc // indirect golang.org/x/net v0.27.0 // indirect golang.org/x/sys v0.22.0 // indirect golang.org/x/text v0.16.0 // indirect From 9308ae777732be06cf5d78901833312c8b3ec4c6 Mon Sep 17 00:00:00 2001 From: testinginprod Date: Mon, 29 Jul 2024 13:58:31 +0200 Subject: [PATCH 07/11] lint --- server/v2/api/grpc/gogoreflection/serverreflection.go | 9 ++++----- server/v2/api/grpc/server.go | 5 +++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/server/v2/api/grpc/gogoreflection/serverreflection.go b/server/v2/api/grpc/gogoreflection/serverreflection.go index ac24d50a45a5..037bfd155c50 100644 --- a/server/v2/api/grpc/gogoreflection/serverreflection.go +++ b/server/v2/api/grpc/gogoreflection/serverreflection.go @@ -47,17 +47,17 @@ import ( "strings" "sync" - "cosmossdk.io/core/log" gogoproto "github.com/cosmos/gogoproto/proto" - "google.golang.org/protobuf/reflect/protodesc" - "google.golang.org/protobuf/reflect/protoreflect" - // nolint: staticcheck // keep this import for backward compatibility dpb "github.com/golang/protobuf/protoc-gen-go/descriptor" "google.golang.org/grpc" "google.golang.org/grpc/codes" rpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha" "google.golang.org/grpc/status" + "google.golang.org/protobuf/reflect/protodesc" + "google.golang.org/protobuf/reflect/protoreflect" + + "cosmossdk.io/core/log" ) type serverReflectionServer struct { @@ -91,7 +91,6 @@ type protoMessage interface { func (s *serverReflectionServer) getSymbols() (svcNames []string, symbolIndex map[string]*dpb.FileDescriptorProto) { s.initSymbols.Do(func() { - s.symbols = map[string]*dpb.FileDescriptorProto{} services, fds := s.getServices(s.methods) s.serviceNames = services diff --git a/server/v2/api/grpc/server.go b/server/v2/api/grpc/server.go index 254d5c543887..a838923dd3ae 100644 --- a/server/v2/api/grpc/server.go +++ b/server/v2/api/grpc/server.go @@ -89,7 +89,8 @@ func (s *Server[T]) StartCmdFlags() *pflag.FlagSet { func makeUnknownServiceHandler(messageMap map[string]func() proto.Message, querier interface { Query(ctx context.Context, version uint64, msg proto.Message) (proto.Message, error) -}) grpc.StreamHandler { +}, +) grpc.StreamHandler { return func(srv any, stream grpc.ServerStream) error { method, ok := grpc.MethodFromServerStream(stream) if !ok { @@ -137,7 +138,7 @@ func getHeightFromCtx(ctx context.Context) (uint64, error) { return 0, nil } if len(values) != 1 { - return 0, fmt.Errorf("gRPC height metadata must be of lenght 1, got: %d", len(values)) + return 0, fmt.Errorf("gRPC height metadata must be of length 1, got: %d", len(values)) } heightStr := values[0] From 1e612d13f439ab31455d9ee9aae3776605605636 Mon Sep 17 00:00:00 2001 From: testinginprod Date: Mon, 29 Jul 2024 14:13:36 +0200 Subject: [PATCH 08/11] fix more tests --- server/v2/server_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/server/v2/server_test.go b/server/v2/server_test.go index 3faef417757b..a369b26e18f1 100644 --- a/server/v2/server_test.go +++ b/server/v2/server_test.go @@ -7,6 +7,7 @@ import ( "testing" "time" + "cosmossdk.io/server/v2/appmanager" gogoproto "github.com/cosmos/gogoproto/proto" "github.com/spf13/viper" "github.com/stretchr/testify/require" @@ -33,6 +34,14 @@ type mockApp[T transaction.Tx] struct { serverv2.AppI[T] } +func (*mockApp[T]) GetGPRCMethodsToMessageMap() map[string]func() gogoproto.Message { + return map[string]func() gogoproto.Message{} +} + +func (*mockApp[T]) GetAppManager() *appmanager.AppManager[T] { + return nil +} + func (*mockApp[T]) InterfaceRegistry() coreapp.InterfaceRegistry { return &mockInterfaceRegistry{} } From 9839aeaba54b8ff8789f2cbd3240d0e088b7ae76 Mon Sep 17 00:00:00 2001 From: testinginprod Date: Mon, 29 Jul 2024 14:17:27 +0200 Subject: [PATCH 09/11] go mod tidy all --- runtime/v2/go.mod | 7 ------- runtime/v2/go.sum | 19 ------------------- 2 files changed, 26 deletions(-) diff --git a/runtime/v2/go.mod b/runtime/v2/go.mod index 15e9f8c528f4..205e0e19748f 100644 --- a/runtime/v2/go.mod +++ b/runtime/v2/go.mod @@ -30,7 +30,6 @@ require ( cosmossdk.io/server/v2/stf v0.0.0-00010101000000-000000000000 cosmossdk.io/store/v2 v2.0.0-00010101000000-000000000000 cosmossdk.io/x/tx v0.13.3 - github.com/cometbft/cometbft v0.38.10 github.com/cosmos/gogoproto v1.5.0 github.com/spf13/viper v1.19.0 golang.org/x/exp v0.0.0-20240531132922-fd00a4e0eefc @@ -45,7 +44,6 @@ require ( cosmossdk.io/errors v1.0.1 // indirect github.com/DataDog/zstd v1.5.5 // indirect github.com/beorn7/perks v1.0.1 // indirect - github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/cockroachdb/errors v1.11.1 // indirect github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect @@ -57,12 +55,10 @@ require ( github.com/cosmos/iavl v1.2.1-0.20240725141113-7adc688cf179 // indirect github.com/cosmos/ics23/go v0.10.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect - github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect github.com/emicklei/dot v1.6.2 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/getsentry/sentry-go v0.27.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect - github.com/golang/protobuf v1.5.4 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/google/btree v1.1.2 // indirect github.com/google/go-cmp v0.6.0 // indirect @@ -80,10 +76,8 @@ require ( github.com/mattn/go-sqlite3 v1.14.22 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect - github.com/oasisprotocol/curve25519-voi v0.0.0-20220708102147-0a8a51822cae // indirect github.com/onsi/gomega v1.28.1 // indirect github.com/pelletier/go-toml/v2 v2.2.2 // indirect - github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/prometheus/client_golang v1.19.1 // indirect @@ -94,7 +88,6 @@ require ( github.com/rs/zerolog v1.33.0 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect - github.com/sasha-s/go-deadlock v0.3.1 // indirect github.com/sourcegraph/conc v0.3.0 // indirect github.com/spf13/afero v1.11.0 // indirect github.com/spf13/cast v1.6.0 // indirect diff --git a/runtime/v2/go.sum b/runtime/v2/go.sum index fbff9632b90b..4a496ee484f8 100644 --- a/runtime/v2/go.sum +++ b/runtime/v2/go.sum @@ -19,12 +19,6 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24 github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= -github.com/btcsuite/btcd/btcec/v2 v2.3.2 h1:5n0X6hX0Zk+6omWcihdYvdAlGf2DfasC0GMf7DClJ3U= -github.com/btcsuite/btcd/btcec/v2 v2.3.2/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04= -github.com/btcsuite/btcd/btcutil v1.1.3 h1:xfbtw8lwpp0G6NwSHb+UE67ryTFHJAiNuipusjXSohQ= -github.com/btcsuite/btcd/btcutil v1.1.3/go.mod h1:UR7dsSJzJUfMmFiiLlIrMq1lS9jh9EdCV7FStZSnpi0= -github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U= -github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= @@ -45,8 +39,6 @@ github.com/cockroachdb/redact v1.1.5 h1:u1PMllDkdFfPWaNGMyLD1+so+aq3uUItthCFqzwP github.com/cockroachdb/redact v1.1.5/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZZ2lK+dpvRg= github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 h1:zuQyyAKVxetITBuuhv3BI9cMrmStnpT18zmgmTxunpo= github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06/go.mod h1:7nc4anLGjupUW/PeY5qiNYsdNXj7zopG+eqsS7To5IQ= -github.com/cometbft/cometbft v0.38.10 h1:2ePuglchT+j0Iao+cfmt/nw5U7K2lnGDzXSUPGVdXaU= -github.com/cometbft/cometbft v0.38.10/go.mod h1:jHPx9vQpWzPHEAiYI/7EDKaB1NXhK6o3SArrrY8ExKc= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/cosmos/cosmos-db v1.0.2 h1:hwMjozuY1OlJs/uh6vddqnk9j7VamLv+0DBlbEXbAKs= github.com/cosmos/cosmos-db v1.0.2/go.mod h1:Z8IXcFJ9PqKK6BIsVOB3QXtkKoqUOp1vRvPT39kOXEA= @@ -63,10 +55,6 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/decred/dcrd/crypto/blake256 v1.0.0 h1:/8DMNYp9SGi5f0w7uCm6d6M4OU2rGFK09Y2A4Xv7EE0= -github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc= -github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1m5sE92cU+pd5Mcc= -github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs= github.com/emicklei/dot v1.6.2 h1:08GN+DD79cy/tzN6uLCT84+2Wk9u+wvqP+Hkx/dIR8A= github.com/emicklei/dot v1.6.2/go.mod h1:DeV7GvQtIw4h2u73RKBkkFdvVAz0D9fzeJrgPW6gy/s= github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= @@ -180,8 +168,6 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= -github.com/oasisprotocol/curve25519-voi v0.0.0-20220708102147-0a8a51822cae h1:FatpGJD2jmJfhZiFDElaC0QhZUDQnxUeAwTGkfAHN3I= -github.com/oasisprotocol/curve25519-voi v0.0.0-20220708102147-0a8a51822cae/go.mod h1:hVoHR2EVESiICEMbg137etN/Lx+lSrHPTD39Z/uE+2s= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0= @@ -198,8 +184,6 @@ github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0Mw github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= -github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 h1:q2e307iGHPdTGp0hoxKjt1H5pDo6utceo3dQVK3I5XQ= -github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5/go.mod h1:jvVRKCrJTQWu0XVbaOlby/2lO20uSCHEMzzplHXte1o= github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4= github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= @@ -239,8 +223,6 @@ github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6ke github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= -github.com/sasha-s/go-deadlock v0.3.1 h1:sqv7fDNShgjcaxkO0JNcOAlr8B9+cV5Ey/OB71efZx0= -github.com/sasha-s/go-deadlock v0.3.1/go.mod h1:F73l+cr82YSh10GxyRI6qZiCgK64VaZjwesgfQ1/iLM= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= @@ -392,7 +374,6 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU= gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU= sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E= From 1fc27350950511cb70df566a4ced053120ea11dc Mon Sep 17 00:00:00 2001 From: testinginprod <98415576+testinginprod@users.noreply.github.com> Date: Mon, 29 Jul 2024 14:22:13 +0200 Subject: [PATCH 10/11] Update serverreflection.go --- server/v2/api/grpc/gogoreflection/serverreflection.go | 1 + 1 file changed, 1 insertion(+) diff --git a/server/v2/api/grpc/gogoreflection/serverreflection.go b/server/v2/api/grpc/gogoreflection/serverreflection.go index 037bfd155c50..c6de75c998f4 100644 --- a/server/v2/api/grpc/gogoreflection/serverreflection.go +++ b/server/v2/api/grpc/gogoreflection/serverreflection.go @@ -476,6 +476,7 @@ func (s *serverReflectionServer) ServerReflectionInfo(stream rpb.ServerReflectio } } +// getServices gets the unique list of services given a list of methods. func (s *serverReflectionServer) getServices(methods []string) (svcs []string, fds []*dpb.FileDescriptorProto) { registry, err := gogoproto.MergedRegistry() if err != nil { From d6f6d7dd0ee987d54eceb51111dbe342f137a4cb Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Tue, 30 Jul 2024 11:04:38 +0200 Subject: [PATCH 11/11] linting --- schema/appdata/mux_test.go | 1 + .../grpc/gogoreflection/serverreflection.go | 21 +------------------ server/v2/api/grpc/server.go | 2 +- server/v2/server_test.go | 2 +- 4 files changed, 4 insertions(+), 22 deletions(-) diff --git a/schema/appdata/mux_test.go b/schema/appdata/mux_test.go index e5fbad0b64e1..70787fada8a5 100644 --- a/schema/appdata/mux_test.go +++ b/schema/appdata/mux_test.go @@ -62,6 +62,7 @@ func TestListenerMux(t *testing.T) { } func callAllCallbacksOnces(t *testing.T, listener Listener) { + t.Helper() if err := listener.InitializeModuleData(ModuleInitializationData{}); err != nil { t.Error(err) } diff --git a/server/v2/api/grpc/gogoreflection/serverreflection.go b/server/v2/api/grpc/gogoreflection/serverreflection.go index c6de75c998f4..79f520545a87 100644 --- a/server/v2/api/grpc/gogoreflection/serverreflection.go +++ b/server/v2/api/grpc/gogoreflection/serverreflection.go @@ -48,7 +48,6 @@ import ( "sync" gogoproto "github.com/cosmos/gogoproto/proto" - dpb "github.com/golang/protobuf/protoc-gen-go/descriptor" "google.golang.org/grpc" "google.golang.org/grpc/codes" @@ -305,24 +304,6 @@ func (s *serverReflectionServer) fileDescEncodingByFilename(name string, sentFil return fileDescWithDependencies(fd, sentFileDescriptors) } -// parseMetadata finds the file descriptor bytes specified meta. -// For SupportPackageIsVersion4, m is the name of the proto file, we -// call proto.FileDescriptor to get the byte slice. -// For SupportPackageIsVersion3, m is a byte slice itself. -func parseMetadata(meta interface{}) ([]byte, bool) { - // Check if meta is the file name. - if fileNameForMeta, ok := meta.(string); ok { - return getFileDescriptor(fileNameForMeta), true - } - - // Check if meta is the byte slice. - if enc, ok := meta.([]byte); ok { - return enc, true - } - - return nil, false -} - // fileDescEncodingContainingSymbol finds the file descriptor containing the // given symbol, finds all of its previously unsent transitive dependencies, // does marshaling on them, and returns the marshaled result. The given symbol @@ -476,7 +457,7 @@ func (s *serverReflectionServer) ServerReflectionInfo(stream rpb.ServerReflectio } } -// getServices gets the unique list of services given a list of methods. +// getServices gets the unique list of services given a list of methods. func (s *serverReflectionServer) getServices(methods []string) (svcs []string, fds []*dpb.FileDescriptorProto) { registry, err := gogoproto.MergedRegistry() if err != nil { diff --git a/server/v2/api/grpc/server.go b/server/v2/api/grpc/server.go index a838923dd3ae..e59bd99a83c0 100644 --- a/server/v2/api/grpc/server.go +++ b/server/v2/api/grpc/server.go @@ -144,7 +144,7 @@ func getHeightFromCtx(ctx context.Context) (uint64, error) { heightStr := values[0] height, err := strconv.ParseUint(heightStr, 10, 64) if err != nil { - return 0, fmt.Errorf("unable to parse height string from gRPC metadata %s: %v", heightStr, err) + return 0, fmt.Errorf("unable to parse height string from gRPC metadata %s: %w", heightStr, err) } return height, nil diff --git a/server/v2/server_test.go b/server/v2/server_test.go index a369b26e18f1..e757e7ecd5ca 100644 --- a/server/v2/server_test.go +++ b/server/v2/server_test.go @@ -7,7 +7,6 @@ import ( "testing" "time" - "cosmossdk.io/server/v2/appmanager" gogoproto "github.com/cosmos/gogoproto/proto" "github.com/spf13/viper" "github.com/stretchr/testify/require" @@ -17,6 +16,7 @@ import ( "cosmossdk.io/log" serverv2 "cosmossdk.io/server/v2" grpc "cosmossdk.io/server/v2/api/grpc" + "cosmossdk.io/server/v2/appmanager" ) type mockInterfaceRegistry struct{}