[cmd] added more stuff config. Need to implement an internal flag module

pull/3278/head
Jacky Wang 4 years ago
parent 0c6b949cd4
commit 74112a75b1
No known key found for this signature in database
GPG Key ID: 1085CE5F4FF5842C
  1. 37
      cmd/harmony/config.go
  2. 232
      cmd/harmony/config_test.go
  3. 1
      cmd/harmony/main.go
  4. 46
      cmd/harmony/network.go

@ -7,9 +7,13 @@ import (
"github.com/pelletier/go-toml" "github.com/pelletier/go-toml"
) )
type hmyConfig struct { var globalConfig *parsedConfig
type parsedConfig struct {
Run runConfig Run runConfig
Network networkConfig Network networkConfig
P2P p2pConfig
RPC rpcConfig
Consensus consensusConfig Consensus consensusConfig
BLSKey blsConfig BLSKey blsConfig
TxPool txPoolConfig TxPool txPoolConfig
@ -22,22 +26,21 @@ type hmyConfig struct {
type runConfig struct { type runConfig struct {
NodeType string NodeType string
IsStaking bool IsStaking bool
ShardID int
} }
type networkConfig struct { type networkConfig struct {
Network string NetworkType string
IP string
Port int
MinPeers int
P2PKeyFile string
PublicRPC bool
BootNodes []string BootNodes []string
DNSZone string DNSZone string
DNSPort int DNSPort int
} }
type p2pConfig struct {
IP string
Port int
KeyFile string
}
type consensusConfig struct { type consensusConfig struct {
DelayCommit time.Duration DelayCommit time.Duration
BlockTime time.Duration BlockTime time.Duration
@ -75,26 +78,32 @@ type logConfig struct {
LogMaxSize int LogMaxSize int
} }
type rpcConfig struct {
Enabled bool
IP string
Port int
}
type devnetConfig struct { type devnetConfig struct {
NumShards uint NumShards uint
ShardSize int ShardSize int
HmyNodeSize int HmyNodeSize int
} }
func loadConfig(file string) (hmyConfig, error) { func loadConfig(file string) (persistConfig, error) {
b, err := ioutil.ReadFile(file) b, err := ioutil.ReadFile(file)
if err != nil { if err != nil {
return hmyConfig{}, err return persistConfig{}, err
} }
var config hmyConfig var config persistConfig
if err := toml.Unmarshal(b, &config); err != nil { if err := toml.Unmarshal(b, &config); err != nil {
return hmyConfig{}, err return persistConfig{}, err
} }
return config, nil return config, nil
} }
func writeConfigToFile(config hmyConfig, file string) error { func writeConfigToFile(config persistConfig, file string) error {
b, err := toml.Marshal(config) b, err := toml.Marshal(config)
if err != nil { if err != nil {
return err return err

@ -1,116 +1,116 @@
package main //package main
//
import ( //import (
"fmt" // "fmt"
"os" // "os"
"path/filepath" // "path/filepath"
"reflect" // "reflect"
"testing" // "testing"
"time" // "time"
) //)
//
var testHmyConfig = hmyConfig{ //var testHmyConfig = hmyConfig{
Run: runConfig{ // Run: runConfig{
NodeType: "validator", // NodeType: "validator",
IsStaking: true, // IsStaking: true,
ShardID: 0, // ShardID: 0,
}, // },
Network: networkConfig{ // NetworkType: networkConfig{
Network: "mainnet", // NetworkType: "mainnet",
IP: "127.0.0.1", // IP: "127.0.0.1",
Port: 9000, // Port: 9000,
MinPeers: 32, // MinPeers: 32,
P2PKeyFile: "./.hmykey", // P2PKeyFile: "./.hmykey",
PublicRPC: false, // PublicRPC: false,
BootNodes: []string{ // BootNodes: []string{
"/ip4/100.26.90.187/tcp/9874/p2p/Qmdfjtk6hPoyrH1zVD9PEH4zfWLo38dP2mDvvKXfh3tnEv", // "/ip4/100.26.90.187/tcp/9874/p2p/Qmdfjtk6hPoyrH1zVD9PEH4zfWLo38dP2mDvvKXfh3tnEv",
"/ip4/54.213.43.194/tcp/9874/p2p/QmZJJx6AdaoEkGLrYG4JeLCKeCKDjnFz2wfHNHxAqFSGA9", // "/ip4/54.213.43.194/tcp/9874/p2p/QmZJJx6AdaoEkGLrYG4JeLCKeCKDjnFz2wfHNHxAqFSGA9",
"/ip4/13.113.101.219/tcp/12019/p2p/QmQayinFSgMMw5cSpDUiD9pQ2WeP6WNmGxpZ6ou3mdVFJX", // "/ip4/13.113.101.219/tcp/12019/p2p/QmQayinFSgMMw5cSpDUiD9pQ2WeP6WNmGxpZ6ou3mdVFJX",
"/ip4/99.81.170.167/tcp/12019/p2p/QmRVbTpEYup8dSaURZfF6ByrMTSKa4UyUzJhSjahFzRqNj", // "/ip4/99.81.170.167/tcp/12019/p2p/QmRVbTpEYup8dSaURZfF6ByrMTSKa4UyUzJhSjahFzRqNj",
}, // },
DNSZone: "t.hmny.io", // DNSZone: "t.hmny.io",
DNSPort: 9000, // DNSPort: 9000,
}, // },
Consensus: consensusConfig{ // Consensus: consensusConfig{
DelayCommit: 0, // DelayCommit: 0,
BlockTime: 8 * time.Second, // BlockTime: 8 * time.Second,
}, // },
BLSKey: blsConfig{ // BLSKey: blsConfig{
KeyDir: "./.hmy/blskeys", // KeyDir: "./.hmy/blskeys",
KeyFiles: []string{"./xxxx.key"}, // KeyFiles: []string{"./xxxx.key"},
PassSrcType: "auto", // PassSrcType: "auto",
PassFile: "pass.file", // PassFile: "pass.file",
SavePassphrase: true, // SavePassphrase: true,
KmsConfigSrcType: "shared", // KmsConfigSrcType: "shared",
KmsConfigFile: "config.json", // KmsConfigFile: "config.json",
}, // },
TxPool: txPoolConfig{ // TxPool: txPoolConfig{
BlacklistFile: ".hmy/blacklist.txt", // BlacklistFile: ".hmy/blacklist.txt",
BroadcastInvalidTx: false, // BroadcastInvalidTx: false,
}, // },
Storage: storageConfig{ // Storage: storageConfig{
IsArchival: false, // IsArchival: false,
DatabaseDir: "./", // DatabaseDir: "./",
}, // },
Pprof: pprofConfig{ // Pprof: pprofConfig{
Enabled: true, // Enabled: true,
ListenAddr: "localhost:6060", // ListenAddr: "localhost:6060",
}, // },
Log: logConfig{ // Log: logConfig{
LogFolder: "latest", // LogFolder: "latest",
LogMaxSize: 100, // LogMaxSize: 100,
}, // },
Devnet: nil, // Devnet: nil,
} //}
//
var devnetHmyConfig = hmyConfig{ //var devnetHmyConfig = hmyConfig{
Network: networkConfig{ // NetworkType: networkConfig{
Network: "devnet", // NetworkType: "devnet",
BootNodes: []string{}, // BootNodes: []string{},
}, // },
Devnet: &devnetConfig{ // Devnet: &devnetConfig{
NumShards: 2, // NumShards: 2,
}, // },
BLSKey: blsConfig{ // BLSKey: blsConfig{
KeyFiles: []string{}, // KeyFiles: []string{},
}, // },
} //}
//
var testBaseDir = filepath.Join(os.TempDir(), "harmony", "cmd", "harmony") //var testBaseDir = filepath.Join(os.TempDir(), "harmony", "cmd", "harmony")
//
func init() { //func init() {
if _, err := os.Stat(testBaseDir); os.IsNotExist(err) { // if _, err := os.Stat(testBaseDir); os.IsNotExist(err) {
os.MkdirAll(testBaseDir, 0777) // os.MkdirAll(testBaseDir, 0777)
} // }
} //}
//
func TestPersistConfig(t *testing.T) { //func TestPersistConfig(t *testing.T) {
testDir := filepath.Join(testBaseDir, t.Name()) // testDir := filepath.Join(testBaseDir, t.Name())
os.RemoveAll(testDir) // os.RemoveAll(testDir)
os.MkdirAll(testDir, 0777) // os.MkdirAll(testDir, 0777)
//
tests := []struct { // tests := []struct {
config hmyConfig // config hmyConfig
}{ // }{
{ // {
config: testHmyConfig, // config: testHmyConfig,
}, // },
{ // {
config: devnetHmyConfig, // config: devnetHmyConfig,
}, // },
} // }
for i, test := range tests { // for i, test := range tests {
file := filepath.Join(testDir, fmt.Sprintf("%d.conf", i)) // file := filepath.Join(testDir, fmt.Sprintf("%d.conf", i))
//
if err := writeConfigToFile(test.config, file); err != nil { // if err := writeConfigToFile(test.config, file); err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
config, err := loadConfig(file) // config, err := loadConfig(file)
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
if !reflect.DeepEqual(config, test.config) { // if !reflect.DeepEqual(config, test.config) {
t.Errorf("Test %v: unexpected config \n\t%+v \n\t%+v", i, config, test.config) // t.Errorf("Test %v: unexpected config \n\t%+v \n\t%+v", i, config, test.config)
} // }
} // }
} //}

@ -18,7 +18,6 @@ import (
"time" "time"
"github.com/spf13/cobra" "github.com/spf13/cobra"
ethCommon "github.com/ethereum/go-ethereum/common" ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/bls/ffi/go/bls"

@ -0,0 +1,46 @@
package main
import "github.com/spf13/cobra"
var networkFlags *networkCmdFlags
type networkCmdFlags struct {
networkType string
bootNodes []string
dnsZone string
dnsPort int
// legacy flags
legacyDNSZone string
legacyDNSPort string
legacyDNSFlag bool
legacyNetworkType string
}
func registerNetworkFlags(cmd *cobra.Command) error {
flags := cmd.Flags()
flags.StringVarP(&networkFlags.networkType, "network", "n", "mainnet", "network to join (mainnet, testnet, pangaea, localnet, partner, stressnet, devnet")
flags.StringSliceVarP(&networkFlags.bootNodes, "bootnodes", "", []string{}, "a list of bootnode multiaddress (delimited by ,)")
flags.StringVarP(&networkFlags.dnsZone, "dns-zone", "", "", "use peers from the zone for state syncing")
flags.IntVarP(&networkFlags.dnsPort, "dns-port", "", 9000, "port of dns node")
flags.StringVarP(&networkFlags.legacyDNSZone, "dns_zone", "", "", "use peers from the zone for state syncing (deprecated: use dns-zone)")
flags.StringVarP(&networkFlags.legacyDNSPort, "dns_port", "", "", "port of dns node (deprecated: use --dns-port)")
flags.BoolVarP(&networkFlags.legacyDNSFlag, "dns", "", true, "deprecated: equivalent to -dns_zone t.hmny.io")
flags.StringVarP(&networkFlags.legacyNetworkType, "network_type", "", "", "network to join (deprecated: use --network-type)")
legacyFlags := []string{"dns_zone", "dns_port", "dns", "network_type"}
for _, legacyFlag := range legacyFlags {
if err := flags.MarkHidden(legacyFlag); err != nil {
return err
}
}
return nil
}
func parseNetworkFlagToConfig(cmd *cobra.Command, cfg *parsedConfig) error {
flags := cmd.Flags()
if flags.Changed("network_type") {
cfg.Network.NetworkType = flags.GetBool()
}
}
Loading…
Cancel
Save