go-waku/waku/v2/node/wakunode2.go

352 lines
7.7 KiB
Go
Raw Normal View History

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"
"github.com/libp2p/go-libp2p-core/host"
2021-03-18 16:40:47 +00:00
"github.com/libp2p/go-libp2p-core/peer"
2021-03-11 20:27:12 +00:00
ma "github.com/multiformats/go-multiaddr"
2021-04-22 00:09:37 +00:00
2021-03-11 20:27:12 +00:00
"github.com/status-im/go-waku/waku/v2/protocol"
"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"
"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"
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-03-11 20:27:12 +00:00
// Default clientId
const clientId string = "Go Waku v2 node"
type Message []byte
type WakuNode struct {
host host.Host
opts *WakuNodeParameters
2021-03-15 16:07:23 +00:00
relay *relay.WakuRelay
lightPush *lightpush.WakuLightPush
subscriptions map[relay.Topic][]*Subscription
2021-03-22 16:45:13 +00:00
subscriptionsMutex sync.Mutex
2021-03-15 16:07:23 +00:00
bcaster Broadcaster
ctx context.Context
cancel context.CancelFunc
2021-03-11 20:27:12 +00:00
}
func New(ctx context.Context, opts ...WakuNodeOption) (*WakuNode, error) {
params := new(WakuNodeParameters)
2021-03-11 20:27:12 +00:00
ctx, cancel := context.WithCancel(ctx)
_ = cancel
params.libP2POpts = DefaultLibP2POptions
for _, opt := range opts {
err := opt(params)
2021-03-11 20:27:12 +00:00
if err != nil {
return nil, err
}
}
if len(params.multiAddr) > 0 {
params.libP2POpts = append(params.libP2POpts, libp2p.ListenAddrs(params.multiAddr...))
}
2021-03-11 20:27:12 +00:00
if params.privKey != nil {
params.libP2POpts = append(params.libP2POpts, libp2p.Identity(*params.privKey))
}
2021-03-15 16:07:23 +00:00
host, err := libp2p.New(ctx, params.libP2POpts...)
2021-03-11 20:27:12 +00:00
if err != nil {
return nil, err
}
w := new(WakuNode)
w.bcaster = NewBroadcaster(1024)
2021-03-15 16:07:23 +00:00
w.host = host
w.cancel = cancel
w.ctx = ctx
w.subscriptions = make(map[relay.Topic][]*Subscription)
w.opts = params
if params.enableRelay {
err := w.mountRelay(params.wOpts...)
if err != nil {
return nil, err
}
}
if params.enableStore {
err := w.startStore()
if err != nil {
return nil, err
}
}
2021-03-11 20:27:12 +00:00
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
for _, topic := range w.relay.Topics() {
for _, sub := range w.subscriptions[topic] {
sub.Unsubscribe()
}
2021-03-22 16:45:13 +00:00
}
w.subscriptions = nil
2021-03-15 16:07:23 +00:00
}
func (w *WakuNode) Host() host.Host {
return w.host
}
func (w *WakuNode) ID() string {
return w.host.ID().Pretty()
}
2021-04-04 17:05:33 +00:00
func (w *WakuNode) ListenAddresses() []string {
hostInfo, _ := ma.NewMultiaddr(fmt.Sprintf("/p2p/%s", w.host.ID().Pretty()))
var result []string
for _, addr := range w.host.Addrs() {
result = append(result, addr.Encapsulate(hostInfo).String())
}
return result
}
func (w *WakuNode) Relay() *relay.WakuRelay {
return w.relay
2021-03-15 16:07:23 +00:00
}
func (w *WakuNode) mountRelay(opts ...wakurelay.Option) error {
var err error
w.relay, err = relay.NewWakuRelay(w.ctx, w.host, opts...)
2021-03-15 16:07:23 +00:00
// TODO: filters
// TODO: rlnRelay
return err
2021-03-18 16:40:47 +00:00
}
func (w *WakuNode) startStore() error {
_, err := w.Subscribe(nil)
if err != nil {
return err
}
w.opts.store.Start(w.host)
return nil
}
func (w *WakuNode) AddStorePeer(address string) (*peer.ID, error) {
if w.opts.store == nil {
return nil, errors.New("WakuStore is not set")
2021-03-18 16:40:47 +00:00
}
storePeer, err := ma.NewMultiaddr(address)
if err != nil {
return nil, err
2021-03-18 16:40:47 +00:00
}
// Extract the peer ID from the multiaddr.
info, err := peer.AddrInfoFromP2pAddr(storePeer)
if err != nil {
return nil, err
2021-03-18 16:40:47 +00:00
}
return &info.ID, w.opts.store.AddPeer(info.ID, info.Addrs)
2021-03-18 16:40:47 +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) {
if w.opts.store == nil {
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
}
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...)
if err != nil {
return nil, err
}
return result, nil
}
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.
if node.relay == nil {
return nil, errors.New("WakuRelay hasn't been set.")
2021-03-15 16:07:23 +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
if err != nil {
return nil, err
}
// Create client subscription
subscription := new(Subscription)
subscription.closed = false
2021-04-22 00:09:37 +00:00
subscription.C = make(chan *protocol.Envelope, 1024) // To avoid blocking
subscription.quit = make(chan struct{})
2021-03-15 16:07:23 +00:00
node.subscriptionsMutex.Lock()
defer node.subscriptionsMutex.Unlock()
node.subscriptions[t] = append(node.subscriptions[t], subscription)
node.bcaster.Register(subscription.C)
go func(t relay.Topic) {
2021-03-15 23:59:18 +00:00
nextMsgTicker := time.NewTicker(time.Millisecond * 10)
defer nextMsgTicker.Stop()
2021-03-15 16:07:23 +00:00
for {
select {
case <-subscription.quit:
subscription.mutex.Lock()
node.bcaster.Unregister(subscription.C) // Remove from broadcast list
close(subscription.C)
subscription.mutex.Unlock()
2021-03-15 23:59:18 +00:00
case <-nextMsgTicker.C:
msg, err := sub.Next(node.ctx)
if err != nil {
2021-04-04 17:05:33 +00:00
subscription.mutex.Lock()
for _, subscription := range node.subscriptions[t] {
subscription.Unsubscribe()
2021-04-04 17:05:33 +00:00
}
subscription.mutex.Unlock()
2021-03-18 16:40:47 +00:00
return
}
2021-03-15 16:07:23 +00:00
2021-04-22 00:09:37 +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
envelope := protocol.NewEnvelope(wakuMessage, string(t))
node.bcaster.Submit(envelope)
}
2021-03-15 16:07:23 +00:00
}
}(t)
2021-03-22 16:45:13 +00:00
return subscription, nil
2021-03-15 16:07:23 +00:00
}
func (node *WakuNode) Publish(ctx context.Context, message *pb.WakuMessage, topic *relay.Topic) ([]byte, error) {
if node.relay == nil {
return nil, errors.New("WakuRelay hasn't been set.")
2021-03-15 16:07:23 +00:00
}
if message == nil {
return nil, errors.New("message can't be null")
}
hash, err := node.relay.Publish(ctx, message, topic)
if err != nil {
return nil, err
}
return hash, nil
}
func (node *WakuNode) LightPush(ctx context.Context, message *pb.WakuMessage, topic *relay.Topic, opts ...lightpush.LightPushOption) ([]byte, error) {
if node.lightPush == nil {
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
}
req := new(pb.PushRequest)
req.Message = message
req.PubsubTopic = string(relay.GetTopic(topic))
2021-03-22 16:45:13 +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
}
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
}
func (w *WakuNode) DialPeer(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
}
w.host.Connect(w.ctx, *info)
return nil
}
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 {
return w.host.Network().ClosePeer(id)
}
func (w *WakuNode) PeerCount() int {
return len(w.host.Network().Peers())
}