fix: proto gen script

This commit is contained in:
Richard Ramos 2021-10-01 15:31:29 -04:00
parent 39d264435e
commit da00a45856
No known key found for this signature in database
GPG Key ID: 80D4B01265FDFE8F
6 changed files with 35 additions and 36 deletions

View File

@ -21,9 +21,8 @@ type rendezvousDiscovery struct {
}
type discoveryCache struct {
recs map[peer.ID]*peerRecord
cookie []byte
mux sync.Mutex
recs map[peer.ID]*peerRecord
mux sync.Mutex
}
type peerRecord struct {

2
go.mod
View File

@ -8,7 +8,7 @@ require (
github.com/ipfs/go-log/v2 v2.0.5
github.com/kr/pretty v0.2.0 // indirect
github.com/libp2p/go-libp2p-core v0.8.5
github.com/multiformats/go-multiaddr v0.3.1
github.com/multiformats/go-multiaddr v0.3.1 // indirect
github.com/onsi/ginkgo v1.12.0 // indirect
github.com/onsi/gomega v1.9.0 // indirect
github.com/pkg/errors v0.9.1 // indirect

View File

@ -1,2 +1,5 @@
#!/bin/bash
protoc --gofast_out=. --proto_path=$(go list -f '{{ .Dir }}' -m github.com/libp2p/go-libp2p-core) --proto_path=. rendezvous.proto
# 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,7 @@ import (
io "io"
math "math"
math_bits "math/bits"
pb "record/pb"
pb "github.com/libp2p/go-libp2p-core/record/pb"
)
// Reference imports to suppress errors if they are not otherwise used.

View File

@ -61,7 +61,12 @@ func newRegisterMessage(privKey libp2pCrypto.PrivKey, ns string, pi peer.AddrInf
return nil, err
}
msg.Register.Peer = envPayload
var peerEnvelop *record_pb.Envelope
if err = proto.Unmarshal(envPayload, peerEnvelop); err != nil {
return nil, err
}
msg.Register.Peer = peerEnvelop
return msg, nil
}
@ -80,6 +85,10 @@ func newDiscoverMessage(ns string, limit int) *pb.Message {
return msg
}
func marshalEnvelope(pbEnvelope *record_pb.Envelope) ([]byte, error) {
return proto.Marshal(pbEnvelope)
}
func pbToPeerRecord(pbEnvelope *record_pb.Envelope) (peer.AddrInfo, error) {
if pbEnvelope == nil {
return peer.AddrInfo{}, errors.New("missing envelope information")

46
svc.go
View File

@ -26,22 +26,15 @@ type RendezvousService struct {
h host.Host
storage Storage
cleaner *Cleaner
rzs []RendezvousSync
wg sync.WaitGroup
quit chan struct{}
}
type RendezvousSync interface {
Register(p peer.ID, ns string, addrs [][]byte, ttl int)
Unregister(p peer.ID, ns string)
}
func NewRendezvousService(host host.Host, storage Storage, rzs ...RendezvousSync) *RendezvousService {
func NewRendezvousService(host host.Host, storage Storage) *RendezvousService {
rz := &RendezvousService{
storage: storage,
h: host,
cleaner: NewCleaner(),
rzs: rzs,
}
return rz
@ -169,28 +162,22 @@ func (rz *RendezvousService) handleRegister(p peer.ID, m *pb.Message_Register) *
return newRegisterResponseError(pb.Message_E_INVALID_PEER_INFO, "missing peer info")
}
mpid := mpi.GetId()
var mp peer.ID
if mpid != nil {
var err error
mp, err = peer.IDFromBytes(mpid)
if err != nil {
return newRegisterResponseError(pb.Message_E_INVALID_PEER_INFO, "bad peer id")
}
if mp != p {
return newRegisterResponseError(pb.Message_E_INVALID_PEER_INFO, "peer id mismatch")
}
peerRecord, err := pbToPeerRecord(mpi)
if err != nil {
return newRegisterResponseError(pb.Message_E_INVALID_PEER_INFO, "invalid peer record")
}
maddrs := mpi.GetAddrs()
if len(maddrs) == 0 {
if peerRecord.ID != p {
return newRegisterResponseError(pb.Message_E_INVALID_PEER_INFO, "peer id mismatch")
}
if len(peerRecord.Addrs) == 0 {
return newRegisterResponseError(pb.Message_E_INVALID_PEER_INFO, "missing peer addresses")
}
mlen := 0
for _, maddr := range maddrs {
mlen += len(maddr)
for _, maddr := range peerRecord.Addrs {
mlen += len(maddr.Bytes())
}
if mlen > MaxPeerAddressLength {
return newRegisterResponseError(pb.Message_E_INVALID_PEER_INFO, "peer info too long")
@ -212,7 +199,12 @@ func (rz *RendezvousService) handleRegister(p peer.ID, m *pb.Message_Register) *
deadline := time.Now().Add(time.Duration(ttl)).Add(networkDelay)
key, err := rz.storage.Add(ns, mp, maddrs, ttl, deadline)
envPayload, err := marshalEnvelope(mpi)
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 {
return newRegisterResponseError(pb.Message_E_INTERNAL_ERROR, err.Error())
}
@ -226,10 +218,6 @@ func (rz *RendezvousService) handleRegister(p peer.ID, m *pb.Message_Register) *
log.Infof("registered peer %s %s (%d)", p, ns, ttl)
for _, rzs := range rz.rzs {
rzs.Register(p, ns, maddrs, ttl)
}
return newRegisterResponse(ttl)
}