mirror of
https://github.com/logos-messaging/go-waku-rendezvous.git
synced 2026-02-12 09:13:08 +00:00
fix: proto gen script
This commit is contained in:
parent
39d264435e
commit
da00a45856
@ -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
2
go.mod
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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.
|
||||
|
||||
11
proto.go
11
proto.go
@ -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
46
svc.go
@ -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)
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user