go-waku/waku/v2/node/wakuoptions.go

497 lines
15 KiB
Go
Raw Normal View History

package node
import (
"crypto/ecdsa"
2022-03-22 13:12:58 +00:00
"crypto/tls"
"errors"
2021-10-15 02:15:02 +00:00
"fmt"
"net"
"time"
2022-07-28 14:04:33 +00:00
"github.com/ethereum/go-ethereum/common"
2022-09-11 21:08:58 +00:00
"github.com/ethereum/go-ethereum/core/types"
2021-11-17 16:19:42 +00:00
"github.com/ethereum/go-ethereum/p2p/enode"
logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/config"
2022-10-19 19:39:32 +00:00
"github.com/libp2p/go-libp2p/core/crypto"
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"
2021-10-15 02:15:02 +00:00
"github.com/multiformats/go-multiaddr"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
"github.com/waku-org/go-waku/waku/v2/protocol/filter"
"github.com/waku-org/go-waku/waku/v2/protocol/filterv2"
"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"
"github.com/waku-org/go-waku/waku/v2/utils"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
2022-10-26 12:04:18 +00:00
// Default userAgent
const userAgent string = "go-waku"
2021-06-28 14:14:28 +00:00
// Default minRelayPeersToPublish
2022-03-21 23:15:53 +00:00
const defaultMinRelayPeersToPublish = 0
type WakuNodeParameters struct {
2021-11-17 16:19:42 +00:00
hostAddr *net.TCPAddr
2022-03-22 00:48:46 +00:00
dns4Domain string
advertiseAddrs []multiaddr.Multiaddr
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
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
noDefaultWakuTopic bool
enableRelay bool
enableFilter bool
isFilterFullNode bool
enableFilterV2LightNode bool
enableFilterV2FullNode bool
filterOpts []filter.Option
filterV2Opts []filterv2.Option
wOpts []pubsub.Option
minRelayPeersToPublish int
2021-11-01 12:38:03 +00:00
enableStore bool
2022-06-19 21:47:39 +00:00
enableSwap bool
2022-11-25 20:54:11 +00:00
resumeNodes []multiaddr.Multiaddr
2021-11-01 12:38:03 +00:00
messageProvider store.MessageProvider
2023-03-09 15:48:25 +00:00
enableRendezvous bool
rendezvousDB *rendezvous.DB
swapMode int
swapDisconnectThreshold int
swapPaymentThreshold int
discoveryMinPeers int
2021-11-17 16:19:42 +00:00
enableDiscV5 bool
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
2022-08-12 12:44:13 +00:00
rlnRelayMemIndex uint
2022-07-28 14:04:33 +00:00
rlnRelayPubsubTopic string
rlnRelayContentTopic string
rlnRelayDynamic bool
2022-08-12 12:44:13 +00:00
rlnSpamHandler func(message *pb.WakuMessage) error
rlnRelayIDKey *[32]byte
rlnRelayIDCommitment *[32]byte
2022-07-28 14:04:33 +00:00
rlnETHPrivateKey *ecdsa.PrivateKey
rlnETHClientAddress string
rlnMembershipContractAddress common.Address
2022-09-11 21:08:58 +00:00
rlnRegistrationHandler func(tx *types.Transaction)
2022-07-05 21:28:34 +00:00
keepAliveInterval time.Duration
enableLightPush bool
2021-06-16 10:14:22 +00:00
connStatusC chan ConnStatus
storeFactory storeFactory
}
type WakuNodeOption func(*WakuNodeParameters) error
2021-11-17 16:19:42 +00:00
// Default options used in the libp2p node
var DefaultWakuNodeOptions = []WakuNodeOption{
WithDiscoverParams(150),
2021-11-17 16:19:42 +00:00
}
// MultiAddresses return the list of multiaddresses configured in the node
func (w WakuNodeParameters) MultiAddresses() []multiaddr.Multiaddr {
return w.multiAddr
}
// Identity returns a libp2p option containing the identity used by the node
func (w WakuNodeParameters) Identity() config.Option {
2021-11-17 16:19:42 +00:00
return libp2p.Identity(*w.GetPrivKey())
}
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
}
// 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
}
// 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
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)
return nil
}
}
2022-03-22 00:48:46 +00:00
// WithDns4Domain is a WakuNodeOption that adds a custom domain name to listen
func WithDns4Domain(dns4Domain string) WakuNodeOption {
return func(params *WakuNodeParameters) error {
params.dns4Domain = dns4Domain
params.addressFactory = func([]multiaddr.Multiaddr) []multiaddr.Multiaddr {
2022-03-22 00:48:46 +00:00
var result []multiaddr.Multiaddr
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()))
}
tcp, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/tcp/%d", params.hostAddr.Port))
2022-03-22 00:48:46 +00:00
result = append(result, hostAddrMA.Encapsulate(tcp))
if params.enableWS || params.enableWSS {
if params.enableWSS {
wss, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/tcp/%d/wss", params.wssPort))
2022-03-22 00:48:46 +00:00
result = append(result, hostAddrMA.Encapsulate(wss))
} else {
ws, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/tcp/%d/ws", params.wsPort))
2022-03-22 00:48:46 +00:00
result = append(result, hostAddrMA.Encapsulate(ws))
}
}
return result
}
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 {
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-11-17 16:19:42 +00:00
params.multiAddr = append(params.multiAddr, hostAddrMA)
return nil
}
}
// 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 {
params.advertiseAddrs = advertiseAddrs
2023-01-08 21:12:29 +00:00
params.addressFactory = func([]multiaddr.Multiaddr) (addresses []multiaddr.Multiaddr) {
return advertiseAddrs
2021-10-15 02:15:02 +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
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
func WithPrivateKey(privKey *ecdsa.PrivateKey) WakuNodeOption {
return func(params *WakuNodeParameters) error {
2021-11-17 16:19:42 +00:00
params.privKey = privKey
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
}
}
// 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
func WithLibP2POptions(opts ...libp2p.Option) WakuNodeOption {
return func(params *WakuNodeParameters) error {
params.libP2POpts = opts
return nil
}
}
// NoDefaultWakuTopic will stop the node from subscribing to the default
// pubsub topic automatically
func NoDefaultWakuTopic() WakuNodeOption {
return func(params *WakuNodeParameters) error {
params.noDefaultWakuTopic = true
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
func WithWakuRelay(opts ...pubsub.Option) WakuNodeOption {
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 {
return func(params *WakuNodeParameters) error {
params.enableRelay = true
params.wOpts = opts
params.minRelayPeersToPublish = minRelayPeersToPublish
return nil
}
}
func WithDiscoverParams(minPeers int) WakuNodeOption {
return func(params *WakuNodeParameters) error {
params.discoveryMinPeers = minPeers
return nil
}
}
2021-11-17 16:19:42 +00:00
// WithDiscoveryV5 is a WakuOption used to enable DiscV5 peer discovery
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
}
}
// WithWakuFilter enables the Waku Filter protocol. This WakuNodeOption
// accepts a list of WakuFilter gossipsub options to setup the protocol
func WithWakuFilter(fullNode bool, filterOpts ...filter.Option) WakuNodeOption {
2021-06-10 12:59:51 +00:00
return func(params *WakuNodeParameters) error {
params.enableFilter = true
params.isFilterFullNode = fullNode
params.filterOpts = filterOpts
2021-06-10 12:59:51 +00:00
return nil
}
}
// WithWakuFilterV2 enables the Waku Filter V2 protocol for lightnode functionality
func WithWakuFilterV2LightNode() WakuNodeOption {
return func(params *WakuNodeParameters) error {
params.enableFilterV2LightNode = true
return nil
}
}
// WithWakuFilterV2FullNode enables the Waku Filter V2 protocol full node functionality.
// This WakuNodeOption accepts a list of WakuFilter options to setup the protocol
func WithWakuFilterV2FullNode(filterOpts ...filterv2.Option) WakuNodeOption {
return func(params *WakuNodeParameters) error {
params.enableFilterV2FullNode = true
params.filterV2Opts = filterOpts
return nil
}
}
2021-04-22 20:42:44 +00:00
// WithWakuStore enables the Waku V2 Store protocol and if the messages should
// be stored or not in a message provider. If resumeNodes are specified, the
// store will attempt to resume message history using those nodes
func WithWakuStore(resumeNodes ...multiaddr.Multiaddr) WakuNodeOption {
return func(params *WakuNodeParameters) error {
params.enableStore = true
2022-11-25 20:54:11 +00:00
params.resumeNodes = resumeNodes
return nil
}
}
// WithWakuStoreFactory is used to replace the default WakuStore with a custom
// implementation that implements the store.Store interface
func WithWakuStoreFactory(factory storeFactory) WakuNodeOption {
return func(params *WakuNodeParameters) error {
params.storeFactory = factory
return nil
}
}
// WithWakuSwap set the option of the Waku V2 Swap protocol
func WithWakuSwap(mode int, disconnectThreshold, paymentThreshold int) WakuNodeOption {
return func(params *WakuNodeParameters) error {
2022-06-19 21:47:39 +00:00
params.enableSwap = true
params.swapMode = mode
params.swapDisconnectThreshold = disconnectThreshold
params.swapPaymentThreshold = paymentThreshold
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
func WithMessageProvider(s store.MessageProvider) WakuNodeOption {
return func(params *WakuNodeParameters) error {
if s == nil {
return errors.New("message provider can't be nil")
}
2021-11-01 12:38:03 +00:00
params.messageProvider = s
return nil
}
}
// WithLightPush is a WakuNodeOption that enables the lightpush protocol
func WithLightPush() WakuNodeOption {
return func(params *WakuNodeParameters) error {
params.enableLightPush = true
return nil
}
}
// WithKeepAlive is a WakuNodeOption used to set the interval of time when
// each peer will be ping to keep the TCP connection alive
func WithKeepAlive(t time.Duration) WakuNodeOption {
return func(params *WakuNodeParameters) error {
params.keepAliveInterval = t
return nil
}
}
// 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 {
params.connStatusC = connStatus
2021-06-16 10:14:22 +00:00
return nil
}
}
// 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-03-09 15:48:25 +00:00
// WithRendezvousServer is a WakuOption used to set the node as a rendezvous
// point, using an specific storage for the peer information
func WithRendezvousServer(db *rendezvous.DB) WakuNodeOption {
return func(params *WakuNodeParameters) error {
params.enableRendezvous = true
params.rendezvousDB = db
return nil
}
}
// 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
var DefaultLibP2POptions = []libp2p.Option{
2022-03-22 13:12:58 +00:00
libp2p.ChainOptions(
libp2p.Transport(tcp.NewTCPTransport),
libp2p.Transport(quic.NewTransport),
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))),
}
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
}