OSDN Git Service

V0.1 pk interface (#58)
[bytom/vapor.git] / config / config.go
index be5dce6..3b4b28b 100644 (file)
@@ -1,13 +1,16 @@
 package config
 
 import (
+       "io"
+       "io/ioutil"
        "os"
        "os/user"
        "path/filepath"
        "runtime"
 
        log "github.com/sirupsen/logrus"
-       "github.com/vapor/common"
+
+       "github.com/vapor/crypto/ed25519"
 )
 
 var (
@@ -19,14 +22,11 @@ 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"`
-       Side      *SideChainConfig    `mapstructure:"side"`
-       MainChain *MainChainRpcConfig `mapstructure:"mainchain"`
-       Websocket *WebsocketConfig    `mapstructure:"ws"`
-       Consensus *ConsensusConfig    `mapstructure:"consensus"`
+       P2P       *P2PConfig       `mapstructure:"p2p"`
+       Wallet    *WalletConfig    `mapstructure:"wallet"`
+       Auth      *RPCAuthConfig   `mapstructure:"auth"`
+       Web       *WebConfig       `mapstructure:"web"`
+       Websocket *WebsocketConfig `mapstructure:"ws"`
 }
 
 // Default configurable parameters.
@@ -37,10 +37,7 @@ func DefaultConfig() *Config {
                Wallet:     DefaultWalletConfig(),
                Auth:       DefaultRPCAuthConfig(),
                Web:        DefaultWebConfig(),
-               Side:       DefaultSideChainConfig(),
-               MainChain:  DefaultMainChainRpc(),
                Websocket:  DefaultWebsocketConfig(),
-               Consensus:  DefaultConsensusCOnfig(),
        }
 }
 
@@ -50,6 +47,37 @@ func (cfg *Config) SetRoot(root string) *Config {
        return cfg
 }
 
+// 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
+       }
+
+       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
+               }
+       }
+
+       log.WithField("err", err).Warning("key file access failed")
+       _, privKey, err := ed25519.GenerateKey(nil)
+       if err != nil {
+               return "", err
+       }
+
+       if err = ioutil.WriteFile(keyFile, []byte(privKey.String()), 0600); err != nil {
+               return "", err
+       }
+       return privKey.String(), nil
+}
+
 //-----------------------------------------------------------------------------
 // BaseConfig
 type BaseConfig struct {
@@ -57,6 +85,9 @@ type BaseConfig struct {
        // This should be set in viper so it can unmarshal into this struct
        RootDir string `mapstructure:"home"`
 
+       //The alias of the node
+       NodeAlias string `mapstructure:"node_alias"`
+
        //The ID of the network to json
        ChainID string `mapstructure:"chain_id"`
 
@@ -87,13 +118,8 @@ type BaseConfig struct {
        // log file name
        LogFile string `mapstructure:"log_file"`
 
-       // Validate pegin proof by checking bytom transaction inclusion in mainchain.
-       ValidatePegin bool   `mapstructure:"validate_pegin"`
-       Signer        string `mapstructure:"signer"`
-
-       ConsensusConfigFile string `mapstructure:"consensus_config_file"`
-
-       IpfsAddress string `mapstructure:"ipfs_addr"`
+       // Cipher Service Provider
+       // CipherServiceProvider string `mapstructure:"csp"`
 }
 
 // Default configurable base parameters.
@@ -105,7 +131,8 @@ func DefaultBaseConfig() BaseConfig {
                DBBackend:         "leveldb",
                DBPath:            "data",
                KeysPath:          "keystore",
-               IpfsAddress:       "127.0.0.1:5001",
+               NodeAlias:         "",
+               // CipherServiceProvider: "ed25519",
        }
 }
 
@@ -121,20 +148,26 @@ func (b BaseConfig) KeysDir() string {
 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"`
        HandshakeTimeout int    `mapstructure:"handshake_timeout"`
        DialTimeout      int    `mapstructure:"dial_timeout"`
        ProxyAddress     string `mapstructure:"proxy_address"`
        ProxyUsername    string `mapstructure:"proxy_username"`
        ProxyPassword    string `mapstructure:"proxy_password"`
+       KeepDial         string `mapstructure:"keep_dial"`
 }
 
 // Default configurable p2p parameters.
 func DefaultP2PConfig() *P2PConfig {
        return &P2PConfig{
                ListenAddress:    "tcp://0.0.0.0:46656",
+               NodeKeyFile:      "nodekey",
                SkipUPNP:         false,
+               LANDiscover:      true,
                MaxNumPeers:      50,
                HandshakeTimeout: 30,
                DialTimeout:      3,
@@ -148,6 +181,7 @@ func DefaultP2PConfig() *P2PConfig {
 type WalletConfig struct {
        Disable  bool   `mapstructure:"disable"`
        Rescan   bool   `mapstructure:"rescan"`
+       TxIndex  bool   `mapstructure:"txindex"`
        MaxTxFee uint64 `mapstructure:"max_tx_fee"`
 }
 
@@ -159,47 +193,11 @@ type WebConfig struct {
        Closed bool `mapstructure:"closed"`
 }
 
-type SideChainConfig struct {
-       FedpegXPubs            string `mapstructure:"fedpeg_xpubs"`
-       SignBlockXPubs         string `mapstructure:"sign_block_xpubs"`
-       PeginMinDepth          uint64 `mapstructure:"pegin_confirmation_depth"`
-       ParentGenesisBlockHash string `mapstructure:"parent_genesis_block_hash"`
-}
-
-type MainChainRpcConfig struct {
-       MainchainRpcHost string `mapstructure:"mainchain_rpc_host"`
-       MainchainRpcPort string `mapstructure:"mainchain_rpc_port"`
-       MainchainToken   string `mapstructure:"mainchain_rpc_token"`
-}
-
 type WebsocketConfig struct {
        MaxNumWebsockets     int `mapstructure:"max_num_websockets"`
        MaxNumConcurrentReqs int `mapstructure:"max_num_concurrent_reqs"`
 }
 
-type ConsensusConfig struct {
-       Type             string   `mapstructure:"consensus_type"`
-       Period           uint64   `json:"period"`            // Number of seconds between blocks to enforce
-       MaxSignerCount   uint64   `json:"max_signers_count"` // Max count of signers
-       MinVoterBalance  uint64   `json:"min_boter_balance"` // Min voter balance to valid this vote
-       GenesisTimestamp uint64   `json:"genesis_timestamp"` // The LoopStartTime of first Block
-       Coinbase         string   `json:"coinbase"`
-       XPrv             string   `json:"xprv"`
-       SelfVoteSigners  []string `json:"signers"` // Signers vote by themselves to seal the block, make sure the signer accounts are pre-funded
-       Signers          []common.Address
-}
-
-type DposConfig struct {
-       Period           uint64   `json:"period"`            // Number of seconds between blocks to enforce
-       MaxSignerCount   uint64   `json:"max_signers_count"` // Max count of signers
-       MinVoterBalance  uint64   `json:"min_boter_balance"` // Min voter balance to valid this vote
-       GenesisTimestamp uint64   `json:"genesis_timestamp"` // The LoopStartTime of first Block
-       Coinbase         string   `json:"coinbase"`
-       XPrv             string   `json:"xprv"`
-       SelfVoteSigners  []string `json:"signers"` // Signers vote by themselves to seal the block, make sure the signer accounts are pre-funded
-       Signers          []common.Address
-}
-
 // Default configurable rpc's auth parameters.
 func DefaultRPCAuthConfig() *RPCAuthConfig {
        return &RPCAuthConfig{
@@ -219,25 +217,11 @@ func DefaultWalletConfig() *WalletConfig {
        return &WalletConfig{
                Disable:  false,
                Rescan:   false,
+               TxIndex:  false,
                MaxTxFee: uint64(1000000000),
        }
 }
 
-// DeafultSideChainConfig for sidechain
-func DefaultSideChainConfig() *SideChainConfig {
-       return &SideChainConfig{
-               PeginMinDepth:          6,
-               ParentGenesisBlockHash: "a75483474799ea1aa6bb910a1a5025b4372bf20bef20f246a2c2dc5e12e8a053",
-       }
-}
-
-func DefaultMainChainRpc() *MainChainRpcConfig {
-       return &MainChainRpcConfig{
-               MainchainRpcHost: "127.0.0.1",
-               MainchainRpcPort: "9888",
-       }
-}
-
 func DefaultWebsocketConfig() *WebsocketConfig {
        return &WebsocketConfig{
                MaxNumWebsockets:     25,
@@ -245,24 +229,6 @@ func DefaultWebsocketConfig() *WebsocketConfig {
        }
 }
 
-func DefaultDposConfig() *DposConfig {
-       return &DposConfig{
-               Period:           1,
-               MaxSignerCount:   1,
-               MinVoterBalance:  0,
-               GenesisTimestamp: 1524549600,
-       }
-}
-
-func DefaultConsensusCOnfig() *ConsensusConfig {
-       return &ConsensusConfig{
-               Type:             "dpos",
-               Period:           1,
-               MaxSignerCount:   1,
-               MinVoterBalance:  0,
-               GenesisTimestamp: 1524549600}
-}
-
 //-----------------------------------------------------------------------------
 // Utils
 
@@ -280,14 +246,14 @@ func DefaultDataDir() string {
        // Try to place the data folder in the user's home dir
        home := homeDir()
        if home == "" {
-               return "./.bytom_sidechain"
+               return "./.bytom"
        }
        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", "Bytom_sidechain")
-               newPath := filepath.Join(home, "Library", "Application Support", "Bytom_sidechain")
+               oldPath := filepath.Join(home, "Library", "Bytom")
+               newPath := filepath.Join(home, "Library", "Application Support", "Bytom")
                if !isFolderNotExists(oldPath) && isFolderNotExists(newPath) {
                        if err := os.Rename(oldPath, newPath); err != nil {
                                log.Errorf("DefaultDataDir: %v", err)
@@ -296,9 +262,9 @@ func DefaultDataDir() string {
                }
                return newPath
        case "windows":
-               return filepath.Join(home, "AppData", "Roaming", "Bytom_sidechain")
+               return filepath.Join(home, "AppData", "Roaming", "Bytom")
        default:
-               return filepath.Join(home, ".bytom_sidechain")
+               return filepath.Join(home, ".bytom")
        }
 }