124 lines
2.8 KiB
Go
124 lines
2.8 KiB
Go
package node
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
|
|
"github.com/libp2p/go-libp2p"
|
|
"github.com/libp2p/go-libp2p-core/crypto"
|
|
"github.com/libp2p/go-libp2p-core/host"
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
manet "github.com/multiformats/go-multiaddr-net"
|
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol"
|
|
)
|
|
|
|
// Default clientId
|
|
const clientId string = "Go Waku v2 node"
|
|
|
|
// XXX: Weird type, should probably be using pubsub Topic object name?
|
|
type Topic string
|
|
|
|
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
|
|
}
|
|
|
|
// NOTE based on Eth2Node in NBC eth2_network.nim
|
|
type WakuNode struct {
|
|
peerManager *PeerManager
|
|
sw host.Host
|
|
// wakuRelay *WakuRelay
|
|
// wakuStore *WakuStore
|
|
// wakuFilter *WakuFilter
|
|
//wakuSwap *WakuSwap
|
|
//wakuRlnRelay *WakuRLNRelay
|
|
peerInfo peer.AddrInfo
|
|
// libp2pTransportLoops []Future[void] ??
|
|
// TODO Revisit messages field indexing as well as if this should be Message or WakuMessage
|
|
messages []MessagePair
|
|
//filters *Filters
|
|
subscriptions protocol.MessageNotificationSubscriptions
|
|
// rng *BrHmacDrbgContext // ???
|
|
|
|
cancel context.CancelFunc
|
|
}
|
|
|
|
// Public API
|
|
//
|
|
|
|
func New(nodeKey crypto.PrivKey, hostAddr net.Addr, extAddr net.Addr) (*WakuNode, error) {
|
|
// 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)
|
|
}
|
|
|
|
opts := []libp2p.Option{
|
|
libp2p.ListenAddrs(multiAddresses...),
|
|
libp2p.Identity(nodeKey),
|
|
libp2p.DefaultTransports, //
|
|
libp2p.NATPortMap(), // Attempt to open ports using uPNP for NATed hosts.
|
|
libp2p.DisableRelay(), // TODO: what is this?
|
|
libp2p.EnableNATService(), // TODO: what is this?
|
|
}
|
|
|
|
// The context governs the lifetime of the libp2p node.
|
|
// Cancelling it will stop the the host.
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
host, err := libp2p.New(ctx, opts...)
|
|
if err != nil {
|
|
cancel()
|
|
return nil, err
|
|
}
|
|
|
|
w := new(WakuNode)
|
|
w.peerManager = NewPeerManager(host)
|
|
w.sw = host
|
|
w.cancel = cancel
|
|
// w.filters = new(Filters)
|
|
|
|
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
|
|
}
|
|
|
|
func (node *WakuNode) Stop() {
|
|
// TODO:
|
|
//if not node.wakuRelay.isNil:
|
|
// await node.wakuRelay.stop()
|
|
|
|
node.cancel()
|
|
}
|