[cmd] added network.go

pull/3278/head
Jacky Wang 4 years ago
parent 3256b8eb81
commit 3259aafce0
No known key found for this signature in database
GPG Key ID: 1085CE5F4FF5842C
  1. 23
      cmd/harmony/config.go
  2. 57
      cmd/harmony/network.go
  3. 1
      cmd/harmony/p2p.go
  4. 1
      internal/configs/node/config.go
  5. 79
      internal/configs/node/network.go

@ -10,7 +10,7 @@ import (
var globalConfig *parsedConfig
type parsedConfig struct {
Run runConfig
General generalConfig
Network networkConfig
P2P p2pConfig
RPC rpcConfig
@ -23,20 +23,11 @@ type parsedConfig struct {
Devnet *devnetConfig `toml:",omitempty"`
}
type runConfig struct {
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
@ -92,20 +83,20 @@ type devnetConfig struct {
HmyNodeSize int
}
func loadConfig(file string) (persistConfig, error) {
func loadConfig(file string) (parsedConfig, error) {
b, err := ioutil.ReadFile(file)
if err != nil {
return persistConfig{}, err
return parsedConfig{}, err
}
var config persistConfig
var config parsedConfig
if err := toml.Unmarshal(b, &config); err != nil {
return persistConfig{}, err
return parsedConfig{}, err
}
return config, nil
}
func writeConfigToFile(config persistConfig, file string) error {
func writeConfigToFile(config parsedConfig, file string) error {
b, err := toml.Marshal(config)
if err != nil {
return err

@ -8,6 +8,15 @@ 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,
@ -62,25 +71,45 @@ var (
}
)
func getNetworkType(cmd *cobra.Command) (string, error) {
func getNetworkType(cmd *cobra.Command) (nodeconfig.NetworkType, error) {
var (
nt string
raw string
err error
)
if cmd.Flags().Changed(legacyNetworkTypeFlag.Name) {
nt, err = cmd.Flags().GetString(legacyNetworkTypeFlag.Name)
raw, err = cmd.Flags().GetString(legacyNetworkTypeFlag.Name)
} else {
nt, err = cmd.Flags().GetString(networkTypeFlag.Name)
raw, err = cmd.Flags().GetString(networkTypeFlag.Name)
}
if err != nil {
return "", err
}
switch nt {
case nodeconfig.Mainnet, nodeconfig.Testnet, nodeconfig.Pangaea, nodeconfig.Localnet,
nodeconfig.Partner, nodeconfig.Stressnet, nodeconfig.Devnet:
nt := parseNetworkType(raw)
if len(nt) == 0 {
return "", fmt.Errorf("unrecognized network type: %v", nt)
}
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 "", fmt.Errorf("unrecognized network type: %v", nt)
return ""
}
}
@ -111,4 +140,14 @@ func applyNetworkFlags(cmd *cobra.Command, cfg *parsedConfig) {
}
}
func getDefaultConfig
func getDefaultNetworkConfig(nt nodeconfig.NetworkType) networkConfig {
bn := nodeconfig.GetDefaultBootNodes(nt)
zone := nodeconfig.GetDefaultDNSZone(nt)
port := nodeconfig.GetDefaultDNSPort(nt)
return networkConfig{
NetworkType: string(nt),
BootNodes: bn,
DNSZone: zone,
DNSPort: port,
}
}

@ -0,0 +1 @@
package main

@ -46,6 +46,7 @@ func (role Role) String() string {
type NetworkType string
// Constants for NetworkType
// TODO: replace this with iota. Leave the string parsing in command line
const (
Mainnet = "mainnet"
Testnet = "testnet"

@ -0,0 +1,79 @@
package nodeconfig
var (
mainnetBootNodes = []string{
"/ip4/100.26.90.187/tcp/9874/p2p/Qmdfjtk6hPoyrH1zVD9PEH4zfWLo38dP2mDvvKXfh3tnEv",
"/ip4/54.213.43.194/tcp/9874/p2p/QmZJJx6AdaoEkGLrYG4JeLCKeCKDjnFz2wfHNHxAqFSGA9",
"/ip4/13.113.101.219/tcp/12019/p2p/QmQayinFSgMMw5cSpDUiD9pQ2WeP6WNmGxpZ6ou3mdVFJX",
"/ip4/99.81.170.167/tcp/12019/p2p/QmRVbTpEYup8dSaURZfF6ByrMTSKa4UyUzJhSjahFzRqNj",
}
testnetBootNodes = []string{
"/ip4/54.86.126.90/tcp/9867/p2p/Qmdfjtk6hPoyrH1zVD9PEH4zfWLo38dP2mDvvKXfh3tnEv",
"/ip4/52.40.84.2/tcp/9867/p2p/QmbPVwrqWsTYXq1RxGWcxx9SWaTUCfoo1wA6wmdbduWe29",
}
pangaeaBootNodes = []string{
"/ip4/52.40.84.2/tcp/9800/p2p/QmbPVwrqWsTYXq1RxGWcxx9SWaTUCfoo1wA6wmdbduWe29",
"/ip4/54.86.126.90/tcp/9800/p2p/Qmdfjtk6hPoyrH1zVD9PEH4zfWLo38dP2mDvvKXfh3tnEv",
}
partnerBootNodes = []string{
"/ip4/52.40.84.2/tcp/9800/p2p/QmbPVwrqWsTYXq1RxGWcxx9SWaTUCfoo1wA6wmdbduWe29",
"/ip4/54.86.126.90/tcp/9800/p2p/Qmdfjtk6hPoyrH1zVD9PEH4zfWLo38dP2mDvvKXfh3tnEv",
}
stressBootNodes = []string{
"/ip4/52.40.84.2/tcp/9842/p2p/QmbPVwrqWsTYXq1RxGWcxx9SWaTUCfoo1wA6wmdbduWe29",
}
)
const (
mainnetDNSZone = "t.hmny.io"
testnetDNSZone = "b.hmny.io"
pangaeaDNSZone = "os.hmny.io"
partnerDNSZone = "ps.hmny.io"
stressnetDNSZone = "stn.hmny.io"
)
const (
defaultDNSPort = 9000
)
// GetDefaultBootNodes get the default bootnode with the given network type
func GetDefaultBootNodes(networkType NetworkType) []string {
switch networkType {
case Mainnet:
return mainnetBootNodes
case Testnet:
return testnetBootNodes
case Pangaea:
return pangaeaBootNodes
case Partner:
return partnerBootNodes
case Stressnet:
return stressBootNodes
}
return nil
}
// GetDefaultDNSZone get the default DNS zone with the given network type
func GetDefaultDNSZone(networkType NetworkType) string {
switch networkType {
case Mainnet:
return mainnetDNSZone
case Testnet:
return testnetDNSZone
case Pangaea:
return pangaeaDNSZone
case Partner:
return partnerDNSZone
case Stressnet:
return stressnetDNSZone
}
}
// GetDefaultDNSPort get the default DNS port for the given network type
func GetDefaultDNSPort(NetworkType) int {
return defaultDNSPort
}
Loading…
Cancel
Save