2021-04-18 23:41:42 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2022-03-22 13:12:58 +00:00
|
|
|
"crypto/tls"
|
2022-05-30 18:48:22 +00:00
|
|
|
"errors"
|
2021-10-15 02:15:02 +00:00
|
|
|
"fmt"
|
2021-04-18 23:41:42 +00:00
|
|
|
"net"
|
2021-06-24 13:02:53 +00:00
|
|
|
"time"
|
2021-04-18 23:41:42 +00:00
|
|
|
|
2022-07-28 14:04:33 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-11-17 16:19:42 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2023-02-01 23:35:31 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2021-04-18 23:41:42 +00:00
|
|
|
"github.com/libp2p/go-libp2p"
|
2021-07-29 22:08:53 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
2021-10-15 12:38:55 +00:00
|
|
|
"github.com/libp2p/go-libp2p/config"
|
2022-10-19 19:39:32 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/crypto"
|
2023-06-05 14:39:38 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peerstore"
|
2021-10-15 02:15:02 +00:00
|
|
|
basichost "github.com/libp2p/go-libp2p/p2p/host/basic"
|
2022-05-31 19:51:53 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/muxer/mplex"
|
|
|
|
"github.com/libp2p/go-libp2p/p2p/muxer/yamux"
|
2022-05-27 19:55:35 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
|
|
|
|
quic "github.com/libp2p/go-libp2p/p2p/transport/quic"
|
|
|
|
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
|
2023-04-14 16:32:03 +00:00
|
|
|
libp2pwebtransport "github.com/libp2p/go-libp2p/p2p/transport/webtransport"
|
2021-10-15 02:15:02 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2023-02-08 16:02:06 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2021-10-08 13:50:56 +00:00
|
|
|
manet "github.com/multiformats/go-multiaddr/net"
|
2023-08-16 01:40:00 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/filter"
|
2023-04-11 14:38:16 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/legacy_filter"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/store"
|
2023-03-09 15:48:25 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/rendezvous"
|
2022-12-09 03:08:04 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/timesource"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/utils"
|
2022-01-18 18:17:06 +00:00
|
|
|
"go.uber.org/zap"
|
2023-02-01 23:35:31 +00:00
|
|
|
"go.uber.org/zap/zapcore"
|
2021-04-18 23:41:42 +00:00
|
|
|
)
|
|
|
|
|
2022-10-26 12:04:18 +00:00
|
|
|
// Default userAgent
|
|
|
|
const userAgent string = "go-waku"
|
2021-06-28 14:14:28 +00:00
|
|
|
|
2021-12-06 08:43:00 +00:00
|
|
|
// Default minRelayPeersToPublish
|
2022-03-21 23:15:53 +00:00
|
|
|
const defaultMinRelayPeersToPublish = 0
|
2021-12-06 08:43:00 +00:00
|
|
|
|
2021-04-18 23:41:42 +00:00
|
|
|
type WakuNodeParameters struct {
|
2021-11-17 16:19:42 +00:00
|
|
|
hostAddr *net.TCPAddr
|
2022-03-22 00:48:46 +00:00
|
|
|
dns4Domain string
|
2023-02-08 16:02:06 +00:00
|
|
|
advertiseAddrs []multiaddr.Multiaddr
|
2022-05-30 18:48:22 +00:00
|
|
|
multiAddr []multiaddr.Multiaddr
|
2021-10-15 02:15:02 +00:00
|
|
|
addressFactory basichost.AddrsFactory
|
2021-11-17 16:19:42 +00:00
|
|
|
privKey *ecdsa.PrivateKey
|
2021-10-15 02:15:02 +00:00
|
|
|
libP2POpts []libp2p.Option
|
2023-06-05 14:39:38 +00:00
|
|
|
peerstore peerstore.Peerstore
|
2023-08-16 01:40:00 +00:00
|
|
|
prometheusReg prometheus.Registerer
|
2021-04-18 23:41:42 +00:00
|
|
|
|
2022-12-09 03:08:04 +00:00
|
|
|
enableNTP bool
|
|
|
|
ntpURLs []string
|
|
|
|
|
2022-03-22 13:12:58 +00:00
|
|
|
enableWS bool
|
2022-03-22 00:48:46 +00:00
|
|
|
wsPort int
|
2022-03-22 13:12:58 +00:00
|
|
|
enableWSS bool
|
2022-03-22 00:48:46 +00:00
|
|
|
wssPort int
|
2022-03-22 13:12:58 +00:00
|
|
|
tlsConfig *tls.Config
|
|
|
|
|
2023-02-24 15:58:49 +00:00
|
|
|
logger *zap.Logger
|
|
|
|
logLevel logging.LogLevel
|
2022-01-18 18:17:06 +00:00
|
|
|
|
2023-04-11 14:38:16 +00:00
|
|
|
enableRelay bool
|
2023-04-17 00:04:12 +00:00
|
|
|
enableLegacyFilter bool
|
2023-08-14 20:29:00 +00:00
|
|
|
isLegacyFilterFullNode bool
|
2023-04-11 14:38:16 +00:00
|
|
|
enableFilterLightNode bool
|
2023-04-17 00:04:12 +00:00
|
|
|
enableFilterFullNode bool
|
2023-04-11 14:38:16 +00:00
|
|
|
legacyFilterOpts []legacy_filter.Option
|
|
|
|
filterOpts []filter.Option
|
2023-08-16 01:40:00 +00:00
|
|
|
pubsubOpts []pubsub.Option
|
2021-04-18 23:41:42 +00:00
|
|
|
|
2021-12-06 08:43:00 +00:00
|
|
|
minRelayPeersToPublish int
|
|
|
|
|
2021-11-01 12:38:03 +00:00
|
|
|
enableStore bool
|
|
|
|
messageProvider store.MessageProvider
|
2021-04-28 20:23:03 +00:00
|
|
|
|
2023-07-31 18:58:50 +00:00
|
|
|
enableRendezvousPoint bool
|
|
|
|
rendezvousDB *rendezvous.DB
|
2023-03-09 15:48:25 +00:00
|
|
|
|
2023-08-03 16:21:15 +00:00
|
|
|
maxPeerConnections int
|
2023-01-13 23:58:22 +00:00
|
|
|
|
2021-11-17 16:19:42 +00:00
|
|
|
enableDiscV5 bool
|
2023-01-12 02:20:23 +00:00
|
|
|
udpPort uint
|
2021-11-17 16:19:42 +00:00
|
|
|
discV5bootnodes []*enode.Node
|
|
|
|
discV5autoUpdate bool
|
|
|
|
|
2022-10-23 13:13:43 +00:00
|
|
|
enablePeerExchange bool
|
|
|
|
|
2022-07-28 14:04:33 +00:00
|
|
|
enableRLN bool
|
2023-09-04 21:44:41 +00:00
|
|
|
rlnRelayMemIndex *uint
|
2022-07-28 14:04:33 +00:00
|
|
|
rlnRelayDynamic bool
|
2022-08-12 12:44:13 +00:00
|
|
|
rlnSpamHandler func(message *pb.WakuMessage) error
|
2022-07-28 14:04:33 +00:00
|
|
|
rlnETHClientAddress string
|
2023-04-05 19:44:46 +00:00
|
|
|
keystorePath string
|
|
|
|
keystorePassword string
|
2023-08-18 13:59:37 +00:00
|
|
|
rlnTreePath string
|
2022-07-28 14:04:33 +00:00
|
|
|
rlnMembershipContractAddress common.Address
|
2022-07-05 21:28:34 +00:00
|
|
|
|
2021-06-24 13:02:53 +00:00
|
|
|
keepAliveInterval time.Duration
|
|
|
|
|
2021-04-28 20:23:03 +00:00
|
|
|
enableLightPush bool
|
2021-06-16 10:14:22 +00:00
|
|
|
|
2023-05-10 14:13:10 +00:00
|
|
|
connStatusC chan<- ConnStatus
|
|
|
|
connNotifCh chan<- PeerConnection
|
2022-03-18 19:56:34 +00:00
|
|
|
|
|
|
|
storeFactory storeFactory
|
2021-04-18 23:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type WakuNodeOption func(*WakuNodeParameters) error
|
|
|
|
|
2021-11-17 16:19:42 +00:00
|
|
|
// Default options used in the libp2p node
|
|
|
|
var DefaultWakuNodeOptions = []WakuNodeOption{
|
2023-08-16 01:40:00 +00:00
|
|
|
WithPrometheusRegisterer(prometheus.NewRegistry()),
|
2023-08-03 16:21:15 +00:00
|
|
|
WithMaxPeerConnections(50),
|
2021-11-17 16:19:42 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 20:09:48 +00:00
|
|
|
// MultiAddresses return the list of multiaddresses configured in the node
|
2022-05-30 18:48:22 +00:00
|
|
|
func (w WakuNodeParameters) MultiAddresses() []multiaddr.Multiaddr {
|
2021-10-14 18:17:01 +00:00
|
|
|
return w.multiAddr
|
|
|
|
}
|
|
|
|
|
2021-11-05 20:09:48 +00:00
|
|
|
// Identity returns a libp2p option containing the identity used by the node
|
2021-10-15 12:38:55 +00:00
|
|
|
func (w WakuNodeParameters) Identity() config.Option {
|
2021-11-17 16:19:42 +00:00
|
|
|
return libp2p.Identity(*w.GetPrivKey())
|
2021-10-15 12:38:55 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 20:36:04 +00:00
|
|
|
// TLSConfig returns the TLS config used for setting up secure websockets
|
|
|
|
func (w WakuNodeParameters) TLSConfig() *tls.Config {
|
|
|
|
return w.tlsConfig
|
|
|
|
}
|
|
|
|
|
2022-04-25 19:31:26 +00:00
|
|
|
// AddressFactory returns the address factory used by the node's host
|
2021-11-17 16:19:42 +00:00
|
|
|
func (w WakuNodeParameters) AddressFactory() basichost.AddrsFactory {
|
|
|
|
return w.addressFactory
|
|
|
|
}
|
|
|
|
|
2022-01-18 18:17:06 +00:00
|
|
|
// WithLogger is a WakuNodeOption that adds a custom logger
|
|
|
|
func WithLogger(l *zap.Logger) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
2022-05-27 13:25:06 +00:00
|
|
|
params.logger = l
|
2023-02-01 23:35:31 +00:00
|
|
|
logging.SetPrimaryCore(l.Core())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithLogLevel is a WakuNodeOption that sets the log level for go-waku
|
|
|
|
func WithLogLevel(lvl zapcore.Level) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
2023-02-24 15:58:49 +00:00
|
|
|
params.logLevel = logging.LogLevel(lvl)
|
|
|
|
logging.SetAllLoggers(params.logLevel)
|
2022-01-18 18:17:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-16 01:40:00 +00:00
|
|
|
// WithPrometheusRegisterer configures go-waku to use reg as the Registerer for all metrics subsystems
|
|
|
|
func WithPrometheusRegisterer(reg prometheus.Registerer) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
if reg == nil {
|
|
|
|
return errors.New("registerer cannot be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
params.prometheusReg = reg
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
// WithDNS4Domain is a WakuNodeOption that adds a custom domain name to listen
|
|
|
|
func WithDNS4Domain(dns4Domain string) WakuNodeOption {
|
2022-03-22 00:48:46 +00:00
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.dns4Domain = dns4Domain
|
2023-05-15 18:44:36 +00:00
|
|
|
previousAddrFactory := params.addressFactory
|
|
|
|
params.addressFactory = func(inputAddr []multiaddr.Multiaddr) (addresses []multiaddr.Multiaddr) {
|
|
|
|
addresses = append(addresses, inputAddr...)
|
2022-03-22 00:48:46 +00:00
|
|
|
|
2022-05-30 18:48:22 +00:00
|
|
|
hostAddrMA, err := multiaddr.NewMultiaddr("/dns4/" + params.dns4Domain)
|
2022-03-22 00:48:46 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("invalid dns4 address: %s", err.Error()))
|
|
|
|
}
|
|
|
|
|
2022-05-30 18:48:22 +00:00
|
|
|
tcp, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/tcp/%d", params.hostAddr.Port))
|
2022-03-22 00:48:46 +00:00
|
|
|
|
2023-05-15 18:44:36 +00:00
|
|
|
addresses = append(addresses, hostAddrMA.Encapsulate(tcp))
|
2022-03-22 00:48:46 +00:00
|
|
|
|
|
|
|
if params.enableWS || params.enableWSS {
|
|
|
|
if params.enableWSS {
|
2023-09-06 12:37:15 +00:00
|
|
|
// WSS is deprecated in https://github.com/multiformats/multiaddr/pull/109
|
2022-05-30 18:48:22 +00:00
|
|
|
wss, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/tcp/%d/wss", params.wssPort))
|
2023-05-15 18:44:36 +00:00
|
|
|
addresses = append(addresses, hostAddrMA.Encapsulate(wss))
|
2023-09-06 12:37:15 +00:00
|
|
|
tlsws, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/tcp/%d/tls/ws", params.wssPort))
|
|
|
|
addresses = append(addresses, hostAddrMA.Encapsulate(tlsws))
|
2022-03-22 00:48:46 +00:00
|
|
|
} else {
|
2022-05-30 18:48:22 +00:00
|
|
|
ws, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/tcp/%d/ws", params.wsPort))
|
2023-05-15 18:44:36 +00:00
|
|
|
addresses = append(addresses, hostAddrMA.Encapsulate(ws))
|
2022-03-22 00:48:46 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-15 18:44:36 +00:00
|
|
|
|
|
|
|
if previousAddrFactory != nil {
|
|
|
|
return previousAddrFactory(addresses)
|
|
|
|
}
|
2023-09-11 14:24:05 +00:00
|
|
|
|
|
|
|
return addresses
|
2022-03-22 00:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-17 16:19:42 +00:00
|
|
|
// WithHostAddress is a WakuNodeOption that configures libp2p to listen on a specific address
|
|
|
|
func WithHostAddress(hostAddr *net.TCPAddr) WakuNodeOption {
|
2021-04-18 23:41:42 +00:00
|
|
|
return func(params *WakuNodeParameters) error {
|
2021-11-17 16:19:42 +00:00
|
|
|
params.hostAddr = hostAddr
|
|
|
|
hostAddrMA, err := manet.FromNetAddr(hostAddr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-04-18 23:41:42 +00:00
|
|
|
}
|
2021-11-17 16:19:42 +00:00
|
|
|
params.multiAddr = append(params.multiAddr, hostAddrMA)
|
2021-04-18 23:41:42 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-08 16:02:06 +00:00
|
|
|
// WithAdvertiseAddresses is a WakuNodeOption that allows overriding the address used in the waku node with custom value
|
|
|
|
func WithAdvertiseAddresses(advertiseAddrs ...ma.Multiaddr) WakuNodeOption {
|
2021-10-15 02:15:02 +00:00
|
|
|
return func(params *WakuNodeParameters) error {
|
2023-02-08 16:02:06 +00:00
|
|
|
params.advertiseAddrs = advertiseAddrs
|
2023-05-15 18:44:36 +00:00
|
|
|
return WithMultiaddress(advertiseAddrs...)(params)
|
2021-10-15 02:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-08 15:19:44 +00:00
|
|
|
// WithExternalIP is a WakuNodeOption that allows overriding the advertised external IP used in the waku node with custom value
|
|
|
|
func WithExternalIP(ip net.IP) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
2023-05-15 18:44:36 +00:00
|
|
|
oldAddrFactory := params.addressFactory
|
2023-05-08 15:19:44 +00:00
|
|
|
params.addressFactory = func(inputAddr []multiaddr.Multiaddr) (addresses []multiaddr.Multiaddr) {
|
2023-05-15 18:44:36 +00:00
|
|
|
addresses = append(addresses, inputAddr...)
|
|
|
|
|
2023-05-30 14:34:34 +00:00
|
|
|
ipType := "/ip4/"
|
|
|
|
if utils.IsIPv6(ip.String()) {
|
|
|
|
ipType = "/ip6/"
|
2023-05-08 15:19:44 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 14:34:34 +00:00
|
|
|
hostAddrMA, err := multiaddr.NewMultiaddr(ipType + ip.String())
|
2023-05-08 15:19:44 +00:00
|
|
|
if err != nil {
|
|
|
|
panic("Could not build external IP")
|
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:39 +00:00
|
|
|
addrSet := make(map[string]multiaddr.Multiaddr)
|
2023-05-08 15:19:44 +00:00
|
|
|
for _, addr := range inputAddr {
|
|
|
|
_, rest := multiaddr.SplitFirst(addr)
|
2023-05-12 13:22:39 +00:00
|
|
|
|
|
|
|
addr := hostAddrMA.Encapsulate(rest)
|
|
|
|
|
|
|
|
addrSet[addr.String()] = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range addrSet {
|
|
|
|
addresses = append(addresses, addr)
|
2023-05-08 15:19:44 +00:00
|
|
|
}
|
|
|
|
|
2023-05-15 18:44:36 +00:00
|
|
|
if oldAddrFactory != nil {
|
|
|
|
return oldAddrFactory(addresses)
|
|
|
|
} else {
|
|
|
|
return addresses
|
|
|
|
}
|
2023-05-08 15:19:44 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 20:42:44 +00:00
|
|
|
// WithMultiaddress is a WakuNodeOption that configures libp2p to listen on a list of multiaddresses
|
2023-05-15 18:44:36 +00:00
|
|
|
func WithMultiaddress(addresses ...multiaddr.Multiaddr) WakuNodeOption {
|
2021-04-22 13:07:22 +00:00
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.multiAddr = append(params.multiAddr, addresses...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 20:42:44 +00:00
|
|
|
// WithPrivateKey is used to set an ECDSA private key in a libp2p node
|
2021-04-18 23:41:42 +00:00
|
|
|
func WithPrivateKey(privKey *ecdsa.PrivateKey) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
2021-11-17 16:19:42 +00:00
|
|
|
params.privKey = privKey
|
2021-04-18 23:41:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-09 03:08:04 +00:00
|
|
|
// WithNTP is used to use ntp for any operation that requires obtaining time
|
|
|
|
// A list of ntp servers can be passed but if none is specified, some defaults
|
|
|
|
// will be used
|
|
|
|
func WithNTP(ntpURLs ...string) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
if len(ntpURLs) == 0 {
|
|
|
|
ntpURLs = timesource.DefaultServers
|
|
|
|
}
|
|
|
|
|
|
|
|
params.enableNTP = true
|
|
|
|
params.ntpURLs = ntpURLs
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 19:31:26 +00:00
|
|
|
// GetPrivKey returns the private key used in the node
|
2021-11-17 16:19:42 +00:00
|
|
|
func (w *WakuNodeParameters) GetPrivKey() *crypto.PrivKey {
|
2022-05-27 19:55:35 +00:00
|
|
|
privKey := crypto.PrivKey(utils.EcdsaPrivKeyToSecp256k1PrivKey(w.privKey))
|
2021-11-17 16:19:42 +00:00
|
|
|
return &privKey
|
|
|
|
}
|
|
|
|
|
2021-04-22 20:42:44 +00:00
|
|
|
// WithLibP2POptions is a WakuNodeOption used to configure the libp2p node.
|
|
|
|
// This can potentially override any libp2p config that was set with other
|
|
|
|
// WakuNodeOption
|
2021-04-18 23:41:42 +00:00
|
|
|
func WithLibP2POptions(opts ...libp2p.Option) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.libP2POpts = opts
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-05 14:39:38 +00:00
|
|
|
func WithPeerStore(ps peerstore.Peerstore) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.peerstore = ps
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 20:42:44 +00:00
|
|
|
// WithWakuRelay enables the Waku V2 Relay protocol. This WakuNodeOption
|
|
|
|
// accepts a list of WakuRelay gossipsub option to setup the protocol
|
2021-07-29 22:08:53 +00:00
|
|
|
func WithWakuRelay(opts ...pubsub.Option) WakuNodeOption {
|
2021-12-06 08:43:00 +00:00
|
|
|
return WithWakuRelayAndMinPeers(defaultMinRelayPeersToPublish, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithWakuRelayAndMinPeers enables the Waku V2 Relay protocol. This WakuNodeOption
|
|
|
|
// accepts a min peers require to publish and a list of WakuRelay gossipsub option to setup the protocol
|
|
|
|
func WithWakuRelayAndMinPeers(minRelayPeersToPublish int, opts ...pubsub.Option) WakuNodeOption {
|
2021-04-18 23:41:42 +00:00
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.enableRelay = true
|
2023-08-16 01:40:00 +00:00
|
|
|
params.pubsubOpts = opts
|
2021-12-06 08:43:00 +00:00
|
|
|
params.minRelayPeersToPublish = minRelayPeersToPublish
|
2021-04-18 23:41:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 16:21:15 +00:00
|
|
|
func WithMaxPeerConnections(maxPeers int) WakuNodeOption {
|
2023-01-13 23:58:22 +00:00
|
|
|
return func(params *WakuNodeParameters) error {
|
2023-08-03 16:21:15 +00:00
|
|
|
params.maxPeerConnections = maxPeers
|
2023-01-13 23:58:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-17 16:19:42 +00:00
|
|
|
// WithDiscoveryV5 is a WakuOption used to enable DiscV5 peer discovery
|
2023-01-13 23:58:22 +00:00
|
|
|
func WithDiscoveryV5(udpPort uint, bootnodes []*enode.Node, autoUpdate bool) WakuNodeOption {
|
2021-11-17 16:19:42 +00:00
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.enableDiscV5 = true
|
|
|
|
params.udpPort = udpPort
|
|
|
|
params.discV5bootnodes = bootnodes
|
|
|
|
params.discV5autoUpdate = autoUpdate
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-23 13:13:43 +00:00
|
|
|
// WithPeerExchange is a WakuOption used to enable Peer Exchange
|
|
|
|
func WithPeerExchange() WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.enablePeerExchange = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-11 14:38:16 +00:00
|
|
|
// WithLegacyWakuFilter enables the legacy Waku Filter protocol. This WakuNodeOption
|
2021-07-29 22:08:53 +00:00
|
|
|
// accepts a list of WakuFilter gossipsub options to setup the protocol
|
2023-04-11 14:38:16 +00:00
|
|
|
func WithLegacyWakuFilter(fullnode bool, filterOpts ...legacy_filter.Option) WakuNodeOption {
|
2021-06-10 12:59:51 +00:00
|
|
|
return func(params *WakuNodeParameters) error {
|
2023-04-17 00:04:12 +00:00
|
|
|
params.enableLegacyFilter = true
|
2023-08-14 20:29:00 +00:00
|
|
|
params.isLegacyFilterFullNode = fullnode
|
2023-04-11 14:38:16 +00:00
|
|
|
params.legacyFilterOpts = filterOpts
|
2021-06-10 12:59:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-11 14:38:16 +00:00
|
|
|
// WithWakuFilter enables the Waku Filter V2 protocol for lightnode functionality
|
|
|
|
func WithWakuFilterLightNode() WakuNodeOption {
|
2023-02-07 22:28:46 +00:00
|
|
|
return func(params *WakuNodeParameters) error {
|
2023-04-11 14:38:16 +00:00
|
|
|
params.enableFilterLightNode = true
|
2023-02-07 22:28:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-11 14:38:16 +00:00
|
|
|
// WithWakuFilterFullNode enables the Waku Filter V2 protocol full node functionality.
|
2023-02-07 22:28:46 +00:00
|
|
|
// This WakuNodeOption accepts a list of WakuFilter options to setup the protocol
|
2023-04-11 14:38:16 +00:00
|
|
|
func WithWakuFilterFullNode(filterOpts ...filter.Option) WakuNodeOption {
|
2023-02-07 22:28:46 +00:00
|
|
|
return func(params *WakuNodeParameters) error {
|
2023-04-17 00:04:12 +00:00
|
|
|
params.enableFilterFullNode = true
|
2023-04-11 14:38:16 +00:00
|
|
|
params.filterOpts = filterOpts
|
2023-02-07 22:28:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 20:42:44 +00:00
|
|
|
// WithWakuStore enables the Waku V2 Store protocol and if the messages should
|
2023-07-05 19:17:43 +00:00
|
|
|
// be stored or not in a message provider.
|
|
|
|
func WithWakuStore() WakuNodeOption {
|
2021-04-18 23:41:42 +00:00
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.enableStore = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 19:31:26 +00:00
|
|
|
// WithWakuStoreFactory is used to replace the default WakuStore with a custom
|
|
|
|
// implementation that implements the store.Store interface
|
2022-03-18 19:56:34 +00:00
|
|
|
func WithWakuStoreFactory(factory storeFactory) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.storeFactory = factory
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 20:42:44 +00:00
|
|
|
// WithMessageProvider is a WakuNodeOption that sets the MessageProvider
|
|
|
|
// used to store and retrieve persisted messages
|
2021-04-18 23:41:42 +00:00
|
|
|
func WithMessageProvider(s store.MessageProvider) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
2022-05-30 18:48:22 +00:00
|
|
|
if s == nil {
|
|
|
|
return errors.New("message provider can't be nil")
|
|
|
|
}
|
2021-11-01 12:38:03 +00:00
|
|
|
params.messageProvider = s
|
2021-04-18 23:41:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-28 20:23:03 +00:00
|
|
|
// WithLightPush is a WakuNodeOption that enables the lightpush protocol
|
|
|
|
func WithLightPush() WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.enableLightPush = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-05 20:09:48 +00:00
|
|
|
// WithKeepAlive is a WakuNodeOption used to set the interval of time when
|
|
|
|
// each peer will be ping to keep the TCP connection alive
|
2021-06-24 13:02:53 +00:00
|
|
|
func WithKeepAlive(t time.Duration) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.keepAliveInterval = t
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-05 20:09:48 +00:00
|
|
|
// WithConnectionStatusChannel is a WakuNodeOption used to set a channel where the
|
|
|
|
// connection status changes will be pushed to. It's useful to identify when peer
|
|
|
|
// connections and disconnections occur
|
|
|
|
func WithConnectionStatusChannel(connStatus chan ConnStatus) WakuNodeOption {
|
2021-06-16 10:14:22 +00:00
|
|
|
return func(params *WakuNodeParameters) error {
|
2021-11-05 20:09:48 +00:00
|
|
|
params.connStatusC = connStatus
|
2021-06-16 10:14:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-10 14:13:10 +00:00
|
|
|
func WithConnectionNotification(ch chan<- PeerConnection) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.connNotifCh = ch
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 19:31:26 +00:00
|
|
|
// WithWebsockets is a WakuNodeOption used to enable websockets support
|
2022-03-22 13:12:58 +00:00
|
|
|
func WithWebsockets(address string, port int) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.enableWS = true
|
2022-03-22 00:48:46 +00:00
|
|
|
params.wsPort = port
|
2022-03-22 13:12:58 +00:00
|
|
|
|
|
|
|
wsMa, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d/%s", address, port, "ws"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
params.multiAddr = append(params.multiAddr, wsMa)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:04:08 +00:00
|
|
|
// WithRendezvous is a WakuOption used to set the node as a rendezvous
|
2023-03-09 15:48:25 +00:00
|
|
|
// point, using an specific storage for the peer information
|
2023-07-27 17:04:08 +00:00
|
|
|
func WithRendezvous(db *rendezvous.DB) WakuNodeOption {
|
2023-03-09 15:48:25 +00:00
|
|
|
return func(params *WakuNodeParameters) error {
|
2023-07-31 18:58:50 +00:00
|
|
|
params.enableRendezvousPoint = true
|
2023-03-09 15:48:25 +00:00
|
|
|
params.rendezvousDB = db
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 19:31:26 +00:00
|
|
|
// WithSecureWebsockets is a WakuNodeOption used to enable secure websockets support
|
2022-03-22 13:12:58 +00:00
|
|
|
func WithSecureWebsockets(address string, port int, certPath string, keyPath string) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.enableWSS = true
|
2022-03-22 00:48:46 +00:00
|
|
|
params.wssPort = port
|
2022-03-22 13:12:58 +00:00
|
|
|
|
|
|
|
wsMa, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d/%s", address, port, "wss"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
params.multiAddr = append(params.multiAddr, wsMa)
|
|
|
|
|
|
|
|
certificate, err := tls.LoadX509KeyPair(certPath, keyPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
params.tlsConfig = &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{certificate},
|
2022-11-09 19:34:57 +00:00
|
|
|
MinVersion: tls.VersionTLS12,
|
2022-03-22 13:12:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 20:42:44 +00:00
|
|
|
// Default options used in the libp2p node
|
2021-04-18 23:41:42 +00:00
|
|
|
var DefaultLibP2POptions = []libp2p.Option{
|
2022-03-22 13:12:58 +00:00
|
|
|
libp2p.ChainOptions(
|
|
|
|
libp2p.Transport(tcp.NewTCPTransport),
|
|
|
|
libp2p.Transport(quic.NewTransport),
|
2023-04-14 16:32:03 +00:00
|
|
|
libp2p.Transport(libp2pwebtransport.New),
|
2022-10-26 13:28:28 +00:00
|
|
|
),
|
|
|
|
libp2p.UserAgent(userAgent),
|
2022-05-31 19:51:53 +00:00
|
|
|
libp2p.ChainOptions(
|
|
|
|
libp2p.Muxer("/yamux/1.0.0", yamux.DefaultTransport),
|
|
|
|
libp2p.Muxer("/mplex/6.7.0", mplex.DefaultTransport),
|
|
|
|
),
|
2022-03-22 13:12:58 +00:00
|
|
|
libp2p.EnableNATService(),
|
|
|
|
libp2p.ConnectionManager(newConnManager(200, 300, connmgr.WithGracePeriod(0))),
|
2023-05-12 21:52:42 +00:00
|
|
|
libp2p.EnableHolePunching(),
|
2022-03-22 13:12:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newConnManager(lo int, hi int, opts ...connmgr.Option) *connmgr.BasicConnMgr {
|
|
|
|
mgr, err := connmgr.NewConnManager(lo, hi, opts...)
|
|
|
|
if err != nil {
|
|
|
|
panic("could not create ConnManager: " + err.Error())
|
|
|
|
}
|
|
|
|
return mgr
|
2021-04-18 23:41:42 +00:00
|
|
|
}
|