diff --git a/cmd/harmony/config.go b/cmd/harmony/config.go index 2eb3f2833..d582c36d1 100644 --- a/cmd/harmony/config.go +++ b/cmd/harmony/config.go @@ -4,12 +4,24 @@ import ( "io/ioutil" "time" + nodeconfig "github.com/harmony-one/harmony/internal/configs/node" "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 Network networkConfig P2P p2pConfig @@ -23,17 +35,25 @@ type parsedConfig struct { Devnet *devnetConfig `toml:",omitempty"` } -type generalConfig struct { - NodeType string - IsStaking bool +type networkConfig struct { + NetworkType string + BootNodes []string + + LegacySyncing bool // if true, use LegacySyncingPeerProvider + DNSZone string + DNSPort int } type p2pConfig struct { - IP string Port int KeyFile string } +type generalConfig struct { + NodeType string + IsStaking bool +} + type consensusConfig struct { DelayCommit time.Duration BlockTime time.Duration @@ -83,20 +103,20 @@ type devnetConfig struct { HmyNodeSize int } -func loadConfig(file string) (parsedConfig, error) { +func loadConfig(file string) (hmyConfig, error) { b, err := ioutil.ReadFile(file) if err != nil { - return parsedConfig{}, err + return hmyConfig{}, err } - var config parsedConfig + var config hmyConfig if err := toml.Unmarshal(b, &config); err != nil { - return parsedConfig{}, err + return hmyConfig{}, err } return config, nil } -func writeConfigToFile(config parsedConfig, file string) error { +func writeConfigToFile(config hmyConfig, file string) error { b, err := toml.Marshal(config) if err != nil { return err diff --git a/cmd/harmony/misc.go b/cmd/harmony/misc.go new file mode 100644 index 000000000..1b3606e06 --- /dev/null +++ b/cmd/harmony/misc.go @@ -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 + } +} diff --git a/cmd/harmony/network.go b/cmd/harmony/network.go index 92cb65efc..1c9099a32 100644 --- a/cmd/harmony/network.go +++ b/cmd/harmony/network.go @@ -8,15 +8,6 @@ import ( "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{ networkTypeFlag, bootNodeFlag, @@ -45,7 +36,7 @@ var ( } dnsPortFlag = cli.IntFlag{ Name: "dns.port", - DefValue: defDNSPort, + DefValue: nodeconfig.DefaultDNSPort, Usage: "port of customized dns node", } legacyDNSZoneFlag = cli.StringFlag{ @@ -92,28 +83,7 @@ func getNetworkType(cmd *cobra.Command) (nodeconfig.NetworkType, error) { return nt, nil } -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 applyNetworkFlags(cmd *cobra.Command, cfg *parsedConfig) { +func applyNetworkFlags(cmd *cobra.Command, cfg *hmyConfig) { fs := cmd.Flags() 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 { bn := nodeconfig.GetDefaultBootNodes(nt) zone := nodeconfig.GetDefaultDNSZone(nt) @@ -151,3 +142,105 @@ func getDefaultNetworkConfig(nt nodeconfig.NetworkType) networkConfig { 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 + } +} diff --git a/cmd/harmony/p2p.go b/cmd/harmony/p2p.go deleted file mode 100644 index b2932ba92..000000000 --- a/cmd/harmony/p2p.go +++ /dev/null @@ -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., - } -) diff --git a/internal/configs/node/network.go b/internal/configs/node/network.go index 8f9f747c1..5a0ded095 100644 --- a/internal/configs/node/network.go +++ b/internal/configs/node/network.go @@ -43,6 +43,9 @@ const ( // very bad design. Will refactor later // TODO: refactor all 9000-3000 = 6000 stuff 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