2023-07-06 17:40:57 -04:00
|
|
|
package main
|
2021-03-18 19:21:45 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-10-04 18:38:27 -04:00
|
|
|
"crypto/ecdsa"
|
2021-04-18 20:03:16 -04:00
|
|
|
"database/sql"
|
2022-07-25 08:24:42 -04:00
|
|
|
"encoding/json"
|
2021-03-18 19:21:45 -04:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2022-12-07 23:04:56 +05:30
|
|
|
"sync"
|
2021-03-18 19:21:45 -04:00
|
|
|
"syscall"
|
2021-06-24 09:02:53 -04:00
|
|
|
"time"
|
2021-03-18 19:21:45 -04:00
|
|
|
|
2023-05-24 11:34:35 -04:00
|
|
|
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
|
|
|
|
"github.com/pbnjay/memory"
|
2023-08-15 21:40:00 -04:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2023-10-18 15:21:37 -04:00
|
|
|
"github.com/urfave/cli/v2"
|
2023-05-24 11:34:35 -04:00
|
|
|
|
2023-07-06 17:40:57 -04:00
|
|
|
dbutils "github.com/waku-org/go-waku/waku/persistence/utils"
|
2023-11-13 19:17:43 +07:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/dnsdisc"
|
2023-08-03 21:51:15 +05:30
|
|
|
wakupeerstore "github.com/waku-org/go-waku/waku/v2/peerstore"
|
2023-03-09 11:48:25 -04:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/rendezvous"
|
2022-11-25 17:24:34 -04:00
|
|
|
|
2022-07-25 08:24:42 -04:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
2021-03-23 10:46:16 -04:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2021-04-13 14:54:06 -04:00
|
|
|
dssql "github.com/ipfs/go-ds-sql"
|
2022-01-18 14:17:06 -04:00
|
|
|
"go.uber.org/zap"
|
2023-02-01 19:35:31 -04:00
|
|
|
"go.uber.org/zap/zapcore"
|
2021-10-03 17:45:07 -04:00
|
|
|
|
2021-04-13 14:54:06 -04:00
|
|
|
"github.com/libp2p/go-libp2p"
|
2021-10-14 22:15:02 -04:00
|
|
|
"github.com/libp2p/go-libp2p/config"
|
2022-10-19 15:39:32 -04:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2023-02-16 12:17:52 -04:00
|
|
|
"github.com/libp2p/go-libp2p/core/protocol"
|
2022-08-25 16:36:04 -04:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
|
2021-10-04 18:38:27 -04:00
|
|
|
|
2021-10-08 09:50:56 -04:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
2024-01-12 23:10:27 +05:30
|
|
|
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoreds" // nolint: staticcheck
|
2022-08-25 16:36:04 -04:00
|
|
|
ws "github.com/libp2p/go-libp2p/p2p/transport/websocket"
|
2021-04-22 09:07:22 -04:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2023-09-07 14:00:59 -04:00
|
|
|
"github.com/waku-org/go-waku/cmd/waku/server/rest"
|
2022-11-09 15:53:01 -04:00
|
|
|
"github.com/waku-org/go-waku/logging"
|
|
|
|
"github.com/waku-org/go-waku/waku/metrics"
|
|
|
|
"github.com/waku-org/go-waku/waku/persistence"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/node"
|
2023-09-06 10:07:21 +05:30
|
|
|
wprotocol "github.com/waku-org/go-waku/waku/v2/protocol"
|
2022-11-09 15:53:01 -04:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/filter"
|
2024-05-03 12:07:03 -04:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/legacy_store"
|
2022-11-09 15:53:01 -04:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/lightpush"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/peer_exchange"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/relay"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/utils"
|
2024-01-03 07:06:41 +05:30
|
|
|
|
|
|
|
humanize "github.com/dustin/go-humanize"
|
2021-03-18 19:21:45 -04:00
|
|
|
)
|
|
|
|
|
2023-08-09 14:14:54 -04:00
|
|
|
func requiresDB(options NodeOptions) bool {
|
2023-07-27 13:04:08 -04:00
|
|
|
return options.Store.Enable || options.Rendezvous.Enable
|
2023-03-09 11:48:25 -04:00
|
|
|
}
|
|
|
|
|
2023-05-24 11:34:35 -04:00
|
|
|
func scalePerc(value float64) float64 {
|
|
|
|
if value > 100 {
|
|
|
|
return 100
|
|
|
|
}
|
|
|
|
|
|
|
|
if value < 0.1 {
|
|
|
|
return 0.1
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2022-08-25 20:18:03 -04:00
|
|
|
const dialTimeout = 7 * time.Second
|
|
|
|
|
2023-10-18 15:21:37 -04:00
|
|
|
func nonRecoverErrorMsg(format string, a ...any) error {
|
|
|
|
err := fmt.Errorf(format, a...)
|
|
|
|
return nonRecoverError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func nonRecoverError(err error) error {
|
|
|
|
return cli.Exit(err.Error(), 166)
|
|
|
|
}
|
|
|
|
|
2021-10-09 14:18:53 -04:00
|
|
|
// Execute starts a go-waku node with settings determined by the Options parameter
|
2023-10-18 15:21:37 -04:00
|
|
|
func Execute(options NodeOptions) error {
|
2023-08-18 23:18:42 -04:00
|
|
|
// Set encoding for logs (console, json, ...)
|
|
|
|
// Note that libp2p reads the encoding from GOLOG_LOG_FMT env var.
|
2024-06-05 11:56:26 -04:00
|
|
|
utils.InitLogger(options.LogEncoding, options.LogOutput, "gowaku")
|
2023-08-18 23:18:42 -04:00
|
|
|
|
2021-10-14 22:15:02 -04:00
|
|
|
hostAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", options.Address, options.Port))
|
2023-10-18 15:21:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return nonRecoverErrorMsg("invalid host address: %w", err)
|
|
|
|
}
|
2021-03-18 19:21:45 -04:00
|
|
|
|
2021-10-04 18:38:27 -04:00
|
|
|
prvKey, err := getPrivKey(options)
|
2023-10-18 15:21:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-13 14:54:06 -04:00
|
|
|
|
2022-05-27 15:55:35 -04:00
|
|
|
p2pPrvKey := utils.EcdsaPrivKeyToSecp256k1PrivKey(prvKey)
|
|
|
|
id, err := peer.IDFromPublicKey(p2pPrvKey.GetPublic())
|
2023-10-18 15:21:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-27 09:25:06 -04:00
|
|
|
logger := utils.Logger().With(logging.HostID("node", id))
|
|
|
|
|
2021-10-03 17:45:07 -04:00
|
|
|
var db *sql.DB
|
2024-01-03 12:49:54 -04:00
|
|
|
var migrationFn func(*sql.DB, *zap.Logger) error
|
2023-08-08 11:46:32 -04:00
|
|
|
if requiresDB(options) && options.Store.Migration {
|
2023-11-09 16:10:40 -04:00
|
|
|
dbSettings := dbutils.DBSettings{}
|
2024-01-02 12:35:58 -04:00
|
|
|
db, migrationFn, err = dbutils.ParseURL(options.Store.DatabaseURL, dbSettings, logger)
|
2023-10-18 15:21:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return nonRecoverErrorMsg("could not connect to DB: %w", err)
|
|
|
|
}
|
2021-10-03 17:45:07 -04:00
|
|
|
}
|
2021-04-22 09:07:22 -04:00
|
|
|
|
2021-10-03 17:45:07 -04:00
|
|
|
ctx := context.Background()
|
2021-04-18 20:03:16 -04:00
|
|
|
|
2021-10-03 17:45:07 -04:00
|
|
|
var metricsServer *metrics.Server
|
|
|
|
if options.Metrics.Enable {
|
2022-05-30 11:55:30 -04:00
|
|
|
metricsServer = metrics.NewMetricsServer(options.Metrics.Address, options.Metrics.Port, logger)
|
2021-10-03 17:45:07 -04:00
|
|
|
go metricsServer.Start()
|
|
|
|
}
|
2021-04-18 20:03:16 -04:00
|
|
|
|
2023-02-01 19:35:31 -04:00
|
|
|
lvl, err := zapcore.ParseLevel(options.LogLevel)
|
|
|
|
if err != nil {
|
2023-10-18 15:21:37 -04:00
|
|
|
return err
|
2023-02-01 19:35:31 -04:00
|
|
|
}
|
|
|
|
|
2021-10-03 17:45:07 -04:00
|
|
|
nodeOpts := []node.WakuNodeOption{
|
2022-05-27 09:25:06 -04:00
|
|
|
node.WithLogger(logger),
|
2023-02-01 19:35:31 -04:00
|
|
|
node.WithLogLevel(lvl),
|
2021-10-03 17:45:07 -04:00
|
|
|
node.WithPrivateKey(prvKey),
|
2021-11-17 12:19:42 -04:00
|
|
|
node.WithHostAddress(hostAddr),
|
2024-07-11 12:02:52 -04:00
|
|
|
node.WithKeepAlive(10*time.Second, options.KeepAlive),
|
2023-08-03 21:51:15 +05:30
|
|
|
node.WithMaxPeerConnections(options.MaxPeerConnections),
|
2023-08-15 21:40:00 -04:00
|
|
|
node.WithPrometheusRegisterer(prometheus.DefaultRegisterer),
|
2023-09-27 12:16:37 +05:30
|
|
|
node.WithPeerStoreCapacity(options.PeerStoreCapacity),
|
2024-01-08 15:05:21 -04:00
|
|
|
node.WithMaxConnectionsPerIP(options.IPColocationLimit),
|
2023-10-15 15:16:40 -04:00
|
|
|
node.WithClusterID(uint16(options.ClusterID)),
|
2021-10-03 17:45:07 -04:00
|
|
|
}
|
2023-02-08 12:02:06 -04:00
|
|
|
if len(options.AdvertiseAddresses) != 0 {
|
|
|
|
nodeOpts = append(nodeOpts, node.WithAdvertiseAddresses(options.AdvertiseAddresses...))
|
2022-03-21 20:48:46 -04:00
|
|
|
}
|
|
|
|
|
2023-05-08 11:19:44 -04:00
|
|
|
if options.ExtIP != "" {
|
|
|
|
ip := net.ParseIP(options.ExtIP)
|
|
|
|
if ip == nil {
|
2023-10-18 15:21:37 -04:00
|
|
|
return nonRecoverErrorMsg("could not set external IP address: invalid IP")
|
2023-05-08 11:19:44 -04:00
|
|
|
}
|
2023-10-18 15:21:37 -04:00
|
|
|
|
2023-05-08 11:19:44 -04:00
|
|
|
nodeOpts = append(nodeOpts, node.WithExternalIP(ip))
|
|
|
|
}
|
|
|
|
|
2023-07-06 17:40:57 -04:00
|
|
|
if options.DNS4DomainName != "" {
|
2023-09-11 10:24:05 -04:00
|
|
|
nodeOpts = append(nodeOpts, node.WithDNS4Domain(options.DNS4DomainName))
|
2023-05-15 11:49:51 -04:00
|
|
|
}
|
|
|
|
|
2022-03-22 09:12:58 -04:00
|
|
|
libp2pOpts := node.DefaultLibP2POptions
|
2023-05-24 11:34:35 -04:00
|
|
|
|
2023-08-15 21:40:00 -04:00
|
|
|
libp2pOpts = append(libp2pOpts, libp2p.PrometheusRegisterer(prometheus.DefaultRegisterer))
|
|
|
|
|
2023-05-24 11:34:35 -04:00
|
|
|
memPerc := scalePerc(options.ResourceScalingMemoryPercent)
|
|
|
|
fdPerc := scalePerc(options.ResourceScalingFDPercent)
|
|
|
|
limits := rcmgr.DefaultLimits // Default memory limit: 1/8th of total memory, minimum 128MB, maximum 1GB
|
|
|
|
scaledLimits := limits.Scale(int64(float64(memory.TotalMemory())*memPerc/100), int(float64(getNumFDs())*fdPerc/100))
|
|
|
|
resourceManager, err := rcmgr.NewResourceManager(rcmgr.NewFixedLimiter(scaledLimits))
|
2023-10-18 15:21:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not set resource limits: %w", err)
|
|
|
|
}
|
2023-05-24 11:34:35 -04:00
|
|
|
|
|
|
|
libp2pOpts = append(libp2pOpts, libp2p.ResourceManager(resourceManager))
|
|
|
|
libp2p.SetDefaultServiceLimits(&limits)
|
|
|
|
|
2023-02-08 12:02:06 -04:00
|
|
|
if len(options.AdvertiseAddresses) == 0 {
|
2022-03-22 09:12:58 -04:00
|
|
|
libp2pOpts = append(libp2pOpts, libp2p.NATPortMap()) // Attempt to open ports using uPNP for NATed hosts.)
|
|
|
|
}
|
|
|
|
|
2023-05-12 17:52:42 -04:00
|
|
|
// Node can be a circuit relay server
|
|
|
|
if options.CircuitRelay {
|
|
|
|
libp2pOpts = append(libp2pOpts, libp2p.EnableRelayService())
|
|
|
|
}
|
|
|
|
|
2023-09-28 16:08:40 -04:00
|
|
|
if options.ForceReachability != "" {
|
|
|
|
libp2pOpts = append(libp2pOpts, libp2p.EnableRelay())
|
2023-09-20 02:54:16 -04:00
|
|
|
nodeOpts = append(nodeOpts, node.WithCircuitRelayParams(2*time.Second, 2*time.Second))
|
2023-09-28 16:08:40 -04:00
|
|
|
if options.ForceReachability == "private" {
|
|
|
|
logger.Warn("node forced to be unreachable!")
|
|
|
|
libp2pOpts = append(libp2pOpts, libp2p.ForceReachabilityPrivate())
|
|
|
|
} else if options.ForceReachability == "public" {
|
|
|
|
logger.Warn("node forced to be publicly reachable!")
|
|
|
|
libp2pOpts = append(libp2pOpts, libp2p.ForceReachabilityPublic())
|
|
|
|
} else {
|
2023-10-18 15:21:37 -04:00
|
|
|
return nonRecoverErrorMsg("invalid reachability value")
|
2023-09-28 16:08:40 -04:00
|
|
|
}
|
2023-09-20 02:54:16 -04:00
|
|
|
}
|
|
|
|
|
2022-10-26 09:28:28 -04:00
|
|
|
if options.UserAgent != "" {
|
|
|
|
libp2pOpts = append(libp2pOpts, libp2p.UserAgent(options.UserAgent))
|
|
|
|
}
|
|
|
|
|
2022-03-22 09:12:58 -04:00
|
|
|
if options.Websocket.Enable {
|
2022-08-25 16:36:04 -04:00
|
|
|
nodeOpts = append(nodeOpts, node.WithWebsockets(options.Websocket.Address, options.Websocket.WSPort))
|
2022-03-22 09:12:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if options.Websocket.Secure {
|
2022-08-25 16:36:04 -04:00
|
|
|
nodeOpts = append(nodeOpts, node.WithSecureWebsockets(options.Websocket.Address, options.Websocket.WSSPort, options.Websocket.CertPath, options.Websocket.KeyPath))
|
2021-10-03 17:45:07 -04:00
|
|
|
}
|
2021-04-18 20:03:16 -04:00
|
|
|
|
2021-10-14 14:17:01 -04:00
|
|
|
if options.ShowAddresses {
|
2021-10-14 22:15:02 -04:00
|
|
|
printListeningAddresses(ctx, nodeOpts, options)
|
2023-10-18 15:21:37 -04:00
|
|
|
return nil
|
2021-10-14 14:17:01 -04:00
|
|
|
}
|
|
|
|
|
2022-11-25 16:54:11 -04:00
|
|
|
if options.Store.Enable && options.PersistPeers {
|
|
|
|
// Create persistent peerstore
|
2023-10-05 07:20:02 +07:00
|
|
|
queries, err := dbutils.NewQueries("peerstore", db)
|
2023-10-18 15:21:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return nonRecoverErrorMsg("could not setup persistent peerstore database: %w", err)
|
|
|
|
|
|
|
|
}
|
2021-10-01 13:43:03 -04:00
|
|
|
|
2022-11-25 16:54:11 -04:00
|
|
|
datastore := dssql.NewDatastore(db, queries)
|
|
|
|
opts := pstoreds.DefaultOpts()
|
|
|
|
peerStore, err := pstoreds.NewPeerstore(ctx, datastore, opts)
|
2023-10-18 15:21:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return nonRecoverErrorMsg("could not create persistent peerstore: %w", err)
|
|
|
|
}
|
2021-10-01 13:43:03 -04:00
|
|
|
|
2023-06-05 10:39:38 -04:00
|
|
|
nodeOpts = append(nodeOpts, node.WithPeerStore(peerStore))
|
2021-10-03 17:45:07 -04:00
|
|
|
}
|
2021-03-18 19:21:45 -04:00
|
|
|
|
2021-10-03 17:45:07 -04:00
|
|
|
nodeOpts = append(nodeOpts, node.WithLibP2POptions(libp2pOpts...))
|
2022-12-08 23:08:04 -04:00
|
|
|
nodeOpts = append(nodeOpts, node.WithNTP())
|
2021-10-01 13:49:50 -04:00
|
|
|
|
2024-01-03 07:06:41 +05:30
|
|
|
maxMsgSize := parseMsgSizeConfig(options.Relay.MaxMsgSize)
|
|
|
|
|
2022-02-21 11:23:52 -04:00
|
|
|
if options.Relay.Enable {
|
2021-10-03 17:45:07 -04:00
|
|
|
var wakurelayopts []pubsub.Option
|
|
|
|
wakurelayopts = append(wakurelayopts, pubsub.WithPeerExchange(options.Relay.PeerExchange))
|
2024-01-03 07:06:41 +05:30
|
|
|
wakurelayopts = append(wakurelayopts, pubsub.WithMaxMessageSize(maxMsgSize))
|
|
|
|
|
2021-12-06 09:43:00 +01:00
|
|
|
nodeOpts = append(nodeOpts, node.WithWakuRelayAndMinPeers(options.Relay.MinRelayPeersToPublish, wakurelayopts...))
|
2024-01-03 07:06:41 +05:30
|
|
|
nodeOpts = append(nodeOpts, node.WithMaxMsgSize(maxMsgSize))
|
2021-10-03 17:45:07 -04:00
|
|
|
}
|
2021-06-10 15:59:51 +03:00
|
|
|
|
2023-04-13 15:28:46 -04:00
|
|
|
nodeOpts = append(nodeOpts, node.WithWakuFilterLightNode())
|
|
|
|
|
2021-10-03 17:45:07 -04:00
|
|
|
if options.Filter.Enable {
|
2023-04-13 15:28:46 -04:00
|
|
|
nodeOpts = append(nodeOpts, node.WithWakuFilterFullNode(filter.WithTimeout(options.Filter.Timeout)))
|
|
|
|
}
|
|
|
|
|
2023-03-09 11:48:25 -04:00
|
|
|
var dbStore *persistence.DBStore
|
|
|
|
if requiresDB(options) {
|
2023-08-08 11:46:32 -04:00
|
|
|
dbOptions := []persistence.DBOption{
|
2023-01-04 13:58:14 -04:00
|
|
|
persistence.WithDB(db),
|
|
|
|
persistence.WithRetentionPolicy(options.Store.RetentionMaxMessages, options.Store.RetentionTime),
|
2023-08-08 11:46:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if options.Store.Migration {
|
|
|
|
dbOptions = append(dbOptions, persistence.WithMigrations(migrationFn)) // TODO: refactor migrations out of DBStore, or merge DBStore with rendezvous DB
|
|
|
|
}
|
|
|
|
|
2023-08-15 21:40:00 -04:00
|
|
|
dbStore, err = persistence.NewDBStore(prometheus.DefaultRegisterer, logger, dbOptions...)
|
2023-10-18 15:21:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return nonRecoverErrorMsg("error setting up db store: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-09-12 15:58:15 -04:00
|
|
|
nodeOpts = append(nodeOpts, node.WithMessageProvider(dbStore))
|
2021-10-03 17:45:07 -04:00
|
|
|
}
|
2021-09-30 12:01:53 -04:00
|
|
|
|
2023-03-09 11:48:25 -04:00
|
|
|
if options.Store.Enable {
|
2023-07-15 12:09:00 -04:00
|
|
|
nodeOpts = append(nodeOpts, node.WithWakuStore())
|
2023-03-09 11:48:25 -04:00
|
|
|
nodeOpts = append(nodeOpts, node.WithMessageProvider(dbStore))
|
|
|
|
}
|
|
|
|
|
2021-10-03 17:45:07 -04:00
|
|
|
if options.LightPush.Enable {
|
|
|
|
nodeOpts = append(nodeOpts, node.WithLightPush())
|
|
|
|
}
|
2021-04-12 13:59:41 -04:00
|
|
|
|
2022-10-23 09:13:43 -04:00
|
|
|
if options.PeerExchange.Enable {
|
|
|
|
nodeOpts = append(nodeOpts, node.WithPeerExchange())
|
|
|
|
}
|
|
|
|
|
2023-03-09 11:48:25 -04:00
|
|
|
if options.Rendezvous.Enable {
|
2023-09-11 10:24:05 -04:00
|
|
|
rdb := rendezvous.NewDB(db, logger)
|
2023-07-27 13:04:08 -04:00
|
|
|
nodeOpts = append(nodeOpts, node.WithRendezvous(rdb))
|
2023-03-09 11:48:25 -04:00
|
|
|
}
|
|
|
|
|
2023-10-18 15:21:37 -04:00
|
|
|
utils.Logger().Info("Version details ", zap.String("version", node.Version), zap.String("commit", node.GitCommit))
|
2022-07-05 17:28:34 -04:00
|
|
|
|
2023-10-18 15:21:37 -04:00
|
|
|
if err = checkForRLN(logger, options, &nodeOpts); err != nil {
|
|
|
|
return nonRecoverError(err)
|
|
|
|
}
|
2021-04-15 17:23:07 -04:00
|
|
|
|
2023-11-14 16:47:49 +05:30
|
|
|
var discoveredNodes []dnsdisc.DiscoveredNode
|
|
|
|
if options.DNSDiscovery.Enable {
|
|
|
|
if len(options.DNSDiscovery.URLs.Value()) == 0 {
|
|
|
|
return nonRecoverErrorMsg("DNS discovery URL is required")
|
|
|
|
}
|
|
|
|
discoveredNodes = node.GetNodesFromDNSDiscovery(logger, ctx, options.DNSDiscovery.Nameserver, options.DNSDiscovery.URLs.Value())
|
|
|
|
}
|
|
|
|
if options.DiscV5.Enable {
|
|
|
|
discv5Opts, err := node.GetDiscv5Option(discoveredNodes, options.DiscV5.Nodes.Value(), options.DiscV5.Port, options.DiscV5.AutoUpdate)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal("parsing ENR", zap.Error(err))
|
|
|
|
}
|
|
|
|
nodeOpts = append(nodeOpts, discv5Opts)
|
|
|
|
}
|
|
|
|
|
2023-10-18 15:21:37 -04:00
|
|
|
wakuNode, err := node.New(nodeOpts...)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not instantiate waku: %w", err)
|
|
|
|
}
|
2023-02-01 19:35:31 -04:00
|
|
|
|
2023-09-14 20:30:06 +05:30
|
|
|
//Process pubSub and contentTopics specified and arrive at all corresponding pubSubTopics
|
2023-10-18 15:21:37 -04:00
|
|
|
pubSubTopicMap, err := processTopics(options)
|
|
|
|
if err != nil {
|
|
|
|
return nonRecoverError(err)
|
|
|
|
}
|
|
|
|
|
2023-09-14 20:30:06 +05:30
|
|
|
pubSubTopicMapKeys := make([]string, 0, len(pubSubTopicMap))
|
|
|
|
for k := range pubSubTopicMap {
|
|
|
|
pubSubTopicMapKeys = append(pubSubTopicMapKeys, k)
|
|
|
|
}
|
2021-09-30 12:01:53 -04:00
|
|
|
|
2023-01-06 18:37:57 -04:00
|
|
|
if err = wakuNode.Start(ctx); err != nil {
|
2023-10-18 15:21:37 -04:00
|
|
|
return nonRecoverError(err)
|
2021-10-06 11:42:57 -04:00
|
|
|
}
|
2021-10-04 22:13:54 -04:00
|
|
|
|
2023-06-05 10:39:38 -04:00
|
|
|
for _, d := range discoveredNodes {
|
2024-05-28 18:20:47 +05:30
|
|
|
wakuNode.AddDiscoveredPeer(d.PeerID, d.PeerInfo.Addrs, wakupeerstore.DNSDiscovery, nil, d.ENR, true)
|
2023-06-05 10:39:38 -04:00
|
|
|
}
|
|
|
|
|
2023-09-14 20:30:06 +05:30
|
|
|
//For now assuming that static peers added support/listen on all topics specified via commandLine.
|
2023-10-18 15:21:37 -04:00
|
|
|
staticPeers := map[protocol.ID][]multiaddr.Multiaddr{
|
2024-05-03 12:07:03 -04:00
|
|
|
legacy_store.StoreID_v20beta4: options.Store.Nodes,
|
2023-10-18 15:21:37 -04:00
|
|
|
lightpush.LightPushID_v20beta1: options.LightPush.Nodes,
|
|
|
|
rendezvous.RendezvousID: options.Rendezvous.Nodes,
|
|
|
|
filter.FilterSubscribeID_v20beta1: options.Filter.Nodes,
|
|
|
|
}
|
|
|
|
for protocolID, peers := range staticPeers {
|
|
|
|
if err = addStaticPeers(wakuNode, peers, pubSubTopicMapKeys, protocolID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-06-01 08:26:03 -04:00
|
|
|
|
2023-07-27 13:04:08 -04:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2022-02-21 11:23:52 -04:00
|
|
|
if options.Relay.Enable {
|
2023-11-03 09:47:15 -04:00
|
|
|
if err = handleRelayTopics(ctx, &wg, wakuNode, pubSubTopicMap); err != nil {
|
|
|
|
return err
|
2023-05-02 11:10:45 -04:00
|
|
|
}
|
2021-10-03 17:45:07 -04:00
|
|
|
}
|
2021-03-18 19:21:45 -04:00
|
|
|
|
2022-08-15 13:13:45 -04:00
|
|
|
for _, n := range options.StaticNodes {
|
2022-08-25 20:18:03 -04:00
|
|
|
go func(ctx context.Context, node multiaddr.Multiaddr) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, dialTimeout)
|
|
|
|
defer cancel()
|
2022-08-15 13:13:45 -04:00
|
|
|
err = wakuNode.DialPeerWithMultiAddress(ctx, node)
|
2021-10-03 17:45:07 -04:00
|
|
|
if err != nil {
|
2022-05-27 09:25:06 -04:00
|
|
|
logger.Error("dialing peer", zap.Error(err))
|
2021-03-18 19:21:45 -04:00
|
|
|
}
|
2022-08-25 20:18:03 -04:00
|
|
|
}(ctx, n)
|
2021-10-03 17:45:07 -04:00
|
|
|
}
|
2021-03-18 19:21:45 -04:00
|
|
|
|
2023-07-05 15:17:43 -04:00
|
|
|
if options.DiscV5.Enable {
|
|
|
|
if err = wakuNode.DiscV5().Start(ctx); err != nil {
|
|
|
|
logger.Fatal("starting discovery v5", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve and connect to peer exchange peers
|
|
|
|
if options.PeerExchange.Enable && options.PeerExchange.Node != nil {
|
|
|
|
logger.Info("retrieving peer info via peer exchange protocol")
|
|
|
|
|
2023-09-14 20:30:06 +05:30
|
|
|
peerID, err := wakuNode.AddPeer(*options.PeerExchange.Node, wakupeerstore.Static,
|
|
|
|
pubSubTopicMapKeys, peer_exchange.PeerExchangeID_v20alpha1)
|
2023-07-05 15:17:43 -04:00
|
|
|
if err != nil {
|
|
|
|
logger.Error("adding peer exchange peer", logging.MultiAddrs("node", *options.PeerExchange.Node), zap.Error(err))
|
|
|
|
} else {
|
|
|
|
desiredOutDegree := wakuNode.Relay().Params().D
|
2023-07-06 17:40:57 -04:00
|
|
|
if err = wakuNode.PeerExchange().Request(ctx, desiredOutDegree, peer_exchange.WithPeer(peerID)); err != nil {
|
2023-07-05 15:17:43 -04:00
|
|
|
logger.Error("requesting peers via peer exchange", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-24 16:51:42 -04:00
|
|
|
var restServer *rest.WakuRest
|
|
|
|
if options.RESTServer.Enable {
|
2022-12-07 23:04:56 +05:30
|
|
|
wg.Add(1)
|
2023-11-07 20:26:48 +05:30
|
|
|
restConfig := rest.RestConfig{Address: options.RESTServer.Address,
|
|
|
|
Port: uint(options.RESTServer.Port),
|
|
|
|
EnablePProf: options.PProf,
|
|
|
|
EnableAdmin: options.RESTServer.Admin,
|
|
|
|
RelayCacheCapacity: uint(options.RESTServer.RelayCacheCapacity),
|
|
|
|
FilterCacheCapacity: uint(options.RESTServer.FilterCacheCapacity)}
|
|
|
|
|
|
|
|
restServer = rest.NewWakuRest(wakuNode, restConfig, logger)
|
2022-12-10 12:21:22 -04:00
|
|
|
restServer.Start(ctx, &wg)
|
2022-07-24 16:51:42 -04:00
|
|
|
}
|
|
|
|
|
2022-12-07 23:04:56 +05:30
|
|
|
wg.Wait()
|
2022-05-30 14:48:22 -04:00
|
|
|
logger.Info("Node setup complete")
|
2022-05-26 08:23:10 -04:00
|
|
|
|
2021-10-03 17:45:07 -04:00
|
|
|
// Wait for a SIGINT or SIGTERM signal
|
|
|
|
ch := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
<-ch
|
2022-05-27 09:25:06 -04:00
|
|
|
logger.Info("Received signal, shutting down...")
|
2021-04-18 20:03:16 -04:00
|
|
|
|
2021-10-03 17:45:07 -04:00
|
|
|
// shut the node down
|
|
|
|
wakuNode.Stop()
|
2021-06-28 09:20:23 -04:00
|
|
|
|
2022-07-24 16:51:42 -04:00
|
|
|
if options.RESTServer.Enable {
|
2023-10-18 15:21:37 -04:00
|
|
|
if err := restServer.Stop(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-24 16:51:42 -04:00
|
|
|
}
|
|
|
|
|
2021-10-03 17:45:07 -04:00
|
|
|
if options.Metrics.Enable {
|
2023-10-18 15:21:37 -04:00
|
|
|
if err = metricsServer.Stop(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-03 17:45:07 -04:00
|
|
|
}
|
2021-03-18 19:21:45 -04:00
|
|
|
|
2023-10-18 15:21:37 -04:00
|
|
|
if db != nil {
|
|
|
|
if err = db.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-03 17:45:07 -04:00
|
|
|
}
|
2023-10-18 15:21:37 -04:00
|
|
|
|
|
|
|
return nil
|
2021-03-18 19:21:45 -04:00
|
|
|
}
|
|
|
|
|
2023-10-21 01:26:18 +05:30
|
|
|
func processTopics(options NodeOptions) (map[string][]string, error) {
|
|
|
|
|
2023-09-06 10:07:21 +05:30
|
|
|
//Using a map to avoid duplicate pub-sub topics that can result from autosharding
|
|
|
|
// or same-topic being passed twice.
|
2023-10-21 01:26:18 +05:30
|
|
|
pubSubTopicMap := make(map[string][]string)
|
2023-09-06 10:07:21 +05:30
|
|
|
|
|
|
|
for _, topic := range options.Relay.Topics.Value() {
|
2023-10-21 01:26:18 +05:30
|
|
|
pubSubTopicMap[topic] = []string{}
|
2023-09-06 10:07:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
for _, topic := range options.Relay.PubSubTopics.Value() {
|
2023-10-21 01:26:18 +05:30
|
|
|
pubSubTopicMap[topic] = []string{}
|
2023-09-06 10:07:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
//Get pubSub topics from contentTopics if they are as per autosharding
|
|
|
|
for _, cTopic := range options.Relay.ContentTopics.Value() {
|
|
|
|
contentTopic, err := wprotocol.StringToContentTopic(cTopic)
|
|
|
|
if err != nil {
|
2023-10-18 15:21:37 -04:00
|
|
|
return nil, err
|
2023-09-06 10:07:21 +05:30
|
|
|
}
|
|
|
|
pTopic := wprotocol.GetShardFromContentTopic(contentTopic, wprotocol.GenerationZeroShardsCount)
|
2023-10-21 01:26:18 +05:30
|
|
|
if _, ok := pubSubTopicMap[pTopic.String()]; !ok {
|
|
|
|
pubSubTopicMap[pTopic.String()] = []string{}
|
|
|
|
}
|
|
|
|
pubSubTopicMap[pTopic.String()] = append(pubSubTopicMap[pTopic.String()], cTopic)
|
2023-09-06 10:07:21 +05:30
|
|
|
}
|
|
|
|
//If no topics are passed, then use default waku topic.
|
2024-01-02 18:04:43 +05:30
|
|
|
if len(pubSubTopicMap) == 0 && options.ClusterID == 0 {
|
2023-10-21 01:26:18 +05:30
|
|
|
pubSubTopicMap[relay.DefaultWakuTopic] = []string{}
|
2023-09-06 10:07:21 +05:30
|
|
|
}
|
|
|
|
|
2023-10-18 15:21:37 -04:00
|
|
|
return pubSubTopicMap, nil
|
2023-09-06 10:07:21 +05:30
|
|
|
}
|
|
|
|
|
2023-10-18 15:21:37 -04:00
|
|
|
func addStaticPeers(wakuNode *node.WakuNode, addresses []multiaddr.Multiaddr, pubSubTopics []string, protocols ...protocol.ID) error {
|
2022-08-15 13:13:45 -04:00
|
|
|
for _, addr := range addresses {
|
2023-09-14 20:30:06 +05:30
|
|
|
_, err := wakuNode.AddPeer(addr, wakupeerstore.Static, pubSubTopics, protocols...)
|
2023-10-18 15:21:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not add static peer: %w", err)
|
|
|
|
}
|
2021-09-30 12:01:53 -04:00
|
|
|
}
|
2023-10-18 15:21:37 -04:00
|
|
|
return nil
|
2021-09-30 12:01:53 -04:00
|
|
|
}
|
2021-10-04 18:38:27 -04:00
|
|
|
|
2022-07-25 08:24:42 -04:00
|
|
|
func loadPrivateKeyFromFile(path string, passwd string) (*ecdsa.PrivateKey, error) {
|
2023-02-16 12:17:52 -04:00
|
|
|
src, err := os.ReadFile(path)
|
2021-10-04 18:38:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-07-25 08:24:42 -04:00
|
|
|
var encryptedK keystore.CryptoJSON
|
|
|
|
err = json.Unmarshal(src, &encryptedK)
|
2021-10-04 18:38:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-07-25 08:24:42 -04:00
|
|
|
pKey, err := keystore.DecryptDataV3(encryptedK, passwd)
|
2022-05-27 15:55:35 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-04 18:38:27 -04:00
|
|
|
|
2022-07-25 08:24:42 -04:00
|
|
|
return crypto.ToECDSA(pKey)
|
2021-10-04 18:38:27 -04:00
|
|
|
}
|
|
|
|
|
2023-08-09 14:14:54 -04:00
|
|
|
func getPrivKey(options NodeOptions) (*ecdsa.PrivateKey, error) {
|
2021-10-04 18:38:27 -04:00
|
|
|
var prvKey *ecdsa.PrivateKey
|
2023-04-02 16:23:37 +05:30
|
|
|
// get private key from nodeKey or keyFile
|
2022-08-15 13:13:45 -04:00
|
|
|
if options.NodeKey != nil {
|
|
|
|
prvKey = options.NodeKey
|
2021-10-04 18:38:27 -04:00
|
|
|
} else {
|
2023-04-02 16:23:37 +05:30
|
|
|
if _, err := os.Stat(options.KeyFile); err == nil {
|
|
|
|
if prvKey, err = loadPrivateKeyFromFile(options.KeyFile, options.KeyPasswd); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not read keyfile: %w", err)
|
2021-10-04 18:38:27 -04:00
|
|
|
}
|
|
|
|
} else {
|
2023-04-02 16:23:37 +05:30
|
|
|
if os.IsNotExist(err) {
|
|
|
|
if prvKey, err = crypto.GenerateKey(); err != nil {
|
|
|
|
return nil, fmt.Errorf("error generating key: %w", err)
|
2021-10-04 18:38:27 -04:00
|
|
|
}
|
|
|
|
} else {
|
2023-04-02 16:23:37 +05:30
|
|
|
return nil, fmt.Errorf("could not read keyfile: %w", err)
|
2021-10-04 18:38:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return prvKey, nil
|
|
|
|
}
|
2021-10-14 14:17:01 -04:00
|
|
|
|
2023-08-09 14:14:54 -04:00
|
|
|
func printListeningAddresses(ctx context.Context, nodeOpts []node.WakuNodeOption, options NodeOptions) {
|
2021-10-14 14:17:01 -04:00
|
|
|
params := new(node.WakuNodeParameters)
|
|
|
|
for _, opt := range nodeOpts {
|
|
|
|
err := opt(params)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2021-10-14 22:15:02 -04:00
|
|
|
|
|
|
|
var libp2pOpts []config.Option
|
2021-11-17 12:19:42 -04:00
|
|
|
libp2pOpts = append(libp2pOpts,
|
|
|
|
params.Identity(),
|
|
|
|
libp2p.ListenAddrs(params.MultiAddresses()...),
|
|
|
|
)
|
2021-10-14 22:15:02 -04:00
|
|
|
|
2022-08-25 16:36:04 -04:00
|
|
|
if options.Websocket.Secure {
|
|
|
|
transports := libp2p.ChainOptions(
|
|
|
|
libp2p.Transport(tcp.NewTCPTransport),
|
|
|
|
libp2p.Transport(ws.New, ws.WithTLSConfig(params.TLSConfig())),
|
|
|
|
)
|
|
|
|
libp2pOpts = append(libp2pOpts, transports)
|
|
|
|
}
|
|
|
|
|
2021-11-17 12:19:42 -04:00
|
|
|
addrFactory := params.AddressFactory()
|
|
|
|
if addrFactory != nil {
|
|
|
|
libp2pOpts = append(libp2pOpts, libp2p.AddrsFactory(addrFactory))
|
2021-10-14 22:15:02 -04:00
|
|
|
}
|
|
|
|
|
2022-03-22 09:12:58 -04:00
|
|
|
h, err := libp2p.New(libp2pOpts...)
|
2021-10-14 14:17:01 -04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-07-07 11:51:15 -04:00
|
|
|
hostAddrs := utils.EncapsulatePeerID(h.ID(), h.Addrs()...)
|
|
|
|
for _, addr := range hostAddrs {
|
|
|
|
fmt.Println(addr)
|
2021-10-14 14:17:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2024-01-03 07:06:41 +05:30
|
|
|
|
|
|
|
func parseMsgSizeConfig(msgSizeConfig string) int {
|
|
|
|
|
|
|
|
msgSize, err := humanize.ParseBytes(msgSizeConfig)
|
|
|
|
if err != nil {
|
|
|
|
msgSize = 0
|
|
|
|
}
|
|
|
|
return int(msgSize)
|
|
|
|
}
|