fix: libp2p spec uses bytes instead of a protobuffer for signed peer records

This commit is contained in:
Richard Ramos 2021-10-15 11:22:46 -04:00
parent b53661c585
commit e8f25fa062
7 changed files with 72 additions and 131 deletions

View File

@ -3,12 +3,12 @@
### Overview ### Overview
Similar to [status-im/rendezvous](https://github.com/status-im/rendezvous) Similar to [status-im/rendezvous](https://github.com/status-im/rendezvous)
in using a smaller liveness TTL for records (20s), and not using `UNREGISTER in using a smaller liveness TTL for records (20s), and not using unregistering
REQUEST`, due to assuming that the TTL is very low (making it incompatible records, due to assuming that the TTL is very low (making it incompatible
with libp2p original rendezvous spec). This module is intended to be used with libp2p original rendezvous spec). This module is intended to be used
in go-waku as a lightweight mechanism for generalized peer discovery. in go-waku as a lightweight mechanism for generalized peer discovery.
Another difference compared to [libp2p/rendezvous](https://github.com/libp2p/specs/blob/master/rendezvous/README.md) and status-im/rendezvous is the usage of [routing records](https://github.com/libp2p/specs/blob/master/RFC/0003-routing-records.md) and [signed envelopes](https://github.com/libp2p/specs/blob/master/RFC/0002-signed-envelopes.md) A difference compared to status-im/rendezvous is the usage of [routing records](https://github.com/libp2p/specs/blob/master/RFC/0003-routing-records.md) and [signed envelopes](https://github.com/libp2p/specs/blob/master/RFC/0002-signed-envelopes.md) instead of ENR records
**Protocol identifier**: `/vac/waku/rendezvous/0.0.1` **Protocol identifier**: `/vac/waku/rendezvous/0.0.1`
@ -98,7 +98,7 @@ message Message {
message Register { message Register {
string ns = 1; string ns = 1;
record.pb.Envelope peer = 2; bytes signedPeerRecord = 2;
int64 ttl = 3; // in seconds int64 ttl = 3; // in seconds
} }

View File

@ -15,6 +15,7 @@ import (
inet "github.com/libp2p/go-libp2p-core/network" inet "github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore" "github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p-core/record"
) )
var ( var (
@ -124,7 +125,6 @@ func (rp *rendezvousPoint) Register(ctx context.Context, ns string, ttl int) (ti
} }
return time.Duration(response.Ttl) * time.Second, nil return time.Duration(response.Ttl) * time.Second, nil
return time.Duration(1) * time.Second, nil
} }
func (rc *rendezvousClient) Register(ctx context.Context, ns string, ttl int) (time.Duration, error) { func (rc *rendezvousClient) Register(ctx context.Context, ns string, ttl int) (time.Duration, error) {
@ -205,7 +205,7 @@ func (rp *rendezvousPoint) discoverQuery(ns string, limit int, r ggio.Reader, w
} }
if res.GetType() != pb.Message_DISCOVER_RESPONSE { if res.GetType() != pb.Message_DISCOVER_RESPONSE {
return nil, fmt.Errorf("Unexpected response: %s", res.GetType().String()) return nil, fmt.Errorf("unexpected response: %s", res.GetType().String())
} }
status := res.GetDiscoverResponse().GetStatus() status := res.GetDiscoverResponse().GetStatus()
@ -217,7 +217,8 @@ func (rp *rendezvousPoint) discoverQuery(ns string, limit int, r ggio.Reader, w
result := make([]Registration, 0, len(regs)) result := make([]Registration, 0, len(regs))
for _, reg := range regs { for _, reg := range regs {
envelope, err := pbToEnvelope(reg.GetPeer()) reg.GetSignedPeerRecord()
envelope, err := record.UnmarshalEnvelope(reg.GetSignedPeerRecord())
if err != nil { if err != nil {
log.Errorf("Invalid peer info: %s", err.Error()) log.Errorf("Invalid peer info: %s", err.Error())
continue continue

View File

@ -1,5 +0,0 @@
#!/bin/bash
# rendezvous.proto
protoc --gofast_out=. --proto_path=$(go list -f '{{ .Dir }}' -m github.com/libp2p/go-libp2p-core) --proto_path=. rendezvous.proto
sed -i "s/record\/pb/github.com\/libp2p\/go-libp2p-core\/record\/pb/" rendezvous.pb.go

View File

@ -9,7 +9,6 @@ import (
io "io" io "io"
math "math" math "math"
math_bits "math/bits" math_bits "math/bits"
pb "github.com/libp2p/go-libp2p-core/record/pb"
) )
// Reference imports to suppress errors if they are not otherwise used. // Reference imports to suppress errors if they are not otherwise used.
@ -175,7 +174,7 @@ func (m *Message) GetDiscoverResponse() *Message_DiscoverResponse {
type Message_Register struct { type Message_Register struct {
Ns string `protobuf:"bytes,1,opt,name=ns,proto3" json:"ns,omitempty"` Ns string `protobuf:"bytes,1,opt,name=ns,proto3" json:"ns,omitempty"`
Peer *pb.Envelope `protobuf:"bytes,2,opt,name=peer,proto3" json:"peer,omitempty"` SignedPeerRecord []byte `protobuf:"bytes,2,opt,name=signedPeerRecord,proto3" json:"signedPeerRecord,omitempty"`
Ttl int64 `protobuf:"varint,3,opt,name=ttl,proto3" json:"ttl,omitempty"` Ttl int64 `protobuf:"varint,3,opt,name=ttl,proto3" json:"ttl,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"` XXX_unrecognized []byte `json:"-"`
@ -222,9 +221,9 @@ func (m *Message_Register) GetNs() string {
return "" return ""
} }
func (m *Message_Register) GetPeer() *pb.Envelope { func (m *Message_Register) GetSignedPeerRecord() []byte {
if m != nil { if m != nil {
return m.Peer return m.SignedPeerRecord
} }
return nil return nil
} }
@ -430,41 +429,40 @@ func init() {
func init() { proto.RegisterFile("rendezvous.proto", fileDescriptor_ef0a1d5737df1c36) } func init() { proto.RegisterFile("rendezvous.proto", fileDescriptor_ef0a1d5737df1c36) }
var fileDescriptor_ef0a1d5737df1c36 = []byte{ var fileDescriptor_ef0a1d5737df1c36 = []byte{
// 543 bytes of a gzipped FileDescriptorProto // 528 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x54, 0xcb, 0x6e, 0xd3, 0x40, 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x54, 0xcd, 0x6e, 0xd3, 0x4c,
0x14, 0xcd, 0xd8, 0x69, 0x48, 0x6f, 0x49, 0x34, 0x9d, 0x52, 0x11, 0x65, 0x11, 0xaa, 0x48, 0xa8, 0x14, 0xcd, 0xd8, 0x69,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, 0x09, 0x1c, 0xd9, 0xfa, 0x50, 0x37, 0x2d, 0x7d, 0xdf, 0xe2, 0xf4, 0xa3, 0xba, 0x4f, 0x2f, 0xa7,
0xfa, 0xa6, 0x36, 0xd0, 0x74, 0x43, 0x3b, 0x36, 0x38, 0xfd, 0xa4, 0x1e, 0xd3, 0xab, 0x59, 0x8b, 0x1a, 0xf9, 0x3e, 0xd5, 0xc8, 0x8f, 0xa9, 0x46, 0x3e, 0xfd, 0xd4, 0x6a, 0xc7, 0x8d, 0xfc, 0xbf,
0xfc, 0x98, 0xb5, 0xc8, 0xcf, 0x59, 0x8b, 0x7c, 0xfe, 0xd5, 0x2a, 0x8d, 0x2a, 0xe9, 0x57, 0xe4, 0xf2, 0xf0, 0x57, 0x00, 0x00, 0x00, 0xff, 0xff, 0x4c, 0x11, 0x6e, 0xa0, 0x6b, 0x04, 0x00, 0x00,
0xf1, 0xef, 0x00, 0x00, 0x00, 0xff, 0xff, 0x12, 0xe2, 0x82, 0x67, 0x82, 0x04, 0x00, 0x00,
} }
func (m *Message) Marshal() (dAtA []byte, err error) { func (m *Message) Marshal() (dAtA []byte, err error) {
@ -576,15 +574,10 @@ func (m *Message_Register) MarshalToSizedBuffer(dAtA []byte) (int, error) {
i-- i--
dAtA[i] = 0x18 dAtA[i] = 0x18
} }
if m.Peer != nil { if len(m.SignedPeerRecord) > 0 {
{ i -= len(m.SignedPeerRecord)
size, err := m.Peer.MarshalToSizedBuffer(dAtA[:i]) copy(dAtA[i:], m.SignedPeerRecord)
if err != nil { i = encodeVarintRendezvous(dAtA, i, uint64(len(m.SignedPeerRecord)))
return 0, err
}
i -= size
i = encodeVarintRendezvous(dAtA, i, uint64(size))
}
i-- i--
dAtA[i] = 0x12 dAtA[i] = 0x12
} }
@ -786,8 +779,8 @@ func (m *Message_Register) Size() (n int) {
if l > 0 { if l > 0 {
n += 1 + l + sovRendezvous(uint64(l)) n += 1 + l + sovRendezvous(uint64(l))
} }
if m.Peer != nil { l = len(m.SignedPeerRecord)
l = m.Peer.Size() if l > 0 {
n += 1 + l + sovRendezvous(uint64(l)) n += 1 + l + sovRendezvous(uint64(l))
} }
if m.Ttl != 0 { if m.Ttl != 0 {
@ -1148,9 +1141,9 @@ func (m *Message_Register) Unmarshal(dAtA []byte) error {
iNdEx = postIndex iNdEx = postIndex
case 2: case 2:
if wireType != 2 { if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field Peer", wireType) return fmt.Errorf("proto: wrong wireType = %d for field SignedPeerRecord", wireType)
} }
var msglen int var byteLen int
for shift := uint(0); ; shift += 7 { for shift := uint(0); ; shift += 7 {
if shift >= 64 { if shift >= 64 {
return ErrIntOverflowRendezvous return ErrIntOverflowRendezvous
@ -1160,26 +1153,24 @@ func (m *Message_Register) Unmarshal(dAtA []byte) error {
} }
b := dAtA[iNdEx] b := dAtA[iNdEx]
iNdEx++ iNdEx++
msglen |= int(b&0x7F) << shift byteLen |= int(b&0x7F) << shift
if b < 0x80 { if b < 0x80 {
break break
} }
} }
if msglen < 0 { if byteLen < 0 {
return ErrInvalidLengthRendezvous return ErrInvalidLengthRendezvous
} }
postIndex := iNdEx + msglen postIndex := iNdEx + byteLen
if postIndex < 0 { if postIndex < 0 {
return ErrInvalidLengthRendezvous return ErrInvalidLengthRendezvous
} }
if postIndex > l { if postIndex > l {
return io.ErrUnexpectedEOF return io.ErrUnexpectedEOF
} }
if m.Peer == nil { m.SignedPeerRecord = append(m.SignedPeerRecord[:0], dAtA[iNdEx:postIndex]...)
m.Peer = &pb.Envelope{} if m.SignedPeerRecord == nil {
} m.SignedPeerRecord = []byte{}
if err := m.Peer.Unmarshal(dAtA[iNdEx:postIndex]); err != nil {
return err
} }
iNdEx = postIndex iNdEx = postIndex
case 3: case 3:

View File

@ -2,8 +2,6 @@ syntax = "proto3";
package rendezvous.pb; package rendezvous.pb;
import "record/pb/envelope.proto";
message Message { message Message {
enum MessageType { enum MessageType {
REGISTER = 0; REGISTER = 0;
@ -24,7 +22,7 @@ message Message {
message Register { message Register {
string ns = 1; string ns = 1;
record.pb.Envelope peer = 2; bytes signedPeerRecord = 2;
int64 ttl = 3; // in seconds int64 ttl = 3; // in seconds
} }

View File

@ -5,10 +5,8 @@ import (
"fmt" "fmt"
"time" "time"
"github.com/golang/protobuf/proto"
libp2pCrypto "github.com/libp2p/go-libp2p-core/crypto" libp2pCrypto "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/record" "github.com/libp2p/go-libp2p-core/record"
record_pb "github.com/libp2p/go-libp2p-core/record/pb"
pb "github.com/status-im/go-waku-rendezvous/pb" pb "github.com/status-im/go-waku-rendezvous/pb"
@ -61,12 +59,7 @@ func newRegisterMessage(privKey libp2pCrypto.PrivKey, ns string, pi peer.AddrInf
return nil, err return nil, err
} }
peerEnvelop := &record_pb.Envelope{} msg.Register.SignedPeerRecord = envPayload
if err = proto.Unmarshal(envPayload, peerEnvelop); err != nil {
return nil, err
}
msg.Register.Peer = peerEnvelop
return msg, nil return msg, nil
} }
@ -85,33 +78,7 @@ func newDiscoverMessage(ns string, limit int) *pb.Message {
return msg return msg
} }
func marshalEnvelope(pbEnvelope *record_pb.Envelope) ([]byte, error) { func unmarshalSignedPeerRecord(envelopeBytes []byte) (peer.AddrInfo, error) {
return proto.Marshal(pbEnvelope)
}
func pbToEnvelope(pbEnvelope *record_pb.Envelope) (*record.Envelope, error) {
if pbEnvelope == nil {
return nil, errors.New("missing envelope information")
}
envelopeBytes, err := proto.Marshal(pbEnvelope)
if err != nil {
return nil, err
}
return record.UnmarshalEnvelope(envelopeBytes)
}
func pbToPeerRecord(pbEnvelope *record_pb.Envelope) (peer.AddrInfo, error) {
if pbEnvelope == nil {
return peer.AddrInfo{}, errors.New("missing envelope information")
}
envelopeBytes, err := proto.Marshal(pbEnvelope)
if err != nil {
return peer.AddrInfo{}, err
}
envelope, rec, err := record.ConsumeEnvelope(envelopeBytes, peer.PeerRecordEnvelopeDomain) envelope, rec, err := record.ConsumeEnvelope(envelopeBytes, peer.PeerRecordEnvelopeDomain)
if err != nil { if err != nil {
return peer.AddrInfo{}, err return peer.AddrInfo{}, err
@ -150,16 +117,10 @@ func newDiscoverResponse(regs []RegistrationRecord) (*pb.Message_DiscoverRespons
rregs := make([]*pb.Message_Register, len(regs)) rregs := make([]*pb.Message_Register, len(regs))
for i, reg := range regs { for i, reg := range regs {
var env = &record_pb.Envelope{}
if err := env.Unmarshal(reg.PeerEnvelope); err != nil {
return nil, err
}
rreg := new(pb.Message_Register) rreg := new(pb.Message_Register)
rns := reg.Ns rns := reg.Ns
rreg.Ns = rns rreg.Ns = rns
rreg.Peer = env rreg.SignedPeerRecord = reg.PeerEnvelope
rttl := int64(reg.Ttl) rttl := int64(reg.Ttl)
rreg.Ttl = rttl rreg.Ttl = rttl
rregs[i] = rreg rregs[i] = rreg

13
svc.go
View File

@ -157,12 +157,12 @@ func (rz *RendezvousService) handleRegister(p peer.ID, m *pb.Message_Register) *
return newRegisterResponseError(pb.Message_E_INVALID_NAMESPACE, "namespace too long") return newRegisterResponseError(pb.Message_E_INVALID_NAMESPACE, "namespace too long")
} }
mpi := m.GetPeer() mpi := m.GetSignedPeerRecord()
if mpi == nil { if mpi == nil {
return newRegisterResponseError(pb.Message_E_INVALID_PEER_INFO, "missing peer info") return newRegisterResponseError(pb.Message_E_INVALID_PEER_INFO, "missing signed peer record")
} }
peerRecord, err := pbToPeerRecord(mpi) peerRecord, err := unmarshalSignedPeerRecord(mpi)
if err != nil { if err != nil {
return newRegisterResponseError(pb.Message_E_INVALID_PEER_INFO, "invalid peer record") return newRegisterResponseError(pb.Message_E_INVALID_PEER_INFO, "invalid peer record")
} }
@ -199,12 +199,7 @@ func (rz *RendezvousService) handleRegister(p peer.ID, m *pb.Message_Register) *
deadline := time.Now().Add(time.Duration(ttl)).Add(networkDelay) deadline := time.Now().Add(time.Duration(ttl)).Add(networkDelay)
envPayload, err := marshalEnvelope(mpi) key, err := rz.storage.Add(ns, peerRecord.ID, mpi, ttl, deadline)
if err != nil {
return newRegisterResponseError(pb.Message_E_INTERNAL_ERROR, err.Error())
}
key, err := rz.storage.Add(ns, peerRecord.ID, envPayload, ttl, deadline)
if err != nil { if err != nil {
return newRegisterResponseError(pb.Message_E_INTERNAL_ERROR, err.Error()) return newRegisterResponseError(pb.Message_E_INTERNAL_ERROR, err.Error())
} }