From f0156ccb615771393feb7b9ba69a24a093925c5a Mon Sep 17 00:00:00 2001 From: amsanghi Date: Fri, 2 Feb 2024 21:25:02 +0530 Subject: [PATCH] Add P2P options --- cmd/genericconf/server.go | 54 ++++++++++++++++++++++++++++++++++++++ cmd/nitro-val/config.go | 3 +++ cmd/nitro-val/nitro_val.go | 4 +-- cmd/nitro/nitro.go | 7 ++--- 4 files changed, 62 insertions(+), 6 deletions(-) diff --git a/cmd/genericconf/server.go b/cmd/genericconf/server.go index 3da027ab27..f207e9ff3e 100644 --- a/cmd/genericconf/server.go +++ b/cmd/genericconf/server.go @@ -4,6 +4,8 @@ package genericconf import ( + "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/p2p/enode" "time" flag "github.com/spf13/pflag" @@ -185,6 +187,58 @@ func AuthRPCConfigAddOptions(prefix string, f *flag.FlagSet) { f.StringSlice(prefix+".api", AuthRPCConfigDefault.API, "APIs offered over the AUTH-RPC interface") } +type P2PConfig struct { + ListenAddr string `koanf:"listen-addr"` + NoDial bool `koanf:"no-dial"` + NoDiscovery bool `koanf:"no-discovery"` + MaxPeers int `koanf:"max-peers"` + DiscoveryV5 bool `koanf:"discovery-v5"` + DiscoveryV4 bool `koanf:"discovery-v4"` + Bootnodes []string `koanf:"bootnodes"` + BootnodesV5 []string `koanf:"bootnodes-v5"` +} + +func (p P2PConfig) Apply(stackConf *node.Config) { + stackConf.P2P.ListenAddr = p.ListenAddr + stackConf.P2P.NoDial = p.NoDial + stackConf.P2P.NoDiscovery = p.NoDiscovery + stackConf.P2P.MaxPeers = p.MaxPeers + stackConf.P2P.DiscoveryV5 = p.DiscoveryV5 + stackConf.P2P.DiscoveryV4 = p.DiscoveryV4 + stackConf.P2P.BootstrapNodes = parseBootnodes(p.Bootnodes) + stackConf.P2P.BootstrapNodesV5 = parseBootnodes(p.BootnodesV5) +} + +func parseBootnodes(urls []string) []*enode.Node { + nodes := make([]*enode.Node, 0, len(urls)) + for _, url := range urls { + if url != "" { + node, err := enode.Parse(enode.ValidSchemes, url) + if err != nil { + log.Crit("Bootstrap URL invalid", "enode", url, "err", err) + return nil + } + nodes = append(nodes, node) + } + } + return nodes +} + +var P2PConfigDefault = P2PConfig{ + ListenAddr: "", + NoDial: true, + NoDiscovery: true, + MaxPeers: 50, + DiscoveryV5: false, + DiscoveryV4: false, +} + +func P2PConfigAddOptions(prefix string, f *flag.FlagSet) { + f.String(prefix+".listen-addr", P2PConfigDefault.ListenAddr, "P2P listen address") + f.Bool(prefix+".no-dial", P2PConfigDefault.NoDial, "P2P no dial") + f.Bool(prefix+".no-discovery", P2PConfigDefault.NoDiscovery, "P2P no discovery") +} + type MetricsServerConfig struct { Addr string `koanf:"addr"` Port int `koanf:"port"` diff --git a/cmd/nitro-val/config.go b/cmd/nitro-val/config.go index cf10787d6d..51d3978836 100644 --- a/cmd/nitro-val/config.go +++ b/cmd/nitro-val/config.go @@ -27,6 +27,7 @@ type ValidationNodeConfig struct { HTTP genericconf.HTTPConfig `koanf:"http"` WS genericconf.WSConfig `koanf:"ws"` IPC genericconf.IPCConfig `koanf:"ipc"` + P2P genericconf.P2PConfig `koanf:"p2p"` Auth genericconf.AuthRPCConfig `koanf:"auth"` Metrics bool `koanf:"metrics"` MetricsServer genericconf.MetricsServerConfig `koanf:"metrics-server"` @@ -66,6 +67,7 @@ var ValidationNodeConfigDefault = ValidationNodeConfig{ HTTP: HTTPConfigDefault, WS: WSConfigDefault, IPC: IPCConfigDefault, + P2P: genericconf.P2PConfigDefault, Auth: genericconf.AuthRPCConfigDefault, Metrics: false, MetricsServer: genericconf.MetricsServerConfigDefault, @@ -85,6 +87,7 @@ func ValidationNodeConfigAddOptions(f *flag.FlagSet) { genericconf.WSConfigAddOptions("ws", f) genericconf.IPCConfigAddOptions("ipc", f) genericconf.AuthRPCConfigAddOptions("auth", f) + genericconf.P2PConfigAddOptions("p2p", f) f.Bool("metrics", ValidationNodeConfigDefault.Metrics, "enable metrics") genericconf.MetricsServerAddOptions("metrics-server", f) f.Bool("pprof", ValidationNodeConfigDefault.PProf, "enable pprof") diff --git a/cmd/nitro-val/nitro_val.go b/cmd/nitro-val/nitro_val.go index 20b8b23628..fea95cbb15 100644 --- a/cmd/nitro-val/nitro_val.go +++ b/cmd/nitro-val/nitro_val.go @@ -70,9 +70,7 @@ func mainImpl() int { nodeConfig.WS.Apply(&stackConf) nodeConfig.Auth.Apply(&stackConf) nodeConfig.IPC.Apply(&stackConf) - stackConf.P2P.ListenAddr = "" - stackConf.P2P.NoDial = true - stackConf.P2P.NoDiscovery = true + nodeConfig.P2P.Apply(&stackConf) vcsRevision, strippedRevision, vcsTime := confighelpers.GetVersion() stackConf.Version = strippedRevision diff --git a/cmd/nitro/nitro.go b/cmd/nitro/nitro.go index e5b97d4a1c..f956b4674e 100644 --- a/cmd/nitro/nitro.go +++ b/cmd/nitro/nitro.go @@ -178,9 +178,7 @@ func mainImpl() int { if nodeConfig.WS.ExposeAll { stackConf.WSModules = append(stackConf.WSModules, "personal") } - stackConf.P2P.ListenAddr = "" - stackConf.P2P.NoDial = true - stackConf.P2P.NoDiscovery = true + nodeConfig.P2P.Apply(&stackConf) vcsRevision, strippedRevision, vcsTime := confighelpers.GetVersion() stackConf.Version = strippedRevision @@ -674,6 +672,7 @@ type NodeConfig struct { IPC genericconf.IPCConfig `koanf:"ipc"` Auth genericconf.AuthRPCConfig `koanf:"auth"` GraphQL genericconf.GraphQLConfig `koanf:"graphql"` + P2P genericconf.P2PConfig `koanf:"p2p"` Metrics bool `koanf:"metrics"` MetricsServer genericconf.MetricsServerConfig `koanf:"metrics-server"` PProf bool `koanf:"pprof"` @@ -698,6 +697,7 @@ var NodeConfigDefault = NodeConfig{ IPC: genericconf.IPCConfigDefault, Auth: genericconf.AuthRPCConfigDefault, GraphQL: genericconf.GraphQLConfigDefault, + P2P: genericconf.P2PConfigDefault, Metrics: false, MetricsServer: genericconf.MetricsServerConfigDefault, Init: conf.InitConfigDefault, @@ -721,6 +721,7 @@ func NodeConfigAddOptions(f *flag.FlagSet) { genericconf.WSConfigAddOptions("ws", f) genericconf.IPCConfigAddOptions("ipc", f) genericconf.AuthRPCConfigAddOptions("auth", f) + genericconf.P2PConfigAddOptions("p2p", f) genericconf.GraphQLConfigAddOptions("graphql", f) f.Bool("metrics", NodeConfigDefault.Metrics, "enable metrics") genericconf.MetricsServerAddOptions("metrics-server", f)