2023-03-09 15:48:25 +00:00
|
|
|
package rendezvous
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-03-09 22:42:50 +00:00
|
|
|
"math"
|
2023-03-14 00:37:28 +00:00
|
|
|
"math/rand"
|
2023-03-09 22:42:50 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2023-03-09 15:48:25 +00:00
|
|
|
|
|
|
|
rvs "github.com/berty/go-libp2p-rendezvous"
|
|
|
|
"github.com/libp2p/go-libp2p/core/host"
|
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2023-03-09 22:42:50 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/relay"
|
2023-03-09 15:48:25 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
const RendezvousID = rvs.RendezvousProto
|
|
|
|
|
2023-03-14 00:37:28 +00:00
|
|
|
type rendezvousPoint struct {
|
|
|
|
sync.RWMutex
|
|
|
|
|
|
|
|
id peer.ID
|
|
|
|
cookie []byte
|
|
|
|
}
|
|
|
|
|
2023-03-09 15:48:25 +00:00
|
|
|
type Rendezvous struct {
|
2023-03-09 22:42:50 +00:00
|
|
|
host host.Host
|
|
|
|
|
|
|
|
enableServer bool
|
2023-03-09 15:48:25 +00:00
|
|
|
db *DB
|
|
|
|
rendezvousSvc *rvs.RendezvousService
|
|
|
|
|
2023-03-09 22:42:50 +00:00
|
|
|
discoverPeers bool
|
2023-03-14 00:37:28 +00:00
|
|
|
rendezvousPoints []*rendezvousPoint
|
2023-03-09 22:42:50 +00:00
|
|
|
peerConnector PeerConnector
|
|
|
|
|
|
|
|
log *zap.Logger
|
|
|
|
wg sync.WaitGroup
|
|
|
|
cancel context.CancelFunc
|
2023-03-09 15:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PeerConnector interface {
|
|
|
|
PeerChannel() chan<- peer.AddrInfo
|
|
|
|
}
|
|
|
|
|
2023-04-17 00:04:12 +00:00
|
|
|
func NewRendezvous(enableServer bool, db *DB, discoverPeers bool, rendezvousPoints []peer.ID, peerConnector PeerConnector, log *zap.Logger) *Rendezvous {
|
2023-03-09 15:48:25 +00:00
|
|
|
logger := log.Named("rendezvous")
|
|
|
|
|
2023-03-14 00:37:28 +00:00
|
|
|
var rendevousPoints []*rendezvousPoint
|
|
|
|
for _, rp := range rendezvousPoints {
|
|
|
|
rendevousPoints = append(rendevousPoints, &rendezvousPoint{
|
|
|
|
id: rp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-09 15:48:25 +00:00
|
|
|
return &Rendezvous{
|
2023-03-09 22:42:50 +00:00
|
|
|
enableServer: enableServer,
|
|
|
|
db: db,
|
|
|
|
discoverPeers: discoverPeers,
|
|
|
|
rendezvousPoints: rendevousPoints,
|
|
|
|
peerConnector: peerConnector,
|
|
|
|
log: logger,
|
2023-03-09 15:48:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-17 00:04:12 +00:00
|
|
|
// Sets the host to be able to mount or consume a protocol
|
|
|
|
func (r *Rendezvous) SetHost(h host.Host) {
|
|
|
|
r.host = h
|
|
|
|
}
|
|
|
|
|
2023-03-09 15:48:25 +00:00
|
|
|
func (r *Rendezvous) Start(ctx context.Context) error {
|
2023-03-09 22:42:50 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
r.cancel = cancel
|
|
|
|
|
|
|
|
if r.enableServer {
|
2023-03-14 00:37:28 +00:00
|
|
|
err := r.db.Start(ctx)
|
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-09 22:42:50 +00:00
|
|
|
r.rendezvousSvc = rvs.NewRendezvousService(r.host, r.db)
|
|
|
|
}
|
|
|
|
|
2023-03-14 00:37:28 +00:00
|
|
|
r.wg.Add(1)
|
|
|
|
go r.register(ctx)
|
|
|
|
|
2023-03-09 22:42:50 +00:00
|
|
|
if r.discoverPeers {
|
|
|
|
r.wg.Add(1)
|
2023-03-14 00:37:28 +00:00
|
|
|
go r.discover(ctx)
|
2023-03-09 22:42:50 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 15:48:25 +00:00
|
|
|
r.log.Info("rendezvous protocol started")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-09 22:42:50 +00:00
|
|
|
const registerBackoff = 200 * time.Millisecond
|
|
|
|
const registerMaxRetries = 7
|
|
|
|
|
2023-03-14 00:37:28 +00:00
|
|
|
func (r *Rendezvous) getRandomServer() *rendezvousPoint {
|
|
|
|
return r.rendezvousPoints[rand.Intn(len(r.rendezvousPoints))] // nolint: gosec
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Rendezvous) discover(ctx context.Context) {
|
|
|
|
defer r.wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
server := r.getRandomServer()
|
|
|
|
|
|
|
|
rendezvousClient := rvs.NewRendezvousClient(r.host, server.id)
|
|
|
|
|
|
|
|
addrInfo, cookie, err := rendezvousClient.Discover(ctx, relay.DefaultWakuTopic, 5, server.cookie)
|
|
|
|
if err != nil {
|
|
|
|
r.log.Error("could not discover new peers", zap.Error(err))
|
|
|
|
cookie = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(addrInfo) != 0 {
|
|
|
|
server.Lock()
|
|
|
|
server.cookie = cookie
|
|
|
|
server.Unlock()
|
|
|
|
|
|
|
|
for _, addr := range addrInfo {
|
|
|
|
r.peerConnector.PeerChannel() <- addr
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO: change log level to DEBUG in go-libp2p-rendezvous@v0.4.1/svc.go:234 discover query
|
|
|
|
// TODO: improve this by adding an exponential backoff?
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-09 22:42:50 +00:00
|
|
|
func (r *Rendezvous) callRegister(ctx context.Context, rendezvousClient rvs.RendezvousClient, retries int) (<-chan time.Time, int) {
|
|
|
|
ttl, err := rendezvousClient.Register(ctx, relay.DefaultWakuTopic, rvs.DefaultTTL) // TODO: determine which topic to use
|
|
|
|
var t <-chan time.Time
|
|
|
|
if err != nil {
|
|
|
|
r.log.Error("registering rendezvous client", zap.Error(err))
|
|
|
|
backoff := registerBackoff * time.Duration(math.Exp2(float64(retries)))
|
|
|
|
t = time.After(backoff)
|
|
|
|
retries++
|
|
|
|
} else {
|
|
|
|
t = time.After(ttl)
|
|
|
|
}
|
|
|
|
|
|
|
|
return t, retries
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Rendezvous) register(ctx context.Context) {
|
2023-03-14 00:37:28 +00:00
|
|
|
defer r.wg.Done()
|
|
|
|
|
2023-03-09 22:42:50 +00:00
|
|
|
for _, m := range r.rendezvousPoints {
|
|
|
|
r.wg.Add(1)
|
2023-03-14 00:37:28 +00:00
|
|
|
go func(m *rendezvousPoint) {
|
2023-03-09 22:42:50 +00:00
|
|
|
r.wg.Done()
|
|
|
|
|
2023-03-14 00:37:28 +00:00
|
|
|
rendezvousClient := rvs.NewRendezvousClient(r.host, m.id)
|
2023-03-09 22:42:50 +00:00
|
|
|
retries := 0
|
|
|
|
var t <-chan time.Time
|
|
|
|
|
|
|
|
t, retries = r.callRegister(ctx, rendezvousClient, retries)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-t:
|
|
|
|
t, retries = r.callRegister(ctx, rendezvousClient, retries)
|
|
|
|
if retries >= registerMaxRetries {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-09 15:48:25 +00:00
|
|
|
func (r *Rendezvous) Stop() {
|
2023-03-09 22:42:50 +00:00
|
|
|
r.cancel()
|
|
|
|
r.wg.Wait()
|
2023-03-09 15:48:25 +00:00
|
|
|
r.host.RemoveStreamHandler(rvs.RendezvousProto)
|
|
|
|
r.rendezvousSvc = nil
|
|
|
|
}
|