From 07367c2312f9dbd8074ecc1f74f10568cb8337a6 Mon Sep 17 00:00:00 2001
From: battlmonstr <battlmonstr@users.noreply.github.com>
Date: Mon, 25 Apr 2022 16:47:25 +0200
Subject: [PATCH] sentry: refactor flags, add maxpeers. (#3961)

---
 cmd/sentry/main.go | 35 ++++++++++++++++++++++++++---------
 cmd/utils/flags.go | 31 ++++++++++++++++++++++---------
 turbo/cli/flags.go |  6 ------
 3 files changed, 48 insertions(+), 24 deletions(-)

diff --git a/cmd/sentry/main.go b/cmd/sentry/main.go
index f2adea3450..23522a5a9d 100644
--- a/cmd/sentry/main.go
+++ b/cmd/sentry/main.go
@@ -30,27 +30,31 @@ var (
 	nodiscover   bool // disable sentry's discovery mechanism
 	protocol     string
 	netRestrict  string // CIDR to restrict peering to
+	maxPeers     int
+	maxPendPeers int
 	healthCheck  bool
 )
 
 func init() {
 	utils.CobraFlags(rootCmd, append(debug.Flags, utils.MetricFlags...))
 
-	rootCmd.Flags().StringVar(&natSetting, "nat", "", utils.NATFlag.Usage)
-	rootCmd.Flags().IntVar(&port, "port", 30303, "p2p port number")
 	rootCmd.Flags().StringVar(&sentryAddr, "sentry.api.addr", "localhost:9091", "grpc addresses")
-	rootCmd.Flags().StringVar(&protocol, "p2p.protocol", "eth66", "eth66")
-	rootCmd.Flags().StringSliceVar(&staticPeers, "staticpeers", []string{}, "static peer list [enode]")
-	rootCmd.Flags().StringSliceVar(&trustedPeers, "trustedpeers", []string{}, "trusted peer list [enode]")
+	rootCmd.Flags().StringVar(&datadir, utils.DataDirFlag.Name, paths.DefaultDataDir(), utils.DataDirFlag.Usage)
+	rootCmd.Flags().StringVar(&natSetting, utils.NATFlag.Name, utils.NATFlag.Value, utils.NATFlag.Usage)
+	rootCmd.Flags().IntVar(&port, utils.ListenPortFlag.Name, utils.ListenPortFlag.Value, utils.ListenPortFlag.Usage)
+	rootCmd.Flags().StringSliceVar(&staticPeers, utils.StaticPeersFlag.Name, []string{}, utils.StaticPeersFlag.Usage)
+	rootCmd.Flags().StringSliceVar(&trustedPeers, utils.TrustedPeersFlag.Name, []string{}, utils.TrustedPeersFlag.Usage)
 	rootCmd.Flags().StringSliceVar(&discoveryDNS, utils.DNSDiscoveryFlag.Name, []string{}, utils.DNSDiscoveryFlag.Usage)
 	rootCmd.Flags().BoolVar(&nodiscover, utils.NoDiscoverFlag.Name, false, utils.NoDiscoverFlag.Usage)
-	rootCmd.Flags().StringVar(&netRestrict, "netrestrict", "", "CIDR range to accept peers from <CIDR>")
-	rootCmd.Flags().StringVar(&datadir, utils.DataDirFlag.Name, paths.DefaultDataDir(), utils.DataDirFlag.Usage)
+	rootCmd.Flags().StringVar(&protocol, "p2p.protocol", "eth66", "eth66")
+	rootCmd.Flags().StringVar(&netRestrict, utils.NetrestrictFlag.Name, utils.NetrestrictFlag.Value, utils.NetrestrictFlag.Usage)
+	rootCmd.Flags().IntVar(&maxPeers, utils.MaxPeersFlag.Name, utils.MaxPeersFlag.Value, utils.MaxPeersFlag.Usage)
+	rootCmd.Flags().IntVar(&maxPendPeers, utils.MaxPendingPeersFlag.Name, utils.MaxPendingPeersFlag.Value, utils.MaxPendingPeersFlag.Usage)
 	rootCmd.Flags().BoolVar(&healthCheck, utils.HealthCheckFlag.Name, false, utils.HealthCheckFlag.Usage)
+
 	if err := rootCmd.MarkFlagDirname(utils.DataDirFlag.Name); err != nil {
 		panic(err)
 	}
-
 }
 
 var rootCmd = &cobra.Command{
@@ -71,10 +75,23 @@ var rootCmd = &cobra.Command{
 		p := eth.ETH66
 
 		nodeConfig := node2.NewNodeConfig()
-		p2pConfig, err := utils.NewP2PConfig(nodiscover, datadir, netRestrict, natSetting, nodeConfig.NodeName(), staticPeers, trustedPeers, uint(port), uint(p))
+		p2pConfig, err := utils.NewP2PConfig(
+			nodiscover,
+			datadir,
+			netRestrict,
+			natSetting,
+			maxPeers,
+			maxPendPeers,
+			nodeConfig.NodeName(),
+			staticPeers,
+			trustedPeers,
+			uint(port),
+			uint(p),
+		)
 		if err != nil {
 			return err
 		}
+
 		return sentry.Sentry(cmd.Context(), datadir, sentryAddr, discoveryDNS, p2pConfig, uint(p), healthCheck)
 	},
 }
diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go
index b2bf40fea2..3fb1a080c7 100644
--- a/cmd/utils/flags.go
+++ b/cmd/utils/flags.go
@@ -805,7 +805,19 @@ func ParseNodesFromURLs(urls []string) ([]*enode.Node, error) {
 
 // NewP2PConfig
 //  - doesn't setup bootnodes - they will set when genesisHash will know
-func NewP2PConfig(nodiscover bool, datadir, netRestrict, natSetting, nodeName string, staticPeers []string, trustedPeers []string, port, protocol uint) (*p2p.Config, error) {
+func NewP2PConfig(
+	nodiscover bool,
+	datadir string,
+	netRestrict string,
+	natSetting string,
+	maxPeers int,
+	maxPendPeers int,
+	nodeName string,
+	staticPeers []string,
+	trustedPeers []string,
+	port,
+	protocol uint,
+) (*p2p.Config, error) {
 	var enodeDBPath string
 	switch protocol {
 	case eth.ETH66:
@@ -820,14 +832,15 @@ func NewP2PConfig(nodiscover bool, datadir, netRestrict, natSetting, nodeName st
 	}
 
 	cfg := &p2p.Config{
-		ListenAddr:   fmt.Sprintf(":%d", port),
-		MaxPeers:     100,
-		NAT:          nat.Any(),
-		NoDiscovery:  nodiscover,
-		PrivateKey:   serverKey,
-		Name:         nodeName,
-		Log:          log.New(),
-		NodeDatabase: enodeDBPath,
+		ListenAddr:      fmt.Sprintf(":%d", port),
+		MaxPeers:        maxPeers,
+		MaxPendingPeers: maxPendPeers,
+		NAT:             nat.Any(),
+		NoDiscovery:     nodiscover,
+		PrivateKey:      serverKey,
+		Name:            nodeName,
+		Log:             log.New(),
+		NodeDatabase:    enodeDBPath,
 	}
 	if netRestrict != "" {
 		cfg.NetRestrict = new(netutil.Netlist)
diff --git a/turbo/cli/flags.go b/turbo/cli/flags.go
index df09d39778..7e5571ec68 100644
--- a/turbo/cli/flags.go
+++ b/turbo/cli/flags.go
@@ -56,12 +56,6 @@ var (
 		Value: kv.ReadersLimit - 128,
 	}
 
-	MaxPeersFlag = cli.IntFlag{
-		Name:  "maxpeers",
-		Usage: "Maximum number of network peers (network disabled if set to 0)",
-		Value: node.DefaultConfig.P2P.MaxPeers,
-	}
-
 	PruneFlag = cli.StringFlag{
 		Name: "prune",
 		Usage: `Choose which ancient data delete from DB:
-- 
GitLab