cmd, node: initialize ports with --instance

cmd/utils, node: const for listen and disc ports

amend usage description
This commit is contained in:
weiihann 2024-03-15 15:15:30 +08:00
parent c611924727
commit 24c4018e53
4 changed files with 43 additions and 7 deletions

View file

@ -127,6 +127,7 @@ var (
utils.NoDiscoverFlag,
utils.DiscoveryV4Flag,
utils.DiscoveryV5Flag,
utils.InstanceFlag,
utils.LegacyDiscoveryV5Flag, // deprecated
utils.NetrestrictFlag,
utils.NodeKeyFileFlag,

View file

@ -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)",
@ -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)
@ -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)
}

View file

@ -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

View file

@ -17,6 +17,7 @@
package node
import (
"fmt"
"os"
"os/user"
"path/filepath"
@ -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 (
@ -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