github.com/status-im/status-go@v1.1.0/wakuv2/config.go (about)

     1  // Copyright 2019 The Waku Library Authors.
     2  //
     3  // The Waku library is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Lesser General Public License as published by
     5  // the Free Software Foundation, either version 3 of the License, or
     6  // (at your option) any later version.
     7  //
     8  // The Waku library is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty off
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    11  // GNU Lesser General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Lesser General Public License
    14  // along with the Waku library. If not, see <http://www.gnu.org/licenses/>.
    15  //
    16  // This software uses the go-ethereum library, which is licensed
    17  // under the GNU Lesser General Public Library, version 3 or any later.
    18  
    19  package wakuv2
    20  
    21  import (
    22  	"errors"
    23  
    24  	"go.uber.org/zap"
    25  
    26  	"github.com/status-im/status-go/protocol/common/shard"
    27  
    28  	ethdisc "github.com/ethereum/go-ethereum/p2p/dnsdisc"
    29  
    30  	"github.com/status-im/status-go/wakuv2/common"
    31  )
    32  
    33  var (
    34  	ErrBadLightClientConfig = errors.New("either peer exchange server or discv5 must be disabled, and the peer exchange client must be enabled")
    35  	ErrBadFullNodeConfig    = errors.New("peer exchange server and discv5 must be enabled, and the peer exchange client must be disabled")
    36  )
    37  
    38  // Config represents the configuration state of a waku node.
    39  type Config struct {
    40  	MaxMessageSize                         uint32           `toml:",omitempty"` // Maximal message length allowed by the waku node
    41  	Host                                   string           `toml:",omitempty"`
    42  	Port                                   int              `toml:",omitempty"`
    43  	EnablePeerExchangeServer               bool             `toml:",omitempty"` // PeerExchange server makes sense only when discv5 is running locally as it will have a cache of peers that it can respond to in case a PeerExchange request comes from the PeerExchangeClient
    44  	EnablePeerExchangeClient               bool             `toml:",omitempty"`
    45  	MinPeersForRelay                       int              `toml:",omitempty"` // Indicates the minimum number of peers required for using Relay Protocol
    46  	MinPeersForFilter                      int              `toml:",omitempty"` // Indicates the minimum number of peers required for using Filter Protocol
    47  	LightClient                            bool             `toml:",omitempty"` // Indicates if the node is a light client
    48  	WakuNodes                              []string         `toml:",omitempty"`
    49  	DiscV5BootstrapNodes                   []string         `toml:",omitempty"`
    50  	Nameserver                             string           `toml:",omitempty"` // Optional nameserver to use for dns discovery
    51  	Resolver                               ethdisc.Resolver `toml:",omitempty"` // Optional resolver to use for dns discovery
    52  	EnableDiscV5                           bool             `toml:",omitempty"` // Indicates whether discv5 is enabled or not
    53  	DiscoveryLimit                         int              `toml:",omitempty"` // Indicates the number of nodes to discover with peer exchange client
    54  	AutoUpdate                             bool             `toml:",omitempty"`
    55  	UDPPort                                int              `toml:",omitempty"`
    56  	EnableStore                            bool             `toml:",omitempty"`
    57  	StoreCapacity                          int              `toml:",omitempty"`
    58  	StoreSeconds                           int              `toml:",omitempty"`
    59  	TelemetryServerURL                     string           `toml:",omitempty"`
    60  	TelemetrySendPeriodMs                  int              `toml:",omitempty"` // Number of milliseconds to wait between sending requests to telemetry service
    61  	TelemetryPeerCountSendPeriod           int              `toml:",omitempty"` // Number of milliseconds to wait between checking peer count
    62  	DefaultShardPubsubTopic                string           `toml:",omitempty"` // Pubsub topic to be used by default for messages that do not have a topic assigned (depending whether sharding is used or not)
    63  	DefaultShardedPubsubTopics             []string         `toml:", omitempty"`
    64  	ClusterID                              uint16           `toml:",omitempty"`
    65  	EnableConfirmations                    bool             `toml:",omitempty"` // Enable sending message confirmations
    66  	SkipPublishToTopic                     bool             `toml:",omitempty"` // Used in testing
    67  	EnableMissingMessageVerification       bool             `toml:",omitempty"`
    68  	EnableStoreConfirmationForMessagesSent bool             `toml:",omitempty"` //Flag that enables checking with store node for sent message confimration
    69  	UseThrottledPublish                    bool             `toml:",omitempty"` // Flag that indicates whether a rate limited priority queue will be used to send messages or not
    70  }
    71  
    72  func (c *Config) Validate(logger *zap.Logger) error {
    73  	if c.LightClient && (c.EnablePeerExchangeServer || c.EnableDiscV5 || !c.EnablePeerExchangeClient) {
    74  		logger.Warn("bad configuration for a light client", zap.Error(ErrBadLightClientConfig))
    75  		return nil
    76  	}
    77  	if !c.LightClient && (!c.EnablePeerExchangeServer || !c.EnableDiscV5 || c.EnablePeerExchangeClient) {
    78  		logger.Warn("bad configuration for a full node", zap.Error(ErrBadFullNodeConfig))
    79  		return nil
    80  	}
    81  	return nil
    82  }
    83  
    84  var DefaultConfig = Config{
    85  	MaxMessageSize:    common.DefaultMaxMessageSize,
    86  	Host:              "0.0.0.0",
    87  	Port:              0,
    88  	DiscoveryLimit:    20,
    89  	MinPeersForRelay:  1, // TODO: determine correct value with Vac team
    90  	MinPeersForFilter: 2, // TODO: determine correct value with Vac team and via testing
    91  	AutoUpdate:        false,
    92  }
    93  
    94  func setDefaults(cfg *Config) *Config {
    95  	if cfg == nil {
    96  		cfg = new(Config)
    97  	}
    98  
    99  	if cfg.MaxMessageSize == 0 {
   100  		cfg.MaxMessageSize = DefaultConfig.MaxMessageSize
   101  	}
   102  
   103  	if cfg.Host == "" {
   104  		cfg.Host = DefaultConfig.Host
   105  	}
   106  
   107  	if cfg.DiscoveryLimit == 0 {
   108  		cfg.DiscoveryLimit = DefaultConfig.DiscoveryLimit
   109  	}
   110  
   111  	if cfg.MinPeersForRelay == 0 {
   112  		cfg.MinPeersForRelay = DefaultConfig.MinPeersForRelay
   113  	}
   114  
   115  	if cfg.MinPeersForFilter == 0 {
   116  		cfg.MinPeersForFilter = DefaultConfig.MinPeersForFilter
   117  	}
   118  
   119  	if cfg.DefaultShardPubsubTopic == "" {
   120  		cfg.DefaultShardPubsubTopic = shard.DefaultShardPubsubTopic()
   121  		//For now populating with both used shards, but this can be populated from user subscribed communities etc once community sharding is implemented
   122  		cfg.DefaultShardedPubsubTopics = append(cfg.DefaultShardedPubsubTopics, shard.DefaultShardPubsubTopic())
   123  		cfg.DefaultShardedPubsubTopics = append(cfg.DefaultShardedPubsubTopics, shard.DefaultNonProtectedPubsubTopic())
   124  	}
   125  
   126  	return cfg
   127  }