From ceea91e42f8833e94021202c8fe549c094998951 Mon Sep 17 00:00:00 2001 From: Gerard Snaauw <33763579+gerardsn@users.noreply.github.com> Date: Wed, 2 Oct 2024 09:22:12 +0200 Subject: [PATCH] Tools: protobuf (#3436) * Tools: upgrade protobuf --- makefile | 4 +- network/transport/grpc/testprotocol.pb.go | 8 +- .../transport/grpc/testprotocol_grpc.pb.go | 88 +++++++------------ network/transport/v2/protocol.pb.go | 32 +++---- network/transport/v2/protocol_grpc.pb.go | 88 +++++++------------ 5 files changed, 84 insertions(+), 136 deletions(-) diff --git a/makefile b/makefile index 4e9f25e0d2..eae9c441f1 100644 --- a/makefile +++ b/makefile @@ -4,9 +4,9 @@ run-generators: gen-mocks gen-api gen-protobuf install-tools: go install github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@v2.3.0 - go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.34.1 + go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.34.2 go install go.uber.org/mock/mockgen@v0.4.0 - go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.3.0 + go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.5.1 go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.57.2 gen-mocks: diff --git a/network/transport/grpc/testprotocol.pb.go b/network/transport/grpc/testprotocol.pb.go index 263a659ec8..dc9d6f9dc1 100644 --- a/network/transport/grpc/testprotocol.pb.go +++ b/network/transport/grpc/testprotocol.pb.go @@ -17,8 +17,8 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.34.1 -// protoc v4.25.3 +// protoc-gen-go v1.34.2 +// protoc v5.27.3 // source: transport/grpc/testprotocol.proto package grpc @@ -116,7 +116,7 @@ func file_transport_grpc_testprotocol_proto_rawDescGZIP() []byte { } var file_transport_grpc_testprotocol_proto_msgTypes = make([]protoimpl.MessageInfo, 1) -var file_transport_grpc_testprotocol_proto_goTypes = []interface{}{ +var file_transport_grpc_testprotocol_proto_goTypes = []any{ (*TestMessage)(nil), // 0: grpc.TestMessage } var file_transport_grpc_testprotocol_proto_depIdxs = []int32{ @@ -135,7 +135,7 @@ func file_transport_grpc_testprotocol_proto_init() { return } if !protoimpl.UnsafeEnabled { - file_transport_grpc_testprotocol_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + file_transport_grpc_testprotocol_proto_msgTypes[0].Exporter = func(v any, i int) any { switch v := v.(*TestMessage); i { case 0: return &v.state diff --git a/network/transport/grpc/testprotocol_grpc.pb.go b/network/transport/grpc/testprotocol_grpc.pb.go index b208b01b2d..0461d719fc 100644 --- a/network/transport/grpc/testprotocol_grpc.pb.go +++ b/network/transport/grpc/testprotocol_grpc.pb.go @@ -17,8 +17,8 @@ // Code generated by protoc-gen-go-grpc. DO NOT EDIT. // versions: -// - protoc-gen-go-grpc v1.3.0 -// - protoc v4.25.3 +// - protoc-gen-go-grpc v1.5.1 +// - protoc v5.27.3 // source: transport/grpc/testprotocol.proto package grpc @@ -32,8 +32,8 @@ import ( // This is a compile-time assertion to ensure that this generated file // is compatible with the grpc package it is being compiled against. -// Requires gRPC-Go v1.32.0 or later. -const _ = grpc.SupportPackageIsVersion7 +// Requires gRPC-Go v1.64.0 or later. +const _ = grpc.SupportPackageIsVersion9 const ( Test_DoStuff_FullMethodName = "/grpc.Test/DoStuff" @@ -43,7 +43,7 @@ const ( // // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. type TestClient interface { - DoStuff(ctx context.Context, opts ...grpc.CallOption) (Test_DoStuffClient, error) + DoStuff(ctx context.Context, opts ...grpc.CallOption) (grpc.BidiStreamingClient[TestMessage, TestMessage], error) } type testClient struct { @@ -54,51 +54,37 @@ func NewTestClient(cc grpc.ClientConnInterface) TestClient { return &testClient{cc} } -func (c *testClient) DoStuff(ctx context.Context, opts ...grpc.CallOption) (Test_DoStuffClient, error) { - stream, err := c.cc.NewStream(ctx, &Test_ServiceDesc.Streams[0], Test_DoStuff_FullMethodName, opts...) +func (c *testClient) DoStuff(ctx context.Context, opts ...grpc.CallOption) (grpc.BidiStreamingClient[TestMessage, TestMessage], error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + stream, err := c.cc.NewStream(ctx, &Test_ServiceDesc.Streams[0], Test_DoStuff_FullMethodName, cOpts...) if err != nil { return nil, err } - x := &testDoStuffClient{stream} + x := &grpc.GenericClientStream[TestMessage, TestMessage]{ClientStream: stream} return x, nil } -type Test_DoStuffClient interface { - Send(*TestMessage) error - Recv() (*TestMessage, error) - grpc.ClientStream -} - -type testDoStuffClient struct { - grpc.ClientStream -} - -func (x *testDoStuffClient) Send(m *TestMessage) error { - return x.ClientStream.SendMsg(m) -} - -func (x *testDoStuffClient) Recv() (*TestMessage, error) { - m := new(TestMessage) - if err := x.ClientStream.RecvMsg(m); err != nil { - return nil, err - } - return m, nil -} +// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name. +type Test_DoStuffClient = grpc.BidiStreamingClient[TestMessage, TestMessage] // TestServer is the server API for Test service. // All implementations should embed UnimplementedTestServer -// for forward compatibility +// for forward compatibility. type TestServer interface { - DoStuff(Test_DoStuffServer) error + DoStuff(grpc.BidiStreamingServer[TestMessage, TestMessage]) error } -// UnimplementedTestServer should be embedded to have forward compatible implementations. -type UnimplementedTestServer struct { -} +// UnimplementedTestServer should be embedded to have +// forward compatible implementations. +// +// NOTE: this should be embedded by value instead of pointer to avoid a nil +// pointer dereference when methods are called. +type UnimplementedTestServer struct{} -func (UnimplementedTestServer) DoStuff(Test_DoStuffServer) error { +func (UnimplementedTestServer) DoStuff(grpc.BidiStreamingServer[TestMessage, TestMessage]) error { return status.Errorf(codes.Unimplemented, "method DoStuff not implemented") } +func (UnimplementedTestServer) testEmbeddedByValue() {} // UnsafeTestServer may be embedded to opt out of forward compatibility for this service. // Use of this interface is not recommended, as added methods to TestServer will @@ -108,34 +94,22 @@ type UnsafeTestServer interface { } func RegisterTestServer(s grpc.ServiceRegistrar, srv TestServer) { + // If the following call pancis, it indicates UnimplementedTestServer was + // embedded by pointer and is nil. This will cause panics if an + // unimplemented method is ever invoked, so we test this at initialization + // time to prevent it from happening at runtime later due to I/O. + if t, ok := srv.(interface{ testEmbeddedByValue() }); ok { + t.testEmbeddedByValue() + } s.RegisterService(&Test_ServiceDesc, srv) } func _Test_DoStuff_Handler(srv interface{}, stream grpc.ServerStream) error { - return srv.(TestServer).DoStuff(&testDoStuffServer{stream}) -} - -type Test_DoStuffServer interface { - Send(*TestMessage) error - Recv() (*TestMessage, error) - grpc.ServerStream -} - -type testDoStuffServer struct { - grpc.ServerStream + return srv.(TestServer).DoStuff(&grpc.GenericServerStream[TestMessage, TestMessage]{ServerStream: stream}) } -func (x *testDoStuffServer) Send(m *TestMessage) error { - return x.ServerStream.SendMsg(m) -} - -func (x *testDoStuffServer) Recv() (*TestMessage, error) { - m := new(TestMessage) - if err := x.ServerStream.RecvMsg(m); err != nil { - return nil, err - } - return m, nil -} +// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name. +type Test_DoStuffServer = grpc.BidiStreamingServer[TestMessage, TestMessage] // Test_ServiceDesc is the grpc.ServiceDesc for Test service. // It's only intended for direct use with grpc.RegisterService, diff --git a/network/transport/v2/protocol.pb.go b/network/transport/v2/protocol.pb.go index 5af2f30ef2..061eff71ca 100644 --- a/network/transport/v2/protocol.pb.go +++ b/network/transport/v2/protocol.pb.go @@ -17,8 +17,8 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.34.1 -// protoc v4.25.3 +// protoc-gen-go v1.34.2 +// protoc v5.27.3 // source: transport/v2/protocol.proto package v2 @@ -1051,7 +1051,7 @@ func file_transport_v2_protocol_proto_rawDescGZIP() []byte { } var file_transport_v2_protocol_proto_msgTypes = make([]protoimpl.MessageInfo, 11) -var file_transport_v2_protocol_proto_goTypes = []interface{}{ +var file_transport_v2_protocol_proto_goTypes = []any{ (*Envelope)(nil), // 0: v2.Envelope (*Transaction)(nil), // 1: v2.Transaction (*Gossip)(nil), // 2: v2.Gossip @@ -1090,7 +1090,7 @@ func file_transport_v2_protocol_proto_init() { return } if !protoimpl.UnsafeEnabled { - file_transport_v2_protocol_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + file_transport_v2_protocol_proto_msgTypes[0].Exporter = func(v any, i int) any { switch v := v.(*Envelope); i { case 0: return &v.state @@ -1102,7 +1102,7 @@ func file_transport_v2_protocol_proto_init() { return nil } } - file_transport_v2_protocol_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + file_transport_v2_protocol_proto_msgTypes[1].Exporter = func(v any, i int) any { switch v := v.(*Transaction); i { case 0: return &v.state @@ -1114,7 +1114,7 @@ func file_transport_v2_protocol_proto_init() { return nil } } - file_transport_v2_protocol_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + file_transport_v2_protocol_proto_msgTypes[2].Exporter = func(v any, i int) any { switch v := v.(*Gossip); i { case 0: return &v.state @@ -1126,7 +1126,7 @@ func file_transport_v2_protocol_proto_init() { return nil } } - file_transport_v2_protocol_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + file_transport_v2_protocol_proto_msgTypes[3].Exporter = func(v any, i int) any { switch v := v.(*State); i { case 0: return &v.state @@ -1138,7 +1138,7 @@ func file_transport_v2_protocol_proto_init() { return nil } } - file_transport_v2_protocol_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { + file_transport_v2_protocol_proto_msgTypes[4].Exporter = func(v any, i int) any { switch v := v.(*TransactionSet); i { case 0: return &v.state @@ -1150,7 +1150,7 @@ func file_transport_v2_protocol_proto_init() { return nil } } - file_transport_v2_protocol_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { + file_transport_v2_protocol_proto_msgTypes[5].Exporter = func(v any, i int) any { switch v := v.(*TransactionListQuery); i { case 0: return &v.state @@ -1162,7 +1162,7 @@ func file_transport_v2_protocol_proto_init() { return nil } } - file_transport_v2_protocol_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} { + file_transport_v2_protocol_proto_msgTypes[6].Exporter = func(v any, i int) any { switch v := v.(*TransactionRangeQuery); i { case 0: return &v.state @@ -1174,7 +1174,7 @@ func file_transport_v2_protocol_proto_init() { return nil } } - file_transport_v2_protocol_proto_msgTypes[7].Exporter = func(v interface{}, i int) interface{} { + file_transport_v2_protocol_proto_msgTypes[7].Exporter = func(v any, i int) any { switch v := v.(*TransactionList); i { case 0: return &v.state @@ -1186,7 +1186,7 @@ func file_transport_v2_protocol_proto_init() { return nil } } - file_transport_v2_protocol_proto_msgTypes[8].Exporter = func(v interface{}, i int) interface{} { + file_transport_v2_protocol_proto_msgTypes[8].Exporter = func(v any, i int) any { switch v := v.(*TransactionPayloadQuery); i { case 0: return &v.state @@ -1198,7 +1198,7 @@ func file_transport_v2_protocol_proto_init() { return nil } } - file_transport_v2_protocol_proto_msgTypes[9].Exporter = func(v interface{}, i int) interface{} { + file_transport_v2_protocol_proto_msgTypes[9].Exporter = func(v any, i int) any { switch v := v.(*TransactionPayload); i { case 0: return &v.state @@ -1210,7 +1210,7 @@ func file_transport_v2_protocol_proto_init() { return nil } } - file_transport_v2_protocol_proto_msgTypes[10].Exporter = func(v interface{}, i int) interface{} { + file_transport_v2_protocol_proto_msgTypes[10].Exporter = func(v any, i int) any { switch v := v.(*Diagnostics); i { case 0: return &v.state @@ -1223,7 +1223,7 @@ func file_transport_v2_protocol_proto_init() { } } } - file_transport_v2_protocol_proto_msgTypes[0].OneofWrappers = []interface{}{ + file_transport_v2_protocol_proto_msgTypes[0].OneofWrappers = []any{ (*Envelope_Gossip)(nil), (*Envelope_DiagnosticsBroadcast)(nil), (*Envelope_State)(nil), @@ -1234,7 +1234,7 @@ func file_transport_v2_protocol_proto_init() { (*Envelope_TransactionList)(nil), (*Envelope_TransactionPayload)(nil), } - file_transport_v2_protocol_proto_msgTypes[1].OneofWrappers = []interface{}{} + file_transport_v2_protocol_proto_msgTypes[1].OneofWrappers = []any{} type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ diff --git a/network/transport/v2/protocol_grpc.pb.go b/network/transport/v2/protocol_grpc.pb.go index 24eb0383b0..56c7a7054e 100644 --- a/network/transport/v2/protocol_grpc.pb.go +++ b/network/transport/v2/protocol_grpc.pb.go @@ -17,8 +17,8 @@ // Code generated by protoc-gen-go-grpc. DO NOT EDIT. // versions: -// - protoc-gen-go-grpc v1.3.0 -// - protoc v4.25.3 +// - protoc-gen-go-grpc v1.5.1 +// - protoc v5.27.3 // source: transport/v2/protocol.proto package v2 @@ -32,8 +32,8 @@ import ( // This is a compile-time assertion to ensure that this generated file // is compatible with the grpc package it is being compiled against. -// Requires gRPC-Go v1.32.0 or later. -const _ = grpc.SupportPackageIsVersion7 +// Requires gRPC-Go v1.64.0 or later. +const _ = grpc.SupportPackageIsVersion9 const ( Protocol_Stream_FullMethodName = "/v2.Protocol/Stream" @@ -43,7 +43,7 @@ const ( // // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. type ProtocolClient interface { - Stream(ctx context.Context, opts ...grpc.CallOption) (Protocol_StreamClient, error) + Stream(ctx context.Context, opts ...grpc.CallOption) (grpc.BidiStreamingClient[Envelope, Envelope], error) } type protocolClient struct { @@ -54,51 +54,37 @@ func NewProtocolClient(cc grpc.ClientConnInterface) ProtocolClient { return &protocolClient{cc} } -func (c *protocolClient) Stream(ctx context.Context, opts ...grpc.CallOption) (Protocol_StreamClient, error) { - stream, err := c.cc.NewStream(ctx, &Protocol_ServiceDesc.Streams[0], Protocol_Stream_FullMethodName, opts...) +func (c *protocolClient) Stream(ctx context.Context, opts ...grpc.CallOption) (grpc.BidiStreamingClient[Envelope, Envelope], error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + stream, err := c.cc.NewStream(ctx, &Protocol_ServiceDesc.Streams[0], Protocol_Stream_FullMethodName, cOpts...) if err != nil { return nil, err } - x := &protocolStreamClient{stream} + x := &grpc.GenericClientStream[Envelope, Envelope]{ClientStream: stream} return x, nil } -type Protocol_StreamClient interface { - Send(*Envelope) error - Recv() (*Envelope, error) - grpc.ClientStream -} - -type protocolStreamClient struct { - grpc.ClientStream -} - -func (x *protocolStreamClient) Send(m *Envelope) error { - return x.ClientStream.SendMsg(m) -} - -func (x *protocolStreamClient) Recv() (*Envelope, error) { - m := new(Envelope) - if err := x.ClientStream.RecvMsg(m); err != nil { - return nil, err - } - return m, nil -} +// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name. +type Protocol_StreamClient = grpc.BidiStreamingClient[Envelope, Envelope] // ProtocolServer is the server API for Protocol service. // All implementations should embed UnimplementedProtocolServer -// for forward compatibility +// for forward compatibility. type ProtocolServer interface { - Stream(Protocol_StreamServer) error + Stream(grpc.BidiStreamingServer[Envelope, Envelope]) error } -// UnimplementedProtocolServer should be embedded to have forward compatible implementations. -type UnimplementedProtocolServer struct { -} +// UnimplementedProtocolServer should be embedded to have +// forward compatible implementations. +// +// NOTE: this should be embedded by value instead of pointer to avoid a nil +// pointer dereference when methods are called. +type UnimplementedProtocolServer struct{} -func (UnimplementedProtocolServer) Stream(Protocol_StreamServer) error { +func (UnimplementedProtocolServer) Stream(grpc.BidiStreamingServer[Envelope, Envelope]) error { return status.Errorf(codes.Unimplemented, "method Stream not implemented") } +func (UnimplementedProtocolServer) testEmbeddedByValue() {} // UnsafeProtocolServer may be embedded to opt out of forward compatibility for this service. // Use of this interface is not recommended, as added methods to ProtocolServer will @@ -108,34 +94,22 @@ type UnsafeProtocolServer interface { } func RegisterProtocolServer(s grpc.ServiceRegistrar, srv ProtocolServer) { + // If the following call pancis, it indicates UnimplementedProtocolServer was + // embedded by pointer and is nil. This will cause panics if an + // unimplemented method is ever invoked, so we test this at initialization + // time to prevent it from happening at runtime later due to I/O. + if t, ok := srv.(interface{ testEmbeddedByValue() }); ok { + t.testEmbeddedByValue() + } s.RegisterService(&Protocol_ServiceDesc, srv) } func _Protocol_Stream_Handler(srv interface{}, stream grpc.ServerStream) error { - return srv.(ProtocolServer).Stream(&protocolStreamServer{stream}) -} - -type Protocol_StreamServer interface { - Send(*Envelope) error - Recv() (*Envelope, error) - grpc.ServerStream -} - -type protocolStreamServer struct { - grpc.ServerStream + return srv.(ProtocolServer).Stream(&grpc.GenericServerStream[Envelope, Envelope]{ServerStream: stream}) } -func (x *protocolStreamServer) Send(m *Envelope) error { - return x.ServerStream.SendMsg(m) -} - -func (x *protocolStreamServer) Recv() (*Envelope, error) { - m := new(Envelope) - if err := x.ServerStream.RecvMsg(m); err != nil { - return nil, err - } - return m, nil -} +// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name. +type Protocol_StreamServer = grpc.BidiStreamingServer[Envelope, Envelope] // Protocol_ServiceDesc is the grpc.ServiceDesc for Protocol service. // It's only intended for direct use with grpc.RegisterService,