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

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