2021-03-11 20:27:12 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-09-11 11:36:54 +00:00
|
|
|
|
|
|
|
//"log/syslog"
|
|
|
|
//"strconv"
|
2021-03-15 16:07:23 +00:00
|
|
|
"sync"
|
2021-03-15 23:59:18 +00:00
|
|
|
"time"
|
2021-03-11 20:27:12 +00:00
|
|
|
|
2021-03-15 16:07:23 +00:00
|
|
|
proto "github.com/golang/protobuf/proto"
|
2021-03-22 16:45:13 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
2021-03-11 20:27:12 +00:00
|
|
|
"github.com/libp2p/go-libp2p"
|
2021-09-11 11:36:54 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/protocol/identify"
|
2021-08-31 18:19:49 +00:00
|
|
|
|
2021-06-16 10:14:22 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/event"
|
2021-03-11 20:27:12 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
2021-06-16 10:14:22 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/network"
|
2021-03-18 16:40:47 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2021-08-31 18:19:49 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peerstore"
|
|
|
|
p2pproto "github.com/libp2p/go-libp2p-core/protocol"
|
2021-06-24 13:02:53 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
|
2021-03-11 20:27:12 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2021-06-28 13:20:23 +00:00
|
|
|
"go.opencensus.io/stats"
|
|
|
|
"go.opencensus.io/tag"
|
2021-04-22 00:09:37 +00:00
|
|
|
|
2021-10-01 17:49:50 +00:00
|
|
|
rendezvous "github.com/status-im/go-libp2p-rendezvous"
|
2021-06-28 13:20:23 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/metrics"
|
2021-03-11 20:27:12 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol"
|
2021-06-10 12:59:51 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/filter"
|
2021-04-28 20:10:44 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/lightpush"
|
2021-04-22 00:09:37 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/pb"
|
2021-04-28 20:10:44 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/relay"
|
2021-04-22 00:09:37 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/store"
|
2021-03-24 15:53:43 +00:00
|
|
|
wakurelay "github.com/status-im/go-wakurelay-pubsub"
|
2021-03-11 20:27:12 +00:00
|
|
|
)
|
|
|
|
|
2021-03-22 16:45:13 +00:00
|
|
|
var log = logging.Logger("wakunode")
|
|
|
|
|
2021-09-11 11:36:54 +00:00
|
|
|
//var logwriter, _ = syslog.New(syslog.LOG_ERR|syslog.LOG_LOCAL0, "WAKU")
|
|
|
|
|
2021-03-11 20:27:12 +00:00
|
|
|
type Message []byte
|
|
|
|
|
2021-06-16 10:14:22 +00:00
|
|
|
// A map of peer IDs to supported protocols
|
|
|
|
type PeerStats map[peer.ID][]string
|
|
|
|
|
|
|
|
type ConnStatus struct {
|
|
|
|
IsOnline bool
|
|
|
|
HasHistory bool
|
2021-08-30 16:14:35 +00:00
|
|
|
Peers PeerStats
|
2021-06-16 10:14:22 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 20:27:12 +00:00
|
|
|
type WakuNode struct {
|
2021-09-11 11:36:54 +00:00
|
|
|
host host.Host
|
|
|
|
idService *identify.IDService
|
|
|
|
opts *WakuNodeParameters
|
2021-03-15 16:07:23 +00:00
|
|
|
|
2021-04-28 20:10:44 +00:00
|
|
|
relay *relay.WakuRelay
|
2021-06-10 12:59:51 +00:00
|
|
|
filter *filter.WakuFilter
|
2021-04-28 20:10:44 +00:00
|
|
|
lightPush *lightpush.WakuLightPush
|
2021-04-15 02:19:31 +00:00
|
|
|
|
2021-10-01 17:49:50 +00:00
|
|
|
rendezvous *rendezvous.RendezvousService
|
|
|
|
|
2021-06-24 13:02:53 +00:00
|
|
|
ping *ping.PingService
|
|
|
|
|
2021-04-28 20:10:44 +00:00
|
|
|
subscriptions map[relay.Topic][]*Subscription
|
2021-03-22 16:45:13 +00:00
|
|
|
subscriptionsMutex sync.Mutex
|
2021-03-15 16:07:23 +00:00
|
|
|
|
2021-04-28 20:10:44 +00:00
|
|
|
bcaster Broadcaster
|
2021-04-15 02:19:31 +00:00
|
|
|
|
2021-06-10 12:59:51 +00:00
|
|
|
filters filter.Filters
|
|
|
|
|
2021-06-16 10:14:22 +00:00
|
|
|
connectednessEventSub event.Subscription
|
|
|
|
protocolEventSub event.Subscription
|
|
|
|
identificationEventSub event.Subscription
|
|
|
|
|
2021-04-18 23:41:42 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
2021-06-24 13:02:53 +00:00
|
|
|
quit chan struct{}
|
2021-06-16 10:14:22 +00:00
|
|
|
|
|
|
|
// Map of peers and their supported protocols
|
2021-08-30 16:14:35 +00:00
|
|
|
peers PeerStats
|
|
|
|
peersMutex sync.Mutex
|
|
|
|
|
2021-06-16 10:14:22 +00:00
|
|
|
// Internal protocol implementations that wish
|
|
|
|
// to listen to peer added/removed events (e.g. Filter)
|
|
|
|
peerListeners []chan *event.EvtPeerConnectednessChanged
|
|
|
|
// Channel passed to WakuNode constructor
|
|
|
|
// receiving connection status notifications
|
|
|
|
connStatusChan chan ConnStatus
|
2021-08-10 14:23:49 +00:00
|
|
|
pingEventsChan chan interface{}
|
2021-06-16 10:14:22 +00:00
|
|
|
}
|
|
|
|
|
2021-08-10 14:23:49 +00:00
|
|
|
func (w *WakuNode) handleConnectednessChanged(ev event.EvtPeerConnectednessChanged) {
|
2021-08-31 18:48:09 +00:00
|
|
|
log.Debug("### EvtPeerConnectednessChanged ", w.Host().ID(), " to ", ev.Peer, " : ", ev.Connectedness)
|
2021-08-30 16:14:35 +00:00
|
|
|
|
|
|
|
w.peersMutex.Lock()
|
|
|
|
defer w.peersMutex.Unlock()
|
|
|
|
|
2021-08-10 14:23:49 +00:00
|
|
|
if ev.Connectedness == network.Connected {
|
|
|
|
_, ok := w.peers[ev.Peer]
|
|
|
|
if !ok {
|
|
|
|
peerProtocols, _ := w.host.Peerstore().GetProtocols(ev.Peer)
|
2021-08-31 18:48:09 +00:00
|
|
|
log.Debug("protocols found for peer: ", ev.Peer, ", protocols: ", peerProtocols)
|
2021-08-10 14:23:49 +00:00
|
|
|
w.peers[ev.Peer] = peerProtocols
|
|
|
|
} else {
|
2021-08-31 18:48:09 +00:00
|
|
|
log.Debug("### Peer already exists")
|
2021-08-10 14:23:49 +00:00
|
|
|
}
|
|
|
|
} else if ev.Connectedness == network.NotConnected {
|
2021-08-31 18:48:09 +00:00
|
|
|
log.Debug("Peer down: ", ev.Peer)
|
2021-08-10 14:23:49 +00:00
|
|
|
delete(w.peers, ev.Peer)
|
|
|
|
// for _, pl := range w.peerListeners {
|
|
|
|
// pl <- &ev
|
|
|
|
// }
|
|
|
|
// TODO
|
|
|
|
// There seems to be no proper way to
|
|
|
|
// remove a dropped peer from Host's Peerstore
|
|
|
|
// https://github.com/libp2p/go-libp2p-host/issues/13
|
|
|
|
//w.Host().Network().ClosePeer(ev.Peer)
|
|
|
|
}
|
2021-06-16 10:14:22 +00:00
|
|
|
|
2021-08-10 14:23:49 +00:00
|
|
|
}
|
|
|
|
func (w *WakuNode) handleProtocolsUpdated(ev event.EvtPeerProtocolsUpdated) {
|
2021-08-31 18:48:09 +00:00
|
|
|
log.Debug("### EvtPeerProtocolsUpdated ", w.Host().ID(), " to ", ev.Peer, " added: ", ev.Added, ", removed: ", ev.Removed)
|
2021-08-30 16:14:35 +00:00
|
|
|
|
|
|
|
w.peersMutex.Lock()
|
|
|
|
defer w.peersMutex.Unlock()
|
|
|
|
|
2021-08-10 14:23:49 +00:00
|
|
|
_, ok := w.peers[ev.Peer]
|
|
|
|
if ok {
|
|
|
|
peerProtocols, _ := w.host.Peerstore().GetProtocols(ev.Peer)
|
2021-08-31 18:48:09 +00:00
|
|
|
log.Debug("updated protocols found for peer: ", ev.Peer, ", protocols: ", peerProtocols)
|
2021-08-10 14:23:49 +00:00
|
|
|
w.peers[ev.Peer] = peerProtocols
|
|
|
|
}
|
2021-06-16 10:14:22 +00:00
|
|
|
|
2021-08-10 14:23:49 +00:00
|
|
|
}
|
|
|
|
func (w *WakuNode) handlePeerIdentificationCompleted(ev event.EvtPeerIdentificationCompleted) {
|
2021-08-31 18:48:09 +00:00
|
|
|
log.Debug("### EvtPeerIdentificationCompleted ", w.Host().ID(), " to ", ev.Peer)
|
2021-08-30 16:14:35 +00:00
|
|
|
|
|
|
|
w.peersMutex.Lock()
|
|
|
|
defer w.peersMutex.Unlock()
|
|
|
|
|
2021-08-10 14:23:49 +00:00
|
|
|
peerProtocols, _ := w.host.Peerstore().GetProtocols(ev.Peer)
|
2021-08-31 18:48:09 +00:00
|
|
|
log.Debug("identified protocols found for peer: ", ev.Peer, ", protocols: ", peerProtocols)
|
2021-09-11 11:36:54 +00:00
|
|
|
w.peers[ev.Peer] = peerProtocols
|
2021-08-10 14:23:49 +00:00
|
|
|
}
|
|
|
|
func (w *WakuNode) processHostEvent(e interface{}) {
|
|
|
|
if e == nil {
|
2021-08-31 18:48:09 +00:00
|
|
|
log.Debug("processHostEvent nil event")
|
2021-08-10 14:23:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
isOnline := w.IsOnline()
|
|
|
|
hasHistory := w.HasHistory()
|
2021-08-30 16:14:35 +00:00
|
|
|
switch e := e.(type) {
|
2021-08-10 14:23:49 +00:00
|
|
|
case event.EvtPeerConnectednessChanged:
|
2021-08-30 16:14:35 +00:00
|
|
|
w.handleConnectednessChanged(e)
|
2021-08-10 14:23:49 +00:00
|
|
|
case event.EvtPeerProtocolsUpdated:
|
2021-08-30 16:14:35 +00:00
|
|
|
w.handleProtocolsUpdated(e)
|
2021-08-10 14:23:49 +00:00
|
|
|
case event.EvtPeerIdentificationCompleted:
|
2021-08-30 16:14:35 +00:00
|
|
|
w.handlePeerIdentificationCompleted(e)
|
2021-08-10 14:23:49 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 18:48:09 +00:00
|
|
|
log.Debug("###processHostEvent before isOnline()")
|
2021-08-10 14:23:49 +00:00
|
|
|
newIsOnline := w.IsOnline()
|
2021-08-31 18:48:09 +00:00
|
|
|
log.Debug("###processHostEvent before hasHistory()")
|
2021-08-10 14:23:49 +00:00
|
|
|
newHasHistory := w.HasHistory()
|
2021-08-31 18:48:09 +00:00
|
|
|
log.Debug("###ConnStatus isOnline: ", isOnline, "/", newIsOnline, " hasHistory: ",
|
2021-08-10 14:23:49 +00:00
|
|
|
hasHistory, "/", newHasHistory)
|
2021-08-31 17:27:42 +00:00
|
|
|
if w.connStatusChan != nil {
|
2021-08-31 20:51:22 +00:00
|
|
|
connStatus := ConnStatus{IsOnline: newIsOnline, HasHistory: newHasHistory, Peers: w.Peers()}
|
2021-08-31 18:48:09 +00:00
|
|
|
log.Debug("New ConnStatus: ", connStatus)
|
2021-08-31 17:27:42 +00:00
|
|
|
w.connStatusChan <- connStatus
|
2021-08-10 14:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
func (w *WakuNode) connectednessListener() {
|
|
|
|
for {
|
|
|
|
var e interface{}
|
2021-08-31 18:48:09 +00:00
|
|
|
log.Debug("connectednessListener before select")
|
2021-08-10 14:23:49 +00:00
|
|
|
select {
|
|
|
|
case e = <-w.connectednessEventSub.Out():
|
2021-08-31 18:48:09 +00:00
|
|
|
log.Debug("connectednessListener connectednessEvent")
|
2021-08-10 14:23:49 +00:00
|
|
|
case e = <-w.protocolEventSub.Out():
|
2021-08-31 18:48:09 +00:00
|
|
|
log.Debug("connectednessListener protocolEvent")
|
2021-08-10 14:23:49 +00:00
|
|
|
case e = <-w.identificationEventSub.Out():
|
2021-08-31 18:48:09 +00:00
|
|
|
log.Debug("connectednessListener identificationEvent")
|
2021-08-10 14:23:49 +00:00
|
|
|
case e = <-w.pingEventsChan:
|
2021-08-31 18:48:09 +00:00
|
|
|
log.Debug("connectednessListener pingEvent")
|
2021-06-16 10:14:22 +00:00
|
|
|
}
|
2021-08-31 18:48:09 +00:00
|
|
|
log.Debug("connectednessListener after select")
|
2021-08-10 14:23:49 +00:00
|
|
|
|
|
|
|
w.processHostEvent(e)
|
2021-08-31 18:48:09 +00:00
|
|
|
log.Debug("connectednessListener after processHostEvent")
|
2021-06-16 10:14:22 +00:00
|
|
|
}
|
2021-03-11 20:27:12 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 23:41:42 +00:00
|
|
|
func New(ctx context.Context, opts ...WakuNodeOption) (*WakuNode, error) {
|
|
|
|
params := new(WakuNodeParameters)
|
2021-03-11 20:27:12 +00:00
|
|
|
|
2021-04-18 23:41:42 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
params.libP2POpts = DefaultLibP2POptions
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
err := opt(params)
|
2021-03-11 20:27:12 +00:00
|
|
|
if err != nil {
|
2021-10-01 17:49:50 +00:00
|
|
|
cancel()
|
2021-03-11 20:27:12 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-18 23:41:42 +00:00
|
|
|
if len(params.multiAddr) > 0 {
|
|
|
|
params.libP2POpts = append(params.libP2POpts, libp2p.ListenAddrs(params.multiAddr...))
|
|
|
|
}
|
2021-03-11 20:27:12 +00:00
|
|
|
|
2021-04-18 23:41:42 +00:00
|
|
|
if params.privKey != nil {
|
|
|
|
params.libP2POpts = append(params.libP2POpts, libp2p.Identity(*params.privKey))
|
|
|
|
}
|
2021-03-15 16:07:23 +00:00
|
|
|
|
2021-04-18 23:41:42 +00:00
|
|
|
host, err := libp2p.New(ctx, params.libP2POpts...)
|
2021-03-11 20:27:12 +00:00
|
|
|
if err != nil {
|
2021-10-01 17:49:50 +00:00
|
|
|
cancel()
|
2021-03-11 20:27:12 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
w := new(WakuNode)
|
2021-04-15 02:19:31 +00:00
|
|
|
w.bcaster = NewBroadcaster(1024)
|
2021-03-15 16:07:23 +00:00
|
|
|
w.host = host
|
2021-09-11 11:36:54 +00:00
|
|
|
w.idService = identify.NewIDService(host)
|
2021-03-15 16:07:23 +00:00
|
|
|
w.cancel = cancel
|
|
|
|
w.ctx = ctx
|
2021-04-28 20:10:44 +00:00
|
|
|
w.subscriptions = make(map[relay.Topic][]*Subscription)
|
2021-04-18 23:41:42 +00:00
|
|
|
w.opts = params
|
2021-06-24 13:02:53 +00:00
|
|
|
w.quit = make(chan struct{})
|
2021-06-16 10:14:22 +00:00
|
|
|
w.peers = make(PeerStats)
|
2021-04-18 23:41:42 +00:00
|
|
|
|
2021-06-16 10:14:22 +00:00
|
|
|
// Subscribe to Connectedness events
|
|
|
|
connectednessEventSub, _ := host.EventBus().Subscribe(new(event.EvtPeerConnectednessChanged))
|
|
|
|
w.connectednessEventSub = connectednessEventSub
|
|
|
|
|
|
|
|
protocolEventSub, _ := host.EventBus().Subscribe(new(event.EvtPeerProtocolsUpdated))
|
|
|
|
w.protocolEventSub = protocolEventSub
|
|
|
|
|
|
|
|
identificationEventSub, _ := host.EventBus().Subscribe(new(event.EvtPeerIdentificationCompleted))
|
|
|
|
w.identificationEventSub = identificationEventSub
|
|
|
|
|
|
|
|
if params.connStatusChan != nil {
|
|
|
|
w.connStatusChan = params.connStatusChan
|
2021-06-28 14:14:28 +00:00
|
|
|
}
|
2021-08-10 14:23:49 +00:00
|
|
|
w.pingEventsChan = make(chan interface{})
|
2021-06-16 10:14:22 +00:00
|
|
|
go w.connectednessListener()
|
2021-06-28 14:14:28 +00:00
|
|
|
|
2021-06-16 10:14:22 +00:00
|
|
|
if params.enableStore {
|
|
|
|
w.startStore()
|
2021-04-18 23:41:42 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 12:59:51 +00:00
|
|
|
if params.enableFilter {
|
|
|
|
w.filters = make(filter.Filters)
|
|
|
|
err := w.mountFilter()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 17:49:50 +00:00
|
|
|
if params.enableRendezvous {
|
2021-09-30 16:01:53 +00:00
|
|
|
rendezvous := rendezvous.NewRendezvousDiscovery(w.host)
|
2021-10-01 17:49:50 +00:00
|
|
|
params.wOpts = append(params.wOpts, wakurelay.WithDiscovery(rendezvous, params.rendezvousOpts...))
|
|
|
|
}
|
|
|
|
|
2021-06-16 10:14:22 +00:00
|
|
|
err = w.mountRelay(params.enableRelay, params.wOpts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-28 20:23:03 +00:00
|
|
|
if params.enableLightPush {
|
|
|
|
w.mountLightPush()
|
|
|
|
}
|
|
|
|
|
2021-06-24 13:02:53 +00:00
|
|
|
if params.keepAliveInterval > time.Duration(0) {
|
|
|
|
w.startKeepAlive(params.keepAliveInterval)
|
|
|
|
}
|
|
|
|
|
2021-10-01 17:49:50 +00:00
|
|
|
if params.enableRendezvousServer {
|
|
|
|
err := w.mountRendezvous()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-04 17:05:33 +00:00
|
|
|
for _, addr := range w.ListenAddresses() {
|
2021-04-04 19:33:21 +00:00
|
|
|
log.Info("Listening on ", addr)
|
2021-03-11 20:27:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
2021-03-22 16:45:13 +00:00
|
|
|
func (w *WakuNode) Stop() {
|
|
|
|
w.subscriptionsMutex.Lock()
|
|
|
|
defer w.subscriptionsMutex.Unlock()
|
2021-03-15 16:07:23 +00:00
|
|
|
defer w.cancel()
|
2021-03-22 16:45:13 +00:00
|
|
|
|
2021-06-24 13:02:53 +00:00
|
|
|
close(w.quit)
|
2021-10-01 18:37:52 +00:00
|
|
|
|
2021-06-16 10:14:22 +00:00
|
|
|
defer w.connectednessEventSub.Close()
|
|
|
|
defer w.protocolEventSub.Close()
|
|
|
|
defer w.identificationEventSub.Close()
|
2021-06-24 13:02:53 +00:00
|
|
|
|
2021-10-01 18:37:52 +00:00
|
|
|
if w.rendezvous != nil {
|
|
|
|
w.rendezvous.Stop()
|
|
|
|
}
|
|
|
|
|
2021-04-28 20:10:44 +00:00
|
|
|
for _, topic := range w.relay.Topics() {
|
2021-04-15 02:19:31 +00:00
|
|
|
for _, sub := range w.subscriptions[topic] {
|
|
|
|
sub.Unsubscribe()
|
|
|
|
}
|
2021-03-22 16:45:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.subscriptions = nil
|
2021-10-01 18:37:52 +00:00
|
|
|
|
|
|
|
w.host.Close()
|
2021-03-15 16:07:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WakuNode) Host() host.Host {
|
|
|
|
return w.host
|
|
|
|
}
|
|
|
|
|
2021-03-18 23:21:45 +00:00
|
|
|
func (w *WakuNode) ID() string {
|
|
|
|
return w.host.ID().Pretty()
|
|
|
|
}
|
|
|
|
|
2021-06-16 10:14:22 +00:00
|
|
|
func (w *WakuNode) IsOnline() bool {
|
2021-09-27 12:47:18 +00:00
|
|
|
w.peersMutex.Lock()
|
|
|
|
defer w.peersMutex.Unlock()
|
|
|
|
|
2021-06-16 10:14:22 +00:00
|
|
|
hasRelay := false
|
|
|
|
hasLightPush := false
|
|
|
|
hasStore := false
|
|
|
|
hasFilter := false
|
|
|
|
for _, v := range w.peers {
|
|
|
|
for _, protocol := range v {
|
2021-07-11 18:11:38 +00:00
|
|
|
if !hasRelay && protocol == string(wakurelay.WakuRelayID_v200) {
|
2021-06-16 10:14:22 +00:00
|
|
|
hasRelay = true
|
|
|
|
}
|
2021-09-30 16:01:53 +00:00
|
|
|
if !hasLightPush && protocol == string(lightpush.LightPushID_v20beta1) {
|
2021-06-16 10:14:22 +00:00
|
|
|
hasLightPush = true
|
|
|
|
}
|
2021-09-30 16:01:53 +00:00
|
|
|
if !hasStore && protocol == string(store.StoreID_v20beta3) {
|
2021-06-16 10:14:22 +00:00
|
|
|
hasStore = true
|
|
|
|
}
|
2021-09-30 16:01:53 +00:00
|
|
|
if !hasFilter && protocol == string(filter.FilterID_v20beta1) {
|
2021-06-16 10:14:22 +00:00
|
|
|
hasFilter = true
|
|
|
|
}
|
|
|
|
if hasRelay || hasLightPush && (hasStore || hasFilter) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WakuNode) HasHistory() bool {
|
2021-09-27 12:47:18 +00:00
|
|
|
w.peersMutex.Lock()
|
|
|
|
defer w.peersMutex.Unlock()
|
|
|
|
|
2021-06-16 10:14:22 +00:00
|
|
|
for _, v := range w.peers {
|
|
|
|
for _, protocol := range v {
|
2021-09-30 16:01:53 +00:00
|
|
|
if protocol == string(store.StoreID_v20beta3) {
|
2021-06-16 10:14:22 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-09-30 23:03:19 +00:00
|
|
|
func (w *WakuNode) ListenAddresses() []ma.Multiaddr {
|
2021-04-04 17:05:33 +00:00
|
|
|
hostInfo, _ := ma.NewMultiaddr(fmt.Sprintf("/p2p/%s", w.host.ID().Pretty()))
|
2021-09-30 23:03:19 +00:00
|
|
|
var result []ma.Multiaddr
|
2021-04-04 17:05:33 +00:00
|
|
|
for _, addr := range w.host.Addrs() {
|
2021-09-30 23:03:19 +00:00
|
|
|
result = append(result, addr.Encapsulate(hostInfo))
|
2021-04-04 17:05:33 +00:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-04-28 20:10:44 +00:00
|
|
|
func (w *WakuNode) Relay() *relay.WakuRelay {
|
|
|
|
return w.relay
|
2021-03-15 16:07:23 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 12:59:51 +00:00
|
|
|
func (w *WakuNode) Filter() *filter.WakuFilter {
|
|
|
|
return w.filter
|
|
|
|
}
|
|
|
|
|
2021-06-28 14:14:28 +00:00
|
|
|
func (w *WakuNode) mountRelay(shouldRelayMessages bool, opts ...wakurelay.Option) error {
|
2021-04-28 20:10:44 +00:00
|
|
|
var err error
|
|
|
|
w.relay, err = relay.NewWakuRelay(w.ctx, w.host, opts...)
|
2021-03-15 16:07:23 +00:00
|
|
|
|
2021-06-28 14:14:28 +00:00
|
|
|
if shouldRelayMessages {
|
|
|
|
_, err := w.Subscribe(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 16:07:23 +00:00
|
|
|
// TODO: rlnRelay
|
|
|
|
|
2021-04-28 20:10:44 +00:00
|
|
|
return err
|
2021-03-18 16:40:47 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 12:59:51 +00:00
|
|
|
func (w *WakuNode) mountFilter() error {
|
|
|
|
filterHandler := func(requestId string, msg pb.MessagePush) {
|
|
|
|
for _, message := range msg.Messages {
|
|
|
|
w.filters.Notify(message, requestId) // Trigger filter handlers on a light node
|
|
|
|
}
|
|
|
|
}
|
2021-06-16 10:14:22 +00:00
|
|
|
peerChan := make(chan *event.EvtPeerConnectednessChanged)
|
|
|
|
w.filter = filter.NewWakuFilter(w.ctx, w.host, filterHandler, peerChan)
|
|
|
|
w.peerListeners = append(w.peerListeners, peerChan)
|
2021-06-10 12:59:51 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
2021-04-28 20:23:03 +00:00
|
|
|
func (w *WakuNode) mountLightPush() {
|
|
|
|
w.lightPush = lightpush.NewWakuLightPush(w.ctx, w.host, w.relay)
|
|
|
|
}
|
|
|
|
|
2021-10-01 17:49:50 +00:00
|
|
|
func (w *WakuNode) mountRendezvous() error {
|
2021-10-01 18:37:52 +00:00
|
|
|
w.rendezvous = rendezvous.NewRendezvousService(w.host, w.opts.rendevousStorage)
|
|
|
|
|
|
|
|
if err := w.rendezvous.Start(); err != nil {
|
2021-10-01 17:49:50 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-10-01 18:37:52 +00:00
|
|
|
|
2021-10-01 17:49:50 +00:00
|
|
|
log.Info("Rendezvous service started")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-28 14:14:28 +00:00
|
|
|
func (w *WakuNode) startStore() {
|
2021-06-16 10:14:22 +00:00
|
|
|
peerChan := make(chan *event.EvtPeerConnectednessChanged)
|
|
|
|
w.opts.store.Start(w.ctx, w.host, peerChan)
|
|
|
|
w.peerListeners = append(w.peerListeners, peerChan)
|
2021-09-06 13:10:19 +00:00
|
|
|
|
|
|
|
if w.opts.shouldResume {
|
|
|
|
if _, err := w.opts.store.Resume(string(relay.GetTopic(nil)), nil); err != nil {
|
|
|
|
log.Error("failed to resume", err)
|
|
|
|
}
|
2021-08-13 11:56:09 +00:00
|
|
|
}
|
2021-03-18 23:21:45 +00:00
|
|
|
}
|
|
|
|
|
2021-09-30 16:01:53 +00:00
|
|
|
func (w *WakuNode) addPeer(info *peer.AddrInfo, protocolID p2pproto.ID) error {
|
|
|
|
log.Info(fmt.Sprintf("adding peer %s", info.ID.Pretty()))
|
|
|
|
w.host.Peerstore().AddAddrs(info.ID, info.Addrs, peerstore.PermanentAddrTTL)
|
|
|
|
return w.host.Peerstore().AddProtocols(info.ID, string(protocolID))
|
2021-06-10 12:59:51 +00:00
|
|
|
|
2021-08-31 18:19:49 +00:00
|
|
|
}
|
2021-06-10 12:59:51 +00:00
|
|
|
|
2021-09-30 16:01:53 +00:00
|
|
|
func (w *WakuNode) AddPeer(address ma.Multiaddr, protocolID p2pproto.ID) (*peer.ID, error) {
|
|
|
|
info, err := peer.AddrInfoFromP2pAddr(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-10 12:59:51 +00:00
|
|
|
|
2021-09-30 16:01:53 +00:00
|
|
|
return &info.ID, w.addPeer(info, protocolID)
|
2021-06-28 14:14:28 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 00:09:37 +00:00
|
|
|
func (w *WakuNode) Query(ctx context.Context, contentTopics []string, startTime float64, endTime float64, opts ...store.HistoryRequestOption) (*pb.HistoryResponse, error) {
|
2021-04-18 23:41:42 +00:00
|
|
|
if w.opts.store == nil {
|
2021-03-18 23:21:45 +00:00
|
|
|
return nil, errors.New("WakuStore is not set")
|
|
|
|
}
|
|
|
|
|
2021-04-22 00:09:37 +00:00
|
|
|
query := new(pb.HistoryQuery)
|
2021-04-20 21:46:35 +00:00
|
|
|
|
|
|
|
for _, ct := range contentTopics {
|
2021-04-22 00:09:37 +00:00
|
|
|
query.ContentFilters = append(query.ContentFilters, &pb.ContentFilter{ContentTopic: ct})
|
2021-04-20 21:46:35 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 02:19:31 +00:00
|
|
|
query.StartTime = startTime
|
|
|
|
query.EndTime = endTime
|
2021-04-22 00:09:37 +00:00
|
|
|
query.PagingInfo = new(pb.PagingInfo)
|
|
|
|
result, err := w.opts.store.Query(ctx, query, opts...)
|
2021-03-18 23:21:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:00:06 +00:00
|
|
|
func (w *WakuNode) Resume(ctx context.Context, peerList []peer.ID) error {
|
|
|
|
if w.opts.store == nil {
|
|
|
|
return errors.New("WakuStore is not set")
|
|
|
|
}
|
|
|
|
|
2021-06-28 13:20:23 +00:00
|
|
|
result, err := w.opts.store.Resume(string(relay.DefaultWakuTopic), peerList)
|
2021-06-10 13:00:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("the number of retrieved messages since the last online time: ", result)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-28 20:10:44 +00:00
|
|
|
func (node *WakuNode) Subscribe(topic *relay.Topic) (*Subscription, error) {
|
2021-03-18 16:40:47 +00:00
|
|
|
// Subscribes to a PubSub topic.
|
2021-03-15 16:07:23 +00:00
|
|
|
// NOTE The data field SHOULD be decoded as a WakuMessage.
|
2021-04-28 20:10:44 +00:00
|
|
|
if node.relay == nil {
|
2021-06-28 14:14:28 +00:00
|
|
|
return nil, errors.New("WakuRelay hasn't been set")
|
2021-03-15 16:07:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 20:10:44 +00:00
|
|
|
t := relay.GetTopic(topic)
|
|
|
|
sub, isNew, err := node.relay.Subscribe(t)
|
|
|
|
|
|
|
|
// Subscribe store to topic
|
|
|
|
if isNew && node.opts.store != nil && node.opts.storeMsgs {
|
|
|
|
log.Info("Subscribing store to topic ", t)
|
|
|
|
node.bcaster.Register(node.opts.store.MsgC)
|
|
|
|
}
|
2021-03-15 16:07:23 +00:00
|
|
|
|
2021-06-10 12:59:51 +00:00
|
|
|
// Subscribe filter
|
|
|
|
if isNew && node.filter != nil {
|
|
|
|
log.Info("Subscribing filter to topic ", t)
|
|
|
|
node.bcaster.Register(node.filter.MsgC)
|
|
|
|
}
|
|
|
|
|
2021-03-15 16:07:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-15 02:19:31 +00:00
|
|
|
// Create client subscription
|
2021-03-15 21:17:36 +00:00
|
|
|
subscription := new(Subscription)
|
|
|
|
subscription.closed = false
|
2021-04-22 00:09:37 +00:00
|
|
|
subscription.C = make(chan *protocol.Envelope, 1024) // To avoid blocking
|
2021-03-15 21:17:36 +00:00
|
|
|
subscription.quit = make(chan struct{})
|
2021-03-15 16:07:23 +00:00
|
|
|
|
2021-04-15 02:19:31 +00:00
|
|
|
node.subscriptionsMutex.Lock()
|
|
|
|
defer node.subscriptionsMutex.Unlock()
|
2021-04-28 20:10:44 +00:00
|
|
|
|
2021-04-15 02:19:31 +00:00
|
|
|
node.subscriptions[t] = append(node.subscriptions[t], subscription)
|
|
|
|
|
|
|
|
node.bcaster.Register(subscription.C)
|
|
|
|
|
2021-04-28 20:10:44 +00:00
|
|
|
go func(t relay.Topic) {
|
2021-03-15 23:59:18 +00:00
|
|
|
nextMsgTicker := time.NewTicker(time.Millisecond * 10)
|
|
|
|
defer nextMsgTicker.Stop()
|
|
|
|
|
2021-06-28 13:20:23 +00:00
|
|
|
ctx, err := tag.New(node.ctx, tag.Insert(metrics.KeyType, "relay"))
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-15 16:07:23 +00:00
|
|
|
for {
|
2021-03-15 21:17:36 +00:00
|
|
|
select {
|
|
|
|
case <-subscription.quit:
|
2021-03-15 21:43:26 +00:00
|
|
|
subscription.mutex.Lock()
|
2021-04-15 02:19:31 +00:00
|
|
|
node.bcaster.Unregister(subscription.C) // Remove from broadcast list
|
2021-03-15 21:17:36 +00:00
|
|
|
close(subscription.C)
|
2021-04-15 02:19:31 +00:00
|
|
|
subscription.mutex.Unlock()
|
2021-03-15 23:59:18 +00:00
|
|
|
case <-nextMsgTicker.C:
|
2021-04-15 02:19:31 +00:00
|
|
|
msg, err := sub.Next(node.ctx)
|
2021-03-15 21:17:36 +00:00
|
|
|
if err != nil {
|
2021-04-04 17:05:33 +00:00
|
|
|
subscription.mutex.Lock()
|
2021-04-15 02:19:31 +00:00
|
|
|
for _, subscription := range node.subscriptions[t] {
|
|
|
|
subscription.Unsubscribe()
|
2021-04-04 17:05:33 +00:00
|
|
|
}
|
2021-04-15 02:19:31 +00:00
|
|
|
subscription.mutex.Unlock()
|
2021-03-18 16:40:47 +00:00
|
|
|
return
|
2021-03-15 21:17:36 +00:00
|
|
|
}
|
2021-03-15 16:07:23 +00:00
|
|
|
|
2021-06-28 13:20:23 +00:00
|
|
|
stats.Record(ctx, metrics.Messages.M(1))
|
|
|
|
|
2021-04-22 00:09:37 +00:00
|
|
|
wakuMessage := &pb.WakuMessage{}
|
2021-03-15 21:17:36 +00:00
|
|
|
if err := proto.Unmarshal(msg.Data, wakuMessage); err != nil {
|
2021-03-24 20:39:12 +00:00
|
|
|
log.Error("could not decode message", err)
|
2021-03-15 21:17:36 +00:00
|
|
|
return
|
|
|
|
}
|
2021-03-15 16:07:23 +00:00
|
|
|
|
2021-04-28 20:10:44 +00:00
|
|
|
envelope := protocol.NewEnvelope(wakuMessage, string(t))
|
2021-04-15 02:19:31 +00:00
|
|
|
|
|
|
|
node.bcaster.Submit(envelope)
|
2021-03-15 21:17:36 +00:00
|
|
|
}
|
2021-03-15 16:07:23 +00:00
|
|
|
}
|
2021-04-28 15:11:32 +00:00
|
|
|
}(t)
|
2021-03-22 16:45:13 +00:00
|
|
|
|
2021-03-15 21:17:36 +00:00
|
|
|
return subscription, nil
|
2021-03-15 16:07:23 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 12:59:51 +00:00
|
|
|
// Wrapper around WakuFilter.Subscribe
|
|
|
|
// that adds a Filter object to node.filters
|
2021-06-28 14:14:28 +00:00
|
|
|
func (node *WakuNode) SubscribeFilter(ctx context.Context, request pb.FilterRequest, ch filter.ContentFilterChan) error {
|
2021-06-10 12:59:51 +00:00
|
|
|
// Registers for messages that match a specific filter. Triggers the handler whenever a message is received.
|
|
|
|
// ContentFilterChan takes MessagePush structs
|
|
|
|
|
|
|
|
// Status: Implemented.
|
|
|
|
|
|
|
|
// Sanity check for well-formed subscribe FilterRequest
|
|
|
|
//doAssert(request.subscribe, "invalid subscribe request")
|
|
|
|
|
2021-06-14 12:54:50 +00:00
|
|
|
log.Info("SubscribeFilter, request: ", request)
|
2021-06-10 12:59:51 +00:00
|
|
|
|
|
|
|
var id string
|
2021-06-28 14:14:28 +00:00
|
|
|
var err error
|
2021-06-10 12:59:51 +00:00
|
|
|
|
2021-06-28 14:14:28 +00:00
|
|
|
if node.filter == nil {
|
|
|
|
return errors.New("WakuFilter is not set")
|
|
|
|
}
|
2021-06-10 12:59:51 +00:00
|
|
|
|
2021-06-28 14:14:28 +00:00
|
|
|
id, err = node.filter.Subscribe(ctx, request)
|
|
|
|
if id == "" || err != nil {
|
|
|
|
// Failed to subscribe
|
|
|
|
log.Error("remote subscription to filter failed", request)
|
|
|
|
//waku_node_errors.inc(labelValues = ["subscribe_filter_failure"])
|
|
|
|
return err
|
2021-06-10 12:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register handler for filter, whether remote subscription succeeded or not
|
2021-06-28 14:14:28 +00:00
|
|
|
node.filters[id] = filter.Filter{
|
|
|
|
Topic: request.Topic,
|
|
|
|
ContentFilters: request.ContentFilters,
|
|
|
|
Chan: ch,
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-06-14 12:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (node *WakuNode) UnsubscribeFilter(ctx context.Context, request pb.FilterRequest) {
|
|
|
|
|
|
|
|
log.Info("UnsubscribeFilter, request: ", request)
|
|
|
|
// Send message to full node in order to unsubscribe
|
|
|
|
node.filter.Unsubscribe(ctx, request)
|
|
|
|
|
|
|
|
// Remove local filter
|
|
|
|
var idsToRemove []string
|
|
|
|
for id, f := range node.filters {
|
|
|
|
// Iterate filter entries to remove matching content topics
|
|
|
|
// make sure we delete the content filter
|
|
|
|
// if no more topics are left
|
|
|
|
for _, cfToDelete := range request.ContentFilters {
|
|
|
|
for i, cf := range f.ContentFilters {
|
|
|
|
if cf.ContentTopic == cfToDelete.ContentTopic {
|
|
|
|
l := len(f.ContentFilters) - 1
|
|
|
|
f.ContentFilters[l], f.ContentFilters[i] = f.ContentFilters[i], f.ContentFilters[l]
|
|
|
|
f.ContentFilters = f.ContentFilters[:l]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if len(f.ContentFilters) == 0 {
|
|
|
|
idsToRemove = append(idsToRemove, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rId := range idsToRemove {
|
2021-07-29 12:40:54 +00:00
|
|
|
for id := range node.filters {
|
2021-06-14 12:54:50 +00:00
|
|
|
if id == rId {
|
|
|
|
delete(node.filters, id)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-10 12:59:51 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 20:10:44 +00:00
|
|
|
func (node *WakuNode) Publish(ctx context.Context, message *pb.WakuMessage, topic *relay.Topic) ([]byte, error) {
|
|
|
|
if node.relay == nil {
|
2021-06-28 13:20:23 +00:00
|
|
|
return nil, errors.New("WakuRelay hasn't been set")
|
2021-03-15 16:07:23 +00:00
|
|
|
}
|
2021-04-15 02:19:31 +00:00
|
|
|
|
2021-04-28 20:10:44 +00:00
|
|
|
if message == nil {
|
|
|
|
return nil, errors.New("message can't be null")
|
|
|
|
}
|
2021-04-18 23:41:42 +00:00
|
|
|
|
2021-06-28 14:14:28 +00:00
|
|
|
if node.lightPush != nil {
|
|
|
|
return node.LightPush(ctx, message, topic)
|
|
|
|
}
|
|
|
|
|
2021-04-28 20:10:44 +00:00
|
|
|
hash, err := node.relay.Publish(ctx, message, topic)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-04-15 02:19:31 +00:00
|
|
|
}
|
2021-04-28 20:10:44 +00:00
|
|
|
return hash, nil
|
2021-04-15 02:19:31 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 20:10:44 +00:00
|
|
|
func (node *WakuNode) LightPush(ctx context.Context, message *pb.WakuMessage, topic *relay.Topic, opts ...lightpush.LightPushOption) ([]byte, error) {
|
|
|
|
if node.lightPush == nil {
|
2021-06-28 13:20:23 +00:00
|
|
|
return nil, errors.New("WakuLightPush hasn't been set")
|
2021-03-15 16:07:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if message == nil {
|
2021-04-11 23:43:59 +00:00
|
|
|
return nil, errors.New("message can't be null")
|
2021-03-15 16:07:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 20:10:44 +00:00
|
|
|
req := new(pb.PushRequest)
|
|
|
|
req.Message = message
|
|
|
|
req.PubsubTopic = string(relay.GetTopic(topic))
|
2021-03-22 16:45:13 +00:00
|
|
|
|
2021-04-28 20:10:44 +00:00
|
|
|
response, err := node.lightPush.Request(ctx, req, opts...)
|
2021-03-15 16:07:23 +00:00
|
|
|
if err != nil {
|
2021-04-11 23:43:59 +00:00
|
|
|
return nil, err
|
2021-03-15 16:07:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 20:10:44 +00:00
|
|
|
if response.IsSuccess {
|
|
|
|
hash, _ := message.Hash()
|
|
|
|
return hash, nil
|
|
|
|
} else {
|
|
|
|
return nil, errors.New(response.Info)
|
2021-03-15 16:07:23 +00:00
|
|
|
}
|
2021-03-11 20:27:12 +00:00
|
|
|
}
|
2021-03-18 23:21:45 +00:00
|
|
|
|
2021-10-01 10:32:15 +00:00
|
|
|
func (w *WakuNode) DialPeerWithMultiAddress(ctx context.Context, address ma.Multiaddr) error {
|
2021-09-30 23:03:19 +00:00
|
|
|
info, err := peer.AddrInfoFromP2pAddr(address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-01 10:32:15 +00:00
|
|
|
return w.connect(ctx, *info)
|
2021-09-30 23:03:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 10:32:15 +00:00
|
|
|
func (w *WakuNode) DialPeer(ctx context.Context, address string) error {
|
2021-03-18 23:21:45 +00:00
|
|
|
p, err := ma.NewMultiaddr(address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := peer.AddrInfoFromP2pAddr(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-01 10:32:15 +00:00
|
|
|
return w.connect(ctx, *info)
|
2021-09-06 13:10:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 10:32:15 +00:00
|
|
|
func (w *WakuNode) connect(ctx context.Context, info peer.AddrInfo) error {
|
|
|
|
err := w.host.Connect(ctx, info)
|
2021-09-06 13:10:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.processHostEvent(event.EvtPeerConnectednessChanged{
|
|
|
|
Peer: info.ID,
|
|
|
|
Connectedness: network.Connected,
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
2021-03-18 23:21:45 +00:00
|
|
|
}
|
2021-03-24 20:39:12 +00:00
|
|
|
|
2021-10-01 10:32:15 +00:00
|
|
|
func (w *WakuNode) DialPeerByID(ctx context.Context, peerID peer.ID) error {
|
2021-08-31 19:17:56 +00:00
|
|
|
info := w.host.Peerstore().PeerInfo(peerID)
|
2021-10-01 10:32:15 +00:00
|
|
|
return w.connect(ctx, info)
|
2021-08-31 19:17:56 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 20:39:12 +00:00
|
|
|
func (w *WakuNode) ClosePeerByAddress(address string) error {
|
|
|
|
p, err := ma.NewMultiaddr(address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the peer ID from the multiaddr.
|
|
|
|
info, err := peer.AddrInfoFromP2pAddr(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.ClosePeerById(info.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WakuNode) ClosePeerById(id peer.ID) error {
|
2021-09-06 13:10:19 +00:00
|
|
|
err := w.host.Network().ClosePeer(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.processHostEvent(event.EvtPeerConnectednessChanged{
|
|
|
|
Peer: id,
|
|
|
|
Connectedness: network.NotConnected,
|
|
|
|
})
|
2021-09-06 13:34:58 +00:00
|
|
|
|
|
|
|
return nil
|
2021-03-24 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WakuNode) PeerCount() int {
|
2021-09-27 12:47:18 +00:00
|
|
|
w.peersMutex.Lock()
|
|
|
|
defer w.peersMutex.Unlock()
|
2021-08-31 17:27:42 +00:00
|
|
|
return len(w.peers)
|
2021-03-24 20:39:12 +00:00
|
|
|
}
|
2021-06-24 13:02:53 +00:00
|
|
|
|
2021-08-31 20:51:22 +00:00
|
|
|
func (w *WakuNode) Peers() PeerStats {
|
|
|
|
w.peersMutex.Lock()
|
|
|
|
defer w.peersMutex.Unlock()
|
|
|
|
p := make(PeerStats)
|
|
|
|
for k, v := range w.peers {
|
|
|
|
p[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-06-24 13:02:53 +00:00
|
|
|
func (w *WakuNode) startKeepAlive(t time.Duration) {
|
2021-09-11 11:36:54 +00:00
|
|
|
|
2021-08-31 18:48:09 +00:00
|
|
|
log.Info("Setting up ping protocol with duration of ", t)
|
2021-06-24 13:02:53 +00:00
|
|
|
|
|
|
|
w.ping = ping.NewPingService(w.host)
|
|
|
|
ticker := time.NewTicker(t)
|
|
|
|
go func() {
|
2021-09-11 11:36:54 +00:00
|
|
|
// This map contains peers that we're
|
|
|
|
// waiting for the ping response from
|
2021-08-10 14:23:49 +00:00
|
|
|
peerMap := make(map[peer.ID]<-chan ping.Result)
|
|
|
|
var mu sync.Mutex
|
2021-06-24 13:02:53 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2021-09-18 14:19:19 +00:00
|
|
|
for _, p := range w.host.Peerstore().Peers() {
|
|
|
|
if p == w.host.ID() {
|
|
|
|
log.Info("###PING skip ", p)
|
|
|
|
continue
|
|
|
|
}
|
2021-08-10 14:23:49 +00:00
|
|
|
mu.Lock()
|
2021-09-11 11:36:54 +00:00
|
|
|
_, ok := peerMap[p]
|
2021-08-10 14:23:49 +00:00
|
|
|
mu.Unlock()
|
2021-09-11 11:36:54 +00:00
|
|
|
|
|
|
|
var s = p.Pretty()
|
|
|
|
s = s[len(s)-4:]
|
2021-08-10 14:23:49 +00:00
|
|
|
if !ok {
|
2021-09-11 11:36:54 +00:00
|
|
|
log.Info("###PING ", s)
|
|
|
|
result := w.ping.Ping(w.ctx, p)
|
2021-08-10 14:23:49 +00:00
|
|
|
mu.Lock()
|
2021-09-11 11:36:54 +00:00
|
|
|
peerMap[p] = result
|
2021-08-10 14:23:49 +00:00
|
|
|
mu.Unlock()
|
|
|
|
|
2021-10-01 10:32:15 +00:00
|
|
|
go func(peerID peer.ID) {
|
2021-08-10 14:23:49 +00:00
|
|
|
peerFound := false
|
2021-09-27 12:47:18 +00:00
|
|
|
w.peersMutex.Lock()
|
2021-08-31 18:48:09 +00:00
|
|
|
for p := range w.peers {
|
2021-10-01 10:32:15 +00:00
|
|
|
if p == peerID {
|
2021-08-10 14:23:49 +00:00
|
|
|
peerFound = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-09-27 12:47:18 +00:00
|
|
|
defer w.peersMutex.Unlock()
|
|
|
|
log.Debug("###PING before fetching result")
|
2021-09-30 23:03:19 +00:00
|
|
|
|
2021-08-10 14:23:49 +00:00
|
|
|
pingTicker := time.NewTicker(time.Duration(1) * time.Second)
|
|
|
|
isError := false
|
|
|
|
select {
|
|
|
|
case resVal := <-result:
|
|
|
|
isError = resVal.Error != nil
|
|
|
|
case <-pingTicker.C:
|
|
|
|
isError = true
|
|
|
|
}
|
|
|
|
pingTicker.Stop()
|
|
|
|
if !peerFound && !isError {
|
|
|
|
//EventBus Emitter doesn't seem to work when there's no connection
|
|
|
|
w.pingEventsChan <- event.EvtPeerConnectednessChanged{
|
2021-10-01 10:32:15 +00:00
|
|
|
Peer: peerID,
|
2021-08-10 14:23:49 +00:00
|
|
|
Connectedness: network.Connected,
|
|
|
|
}
|
2021-10-01 10:32:15 +00:00
|
|
|
peerConns := w.host.Network().ConnsToPeer(peerID)
|
2021-09-11 11:36:54 +00:00
|
|
|
if len(peerConns) > 0 {
|
|
|
|
// log.Info("###PING " + s + " IdentifyWait")
|
|
|
|
// logwriter.Write([]byte("###PING " + s + " IdentifyWait"))
|
|
|
|
//w.idService.IdentifyWait(peerConns[0])
|
|
|
|
} else {
|
2021-10-01 10:32:15 +00:00
|
|
|
go func(peerID peer.ID) {
|
|
|
|
ctx, cancel := context.WithTimeout(w.ctx, time.Duration(5)*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
if err := w.DialPeerByID(ctx, peerID); err != nil {
|
|
|
|
log.Warn("could not dial peer ", peerID)
|
|
|
|
}
|
|
|
|
}(peerID)
|
2021-09-11 11:36:54 +00:00
|
|
|
}
|
2021-08-10 14:23:49 +00:00
|
|
|
} else if peerFound && isError {
|
|
|
|
w.pingEventsChan <- event.EvtPeerConnectednessChanged{
|
2021-10-01 10:32:15 +00:00
|
|
|
Peer: peerID,
|
2021-08-10 14:23:49 +00:00
|
|
|
Connectedness: network.NotConnected,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mu.Lock()
|
2021-10-01 10:32:15 +00:00
|
|
|
delete(peerMap, peerID)
|
2021-08-10 14:23:49 +00:00
|
|
|
mu.Unlock()
|
2021-09-11 11:36:54 +00:00
|
|
|
}(p)
|
2021-08-10 14:23:49 +00:00
|
|
|
} else {
|
2021-09-11 11:36:54 +00:00
|
|
|
log.Info("###PING " + s + " already pinged")
|
|
|
|
// logwriter.Write([]byte("###PING " + s + " already pinged"))
|
2021-08-10 14:23:49 +00:00
|
|
|
}
|
2021-06-24 13:02:53 +00:00
|
|
|
}
|
|
|
|
case <-w.quit:
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2021-08-31 18:19:49 +00:00
|
|
|
}
|