Skip to content

Commit

Permalink
cmd, node: initialize ports with --instance
Browse files Browse the repository at this point in the history
cmd/utils, node: const for listen and disc ports

amend usage description
  • Loading branch information
weiihann committed Apr 29, 2024
1 parent c611924 commit 24c4018
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
1 change: 1 addition & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ var (
utils.NoDiscoverFlag,
utils.DiscoveryV4Flag,
utils.DiscoveryV5Flag,
utils.InstanceFlag,
utils.LegacyDiscoveryV5Flag, // deprecated
utils.NetrestrictFlag,
utils.NodeKeyFileFlag,
Expand Down
29 changes: 29 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ var (
Usage: "Minimum free disk space in MB, once reached triggers auto shut down (default = --cache.gc converted to MB, 0 = disabled)",
Category: flags.EthCategory,
}
InstanceFlag = &cli.IntFlag{
Name: "instance",
Usage: "Configures the ports to avoid conflicts when running multiple nodes on the same machine. Maximum is 200. Changes to the following port numbers: - port = default + `instance` - 1 - discovery.port = default + `instance` - 1 - authrpc.port = default + `instance`*100 - 100 - http.port = default - `instance` + 1 - ws.port = default + `instance`*2 - 2",
Value: 1,
Category: flags.EthCategory,
}

KeyStoreDirFlag = &flags.DirectoryFlag{
Name: "keystore",
Usage: "Directory for the keystore (default = inside the datadir)",
Expand Down Expand Up @@ -1386,6 +1393,7 @@ func SetP2PConfig(ctx *cli.Context, cfg *p2p.Config) {

// SetNodeConfig applies node-related command line flags to the config.
func SetNodeConfig(ctx *cli.Context, cfg *node.Config) {
setInstance(ctx, cfg)
SetP2PConfig(ctx, &cfg.P2P)
setIPC(ctx, cfg)
setHTTP(ctx, cfg)
Expand Down Expand Up @@ -2216,3 +2224,24 @@ func MakeTrieDatabase(ctx *cli.Context, disk ethdb.Database, preimage bool, read
}
return triedb.NewDatabase(disk, config)
}

// setInstance configures the port numbers for the given instance.
func setInstance(ctx *cli.Context, cfg *node.Config) {
if ctx.IsSet(InstanceFlag.Name) {
cfg.Instance = ctx.Int(InstanceFlag.Name)
}

if cfg.Instance > 200 {
Fatalf("Instance number %d is too high, maximum is 200", cfg.Instance)
}

if cfg.Instance == 1 { // using default ports
return
}

cfg.AuthPort = node.DefaultConfig.AuthPort + cfg.Instance*100 - 100
cfg.HTTPPort = node.DefaultHTTPPort - cfg.Instance + 1
cfg.WSPort = node.DefaultWSPort + cfg.Instance*2 - 2
cfg.P2P.ListenAddr = fmt.Sprintf(":%d", node.DefaultListenPort+cfg.Instance-1)
cfg.P2P.DiscAddr = fmt.Sprintf(":%d", node.DefaultDiscPort+cfg.Instance-1)
}
2 changes: 2 additions & 0 deletions node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ type Config struct {
EnablePersonal bool `toml:"-"`

DBEngine string `toml:",omitempty"`

Instance int `toml:",omitempty"`
}

// IPCEndpoint resolves an IPC endpoint based on a configured value, taking into
Expand Down
18 changes: 11 additions & 7 deletions node/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package node

import (
"fmt"
"os"
"os/user"
"path/filepath"
Expand All @@ -28,12 +29,14 @@ import (
)

const (
DefaultHTTPHost = "localhost" // Default host interface for the HTTP RPC server
DefaultHTTPPort = 8545 // Default TCP port for the HTTP RPC server
DefaultWSHost = "localhost" // Default host interface for the websocket RPC server
DefaultWSPort = 8546 // Default TCP port for the websocket RPC server
DefaultAuthHost = "localhost" // Default host interface for the authenticated apis
DefaultAuthPort = 8551 // Default port for the authenticated apis
DefaultHTTPHost = "localhost" // Default host interface for the HTTP RPC server
DefaultHTTPPort = 8545 // Default TCP port for the HTTP RPC server
DefaultWSHost = "localhost" // Default host interface for the websocket RPC server
DefaultWSPort = 8546 // Default TCP port for the websocket RPC server
DefaultAuthHost = "localhost" // Default host interface for the authenticated apis
DefaultAuthPort = 8551 // Default port for the authenticated apis
DefaultListenPort = 30303 // Default port for the TCP listening address
DefaultDiscPort = 30303 // Default port for the UDP discovery address
)

const (
Expand Down Expand Up @@ -68,11 +71,12 @@ var DefaultConfig = Config{
BatchResponseMaxSize: 25 * 1000 * 1000,
GraphQLVirtualHosts: []string{"localhost"},
P2P: p2p.Config{
ListenAddr: ":30303",
ListenAddr: fmt.Sprintf(":%d", DefaultListenPort),
MaxPeers: 50,
NAT: nat.Any(),
},
DBEngine: "", // Use whatever exists, will default to Pebble if non-existent and supported
Instance: 1,
}

// DefaultDataDir is the default data directory to use for the databases and other
Expand Down

0 comments on commit 24c4018

Please sign in to comment.