Skip to content

Commit

Permalink
Merge pull request #2124 from OffchainLabs/p2p
Browse files Browse the repository at this point in the history
Add P2P options
  • Loading branch information
amsanghi authored Feb 7, 2024
2 parents 8517340 + 5ff1ad2 commit 69c43da
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 6 deletions.
61 changes: 61 additions & 0 deletions cmd/genericconf/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"`
Expand Down
3 changes: 3 additions & 0 deletions cmd/nitro-val/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -66,6 +67,7 @@ var ValidationNodeConfigDefault = ValidationNodeConfig{
HTTP: HTTPConfigDefault,
WS: WSConfigDefault,
IPC: IPCConfigDefault,
P2P: genericconf.P2PConfigDefault,
Auth: genericconf.AuthRPCConfigDefault,
Metrics: false,
MetricsServer: genericconf.MetricsServerConfigDefault,
Expand All @@ -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")
Expand Down
4 changes: 1 addition & 3 deletions cmd/nitro-val/nitro_val.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 4 additions & 3 deletions cmd/nitro/nitro.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"`
Expand All @@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit 69c43da

Please sign in to comment.