2021-03-11 20:27:12 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-15 16:07:23 +00:00
|
|
|
"crypto/ecdsa"
|
2021-03-11 20:27:12 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
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-11 20:27:12 +00:00
|
|
|
"github.com/libp2p/go-libp2p"
|
|
|
|
"github.com/libp2p/go-libp2p-core/crypto"
|
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
2021-03-18 16:40:47 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2021-03-15 16:07:23 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
2021-03-11 20:27:12 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
|
|
manet "github.com/multiformats/go-multiaddr-net"
|
|
|
|
|
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol"
|
2021-03-18 16:40:47 +00:00
|
|
|
store "github.com/status-im/go-waku/waku/v2/protocol/waku_store"
|
2021-03-11 20:27:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Default clientId
|
|
|
|
const clientId string = "Go Waku v2 node"
|
|
|
|
|
|
|
|
type Topic string
|
|
|
|
|
2021-03-15 16:07:23 +00:00
|
|
|
const DefaultWakuTopic Topic = "/waku/2/default-waku/proto"
|
|
|
|
|
2021-03-11 20:27:12 +00:00
|
|
|
type Message []byte
|
|
|
|
|
|
|
|
type WakuInfo struct {
|
|
|
|
// NOTE One for simplicity, can extend later as needed
|
|
|
|
listenStr string
|
|
|
|
multiaddrStrings []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type MessagePair struct {
|
|
|
|
a *Topic
|
|
|
|
b *protocol.WakuMessage
|
|
|
|
}
|
|
|
|
|
2021-03-15 21:17:36 +00:00
|
|
|
type Subscription struct {
|
2021-03-15 21:43:26 +00:00
|
|
|
C chan *protocol.WakuMessage
|
|
|
|
closed bool
|
|
|
|
mutex sync.Mutex
|
|
|
|
pubSubscription *pubsub.Subscription
|
|
|
|
quit chan struct{}
|
2021-03-15 21:17:36 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 20:27:12 +00:00
|
|
|
type WakuNode struct {
|
2021-03-15 16:07:23 +00:00
|
|
|
// peerManager *PeerManager
|
|
|
|
host host.Host
|
|
|
|
pubsub *pubsub.PubSub
|
2021-03-18 16:40:47 +00:00
|
|
|
store *store.WakuStore
|
2021-03-15 16:07:23 +00:00
|
|
|
|
|
|
|
topics map[Topic]*pubsub.Topic
|
|
|
|
topicsLock sync.Mutex
|
|
|
|
|
|
|
|
// peerInfo peer.AddrInfo
|
2021-03-11 20:27:12 +00:00
|
|
|
// TODO Revisit messages field indexing as well as if this should be Message or WakuMessage
|
2021-03-15 16:07:23 +00:00
|
|
|
// messages []MessagePair
|
|
|
|
|
2021-03-11 20:27:12 +00:00
|
|
|
subscriptions protocol.MessageNotificationSubscriptions
|
2021-03-15 16:07:23 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
privKey crypto.PrivKey
|
2021-03-11 20:27:12 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 16:07:23 +00:00
|
|
|
func New(ctx context.Context, privKey *ecdsa.PrivateKey, hostAddr net.Addr, extAddr net.Addr) (*WakuNode, error) {
|
2021-03-11 20:27:12 +00:00
|
|
|
// Creates a Waku Node.
|
|
|
|
if hostAddr == nil {
|
|
|
|
return nil, errors.New("Host address cannot be null")
|
|
|
|
}
|
|
|
|
|
|
|
|
var multiAddresses []ma.Multiaddr
|
|
|
|
hostAddrMA, err := manet.FromNetAddr(hostAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
multiAddresses = append(multiAddresses, hostAddrMA)
|
|
|
|
|
|
|
|
if extAddr != nil {
|
|
|
|
extAddrMA, err := manet.FromNetAddr(extAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
multiAddresses = append(multiAddresses, extAddrMA)
|
|
|
|
}
|
|
|
|
|
2021-03-15 16:07:23 +00:00
|
|
|
nodeKey := crypto.PrivKey((*crypto.Secp256k1PrivateKey)(privKey))
|
|
|
|
|
2021-03-11 20:27:12 +00:00
|
|
|
opts := []libp2p.Option{
|
|
|
|
libp2p.ListenAddrs(multiAddresses...),
|
|
|
|
libp2p.Identity(nodeKey),
|
2021-03-15 21:17:36 +00:00
|
|
|
libp2p.DefaultTransports, //
|
|
|
|
libp2p.NATPortMap(), // Attempt to open ports using uPNP for NATed hosts.
|
|
|
|
//libp2p.DisableRelay(), // TODO: is this needed?
|
|
|
|
//libp2p.EnableNATService(), // TODO: is this needed?
|
2021-03-11 20:27:12 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 16:07:23 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
_ = cancel
|
|
|
|
|
2021-03-11 20:27:12 +00:00
|
|
|
host, err := libp2p.New(ctx, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
w := new(WakuNode)
|
2021-03-15 16:07:23 +00:00
|
|
|
//w.peerManager = NewPeerManager(host)
|
|
|
|
w.pubsub = nil
|
|
|
|
w.host = host
|
|
|
|
w.cancel = cancel
|
|
|
|
w.privKey = nodeKey
|
|
|
|
w.ctx = ctx
|
|
|
|
w.topics = make(map[Topic]*pubsub.Topic)
|
2021-03-11 20:27:12 +00:00
|
|
|
|
|
|
|
hostInfo, _ := ma.NewMultiaddr(fmt.Sprintf("/p2p/%s", host.ID().Pretty()))
|
|
|
|
for _, addr := range host.Addrs() {
|
|
|
|
fullAddr := addr.Encapsulate(hostInfo)
|
|
|
|
log.Printf("Listening on %s\n", fullAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
2021-03-15 16:07:23 +00:00
|
|
|
func (w *WakuNode) Stop() error {
|
|
|
|
defer w.cancel()
|
|
|
|
// TODO: Is it necessary to stop WakuRelaySubRouter?
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-15 16:07:23 +00:00
|
|
|
func (w *WakuNode) PubSub() *pubsub.PubSub {
|
|
|
|
return w.pubsub
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WakuNode) SetPubSub(pubSub *pubsub.PubSub) {
|
|
|
|
w.pubsub = pubSub
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WakuNode) MountRelay() error {
|
|
|
|
ps, err := protocol.NewWakuRelay(w.ctx, w.host)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.pubsub = ps
|
|
|
|
|
|
|
|
// TODO: filters
|
|
|
|
// TODO: rlnRelay
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-18 16:40:47 +00:00
|
|
|
func (w *WakuNode) MountStore() error {
|
|
|
|
sub, err := w.Subscribe(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: kill subscription on close
|
|
|
|
w.store = store.NewWakuStore(w.ctx, w.host, sub.C, new(store.MessageProvider)) // TODO: pass store
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-18 23:21:45 +00:00
|
|
|
func (w *WakuNode) StartStore() error {
|
|
|
|
if w.store == nil {
|
|
|
|
return errors.New("WakuStore is not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
w.store.Start()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-18 16:40:47 +00:00
|
|
|
func (w *WakuNode) AddStorePeer(address string) error {
|
|
|
|
if w.store == nil {
|
|
|
|
return errors.New("WakuStore is not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
storePeer, err := ma.NewMultiaddr(address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the peer ID from the multiaddr.
|
|
|
|
info, err := peer.AddrInfoFromP2pAddr(storePeer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.store.AddPeer(info.ID, info.Addrs)
|
|
|
|
}
|
|
|
|
|
2021-03-18 23:21:45 +00:00
|
|
|
func (w *WakuNode) Query(contentTopic uint32, asc bool, pageSize int64) (*protocol.HistoryResponse, error) {
|
|
|
|
if w.store == nil {
|
|
|
|
return nil, errors.New("WakuStore is not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
query := new(protocol.HistoryQuery)
|
|
|
|
query.Topics = append(query.Topics, contentTopic)
|
|
|
|
query.PagingInfo = new(protocol.PagingInfo)
|
|
|
|
if asc {
|
|
|
|
query.PagingInfo.Direction = protocol.PagingInfo_FORWARD
|
|
|
|
} else {
|
|
|
|
query.PagingInfo.Direction = protocol.PagingInfo_BACKWARD
|
|
|
|
}
|
|
|
|
query.PagingInfo.PageSize = pageSize
|
|
|
|
|
|
|
|
result, err := w.store.Query(query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2021-03-15 21:17:36 +00:00
|
|
|
func (node *WakuNode) Subscribe(topic *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.pubsub == nil {
|
|
|
|
return nil, errors.New("PubSub hasn't been set. Execute mountWakuRelay() or setPubSub() first")
|
|
|
|
}
|
|
|
|
|
|
|
|
pubSubTopic, err := node.upsertTopic(topic)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sub, err := pubSubTopic.Subscribe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-15 21:17:36 +00:00
|
|
|
subscription := new(Subscription)
|
|
|
|
subscription.closed = false
|
2021-03-15 21:43:26 +00:00
|
|
|
subscription.pubSubscription = sub
|
2021-03-15 21:17:36 +00:00
|
|
|
subscription.C = make(chan *protocol.WakuMessage)
|
|
|
|
subscription.quit = make(chan struct{})
|
2021-03-15 16:07:23 +00:00
|
|
|
|
|
|
|
go func(ctx context.Context, sub *pubsub.Subscription) {
|
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 {
|
2021-03-15 21:17:36 +00:00
|
|
|
select {
|
|
|
|
case <-subscription.quit:
|
2021-03-15 21:43:26 +00:00
|
|
|
subscription.mutex.Lock()
|
|
|
|
defer subscription.mutex.Unlock()
|
2021-03-15 21:17:36 +00:00
|
|
|
close(subscription.C)
|
|
|
|
subscription.closed = true
|
2021-03-15 16:07:23 +00:00
|
|
|
return
|
2021-03-15 23:59:18 +00:00
|
|
|
case <-nextMsgTicker.C:
|
2021-03-15 21:17:36 +00:00
|
|
|
msg, err := sub.Next(ctx)
|
|
|
|
if err != nil {
|
2021-03-15 23:59:18 +00:00
|
|
|
fmt.Println("Error receiving message", err)
|
2021-03-18 16:40:47 +00:00
|
|
|
close(subscription.quit)
|
|
|
|
return
|
2021-03-15 21:17:36 +00:00
|
|
|
}
|
2021-03-15 16:07:23 +00:00
|
|
|
|
2021-03-15 21:17:36 +00:00
|
|
|
wakuMessage := &protocol.WakuMessage{}
|
|
|
|
if err := proto.Unmarshal(msg.Data, wakuMessage); err != nil {
|
|
|
|
fmt.Println("Error decoding WakuMessage: ", err) // TODO: use log lib
|
|
|
|
return
|
|
|
|
}
|
2021-03-15 16:07:23 +00:00
|
|
|
|
2021-03-15 21:17:36 +00:00
|
|
|
subscription.C <- wakuMessage
|
|
|
|
}
|
2021-03-15 16:07:23 +00:00
|
|
|
}
|
|
|
|
}(node.ctx, sub)
|
|
|
|
|
2021-03-15 21:17:36 +00:00
|
|
|
return subscription, nil
|
2021-03-15 16:07:23 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 21:17:36 +00:00
|
|
|
func (subs *Subscription) Unsubscribe() {
|
2021-03-15 16:07:23 +00:00
|
|
|
// Unsubscribes a handler from a PubSub topic.
|
2021-03-15 21:17:36 +00:00
|
|
|
subs.mutex.Lock()
|
|
|
|
defer subs.mutex.Unlock()
|
|
|
|
if !subs.closed {
|
|
|
|
close(subs.quit)
|
|
|
|
}
|
2021-03-15 16:07:23 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 21:43:26 +00:00
|
|
|
func (subs *Subscription) IsClosed() bool {
|
|
|
|
subs.mutex.Lock()
|
|
|
|
defer subs.mutex.Unlock()
|
|
|
|
return subs.closed
|
|
|
|
}
|
|
|
|
|
2021-03-15 16:07:23 +00:00
|
|
|
func (node *WakuNode) upsertTopic(topic *Topic) (*pubsub.Topic, error) {
|
|
|
|
defer node.topicsLock.Unlock()
|
|
|
|
node.topicsLock.Lock()
|
|
|
|
|
|
|
|
var t Topic = DefaultWakuTopic
|
|
|
|
if topic != nil {
|
|
|
|
t = *topic
|
|
|
|
}
|
|
|
|
|
|
|
|
pubSubTopic, ok := node.topics[t]
|
|
|
|
if !ok { // Joins topic if node hasn't joined yet
|
|
|
|
newTopic, err := node.pubsub.Join(string(t))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
node.topics[t] = newTopic
|
|
|
|
pubSubTopic = newTopic
|
|
|
|
}
|
|
|
|
return pubSubTopic, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *WakuNode) Publish(message *protocol.WakuMessage, topic *Topic) error {
|
|
|
|
// Publish a `WakuMessage` to a PubSub topic. `WakuMessage` should contain a
|
|
|
|
// `contentTopic` field for light node functionality. This field may be also
|
|
|
|
// be omitted.
|
|
|
|
|
|
|
|
if node.pubsub == nil {
|
|
|
|
return errors.New("PubSub hasn't been set. Execute mountWakuRelay() or setPubSub() first")
|
|
|
|
}
|
|
|
|
|
|
|
|
if message == nil {
|
|
|
|
return errors.New("Message can't be null")
|
|
|
|
}
|
|
|
|
|
|
|
|
pubSubTopic, err := node.upsertTopic(topic)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := proto.Marshal(message)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pubSubTopic.Publish(node.ctx, out)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-11 20:27:12 +00:00
|
|
|
|
2021-03-15 16:07:23 +00:00
|
|
|
return nil
|
2021-03-11 20:27:12 +00:00
|
|
|
}
|
2021-03-18 23:21:45 +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
|
|
|
|
}
|