From 0fad5ff686719eebb3d62584853aca2dfd4ca4e3 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Thu, 1 Jun 2023 13:25:41 -0400 Subject: [PATCH] chore: use proto2 for nwaku compatibility --- client.go | 7 ++- pb/rendezvous.pb.go | 106 ++++++++++++++++++++++++++------------------ pb/rendezvous.proto | 44 +++++++++--------- proto.go | 37 ++++++++-------- svc.go | 6 +-- 5 files changed, 112 insertions(+), 88 deletions(-) diff --git a/client.go b/client.go index 9e0c4b4..fe36cbd 100644 --- a/client.go +++ b/client.go @@ -110,7 +110,12 @@ func (rp *rendezvousPoint) Register(ctx context.Context, ns string, ttl int) (ti return 0, RendezvousError{Status: status, Text: res.GetRegisterResponse().GetStatusText()} } - return time.Duration(response.Ttl) * time.Second, nil + responseTTL := int64(0) + if response.Ttl != nil { + responseTTL = int64(*response.Ttl) + } + + return time.Duration(responseTTL) * time.Second, nil } func (rc *rendezvousClient) Register(ctx context.Context, ns string, ttl int) (time.Duration, error) { diff --git a/pb/rendezvous.pb.go b/pb/rendezvous.pb.go index b0e1a4e..b580edc 100644 --- a/pb/rendezvous.pb.go +++ b/pb/rendezvous.pb.go @@ -70,6 +70,16 @@ func (x Message_MessageType) Number() protoreflect.EnumNumber { return protoreflect.EnumNumber(x) } +// Deprecated: Do not use. +func (x *Message_MessageType) UnmarshalJSON(b []byte) error { + num, err := protoimpl.X.UnmarshalJSONEnum(x.Descriptor(), b) + if err != nil { + return err + } + *x = Message_MessageType(num) + return nil +} + // Deprecated: Use Message_MessageType.Descriptor instead. func (Message_MessageType) EnumDescriptor() ([]byte, []int) { return file_rendezvous_proto_rawDescGZIP(), []int{0, 0} @@ -134,6 +144,16 @@ func (x Message_ResponseStatus) Number() protoreflect.EnumNumber { return protoreflect.EnumNumber(x) } +// Deprecated: Do not use. +func (x *Message_ResponseStatus) UnmarshalJSON(b []byte) error { + num, err := protoimpl.X.UnmarshalJSONEnum(x.Descriptor(), b) + if err != nil { + return err + } + *x = Message_ResponseStatus(num) + return nil +} + // Deprecated: Use Message_ResponseStatus.Descriptor instead. func (Message_ResponseStatus) EnumDescriptor() ([]byte, []int) { return file_rendezvous_proto_rawDescGZIP(), []int{0, 1} @@ -144,12 +164,12 @@ type Message struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Type Message_MessageType `protobuf:"varint,1,opt,name=type,proto3,enum=rendezvous.pb.Message_MessageType" json:"type,omitempty"` - Register *Message_Register `protobuf:"bytes,2,opt,name=register,proto3" json:"register,omitempty"` - RegisterResponse *Message_RegisterResponse `protobuf:"bytes,3,opt,name=registerResponse,proto3" json:"registerResponse,omitempty"` - Unregister *Message_Unregister `protobuf:"bytes,4,opt,name=unregister,proto3" json:"unregister,omitempty"` - Discover *Message_Discover `protobuf:"bytes,5,opt,name=discover,proto3" json:"discover,omitempty"` - DiscoverResponse *Message_DiscoverResponse `protobuf:"bytes,6,opt,name=discoverResponse,proto3" json:"discoverResponse,omitempty"` + Type *Message_MessageType `protobuf:"varint,1,opt,name=type,enum=rendezvous.pb.Message_MessageType" json:"type,omitempty"` + Register *Message_Register `protobuf:"bytes,2,opt,name=register" json:"register,omitempty"` + RegisterResponse *Message_RegisterResponse `protobuf:"bytes,3,opt,name=registerResponse" json:"registerResponse,omitempty"` + Unregister *Message_Unregister `protobuf:"bytes,4,opt,name=unregister" json:"unregister,omitempty"` + Discover *Message_Discover `protobuf:"bytes,5,opt,name=discover" json:"discover,omitempty"` + DiscoverResponse *Message_DiscoverResponse `protobuf:"bytes,6,opt,name=discoverResponse" json:"discoverResponse,omitempty"` } func (x *Message) Reset() { @@ -185,8 +205,8 @@ func (*Message) Descriptor() ([]byte, []int) { } func (x *Message) GetType() Message_MessageType { - if x != nil { - return x.Type + if x != nil && x.Type != nil { + return *x.Type } return Message_REGISTER } @@ -231,9 +251,9 @@ type Message_Register struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Ns string `protobuf:"bytes,1,opt,name=ns,proto3" json:"ns,omitempty"` - SignedPeerRecord []byte `protobuf:"bytes,2,opt,name=signedPeerRecord,proto3" json:"signedPeerRecord,omitempty"` - Ttl uint64 `protobuf:"varint,3,opt,name=ttl,proto3" json:"ttl,omitempty"` // in seconds + Ns *string `protobuf:"bytes,1,opt,name=ns" json:"ns,omitempty"` + SignedPeerRecord []byte `protobuf:"bytes,2,opt,name=signedPeerRecord" json:"signedPeerRecord,omitempty"` + Ttl *uint64 `protobuf:"varint,3,opt,name=ttl" json:"ttl,omitempty"` // in seconds } func (x *Message_Register) Reset() { @@ -269,8 +289,8 @@ func (*Message_Register) Descriptor() ([]byte, []int) { } func (x *Message_Register) GetNs() string { - if x != nil { - return x.Ns + if x != nil && x.Ns != nil { + return *x.Ns } return "" } @@ -283,8 +303,8 @@ func (x *Message_Register) GetSignedPeerRecord() []byte { } func (x *Message_Register) GetTtl() uint64 { - if x != nil { - return x.Ttl + if x != nil && x.Ttl != nil { + return *x.Ttl } return 0 } @@ -294,9 +314,9 @@ type Message_RegisterResponse struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Status Message_ResponseStatus `protobuf:"varint,1,opt,name=status,proto3,enum=rendezvous.pb.Message_ResponseStatus" json:"status,omitempty"` - StatusText string `protobuf:"bytes,2,opt,name=statusText,proto3" json:"statusText,omitempty"` - Ttl uint64 `protobuf:"varint,3,opt,name=ttl,proto3" json:"ttl,omitempty"` // in seconds + Status *Message_ResponseStatus `protobuf:"varint,1,opt,name=status,enum=rendezvous.pb.Message_ResponseStatus" json:"status,omitempty"` + StatusText *string `protobuf:"bytes,2,opt,name=statusText" json:"statusText,omitempty"` + Ttl *uint64 `protobuf:"varint,3,opt,name=ttl" json:"ttl,omitempty"` // in seconds } func (x *Message_RegisterResponse) Reset() { @@ -332,22 +352,22 @@ func (*Message_RegisterResponse) Descriptor() ([]byte, []int) { } func (x *Message_RegisterResponse) GetStatus() Message_ResponseStatus { - if x != nil { - return x.Status + if x != nil && x.Status != nil { + return *x.Status } return Message_OK } func (x *Message_RegisterResponse) GetStatusText() string { - if x != nil { - return x.StatusText + if x != nil && x.StatusText != nil { + return *x.StatusText } return "" } func (x *Message_RegisterResponse) GetTtl() uint64 { - if x != nil { - return x.Ttl + if x != nil && x.Ttl != nil { + return *x.Ttl } return 0 } @@ -357,7 +377,7 @@ type Message_Unregister struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Ns string `protobuf:"bytes,1,opt,name=ns,proto3" json:"ns,omitempty"` // bytes id = 2; deprecated as per https://github.com/libp2p/specs/issues/335 + Ns *string `protobuf:"bytes,1,opt,name=ns" json:"ns,omitempty"` // optional bytes id = 2; deprecated as per https://github.com/libp2p/specs/issues/335 } func (x *Message_Unregister) Reset() { @@ -393,8 +413,8 @@ func (*Message_Unregister) Descriptor() ([]byte, []int) { } func (x *Message_Unregister) GetNs() string { - if x != nil { - return x.Ns + if x != nil && x.Ns != nil { + return *x.Ns } return "" } @@ -404,9 +424,9 @@ type Message_Discover struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Ns string `protobuf:"bytes,1,opt,name=ns,proto3" json:"ns,omitempty"` - Limit uint64 `protobuf:"varint,2,opt,name=limit,proto3" json:"limit,omitempty"` - Cookie []byte `protobuf:"bytes,3,opt,name=cookie,proto3" json:"cookie,omitempty"` + Ns *string `protobuf:"bytes,1,opt,name=ns" json:"ns,omitempty"` + Limit *uint64 `protobuf:"varint,2,opt,name=limit" json:"limit,omitempty"` + Cookie []byte `protobuf:"bytes,3,opt,name=cookie" json:"cookie,omitempty"` } func (x *Message_Discover) Reset() { @@ -442,15 +462,15 @@ func (*Message_Discover) Descriptor() ([]byte, []int) { } func (x *Message_Discover) GetNs() string { - if x != nil { - return x.Ns + if x != nil && x.Ns != nil { + return *x.Ns } return "" } func (x *Message_Discover) GetLimit() uint64 { - if x != nil { - return x.Limit + if x != nil && x.Limit != nil { + return *x.Limit } return 0 } @@ -467,10 +487,10 @@ type Message_DiscoverResponse struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Registrations []*Message_Register `protobuf:"bytes,1,rep,name=registrations,proto3" json:"registrations,omitempty"` - Cookie []byte `protobuf:"bytes,2,opt,name=cookie,proto3" json:"cookie,omitempty"` - Status Message_ResponseStatus `protobuf:"varint,3,opt,name=status,proto3,enum=rendezvous.pb.Message_ResponseStatus" json:"status,omitempty"` - StatusText string `protobuf:"bytes,4,opt,name=statusText,proto3" json:"statusText,omitempty"` + Registrations []*Message_Register `protobuf:"bytes,1,rep,name=registrations" json:"registrations,omitempty"` + Cookie []byte `protobuf:"bytes,2,opt,name=cookie" json:"cookie,omitempty"` + Status *Message_ResponseStatus `protobuf:"varint,3,opt,name=status,enum=rendezvous.pb.Message_ResponseStatus" json:"status,omitempty"` + StatusText *string `protobuf:"bytes,4,opt,name=statusText" json:"statusText,omitempty"` } func (x *Message_DiscoverResponse) Reset() { @@ -520,15 +540,15 @@ func (x *Message_DiscoverResponse) GetCookie() []byte { } func (x *Message_DiscoverResponse) GetStatus() Message_ResponseStatus { - if x != nil { - return x.Status + if x != nil && x.Status != nil { + return *x.Status } return Message_OK } func (x *Message_DiscoverResponse) GetStatusText() string { - if x != nil { - return x.StatusText + if x != nil && x.StatusText != nil { + return *x.StatusText } return "" } @@ -617,7 +637,7 @@ var file_rendezvous_proto_rawDesc = []byte{ 0x5a, 0x45, 0x44, 0x10, 0xc8, 0x01, 0x12, 0x15, 0x0a, 0x10, 0x45, 0x5f, 0x49, 0x4e, 0x54, 0x45, 0x52, 0x4e, 0x41, 0x4c, 0x5f, 0x45, 0x52, 0x52, 0x4f, 0x52, 0x10, 0xac, 0x02, 0x12, 0x12, 0x0a, 0x0d, 0x45, 0x5f, 0x55, 0x4e, 0x41, 0x56, 0x41, 0x49, 0x4c, 0x41, 0x42, 0x4c, 0x45, 0x10, 0x90, - 0x03, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x03, } var ( diff --git a/pb/rendezvous.proto b/pb/rendezvous.proto index 9c84b2d..c9614c1 100644 --- a/pb/rendezvous.proto +++ b/pb/rendezvous.proto @@ -1,4 +1,4 @@ -syntax = "proto3"; +syntax = "proto2"; package rendezvous.pb; @@ -23,39 +23,39 @@ message Message { } message Register { - string ns = 1; - bytes signedPeerRecord = 2; - uint64 ttl = 3; // in seconds + optional string ns = 1; + optional bytes signedPeerRecord = 2; + optional uint64 ttl = 3; // in seconds } message RegisterResponse { - ResponseStatus status = 1; - string statusText = 2; - uint64 ttl = 3; // in seconds + optional ResponseStatus status = 1; + optional string statusText = 2; + optional uint64 ttl = 3; // in seconds } message Unregister { - string ns = 1; - // bytes id = 2; deprecated as per https://github.com/libp2p/specs/issues/335 + optional string ns = 1; + // optional bytes id = 2; deprecated as per https://github.com/libp2p/specs/issues/335 } message Discover { - string ns = 1; - uint64 limit = 2; - bytes cookie = 3; + optional string ns = 1; + optional uint64 limit = 2; + optional bytes cookie = 3; } message DiscoverResponse { repeated Register registrations = 1; - bytes cookie = 2; - ResponseStatus status = 3; - string statusText = 4; + optional bytes cookie = 2; + optional ResponseStatus status = 3; + optional string statusText = 4; } - MessageType type = 1; - Register register = 2; - RegisterResponse registerResponse = 3; - Unregister unregister = 4; - Discover discover = 5; - DiscoverResponse discoverResponse = 6; -} + optional MessageType type = 1; + optional Register register = 2; + optional RegisterResponse registerResponse = 3; + optional Unregister unregister = 4; + optional Discover discover = 5; + optional DiscoverResponse discoverResponse = 6; +} \ No newline at end of file diff --git a/proto.go b/proto.go index 1bbb90e..f530f16 100644 --- a/proto.go +++ b/proto.go @@ -38,14 +38,14 @@ func NewRegisterMessage(privKey crypto.PrivKey, ns string, pi peer.AddrInfo, ttl func newRegisterMessage(privKey crypto.PrivKey, ns string, pi peer.AddrInfo, ttl int) (*pb.Message, error) { msg := new(pb.Message) - msg.Type = pb.Message_REGISTER + msg.Type = pb.Message_REGISTER.Enum() msg.Register = new(pb.Message_Register) if ns != "" { - msg.Register.Ns = ns + msg.Register.Ns = &ns } if ttl > 0 { - ttl64 := int64(ttl) - msg.Register.Ttl = uint64(ttl64) + ttlu64 := uint64(ttl) + msg.Register.Ttl = &ttlu64 } peerInfo := &peer.PeerRecord{ @@ -71,10 +71,10 @@ func newRegisterMessage(privKey crypto.PrivKey, ns string, pi peer.AddrInfo, ttl func newUnregisterMessage(ns string, pid peer.ID) *pb.Message { msg := new(pb.Message) - msg.Type = pb.Message_UNREGISTER + msg.Type = pb.Message_UNREGISTER.Enum() msg.Unregister = new(pb.Message_Unregister) if ns != "" { - msg.Unregister.Ns = ns + msg.Unregister.Ns = &ns } return msg } @@ -85,14 +85,14 @@ func NewDiscoverMessage(ns string, limit int, cookie []byte) *pb.Message { func newDiscoverMessage(ns string, limit int, cookie []byte) *pb.Message { msg := new(pb.Message) - msg.Type = pb.Message_DISCOVER + msg.Type = pb.Message_DISCOVER.Enum() msg.Discover = new(pb.Message_Discover) if ns != "" { - msg.Discover.Ns = ns + msg.Discover.Ns = &ns } if limit > 0 { limitu64 := uint64(limit) - msg.Discover.Limit = limitu64 + msg.Discover.Limit = &limitu64 } if cookie != nil { msg.Discover.Cookie = cookie @@ -120,30 +120,29 @@ func pbToPeerRecord(envelopeBytes []byte) (peer.AddrInfo, error) { func newRegisterResponse(ttl int) *pb.Message_RegisterResponse { ttlu64 := uint64(ttl) r := new(pb.Message_RegisterResponse) - r.Status = pb.Message_OK - r.Ttl = ttlu64 + r.Status = pb.Message_OK.Enum() + r.Ttl = &ttlu64 return r } func newRegisterResponseError(status pb.Message_ResponseStatus, text string) *pb.Message_RegisterResponse { r := new(pb.Message_RegisterResponse) - r.Status = status - r.StatusText = text + r.Status = status.Enum() + r.StatusText = &text return r } func newDiscoverResponse(regs []db.RegistrationRecord, cookie []byte) *pb.Message_DiscoverResponse { r := new(pb.Message_DiscoverResponse) - r.Status = pb.Message_OK + r.Status = pb.Message_OK.Enum() rregs := make([]*pb.Message_Register, len(regs)) for i, reg := range regs { rreg := new(pb.Message_Register) - rns := reg.Ns - rreg.Ns = rns + rreg.Ns = ®.Ns rreg.SignedPeerRecord = reg.SignedPeerRecord rttl := uint64(reg.Ttl) - rreg.Ttl = rttl + rreg.Ttl = &rttl rregs[i] = rreg } @@ -155,7 +154,7 @@ func newDiscoverResponse(regs []db.RegistrationRecord, cookie []byte) *pb.Messag func newDiscoverResponseError(status pb.Message_ResponseStatus, text string) *pb.Message_DiscoverResponse { r := new(pb.Message_DiscoverResponse) - r.Status = status - r.StatusText = text + r.Status = status.Enum() + r.StatusText = &text return r } diff --git a/svc.go b/svc.go index 0431538..f2dfa94 100644 --- a/svc.go +++ b/svc.go @@ -50,7 +50,7 @@ func (rz *RendezvousService) handleStream(s inet.Stream) { switch t { case pb.Message_REGISTER: r := rz.handleRegister(pid, req.GetRegister()) - res.Type = pb.Message_REGISTER_RESPONSE + res.Type = pb.Message_REGISTER_RESPONSE.Enum() res.RegisterResponse = r err = w.WriteMsg(&res) if err != nil { @@ -66,7 +66,7 @@ func (rz *RendezvousService) handleStream(s inet.Stream) { case pb.Message_DISCOVER: r := rz.handleDiscover(pid, req.GetDiscover()) - res.Type = pb.Message_DISCOVER_RESPONSE + res.Type = pb.Message_DISCOVER_RESPONSE.Enum() res.DiscoverResponse = r err = w.WriteMsg(&res) if err != nil { @@ -194,7 +194,7 @@ func (rz *RendezvousService) handleDiscover(p peer.ID, m *pb.Message_Discover) * return newDiscoverResponseError(pb.Message_E_INTERNAL_ERROR, "database error") } - log.Infof("discover query: %s %s -> %d", p, ns, len(regs)) + log.Debugf("discover query: %s %s -> %d", p, ns, len(regs)) return newDiscoverResponse(regs, cookie) }