[cmd] added rpc config flags and p2p flags

pull/3278/head
Jacky Wang 4 years ago
parent 69d3607fdd
commit 64f69aa699
No known key found for this signature in database
GPG Key ID: 1085CE5F4FF5842C
  1. 42
      cmd/harmony/config.go
  2. 31
      cmd/harmony/misc.go
  3. 157
      cmd/harmony/network.go
  4. 16
      cmd/harmony/p2p.go
  5. 3
      internal/configs/node/network.go

@ -4,12 +4,24 @@ import (
"io/ioutil" "io/ioutil"
"time" "time"
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
"github.com/pelletier/go-toml" "github.com/pelletier/go-toml"
) )
var globalConfig *parsedConfig var defaultConfig = hmyConfig{
Network: getDefaultNetworkConfig(nodeconfig.Mainnet),
P2P: p2pConfig{
Port: nodeconfig.DefaultP2PPort,
KeyFile: "./.hmykey",
},
RPC: rpcConfig{
Enabled: false,
IP: "127.0.0.1",
Port: nodeconfig.DefaultRPCPort,
},
}
type parsedConfig struct { type hmyConfig struct {
General generalConfig General generalConfig
Network networkConfig Network networkConfig
P2P p2pConfig P2P p2pConfig
@ -23,17 +35,25 @@ type parsedConfig struct {
Devnet *devnetConfig `toml:",omitempty"` Devnet *devnetConfig `toml:",omitempty"`
} }
type generalConfig struct { type networkConfig struct {
NodeType string NetworkType string
IsStaking bool BootNodes []string
LegacySyncing bool // if true, use LegacySyncingPeerProvider
DNSZone string
DNSPort int
} }
type p2pConfig struct { type p2pConfig struct {
IP string
Port int Port int
KeyFile string KeyFile string
} }
type generalConfig struct {
NodeType string
IsStaking bool
}
type consensusConfig struct { type consensusConfig struct {
DelayCommit time.Duration DelayCommit time.Duration
BlockTime time.Duration BlockTime time.Duration
@ -83,20 +103,20 @@ type devnetConfig struct {
HmyNodeSize int HmyNodeSize int
} }
func loadConfig(file string) (parsedConfig, error) { func loadConfig(file string) (hmyConfig, error) {
b, err := ioutil.ReadFile(file) b, err := ioutil.ReadFile(file)
if err != nil { if err != nil {
return parsedConfig{}, err return hmyConfig{}, err
} }
var config parsedConfig var config hmyConfig
if err := toml.Unmarshal(b, &config); err != nil { if err := toml.Unmarshal(b, &config); err != nil {
return parsedConfig{}, err return hmyConfig{}, err
} }
return config, nil return config, nil
} }
func writeConfigToFile(config parsedConfig, file string) error { func writeConfigToFile(config hmyConfig, file string) error {
b, err := toml.Marshal(config) b, err := toml.Marshal(config)
if err != nil { if err != nil {
return err return err

@ -0,0 +1,31 @@
package main
import (
"github.com/harmony-one/harmony/internal/cli"
"github.com/spf13/cobra"
)
// miscFlags are legacy flags that have different usage.
var miscFlags = []cli.Flag{
legacyPortFlag,
}
var (
legacyPortFlag = cli.IntFlag{
Name: "port",
Usage: "port of the node",
DefValue: defaultConfig.P2P.Port,
Deprecated: "Use --p2p.port, --http.port instead",
}
)
// TODO: move all port manipulation +500 -1000 logic here
func applyMiscFlags(cmd *cobra.Command, config *hmyConfig) {
fs := cmd.Flags()
if fs.Changed(legacyPortFlag.Name) {
legacyPort, _ := fs.GetInt(legacyPortFlag.Name)
config.P2P.Port = legacyPort
config.RPC.Port = legacyPort
}
}

@ -8,15 +8,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
type networkConfig struct {
NetworkType string
BootNodes []string
LegacySyncing bool // if true, use LegacySyncingPeerProvider
DNSZone string
DNSPort int
}
var networkFlags = []cli.Flag{ var networkFlags = []cli.Flag{
networkTypeFlag, networkTypeFlag,
bootNodeFlag, bootNodeFlag,
@ -45,7 +36,7 @@ var (
} }
dnsPortFlag = cli.IntFlag{ dnsPortFlag = cli.IntFlag{
Name: "dns.port", Name: "dns.port",
DefValue: defDNSPort, DefValue: nodeconfig.DefaultDNSPort,
Usage: "port of customized dns node", Usage: "port of customized dns node",
} }
legacyDNSZoneFlag = cli.StringFlag{ legacyDNSZoneFlag = cli.StringFlag{
@ -92,28 +83,7 @@ func getNetworkType(cmd *cobra.Command) (nodeconfig.NetworkType, error) {
return nt, nil return nt, nil
} }
func parseNetworkType(nt string) nodeconfig.NetworkType { func applyNetworkFlags(cmd *cobra.Command, cfg *hmyConfig) {
switch nt {
case "mainnet":
return nodeconfig.Mainnet
case "testnet":
return nodeconfig.Testnet
case "pangaea", "staking", "stk":
return nodeconfig.Pangaea
case "partner":
return nodeconfig.Partner
case "stressnet", "stress", "stn":
return nodeconfig.Stressnet
case "localnet":
return nodeconfig.Localnet
case "devnet", "dev":
return nodeconfig.Devnet
default:
return ""
}
}
func applyNetworkFlags(cmd *cobra.Command, cfg *parsedConfig) {
fs := cmd.Flags() fs := cmd.Flags()
if fs.Changed(bootNodeFlag.Name) { if fs.Changed(bootNodeFlag.Name) {
@ -140,6 +110,27 @@ func applyNetworkFlags(cmd *cobra.Command, cfg *parsedConfig) {
} }
} }
func parseNetworkType(nt string) nodeconfig.NetworkType {
switch nt {
case "mainnet":
return nodeconfig.Mainnet
case "testnet":
return nodeconfig.Testnet
case "pangaea", "staking", "stk":
return nodeconfig.Pangaea
case "partner":
return nodeconfig.Partner
case "stressnet", "stress", "stn":
return nodeconfig.Stressnet
case "localnet":
return nodeconfig.Localnet
case "devnet", "dev":
return nodeconfig.Devnet
default:
return ""
}
}
func getDefaultNetworkConfig(nt nodeconfig.NetworkType) networkConfig { func getDefaultNetworkConfig(nt nodeconfig.NetworkType) networkConfig {
bn := nodeconfig.GetDefaultBootNodes(nt) bn := nodeconfig.GetDefaultBootNodes(nt)
zone := nodeconfig.GetDefaultDNSZone(nt) zone := nodeconfig.GetDefaultDNSZone(nt)
@ -151,3 +142,105 @@ func getDefaultNetworkConfig(nt nodeconfig.NetworkType) networkConfig {
DNSPort: port, DNSPort: port,
} }
} }
var p2pFlags = []cli.Flag{
p2pPortFlag,
p2pKeyFileFlag,
legacyKeyFileFlag,
}
var (
p2pPortFlag = &cli.IntFlag{
Name: "p2p.port",
Usage: "port to listen for p2p communication",
DefValue: defaultConfig.P2P.Port,
}
p2pKeyFileFlag = &cli.StringFlag{
Name: "p2p.keyfile",
Usage: "the p2p key file of the harmony node",
DefValue: defaultConfig.P2P.KeyFile,
}
legacyKeyFileFlag = &cli.StringFlag{
Name: "key",
Usage: "the p2p key file of the harmony node",
DefValue: defaultConfig.P2P.KeyFile,
Deprecated: "use --p2p.keyfile",
}
)
func parseP2PFlags(cmd *cobra.Command, config *hmyConfig) {
fs := cmd.Flags()
if fs.Changed(p2pPortFlag.Name) {
config.P2P.Port, _ = fs.GetInt(p2pPortFlag.Name)
}
if fs.Changed(p2pKeyFileFlag.Name) {
config.P2P.KeyFile, _ = fs.GetString(p2pKeyFileFlag.Name)
} else if fs.Changed(legacyKeyFileFlag.Name) {
config.P2P.KeyFile, _ = fs.GetString(legacyKeyFileFlag.Name)
}
}
var rpcFlags = []cli.Flag{
rpcEnabledFlag,
rpcIPFlag,
rpcPortFlag,
legacyRPCIPFlag,
legacyPublicRPCFlag
}
var (
rpcEnabledFlag = cli.BoolFlag{
Name: "http",
Usage: "enable HTTP / RPC requests",
DefValue: defaultConfig.RPC.Enabled,
}
rpcIPFlag = cli.StringFlag{
Name: "http.ip",
Usage: "ip address to listen for RPC calls",
DefValue: defaultConfig.RPC.IP,
}
rpcPortFlag = cli.IntFlag{
Name: "http.port",
Usage: "rpc port to listen for RPC calls",
DefValue: defaultConfig.RPC.Port,
}
legacyRPCIPFlag = cli.StringFlag{
Name: "ip",
Usage: "ip of the node",
DefValue: defaultConfig.RPC.IP,
Deprecated: "use --http.ip",
}
legacyPublicRPCFlag = cli.BoolFlag{
Name: "public_rpc",
Usage: "Enable Public RPC Access (default: false)",
DefValue: defaultConfig.RPC.Enabled,
Deprecated: "please use --http.ip to specify the ip address to listen",
}
)
func applyRPCFlags(cmd *cobra.Command, config *hmyConfig) {
fs := cmd.Flags()
var isRPCSpecified bool
if fs.Changed(rpcIPFlag.Name) {
config.RPC.IP, _ = fs.GetString(rpcIPFlag.Name)
isRPCSpecified = true
} else if fs.Changed(legacyRPCIPFlag.Name) {
config.RPC.IP, _ = fs.GetString(legacyRPCIPFlag.Name)
isRPCSpecified = true
}
if fs.Changed(rpcPortFlag.Name) {
config.RPC.Port, _ = fs.GetInt(rpcPortFlag.Name)
isRPCSpecified = true
}
if fs.Changed(rpcEnabledFlag.Name) {
config.RPC.Enabled, _ = fs.GetBool(rpcEnabledFlag.Name)
} else if isRPCSpecified {
config.RPC.Enabled = true
}
}

@ -1,16 +0,0 @@
package main
import "github.com/harmony-one/harmony/internal/cli"
type p2pConfig struct {
Port int
KeyFile string
}
var (
p2pPortFlag = &cli.IntFlag{
Name: "p2p.port",
Usage: "port to listen for p2p communication",
DefValue: nodeconfig.,
}
)

@ -43,6 +43,9 @@ const (
// very bad design. Will refactor later // very bad design. Will refactor later
// TODO: refactor all 9000-3000 = 6000 stuff // TODO: refactor all 9000-3000 = 6000 stuff
DefaultDNSPort = 9000 DefaultDNSPort = 9000
// DefaultRPCPort is the default rpc port. The actual port used is 9000+500, which is a terrible
// design.
DefaultRPCPort = 9000
) )
// GetDefaultBootNodes get the default bootnode with the given network type // GetDefaultBootNodes get the default bootnode with the given network type

Loading…
Cancel
Save