2021-03-11 20:27:12 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
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-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-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-07-29 22:08:53 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
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-05 02:13:54 +00:00
|
|
|
rendezvous "github.com/status-im/go-waku-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-10-10 15:46:00 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/utils"
|
2021-03-11 20:27:12 +00:00
|
|
|
)
|
|
|
|
|
2021-03-22 16:45:13 +00:00
|
|
|
var log = logging.Logger("wakunode")
|
|
|
|
|
2021-03-11 20:27:12 +00:00
|
|
|
type Message []byte
|
|
|
|
|
|
|
|
type WakuNode struct {
|
2021-10-06 15:34:39 +00:00
|
|
|
host host.Host
|
|
|
|
opts *WakuNodeParameters
|
2021-04-15 02:19:31 +00:00
|
|
|
|
2021-10-06 15:34:39 +00:00
|
|
|
relay *relay.WakuRelay
|
|
|
|
filter *filter.WakuFilter
|
|
|
|
lightPush *lightpush.WakuLightPush
|
2021-10-01 17:49:50 +00:00
|
|
|
rendezvous *rendezvous.RendezvousService
|
2021-10-06 15:34:39 +00:00
|
|
|
ping *ping.PingService
|
2021-10-11 22:45:54 +00:00
|
|
|
store *store.WakuStore
|
2021-06-24 13:02:53 +00:00
|
|
|
|
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-10-06 15:34:39 +00:00
|
|
|
connectionNotif ConnectionNotifier
|
2021-06-16 10:14:22 +00:00
|
|
|
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
|
|
|
|
|
|
|
// Channel passed to WakuNode constructor
|
|
|
|
// receiving connection status notifications
|
|
|
|
connStatusChan chan ConnStatus
|
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-10-15 02:15:02 +00:00
|
|
|
if params.addressFactory != nil {
|
|
|
|
params.libP2POpts = append(params.libP2POpts, libp2p.AddrsFactory(params.addressFactory))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2021-10-06 15:34:39 +00:00
|
|
|
if w.protocolEventSub, err = host.EventBus().Subscribe(new(event.EvtPeerProtocolsUpdated)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-16 10:14:22 +00:00
|
|
|
|
2021-10-06 15:34:39 +00:00
|
|
|
if w.identificationEventSub, err = host.EventBus().Subscribe(new(event.EvtPeerIdentificationCompleted)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-16 10:14:22 +00:00
|
|
|
|
|
|
|
if params.connStatusChan != nil {
|
|
|
|
w.connStatusChan = params.connStatusChan
|
2021-06-28 14:14:28 +00:00
|
|
|
}
|
2021-10-06 15:34:39 +00:00
|
|
|
|
|
|
|
w.connectionNotif = NewConnectionNotifier(host)
|
|
|
|
w.host.Network().Notify(w.connectionNotif)
|
2021-06-16 10:14:22 +00:00
|
|
|
go w.connectednessListener()
|
2021-06-28 14:14:28 +00:00
|
|
|
|
2021-10-05 02:13:54 +00:00
|
|
|
if w.opts.keepAliveInterval > time.Duration(0) {
|
|
|
|
w.startKeepAlive(w.opts.keepAliveInterval)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range w.ListenAddresses() {
|
|
|
|
log.Info("Listening on ", addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WakuNode) Start() error {
|
|
|
|
if w.opts.enableStore {
|
2021-06-16 10:14:22 +00:00
|
|
|
w.startStore()
|
2021-04-18 23:41:42 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 02:13:54 +00:00
|
|
|
if w.opts.enableFilter {
|
2021-06-10 12:59:51 +00:00
|
|
|
w.filters = make(filter.Filters)
|
|
|
|
err := w.mountFilter()
|
|
|
|
if err != nil {
|
2021-10-05 02:13:54 +00:00
|
|
|
return err
|
2021-06-10 12:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-05 02:13:54 +00:00
|
|
|
if w.opts.enableRendezvous {
|
2021-09-30 16:01:53 +00:00
|
|
|
rendezvous := rendezvous.NewRendezvousDiscovery(w.host)
|
2021-10-08 13:50:56 +00:00
|
|
|
w.opts.wOpts = append(w.opts.wOpts, pubsub.WithDiscovery(rendezvous, w.opts.rendezvousOpts...))
|
2021-10-01 17:49:50 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 02:13:54 +00:00
|
|
|
err := w.mountRelay(w.opts.enableRelay, w.opts.wOpts...)
|
2021-06-16 10:14:22 +00:00
|
|
|
if err != nil {
|
2021-10-05 02:13:54 +00:00
|
|
|
return err
|
2021-06-16 10:14:22 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 02:13:54 +00:00
|
|
|
if w.opts.enableLightPush {
|
2021-04-28 20:23:03 +00:00
|
|
|
w.mountLightPush()
|
|
|
|
}
|
|
|
|
|
2021-10-05 02:13:54 +00:00
|
|
|
if w.opts.enableRendezvousServer {
|
2021-10-01 17:49:50 +00:00
|
|
|
err := w.mountRendezvous()
|
|
|
|
if err != nil {
|
2021-10-05 02:13:54 +00:00
|
|
|
return err
|
2021-10-01 17:49:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-05 02:13:54 +00:00
|
|
|
return nil
|
2021-03-11 20:27:12 +00:00
|
|
|
}
|
|
|
|
|
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-10-06 15:34:39 +00:00
|
|
|
defer w.connectionNotif.Close()
|
2021-06-16 10:14:22 +00:00
|
|
|
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-10-11 22:45:54 +00:00
|
|
|
if w.relay != nil {
|
|
|
|
for _, topic := range w.relay.Topics() {
|
|
|
|
for _, sub := range w.subscriptions[topic] {
|
|
|
|
sub.Unsubscribe()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.subscriptions = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.filter != nil {
|
|
|
|
w.filter.Stop()
|
|
|
|
for _, filter := range w.filters {
|
|
|
|
close(filter.Chan)
|
2021-04-15 02:19:31 +00:00
|
|
|
}
|
2021-10-11 22:45:54 +00:00
|
|
|
w.filters = nil
|
2021-03-22 16:45:13 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 22:45:54 +00:00
|
|
|
if w.lightPush != nil {
|
|
|
|
w.lightPush.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.store != nil {
|
|
|
|
w.store.Stop()
|
|
|
|
}
|
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-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-07-29 22:08:53 +00:00
|
|
|
func (w *WakuNode) mountRelay(shouldRelayMessages bool, opts ...pubsub.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 {
|
2021-10-10 22:44:07 +00:00
|
|
|
_, err := w.Subscribe(w.ctx, nil)
|
2021-06-28 14:14:28 +00:00
|
|
|
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-10-06 15:34:39 +00:00
|
|
|
w.filter = filter.NewWakuFilter(w.ctx, w.host, filterHandler)
|
2021-06-10 12:59:51 +00:00
|
|
|
|
2021-10-06 15:34:39 +00:00
|
|
|
return nil
|
2021-06-10 12:59:51 +00:00
|
|
|
}
|
2021-10-05 02:13:54 +00:00
|
|
|
|
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-10-11 22:45:54 +00:00
|
|
|
w.store = w.opts.store
|
|
|
|
w.store.Start(w.ctx, w.host)
|
2021-09-06 13:10:19 +00:00
|
|
|
|
|
|
|
if w.opts.shouldResume {
|
2021-10-10 15:46:00 +00:00
|
|
|
// TODO: extract this to a function and run it when you go offline
|
|
|
|
// TODO: determine if a store is listening to a topic
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
t := time.NewTicker(time.Second)
|
|
|
|
peerVerif:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-w.quit:
|
|
|
|
return
|
|
|
|
case <-t.C:
|
|
|
|
_, err := utils.SelectPeer(w.host, string(store.StoreID_v20beta3))
|
|
|
|
if err == nil {
|
|
|
|
break peerVerif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxWithTimeout, ctxCancel := context.WithTimeout(w.ctx, 20*time.Second)
|
|
|
|
defer ctxCancel()
|
|
|
|
if err := w.Resume(ctxWithTimeout, nil); err != nil {
|
|
|
|
log.Info("Retrying in 10s...")
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
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 {
|
2021-10-10 15:46:00 +00:00
|
|
|
log.Info(fmt.Sprintf("Adding peer %s to peerstore", info.ID.Pretty()))
|
2021-09-30 16:01:53 +00:00
|
|
|
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-10-11 22:45:54 +00:00
|
|
|
if w.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)
|
2021-10-11 22:45:54 +00:00
|
|
|
result, err := w.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 {
|
2021-10-11 22:45:54 +00:00
|
|
|
if w.store == nil {
|
2021-06-10 13:00:06 +00:00
|
|
|
return errors.New("WakuStore is not set")
|
|
|
|
}
|
|
|
|
|
2021-10-11 22:45:54 +00:00
|
|
|
result, err := w.store.Resume(ctx, string(relay.DefaultWakuTopic), peerList)
|
2021-06-10 13:00:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-10 15:46:00 +00:00
|
|
|
log.Info("Retrieved messages since the last online time: ", result)
|
2021-06-10 13:00:06 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-10 22:44:07 +00:00
|
|
|
func (node *WakuNode) Subscribe(ctx context.Context, 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-10-10 22:52:16 +00:00
|
|
|
go node.subscribeToTopic(t, subscription, sub)
|
2021-03-15 23:59:18 +00:00
|
|
|
|
2021-10-10 22:52:16 +00:00
|
|
|
return subscription, nil
|
|
|
|
}
|
2021-06-28 13:20:23 +00:00
|
|
|
|
2021-10-10 22:52:16 +00:00
|
|
|
func (node *WakuNode) subscribeToTopic(t relay.Topic, subscription *Subscription, sub *pubsub.Subscription) {
|
|
|
|
nextMsgTicker := time.NewTicker(time.Millisecond * 10)
|
|
|
|
defer nextMsgTicker.Stop()
|
|
|
|
|
|
|
|
ctx, err := tag.New(node.ctx, tag.Insert(metrics.KeyType, "relay"))
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-subscription.quit:
|
|
|
|
subscription.mutex.Lock()
|
|
|
|
node.bcaster.Unregister(subscription.C) // Remove from broadcast list
|
|
|
|
close(subscription.C)
|
|
|
|
subscription.mutex.Unlock()
|
|
|
|
case <-nextMsgTicker.C:
|
|
|
|
msg, err := sub.Next(ctx)
|
|
|
|
if err != nil {
|
2021-03-15 21:43:26 +00:00
|
|
|
subscription.mutex.Lock()
|
2021-10-10 22:52:16 +00:00
|
|
|
for _, subscription := range node.subscriptions[t] {
|
|
|
|
subscription.Unsubscribe()
|
2021-03-15 21:17:36 +00:00
|
|
|
}
|
2021-10-10 22:52:16 +00:00
|
|
|
subscription.mutex.Unlock()
|
|
|
|
return
|
|
|
|
}
|
2021-03-15 16:07:23 +00:00
|
|
|
|
2021-10-10 22:52:16 +00:00
|
|
|
stats.Record(ctx, metrics.Messages.M(1))
|
2021-06-28 13:20:23 +00:00
|
|
|
|
2021-10-10 22:52:16 +00:00
|
|
|
wakuMessage := &pb.WakuMessage{}
|
|
|
|
if err := proto.Unmarshal(msg.Data, wakuMessage); err != nil {
|
|
|
|
log.Error("could not decode message", err)
|
|
|
|
return
|
|
|
|
}
|
2021-03-15 16:07:23 +00:00
|
|
|
|
2021-10-10 22:52:16 +00:00
|
|
|
envelope := protocol.NewEnvelope(wakuMessage, string(t))
|
2021-04-15 02:19:31 +00:00
|
|
|
|
2021-10-10 22:52:16 +00:00
|
|
|
node.bcaster.Submit(envelope)
|
2021-03-15 16:07:23 +00:00
|
|
|
}
|
2021-10-10 22:52:16 +00:00
|
|
|
}
|
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-10-11 22:45:54 +00:00
|
|
|
func (node *WakuNode) SubscribeFilter(ctx context.Context, topic string, contentTopics []string) (filterID string, ch chan *protocol.Envelope, err error) {
|
2021-06-28 14:14:28 +00:00
|
|
|
if node.filter == nil {
|
2021-10-11 22:45:54 +00:00
|
|
|
err = errors.New("WakuFilter is not set")
|
|
|
|
return
|
2021-06-28 14:14:28 +00:00
|
|
|
}
|
2021-06-10 12:59:51 +00:00
|
|
|
|
2021-10-11 22:45:54 +00:00
|
|
|
// TODO: should be possible to pass the peerID as option or autoselect peer.
|
|
|
|
// TODO: check if there's an existing pubsub topic that uses the same peer. If so, reuse filter, and return same channel and filterID
|
|
|
|
|
2021-10-10 22:44:07 +00:00
|
|
|
// Registers for messages that match a specific filter. Triggers the handler whenever a message is received.
|
|
|
|
// ContentFilterChan takes MessagePush structs
|
2021-10-11 22:45:54 +00:00
|
|
|
var peerID *peer.ID
|
|
|
|
filterID, peerID, err = node.filter.Subscribe(ctx, topic, contentTopics)
|
|
|
|
if filterID == "" || err != nil {
|
2021-06-28 14:14:28 +00:00
|
|
|
// Failed to subscribe
|
2021-10-10 22:44:07 +00:00
|
|
|
log.Error("remote subscription to filter failed")
|
2021-10-11 22:45:54 +00:00
|
|
|
return
|
2021-06-10 12:59:51 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 22:45:54 +00:00
|
|
|
ch = make(chan *protocol.Envelope, 1024) // To avoid blocking
|
|
|
|
|
2021-06-10 12:59:51 +00:00
|
|
|
// Register handler for filter, whether remote subscription succeeded or not
|
2021-10-11 22:45:54 +00:00
|
|
|
node.filters[filterID] = filter.Filter{
|
2021-10-10 22:44:07 +00:00
|
|
|
PeerID: *peerID,
|
|
|
|
Topic: topic,
|
|
|
|
ContentFilters: contentTopics,
|
2021-06-28 14:14:28 +00:00
|
|
|
Chan: ch,
|
|
|
|
}
|
|
|
|
|
2021-10-11 22:45:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnsubscribeFilterByID removes a subscription to a filter node completely
|
|
|
|
// using the filterID returned when the subscription was created
|
|
|
|
func (node *WakuNode) UnsubscribeFilterByID(ctx context.Context, filterID string) error {
|
|
|
|
|
|
|
|
var f filter.Filter
|
|
|
|
var ok bool
|
|
|
|
if f, ok = node.filters[filterID]; !ok {
|
|
|
|
return errors.New("filter not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := node.filter.Unsubscribe(ctx, f.Topic, f.ContentFilters, f.PeerID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
close(f.Chan)
|
|
|
|
delete(node.filters, filterID)
|
|
|
|
|
2021-06-28 14:14:28 +00:00
|
|
|
return nil
|
2021-06-14 12:54:50 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 22:45:54 +00:00
|
|
|
// Unsubscribe filter removes content topics from a filter subscription. If all
|
|
|
|
// the contentTopics are removed the subscription is dropped completely
|
|
|
|
func (node *WakuNode) UnsubscribeFilter(ctx context.Context, topic string, contentTopics []string) error {
|
2021-06-14 12:54:50 +00:00
|
|
|
// Remove local filter
|
|
|
|
var idsToRemove []string
|
|
|
|
for id, f := range node.filters {
|
2021-10-10 22:44:07 +00:00
|
|
|
if f.Topic != topic {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send message to full node in order to unsubscribe
|
2021-10-13 12:48:29 +00:00
|
|
|
err := node.filter.Unsubscribe(ctx, topic, contentTopics, f.PeerID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-10 22:44:07 +00:00
|
|
|
|
2021-06-14 12:54:50 +00:00
|
|
|
// Iterate filter entries to remove matching content topics
|
|
|
|
// make sure we delete the content filter
|
|
|
|
// if no more topics are left
|
2021-10-10 22:44:07 +00:00
|
|
|
for _, cfToDelete := range contentTopics {
|
2021-06-14 12:54:50 +00:00
|
|
|
for i, cf := range f.ContentFilters {
|
2021-10-10 22:44:07 +00:00
|
|
|
if cf == cfToDelete {
|
2021-06-14 12:54:50 +00:00
|
|
|
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 {
|
2021-10-11 22:45:54 +00:00
|
|
|
close(node.filters[id].Chan)
|
2021-06-14 12:54:50 +00:00
|
|
|
delete(node.filters, id)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-11 22:45:54 +00:00
|
|
|
|
|
|
|
return nil
|
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
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2021-09-06 13:34:58 +00:00
|
|
|
return nil
|
2021-03-24 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WakuNode) PeerCount() int {
|
2021-10-06 15:34:39 +00:00
|
|
|
return len(w.host.Network().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 {
|
|
|
|
p := make(PeerStats)
|
2021-10-06 15:34:39 +00:00
|
|
|
for _, peerID := range w.host.Network().Peers() {
|
|
|
|
protocols, err := w.host.Peerstore().GetProtocols(peerID)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
p[peerID] = protocols
|
2021-08-31 20:51:22 +00:00
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-06-24 13:02:53 +00:00
|
|
|
func (w *WakuNode) startKeepAlive(t time.Duration) {
|
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() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2021-10-06 15:34:39 +00:00
|
|
|
for _, peer := range w.host.Network().Peers() {
|
|
|
|
log.Debug("Pinging", peer)
|
|
|
|
w.ping.Ping(w.ctx, peer)
|
2021-06-24 13:02:53 +00:00
|
|
|
}
|
|
|
|
case <-w.quit:
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2021-08-31 18:19:49 +00:00
|
|
|
}
|