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"
|
|
|
|
"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-04-11 23:43:59 +00:00
|
|
|
gcrypto "github.com/ethereum/go-ethereum/crypto"
|
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-03-22 16:45:13 +00:00
|
|
|
connmgr "github.com/libp2p/go-libp2p-connmgr"
|
2021-03-11 20:27:12 +00:00
|
|
|
"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-11 20:27:12 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
|
|
manet "github.com/multiformats/go-multiaddr-net"
|
2021-03-30 14:13:33 +00:00
|
|
|
common "github.com/status-im/go-waku/waku/common"
|
2021-03-11 20:27:12 +00:00
|
|
|
"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-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-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 WakuNode struct {
|
2021-03-15 16:07:23 +00:00
|
|
|
host host.Host
|
2021-03-24 15:53:43 +00:00
|
|
|
pubsub *wakurelay.PubSub
|
2021-03-15 16:07:23 +00:00
|
|
|
|
2021-04-15 02:19:31 +00:00
|
|
|
store *store.WakuStore
|
|
|
|
isStore bool
|
2021-03-15 16:07:23 +00:00
|
|
|
|
2021-04-15 02:19:31 +00:00
|
|
|
topics map[Topic]bool
|
|
|
|
topicsMutex sync.Mutex
|
|
|
|
wakuRelayTopics map[Topic]*wakurelay.Topic
|
|
|
|
|
|
|
|
subscriptions map[Topic][]*Subscription
|
2021-03-22 16:45:13 +00:00
|
|
|
subscriptionsMutex sync.Mutex
|
2021-03-15 16:07:23 +00:00
|
|
|
|
2021-04-15 02:19:31 +00:00
|
|
|
bcaster Broadcaster
|
|
|
|
relaySubs map[Topic]*wakurelay.Subscription
|
|
|
|
|
2021-03-22 16:45:13 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
privKey crypto.PrivKey
|
2021-03-11 20:27:12 +00:00
|
|
|
}
|
|
|
|
|
2021-03-30 14:13:33 +00:00
|
|
|
func New(ctx context.Context, privKey *ecdsa.PrivateKey, hostAddr []net.Addr, opts ...libp2p.Option) (*WakuNode, error) {
|
2021-03-11 20:27:12 +00:00
|
|
|
// Creates a Waku Node.
|
|
|
|
if hostAddr == nil {
|
2021-03-22 16:45:13 +00:00
|
|
|
return nil, errors.New("host address cannot be null")
|
2021-03-11 20:27:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var multiAddresses []ma.Multiaddr
|
2021-03-30 14:13:33 +00:00
|
|
|
for _, addr := range hostAddr {
|
|
|
|
hostAddrMA, err := manet.FromNetAddr(addr)
|
2021-03-11 20:27:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-30 14:13:33 +00:00
|
|
|
multiAddresses = append(multiAddresses, hostAddrMA)
|
2021-03-11 20:27:12 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 16:07:23 +00:00
|
|
|
nodeKey := crypto.PrivKey((*crypto.Secp256k1PrivateKey)(privKey))
|
|
|
|
|
2021-03-22 16:45:13 +00:00
|
|
|
opts = append(opts,
|
2021-03-11 20:27:12 +00:00
|
|
|
libp2p.ListenAddrs(multiAddresses...),
|
|
|
|
libp2p.Identity(nodeKey),
|
2021-03-22 16:45:13 +00:00
|
|
|
libp2p.DefaultTransports,
|
|
|
|
libp2p.NATPortMap(), // Attempt to open ports using uPNP for NATed hosts.
|
|
|
|
libp2p.EnableNATService(), // TODO: is this needed?)
|
|
|
|
libp2p.ConnectionManager(connmgr.NewConnManager(200, 300, 0)), // ?
|
|
|
|
)
|
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-04-15 02:19:31 +00:00
|
|
|
w.bcaster = NewBroadcaster(1024)
|
2021-03-15 16:07:23 +00:00
|
|
|
w.pubsub = nil
|
|
|
|
w.host = host
|
|
|
|
w.cancel = cancel
|
|
|
|
w.privKey = nodeKey
|
|
|
|
w.ctx = ctx
|
2021-04-15 02:19:31 +00:00
|
|
|
w.topics = make(map[Topic]bool)
|
|
|
|
w.wakuRelayTopics = make(map[Topic]*wakurelay.Topic)
|
|
|
|
w.relaySubs = make(map[Topic]*wakurelay.Subscription)
|
|
|
|
w.subscriptions = make(map[Topic][]*Subscription)
|
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
|
|
|
|
2021-04-15 02:19:31 +00:00
|
|
|
for topic, _ := range w.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
|
|
|
|
}
|
|
|
|
|
2021-03-18 23:21:45 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-03-24 15:53:43 +00:00
|
|
|
func (w *WakuNode) PubSub() *wakurelay.PubSub {
|
2021-03-15 16:07:23 +00:00
|
|
|
return w.pubsub
|
|
|
|
}
|
|
|
|
|
2021-03-24 15:53:43 +00:00
|
|
|
func (w *WakuNode) SetPubSub(pubSub *wakurelay.PubSub) {
|
2021-03-15 16:07:23 +00:00
|
|
|
w.pubsub = pubSub
|
|
|
|
}
|
|
|
|
|
2021-03-30 14:13:33 +00:00
|
|
|
func (w *WakuNode) MountRelay(opts ...wakurelay.Option) error {
|
|
|
|
ps, err := wakurelay.NewWakuRelaySub(w.ctx, w.host, opts...)
|
2021-03-15 16:07:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.pubsub = ps
|
|
|
|
|
|
|
|
// TODO: filters
|
|
|
|
// TODO: rlnRelay
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-15 02:19:31 +00:00
|
|
|
func (w *WakuNode) MountStore(isStore bool, s store.MessageProvider) error {
|
|
|
|
w.store = store.NewWakuStore(w.ctx, w.host, s)
|
|
|
|
w.isStore = isStore
|
2021-03-18 16:40:47 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2021-04-15 02:19:31 +00:00
|
|
|
_, err := w.Subscribe(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-18 23:21:45 +00:00
|
|
|
w.store.Start()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-15 02:19:31 +00:00
|
|
|
func (w *WakuNode) AddStorePeer(address string) (*peer.ID, error) {
|
2021-03-18 16:40:47 +00:00
|
|
|
if w.store == nil {
|
2021-04-15 02:19:31 +00:00
|
|
|
return nil, errors.New("WakuStore is not set")
|
2021-03-18 16:40:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
storePeer, err := ma.NewMultiaddr(address)
|
|
|
|
if err != nil {
|
2021-04-15 02:19:31 +00:00
|
|
|
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 {
|
2021-04-15 02:19:31 +00:00
|
|
|
return nil, err
|
2021-03-18 16:40:47 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 02:19:31 +00:00
|
|
|
return &info.ID, w.store.AddPeer(info.ID, info.Addrs)
|
2021-03-18 16:40:47 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 02:19:31 +00:00
|
|
|
func (w *WakuNode) Query(contentTopics []string, startTime float64, endTime float64, opts ...store.HistoryRequestOption) (*protocol.HistoryResponse, error) {
|
2021-03-18 23:21:45 +00:00
|
|
|
if w.store == nil {
|
|
|
|
return nil, errors.New("WakuStore is not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
query := new(protocol.HistoryQuery)
|
2021-04-15 02:19:31 +00:00
|
|
|
query.Topics = contentTopics
|
|
|
|
query.StartTime = startTime
|
|
|
|
query.EndTime = endTime
|
2021-03-18 23:21:45 +00:00
|
|
|
query.PagingInfo = new(protocol.PagingInfo)
|
2021-04-15 02:19:31 +00:00
|
|
|
result, err := w.store.Query(query, opts...)
|
2021-03-18 23:21:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2021-04-15 02:19:31 +00:00
|
|
|
func getTopic(topic *Topic) Topic {
|
|
|
|
var t Topic = DefaultWakuTopic
|
|
|
|
if topic != nil {
|
|
|
|
t = *topic
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2021-04-15 02:19:31 +00:00
|
|
|
t := getTopic(topic)
|
2021-03-15 16:07:23 +00:00
|
|
|
|
2021-04-15 02:19:31 +00:00
|
|
|
sub, err := node.upsertSubscription(t)
|
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-15 02:19:31 +00:00
|
|
|
subscription.C = make(chan *common.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()
|
|
|
|
node.subscriptions[t] = append(node.subscriptions[t], subscription)
|
|
|
|
|
|
|
|
node.bcaster.Register(subscription.C)
|
|
|
|
|
|
|
|
go func() {
|
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()
|
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
|
|
|
node.topicsMutex.Lock()
|
|
|
|
for _, subscription := range node.subscriptions[t] {
|
|
|
|
subscription.Unsubscribe()
|
2021-04-04 17:05:33 +00:00
|
|
|
}
|
2021-04-15 02:19:31 +00:00
|
|
|
node.topicsMutex.Unlock()
|
|
|
|
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-03-15 21:17:36 +00:00
|
|
|
wakuMessage := &protocol.WakuMessage{}
|
|
|
|
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-11 23:43:59 +00:00
|
|
|
envelope := common.NewEnvelope(wakuMessage, len(msg.Data), gcrypto.Keccak256(msg.Data))
|
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-15 02:19:31 +00:00
|
|
|
}()
|
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-04-15 02:19:31 +00:00
|
|
|
func (node *WakuNode) upsertTopic(topic Topic) (*wakurelay.Topic, error) {
|
2021-03-22 16:45:13 +00:00
|
|
|
defer node.topicsMutex.Unlock()
|
|
|
|
node.topicsMutex.Lock()
|
2021-03-15 16:07:23 +00:00
|
|
|
|
2021-04-15 02:19:31 +00:00
|
|
|
node.topics[topic] = true
|
|
|
|
pubSubTopic, ok := node.wakuRelayTopics[topic]
|
2021-03-15 16:07:23 +00:00
|
|
|
if !ok { // Joins topic if node hasn't joined yet
|
2021-04-15 02:19:31 +00:00
|
|
|
newTopic, err := node.pubsub.Join(string(topic))
|
2021-03-15 16:07:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-15 02:19:31 +00:00
|
|
|
node.wakuRelayTopics[topic] = newTopic
|
2021-03-15 16:07:23 +00:00
|
|
|
pubSubTopic = newTopic
|
|
|
|
}
|
|
|
|
return pubSubTopic, nil
|
|
|
|
}
|
|
|
|
|
2021-04-15 02:19:31 +00:00
|
|
|
func (node *WakuNode) upsertSubscription(topic Topic) (*wakurelay.Subscription, error) {
|
|
|
|
sub, ok := node.relaySubs[topic]
|
|
|
|
if !ok {
|
|
|
|
pubSubTopic, err := node.upsertTopic(topic)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sub, err = pubSubTopic.Subscribe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
node.relaySubs[topic] = sub
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.store != nil && node.isStore {
|
|
|
|
node.bcaster.Register(node.store.MsgC)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sub, nil
|
|
|
|
}
|
|
|
|
|
2021-04-11 23:43:59 +00:00
|
|
|
func (node *WakuNode) Publish(message *protocol.WakuMessage, topic *Topic) ([]byte, error) {
|
2021-03-15 16:07:23 +00:00
|
|
|
// 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 {
|
2021-04-11 23:43:59 +00:00
|
|
|
return nil, errors.New("PubSub hasn't been set. Execute mountWakuRelay() or setPubSub() first")
|
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-15 02:19:31 +00:00
|
|
|
pubSubTopic, err := node.upsertTopic(getTopic(topic))
|
2021-03-22 16:45:13 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
out, err := proto.Marshal(message)
|
|
|
|
if err != nil {
|
2021-04-11 23:43:59 +00:00
|
|
|
return nil, err
|
2021-03-15 16:07:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = pubSubTopic.Publish(node.ctx, out)
|
2021-03-22 16:45:13 +00:00
|
|
|
|
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-03-11 20:27:12 +00:00
|
|
|
|
2021-04-11 23:43:59 +00:00
|
|
|
hash := gcrypto.Keccak256(out)
|
|
|
|
|
|
|
|
return hash, 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
|
|
|
|
}
|
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 {
|
|
|
|
return w.host.Network().ClosePeer(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WakuNode) PeerCount() int {
|
|
|
|
return len(w.host.Network().Peers())
|
|
|
|
}
|