diff --git a/cmd/genericconf/server.go b/cmd/genericconf/server.go index 3da027ab27..7550791d6d 100644 --- a/cmd/genericconf/server.go +++ b/cmd/genericconf/server.go @@ -8,7 +8,9 @@ import ( flag "github.com/spf13/pflag" + "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/node" + "github.com/ethereum/go-ethereum/p2p/enode" ) type HTTPConfig struct { @@ -185,6 +187,65 @@ 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, + Bootnodes: []string{}, + BootnodesV5: []string{}, +} + +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") + f.Int(prefix+".max-peers", P2PConfigDefault.MaxPeers, "P2P max peers") + f.Bool(prefix+".discovery-v5", P2PConfigDefault.DiscoveryV5, "P2P discovery v5") + f.Bool(prefix+".discovery-v4", P2PConfigDefault.DiscoveryV4, "P2P discovery v4") + f.StringSlice(prefix+".bootnodes", P2PConfigDefault.Bootnodes, "P2P bootnodes") + f.StringSlice(prefix+".bootnodes-v5", P2PConfigDefault.BootnodesV5, "P2P bootnodes v5") +} + 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)