OSDN Git Service

try to fix ban peer bug (#273)
[bytom/vapor.git] / config / config.go
index 69b4882..8a06077 100644 (file)
@@ -1,8 +1,8 @@
 package config
 
 import (
+       "encoding/hex"
        "io"
-       "io/ioutil"
        "os"
        "os/user"
        "path/filepath"
@@ -10,7 +10,7 @@ import (
 
        log "github.com/sirupsen/logrus"
 
-       "github.com/vapor/crypto/ed25519"
+       "github.com/vapor/crypto/ed25519/chainkd"
 )
 
 var (
@@ -22,11 +22,12 @@ type Config struct {
        // Top level options use an anonymous struct
        BaseConfig `mapstructure:",squash"`
        // Options for services
-       P2P       *P2PConfig       `mapstructure:"p2p"`
-       Wallet    *WalletConfig    `mapstructure:"wallet"`
-       Auth      *RPCAuthConfig   `mapstructure:"auth"`
-       Web       *WebConfig       `mapstructure:"web"`
-       Websocket *WebsocketConfig `mapstructure:"ws"`
+       P2P        *P2PConfig        `mapstructure:"p2p"`
+       Wallet     *WalletConfig     `mapstructure:"wallet"`
+       Auth       *RPCAuthConfig    `mapstructure:"auth"`
+       Web        *WebConfig        `mapstructure:"web"`
+       Websocket  *WebsocketConfig  `mapstructure:"ws"`
+       Federation *FederationConfig `mapstructure:"federation"`
 }
 
 // Default configurable parameters.
@@ -38,6 +39,7 @@ func DefaultConfig() *Config {
                Auth:       DefaultRPCAuthConfig(),
                Web:        DefaultWebConfig(),
                Websocket:  DefaultWebsocketConfig(),
+               Federation: DefaultFederationConfig(),
        }
 }
 
@@ -50,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
 }
 
 //-----------------------------------------------------------------------------
@@ -115,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",
        }
 }
 
@@ -139,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"`
@@ -154,13 +164,13 @@ 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",
                SkipUPNP:         false,
                LANDiscover:      true,
                MaxNumPeers:      50,
@@ -169,6 +179,7 @@ func DefaultP2PConfig() *P2PConfig {
                ProxyAddress:     "",
                ProxyUsername:    "",
                ProxyPassword:    "",
+               Compression:      "snappy",
        }
 }
 
@@ -193,6 +204,11 @@ type WebsocketConfig struct {
        MaxNumConcurrentReqs int `mapstructure:"max_num_concurrent_reqs"`
 }
 
+type FederationConfig struct {
+       Xpubs  []chainkd.XPub `json:"xpubs"`
+       Quorum int            `json:"quorum"`
+}
+
 // Default configurable rpc's auth parameters.
 func DefaultRPCAuthConfig() *RPCAuthConfig {
        return &RPCAuthConfig{
@@ -224,6 +240,22 @@ func DefaultWebsocketConfig() *WebsocketConfig {
        }
 }
 
+func DefaultFederationConfig() *FederationConfig {
+       return &FederationConfig{
+               Xpubs: []chainkd.XPub{
+                       xpub("50ef22b3a3fca7bc08916187cc9ec2f4005c9c6b1353aa1decbd4be3f3bb0fbe1967589f0d9dec13a388c0412002d2c267bdf3b920864e1ddc50581be5604ce1"),
+               },
+               Quorum: 1,
+       }
+}
+
+func xpub(str string) (xpub chainkd.XPub) {
+       if err := xpub.UnmarshalText([]byte(str)); err != nil {
+               log.Panicf("Fail converts a string to xpub")
+       }
+       return xpub
+}
+
 //-----------------------------------------------------------------------------
 // Utils
 
@@ -245,17 +277,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: