2018-07-04 10:51:47 +00:00
|
|
|
package discovery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
2018-09-24 06:17:06 +00:00
|
|
|
"errors"
|
2018-07-04 10:51:47 +00:00
|
|
|
"math/rand"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/discv5"
|
2018-11-14 07:03:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2018-07-04 10:51:47 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
|
|
"github.com/status-im/rendezvous"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
registrationPeriod = 10 * time.Second
|
2018-08-17 06:25:55 +00:00
|
|
|
requestTimeout = 5 * time.Second
|
2018-07-04 10:51:47 +00:00
|
|
|
bucketSize = 10
|
|
|
|
)
|
|
|
|
|
2018-09-24 06:17:06 +00:00
|
|
|
var (
|
2018-10-16 09:31:44 +00:00
|
|
|
errNodeIsNil = errors.New("node cannot be nil")
|
|
|
|
errIdentityIsNil = errors.New("identity cannot be nil")
|
|
|
|
errDiscoveryIsStopped = errors.New("discovery is stopped")
|
2018-09-24 06:17:06 +00:00
|
|
|
)
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
func NewRendezvous(servers []ma.Multiaddr, identity *ecdsa.PrivateKey, node *enode.Node) (*Rendezvous, error) {
|
2018-07-04 10:51:47 +00:00
|
|
|
r := new(Rendezvous)
|
2018-09-24 06:17:06 +00:00
|
|
|
r.node = node
|
|
|
|
r.identity = identity
|
2018-07-04 10:51:47 +00:00
|
|
|
r.servers = servers
|
|
|
|
r.registrationPeriod = registrationPeriod
|
|
|
|
r.bucketSize = bucketSize
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2018-08-24 03:17:32 +00:00
|
|
|
func NewRendezvousWithENR(servers []ma.Multiaddr, record enr.Record) *Rendezvous {
|
|
|
|
r := new(Rendezvous)
|
|
|
|
r.servers = servers
|
|
|
|
r.registrationPeriod = registrationPeriod
|
|
|
|
r.bucketSize = bucketSize
|
2018-09-24 06:17:06 +00:00
|
|
|
r.record = &record
|
2018-08-24 03:17:32 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
// Rendezvous is an implementation of discovery interface that uses
|
|
|
|
// rendezvous client.
|
|
|
|
type Rendezvous struct {
|
|
|
|
mu sync.RWMutex
|
|
|
|
client *rendezvous.Client
|
|
|
|
|
2018-08-17 06:25:55 +00:00
|
|
|
// Root context is used to cancel running requests
|
|
|
|
// when Rendezvous is stopped.
|
|
|
|
rootCtx context.Context
|
|
|
|
cancelRootCtx context.CancelFunc
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
servers []ma.Multiaddr
|
|
|
|
registrationPeriod time.Duration
|
|
|
|
bucketSize int
|
2018-11-14 07:03:58 +00:00
|
|
|
node *enode.Node
|
2018-09-24 06:17:06 +00:00
|
|
|
identity *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
recordMu sync.Mutex
|
|
|
|
record *enr.Record // record is set directly if rendezvous is used in proxy mode
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Rendezvous) Running() bool {
|
|
|
|
r.mu.RLock()
|
|
|
|
defer r.mu.RUnlock()
|
|
|
|
return r.client != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start creates client with ephemeral identity.
|
|
|
|
func (r *Rendezvous) Start() error {
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
2018-09-24 05:47:24 +00:00
|
|
|
client, err := rendezvous.NewEphemeral()
|
2018-07-04 10:51:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r.client = &client
|
2018-08-17 06:25:55 +00:00
|
|
|
r.rootCtx, r.cancelRootCtx = context.WithCancel(context.Background())
|
2018-07-04 10:51:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop removes client reference.
|
|
|
|
func (r *Rendezvous) Stop() error {
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
2018-08-17 06:25:55 +00:00
|
|
|
r.cancelRootCtx()
|
2018-07-04 10:51:47 +00:00
|
|
|
r.client = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-24 06:17:06 +00:00
|
|
|
func (r *Rendezvous) MakeRecord() (record enr.Record, err error) {
|
|
|
|
r.recordMu.Lock()
|
|
|
|
defer r.recordMu.Unlock()
|
|
|
|
if r.record != nil {
|
|
|
|
return *r.record, nil
|
|
|
|
}
|
|
|
|
if r.node == nil {
|
|
|
|
return record, errNodeIsNil
|
|
|
|
}
|
|
|
|
if r.identity == nil {
|
|
|
|
return record, errIdentityIsNil
|
|
|
|
}
|
2018-11-14 07:03:58 +00:00
|
|
|
record.Set(enr.IP(r.node.IP()))
|
|
|
|
record.Set(enr.TCP(r.node.TCP()))
|
|
|
|
record.Set(enr.UDP(r.node.UDP()))
|
2018-09-24 06:17:06 +00:00
|
|
|
// public key is added to ENR when ENR is signed
|
2018-11-14 07:03:58 +00:00
|
|
|
if err := enode.SignV4(&record, r.identity); err != nil {
|
2018-09-24 06:17:06 +00:00
|
|
|
return record, err
|
|
|
|
}
|
|
|
|
r.record = &record
|
|
|
|
return record, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Rendezvous) register(topic string, record enr.Record) error {
|
2018-07-04 10:51:47 +00:00
|
|
|
srv := r.servers[rand.Intn(len(r.servers))]
|
2018-08-17 06:25:55 +00:00
|
|
|
ctx, cancel := context.WithTimeout(r.rootCtx, requestTimeout)
|
2018-08-21 08:31:38 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
r.mu.RLock()
|
|
|
|
defer r.mu.RUnlock()
|
2018-10-16 09:31:44 +00:00
|
|
|
if r.client == nil {
|
|
|
|
return errDiscoveryIsStopped
|
|
|
|
}
|
2018-09-24 06:17:06 +00:00
|
|
|
err := r.client.Register(ctx, srv, topic, record, r.registrationPeriod)
|
2018-08-21 08:31:38 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("error registering", "topic", topic, "rendezvous server", srv, "err", err)
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
2018-08-21 08:31:38 +00:00
|
|
|
return err
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register renews registration in the specified server.
|
|
|
|
func (r *Rendezvous) Register(topic string, stop chan struct{}) error {
|
2018-09-24 06:17:06 +00:00
|
|
|
record, err := r.MakeRecord()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-12 07:24:18 +00:00
|
|
|
// sending registration more often than the whole registraton period
|
|
|
|
// will ensure that it won't be accidentally removed
|
|
|
|
ticker := time.NewTicker(r.registrationPeriod / 2)
|
2018-07-04 10:51:47 +00:00
|
|
|
defer ticker.Stop()
|
2018-08-21 08:31:38 +00:00
|
|
|
|
2018-09-24 06:17:06 +00:00
|
|
|
if err := r.register(topic, record); err == context.Canceled {
|
2018-08-21 08:31:38 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
return nil
|
|
|
|
case <-ticker.C:
|
2018-09-24 06:17:06 +00:00
|
|
|
if err := r.register(topic, record); err == context.Canceled {
|
2018-08-21 08:31:38 +00:00
|
|
|
return err
|
2018-10-16 09:31:44 +00:00
|
|
|
} else if err == errDiscoveryIsStopped {
|
|
|
|
return nil
|
2018-08-21 08:31:38 +00:00
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 09:31:44 +00:00
|
|
|
func (r *Rendezvous) discoverRequest(srv ma.Multiaddr, topic string) ([]enr.Record, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(r.rootCtx, requestTimeout)
|
|
|
|
defer cancel()
|
|
|
|
r.mu.RLock()
|
|
|
|
defer r.mu.RUnlock()
|
|
|
|
if r.client == nil {
|
|
|
|
return nil, errDiscoveryIsStopped
|
|
|
|
}
|
|
|
|
return r.client.Discover(ctx, srv, topic, r.bucketSize)
|
|
|
|
}
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
// Discover will search for new records every time period fetched from period channel.
|
|
|
|
func (r *Rendezvous) Discover(
|
2018-08-17 06:25:55 +00:00
|
|
|
topic string, period <-chan time.Duration, found chan<- *discv5.Node, lookup chan<- bool,
|
|
|
|
) error {
|
2018-07-04 10:51:47 +00:00
|
|
|
ticker := time.NewTicker(<-period)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case newPeriod, ok := <-period:
|
|
|
|
ticker.Stop()
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ticker = time.NewTicker(newPeriod)
|
|
|
|
case <-ticker.C:
|
|
|
|
srv := r.servers[rand.Intn(len(r.servers))]
|
2018-10-16 09:31:44 +00:00
|
|
|
records, err := r.discoverRequest(srv, topic)
|
2018-08-21 08:31:38 +00:00
|
|
|
if err == context.Canceled {
|
|
|
|
return err
|
2018-10-16 09:31:44 +00:00
|
|
|
} else if err == errDiscoveryIsStopped {
|
|
|
|
return nil
|
2018-08-21 08:31:38 +00:00
|
|
|
} else if err != nil {
|
2018-07-04 10:51:47 +00:00
|
|
|
log.Debug("error fetching records", "topic", topic, "rendezvous server", srv, "err", err)
|
2018-10-16 09:31:44 +00:00
|
|
|
} else {
|
|
|
|
for i := range records {
|
|
|
|
n, err := enrToNode(records[i])
|
|
|
|
log.Debug("converted enr to", "ENODE", n.String())
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("error converting enr record to node", "err", err)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
select {
|
|
|
|
case found <- n:
|
|
|
|
case newPeriod, ok := <-period:
|
|
|
|
// closing a period channel is a signal to producer that consumer exited
|
|
|
|
ticker.Stop()
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ticker = time.NewTicker(newPeriod)
|
|
|
|
}
|
2018-09-24 06:52:26 +00:00
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func enrToNode(record enr.Record) (*discv5.Node, error) {
|
|
|
|
var (
|
2018-11-14 07:03:58 +00:00
|
|
|
key enode.Secp256k1
|
2018-08-24 03:17:32 +00:00
|
|
|
ip enr.IP
|
|
|
|
tport enr.TCP
|
|
|
|
uport enr.UDP
|
|
|
|
proxied Proxied
|
|
|
|
nodeID discv5.NodeID
|
2018-07-04 10:51:47 +00:00
|
|
|
)
|
2018-08-24 03:17:32 +00:00
|
|
|
if err := record.Load(&proxied); err == nil {
|
|
|
|
nodeID = discv5.NodeID(proxied)
|
|
|
|
} else {
|
|
|
|
if err := record.Load(&key); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ecdsaKey := ecdsa.PublicKey(key)
|
|
|
|
nodeID = discv5.PubkeyID(&ecdsaKey)
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
if err := record.Load(&ip); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := record.Load(&tport); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// ignore absence of udp port, as it is optional
|
|
|
|
_ = record.Load(&uport)
|
2018-08-24 03:17:32 +00:00
|
|
|
return discv5.NewNode(nodeID, net.IP(ip), uint16(uport), uint16(tport)), nil
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|