OSDN Git Service

fix fed membership order (#328)
[bytom/vapor.git] / config / config.go
index c04662f..8b5ede8 100644 (file)
@@ -1,8 +1,8 @@
 package config
 
 import (
+       "encoding/hex"
        "io"
-       "io/ioutil"
        "os"
        "os/user"
        "path/filepath"
@@ -10,7 +10,6 @@ import (
 
        log "github.com/sirupsen/logrus"
 
-       "github.com/vapor/crypto/ed25519"
        "github.com/vapor/crypto/ed25519/chainkd"
 )
 
@@ -53,32 +52,32 @@ func (cfg *Config) SetRoot(root string) *Config {
 // NodeKey retrieves the currently configured private key of the node, checking
 // first any manually set key, falling back to the one found in the configured
 // data folder. If no key can be found, a new one is generated.
-func (cfg *Config) NodeKey() (string, error) {
-       // Use any specifically configured key.
-       if cfg.P2P.PrivateKey != "" {
-               return cfg.P2P.PrivateKey, nil
+func (cfg *Config) PrivateKey() *chainkd.XPrv {
+       if cfg.XPrv != nil {
+               return cfg.XPrv
        }
 
-       keyFile := rootify(cfg.P2P.NodeKeyFile, cfg.BaseConfig.RootDir)
-       buf := make([]byte, ed25519.PrivateKeySize*2)
-       fd, err := os.Open(keyFile)
-       defer fd.Close()
-       if err == nil {
-               if _, err = io.ReadFull(fd, buf); err == nil {
-                       return string(buf), nil
-               }
+       filePath := rootify(cfg.PrivateKeyFile, cfg.BaseConfig.RootDir)
+       fildReader, err := os.Open(filePath)
+       if err != nil {
+               log.WithField("err", err).Panic("fail on open private key file")
        }
 
-       log.WithField("err", err).Warning("key file access failed")
-       _, privKey, err := ed25519.GenerateKey(nil)
-       if err != nil {
-               return "", err
+       defer fildReader.Close()
+       buf := make([]byte, 128)
+       if _, err = io.ReadFull(fildReader, buf); err != nil {
+               log.WithField("err", err).Panic("fail on read private key file")
        }
 
-       if err = ioutil.WriteFile(keyFile, []byte(privKey.String()), 0600); err != nil {
-               return "", err
+       var xprv chainkd.XPrv
+       if _, err := hex.Decode(xprv[:], buf); err != nil {
+               log.WithField("err", err).Panic("fail on decode private key")
        }
-       return privKey.String(), nil
+
+       cfg.XPrv = &xprv
+       xpub := cfg.XPrv.XPub()
+       cfg.XPub = &xpub
+       return cfg.XPrv
 }
 
 //-----------------------------------------------------------------------------
@@ -118,19 +117,25 @@ type BaseConfig struct {
        // log file name
        LogFile string `mapstructure:"log_file"`
 
-       // Cipher Service Provider
-       // CipherServiceProvider string `mapstructure:"csp"`
+       PrivateKeyFile string `mapstructure:"private_key_file"`
+       XPrv           *chainkd.XPrv
+       XPub           *chainkd.XPub
+
+       // Federation file name
+       FederationFileName string `mapstructure:"federation_file"`
 }
 
 // Default configurable base parameters.
 func DefaultBaseConfig() BaseConfig {
        return BaseConfig{
-               Moniker:           "anonymous",
-               ProfListenAddress: "",
-               Mining:            false,
-               DBBackend:         "leveldb",
-               DBPath:            "data",
-               KeysPath:          "keystore",
+               Moniker:            "anonymous",
+               ProfListenAddress:  "",
+               Mining:             false,
+               DBBackend:          "leveldb",
+               DBPath:             "data",
+               KeysPath:           "keystore",
+               PrivateKeyFile:     "node_key.txt",
+               FederationFileName: "federation.json",
        }
 }
 
@@ -142,12 +147,14 @@ func (b BaseConfig) KeysDir() string {
        return rootify(b.KeysPath, b.RootDir)
 }
 
+func (b BaseConfig) FederationFile() string {
+       return rootify(b.FederationFileName, b.RootDir)
+}
+
 // P2PConfig
 type P2PConfig struct {
        ListenAddress    string `mapstructure:"laddr"`
        Seeds            string `mapstructure:"seeds"`
-       PrivateKey       string `mapstructure:"node_key"`
-       NodeKeyFile      string `mapstructure:"node_key_file"`
        SkipUPNP         bool   `mapstructure:"skip_upnp"`
        LANDiscover      bool   `mapstructure:"lan_discoverable"`
        MaxNumPeers      int    `mapstructure:"max_num_peers"`
@@ -157,21 +164,22 @@ type P2PConfig struct {
        ProxyUsername    string `mapstructure:"proxy_username"`
        ProxyPassword    string `mapstructure:"proxy_password"`
        KeepDial         string `mapstructure:"keep_dial"`
+       Compression      string `mapstructure:"compression_backend"`
 }
 
 // Default configurable p2p parameters.
 func DefaultP2PConfig() *P2PConfig {
        return &P2PConfig{
-               ListenAddress:    "tcp://0.0.0.0:46656",
-               NodeKeyFile:      "nodekey",
+               ListenAddress:    "tcp://0.0.0.0:56656",
                SkipUPNP:         false,
                LANDiscover:      true,
-               MaxNumPeers:      50,
+               MaxNumPeers:      20,
                HandshakeTimeout: 30,
                DialTimeout:      3,
                ProxyAddress:     "",
                ProxyUsername:    "",
                ProxyPassword:    "",
+               Compression:      "snappy",
        }
 }
 
@@ -235,14 +243,12 @@ func DefaultWebsocketConfig() *WebsocketConfig {
 func DefaultFederationConfig() *FederationConfig {
        return &FederationConfig{
                Xpubs: []chainkd.XPub{
-                       xpub("7f23aae65ee4307c38d342699e328f21834488e18191ebd66823d220b5a58303496c9d09731784372bade78d5e9a4a6249b2cfe2e3a85464e5a4017aa5611e47"),
-                       xpub("585e20143db413e45fbc82f03cb61f177e9916ef1df0012daa8cbf6dbb1025ce8f98e51ae319327b63505b64fdbbf6d36ef916d79e6dd67d51b0bfe76fe544c5"),
-                       xpub("b58170b51ca61604028ba1cb412377dfc2bc6567c0afc84c83aae1c0c297d0227ccf568561df70851f4144bbf069b525129f2434133c145e35949375b22a6c9d"),
-                       xpub("983705ae71949c1a5d0fcf953658dd9ecc549f02c63e197b4d087ae31148097ece816bbc60d9012c316139fc550fa0f4b00beb0887f6b152f7a69bc8f392b9fa"),
-                       xpub("d72fb92fa13bf3e0deb39de3a47c8d6eef5584719f7877c82a4c009f78fddf924d9706d48f15b2c782ec80b6bdd621a1f7ba2a0044b0e6f92245de9436885cb9"),
-                       xpub("6798460919e8dc7095ee8b9f9d65033ef3da8c2334813149da5a1e52e9c6da07ba7d0e7379baaa0c8bdcb21890a54e6b7290bee077c645ee4b74b0c1ae9da59a"),
+                       xpub("580daf48fa8962100047cb1391da890bb7f2c849fdbc9b368cb4394a4c7cbb0977e2e7ebbf055dc0ef90af6a0d2af01ce7ec56b735d016aab597815ec48552e5"),
+                       xpub("f3f6bcf61b65fa9d1566455a5688ca8b395efdc22e654963134b5e5cb0a45d8be522d21abc384a73177a7b9d64eba915fcfe2862d86a508a3c46dc410bdd72ad"),
+                       xpub("53559612f2b7bcada18948b7de39d63947a0e2bd7336d07db1350c54ba5743996b84bf9d18ff7a2457e1a5c70ce5013e4a3b62666ddb03294c53051d5f5c70c0"),
+                       xpub("7c88cc58adfc71818b08308d43c29de22460b0ea6895449cbec6e458d7dc09e0aea243fa5075ee6621da0d805bd047f6bb207329c5bd2ca3253b172fb323b512"),
                },
-               Quorum: 4,
+               Quorum: 2,
        }
 }
 
@@ -274,17 +280,7 @@ func DefaultDataDir() string {
        }
        switch runtime.GOOS {
        case "darwin":
-               // In order to be compatible with old data path,
-               // copy the data from the old path to the new path
-               oldPath := filepath.Join(home, "Library", "Vapor")
-               newPath := filepath.Join(home, "Library", "Application Support", "Vapor")
-               if !isFolderNotExists(oldPath) && isFolderNotExists(newPath) {
-                       if err := os.Rename(oldPath, newPath); err != nil {
-                               log.Errorf("DefaultDataDir: %v", err)
-                               return oldPath
-                       }
-               }
-               return newPath
+               return filepath.Join(home, "Library", "Application Support", "Vapor")
        case "windows":
                return filepath.Join(home, "AppData", "Roaming", "Vapor")
        default: