OSDN Git Service

Merge pull request #935 from Bytom/dev
[bytom/bytom.git] / config / config.go
index 7463992..5fe31e6 100644 (file)
@@ -1,57 +1,50 @@
 package config
 
 import (
-       "fmt"
+       "os"
+       "os/user"
        "path/filepath"
+       "runtime"
        "time"
 )
 
 type Config struct {
        // Top level options use an anonymous struct
        BaseConfig `mapstructure:",squash"`
-       ChainConfig 
        // Options for services
-       RPC    *RPCConfig    `mapstructure:"rpc"`
-       P2P    *P2PConfig    `mapstructure:"p2p"`
-       Wallet *WalletConfig `mapstructure:"wallet"`
+       P2P    *P2PConfig     `mapstructure:"p2p"`
+       Wallet *WalletConfig  `mapstructure:"wallet"`
+       Auth   *RPCAuthConfig `mapstructure:"auth"`
+       Web    *WebConfig     `mapstructure:"web"`
 }
 
+// Default configurable parameters.
 func DefaultConfig() *Config {
        return &Config{
                BaseConfig: DefaultBaseConfig(),
-               RPC:        DefaultRPCConfig(),
                P2P:        DefaultP2PConfig(),
                Wallet:     DefaultWalletConfig(),
-       }
-}
-
-func TestConfig() *Config {
-       return &Config{
-               BaseConfig: TestBaseConfig(),
-               RPC:        TestRPCConfig(),
-               P2P:        TestP2PConfig(),
-               Wallet:     TestWalletConfig(),
+               Auth:       DefaultRPCAuthConfig(),
+               Web:        DefaultWebConfig(),
        }
 }
 
 // Set the RootDir for all Config structs
 func (cfg *Config) SetRoot(root string) *Config {
        cfg.BaseConfig.RootDir = root
-       cfg.RPC.RootDir = root
        cfg.P2P.RootDir = root
        return cfg
 }
 
 //-----------------------------------------------------------------------------
 // BaseConfig
-
 type BaseConfig struct {
        // The root directory for all data.
        // This should be set in viper so it can unmarshal into this struct
        RootDir string `mapstructure:"home"`
 
-       // A JSON file containing the initial validator set and other meta data
-       Genesis string `mapstructure:"genesis_file"`
+       //The ID of the network to json
+       ChainID string `mapstructure:"chain_id"`
 
        // A JSON file containing the private key to use as a validator in the consensus protocol
        PrivateKey string `mapstructure:"private_key"`
@@ -59,9 +52,6 @@ type BaseConfig struct {
        // A custom human readable name for this node
        Moniker string `mapstructure:"moniker"`
 
-       // Output level for logging
-       LogLevel string `mapstructure:"log_level"`
-
        // TCP or UNIX socket address for the profiling server to listen on
        ProfListenAddress string `mapstructure:"prof_laddr"`
 
@@ -70,6 +60,8 @@ type BaseConfig struct {
        // and verifying their commits
        FastSync bool `mapstructure:"fast_sync"`
 
+       Mining bool `mapstructure:"mining"`
+
        FilterPeers bool `mapstructure:"filter_peers"` // false
 
        // What indexer to use for transactions
@@ -89,37 +81,31 @@ type BaseConfig struct {
 
        ApiAddress string `mapstructure:"api_addr"`
 
+       VaultMode bool `mapstructure:"vault_mode"`
+
        Time time.Time
+
+       // log file name
+       LogName string `mapstructure:"log_name"`
 }
 
+// Default configurable base parameters.
 func DefaultBaseConfig() BaseConfig {
        return BaseConfig{
-               Genesis:           "genesis.json",
                Moniker:           "anonymous",
-               LogLevel:          DefaultPackageLogLevels(),
                ProfListenAddress: "",
                FastSync:          true,
                FilterPeers:       false,
+               Mining:            false,
                TxIndex:           "kv",
                DBBackend:         "leveldb",
                DBPath:            "data",
                KeysPath:          "keystore",
                HsmUrl:            "",
+               LogName:           "bytom.log",
        }
 }
 
-func TestBaseConfig() BaseConfig {
-       conf := DefaultBaseConfig()
-       conf.ChainID = "bytom_test"
-       conf.FastSync = false
-       conf.DBBackend = "memdb"
-       return conf
-}
-
-func (b BaseConfig) GenesisFile() string {
-       return rootify(b.Genesis, b.RootDir)
-}
-
 func (b BaseConfig) DBDir() string {
        return rootify(b.DBPath, b.RootDir)
 }
@@ -128,120 +114,74 @@ func (b BaseConfig) KeysDir() string {
        return rootify(b.KeysPath, b.RootDir)
 }
 
-func DefaultLogLevel() string {
-       return "info"
+// P2PConfig
+type P2PConfig struct {
+       RootDir          string `mapstructure:"home"`
+       ListenAddress    string `mapstructure:"laddr"`
+       Seeds            string `mapstructure:"seeds"`
+       SkipUPNP         bool   `mapstructure:"skip_upnp"`
+       AddrBook         string `mapstructure:"addr_book_file"`
+       AddrBookStrict   bool   `mapstructure:"addr_book_strict"`
+       PexReactor       bool   `mapstructure:"pex"`
+       MaxNumPeers      int    `mapstructure:"max_num_peers"`
+       HandshakeTimeout int    `mapstructure:"handshake_timeout"`
+       DialTimeout      int    `mapstructure:"dial_timeout"`
+}
+
+// Default configurable p2p parameters.
+func DefaultP2PConfig() *P2PConfig {
+       return &P2PConfig{
+               ListenAddress:    "tcp://0.0.0.0:46656",
+               AddrBook:         "addrbook.json",
+               AddrBookStrict:   true,
+               SkipUPNP:         false,
+               MaxNumPeers:      50,
+               HandshakeTimeout: 30,
+               DialTimeout:      3,
+               PexReactor:       true,
+       }
 }
 
-func DefaultPackageLogLevels() string {
-       return fmt.Sprintf("state:info,*:%s", DefaultLogLevel())
+func (p *P2PConfig) AddrBookFile() string {
+       return rootify(p.AddrBook, p.RootDir)
 }
 
-
 //-----------------------------------------------------------------------------
-// ChainConfig
-
-type ChainConfig struct {
-       // The ID of the chain to join (should be signed with every transaction and vote)
-       ChainID string `mapstructure:"chain_id"`
-               // Human-readable part for Bech32 encoded segwit addresses, as defined
-       // in BIP 173.
-       Bech32HRPSegwit string `mapstructure:"bech32_hrp"`
-
+type WalletConfig struct {
+       Disable bool `mapstructure:"disable"`
+       Rescan  bool `mapstructure:"rescan"`
 }
 
-func DefaultChainConfig() *ChainConfig {
-       return &ChainConfig{
-               ChainID:     "bytom-main",
-               Bech32HRPSegwit: "bm"
-       }
+type RPCAuthConfig struct {
+       Disable bool `mapstructure:"disable"`
 }
 
-//-----------------------------------------------------------------------------
-// RPCConfig
-
-type RPCConfig struct {
-       RootDir string `mapstructure:"home"`
-
-       // TCP or UNIX socket address for the RPC server to listen on
-       ListenAddress string `mapstructure:"laddr"`
-
-       // TCP or UNIX socket address for the gRPC server to listen on
-       // NOTE: This server only supports /broadcast_tx_commit
-       GRPCListenAddress string `mapstructure:"grpc_laddr"`
-
-       // Activate unsafe RPC commands like /dial_seeds and /unsafe_flush_mempool
-       Unsafe bool `mapstructure:"unsafe"`
+type WebConfig struct {
+       Closed bool `mapstructure:"closed"`
 }
 
-func DefaultRPCConfig() *RPCConfig {
-       return &RPCConfig{
-               ListenAddress:     "tcp://0.0.0.0:46657",
-               GRPCListenAddress: "",
-               Unsafe:            false,
+// Default configurable rpc's auth parameters.
+func DefaultRPCAuthConfig() *RPCAuthConfig {
+       return &RPCAuthConfig{
+               Disable: false,
        }
 }
 
-func TestRPCConfig() *RPCConfig {
-       conf := DefaultRPCConfig()
-       conf.ListenAddress = "tcp://0.0.0.0:36657"
-       conf.GRPCListenAddress = "tcp://0.0.0.0:36658"
-       conf.Unsafe = true
-       return conf
-}
-
-//-----------------------------------------------------------------------------
-// P2PConfig
-
-type P2PConfig struct {
-       RootDir        string `mapstructure:"home"`
-       ListenAddress  string `mapstructure:"laddr"`
-       Seeds          string `mapstructure:"seeds"`
-       SkipUPNP       bool   `mapstructure:"skip_upnp"`
-       AddrBook       string `mapstructure:"addr_book_file"`
-       AddrBookStrict bool   `mapstructure:"addr_book_strict"`
-       PexReactor     bool   `mapstructure:"pex"`
-       MaxNumPeers    int    `mapstructure:"max_num_peers"`
-}
-
-func DefaultP2PConfig() *P2PConfig {
-       return &P2PConfig{
-               ListenAddress:  "tcp://0.0.0.0:46656",
-               AddrBook:       "addrbook.json",
-               AddrBookStrict: true,
-               SkipUPNP:       false,
-               MaxNumPeers:    50,
+// Default configurable web parameters.
+func DefaultWebConfig() *WebConfig {
+       return &WebConfig{
+               Closed: false,
        }
 }
 
-func TestP2PConfig() *P2PConfig {
-       conf := DefaultP2PConfig()
-       conf.ListenAddress = "tcp://0.0.0.0:36656"
-       conf.SkipUPNP = true
-       return conf
-}
-
-func (p *P2PConfig) AddrBookFile() string {
-       return rootify(p.AddrBook, p.RootDir)
-}
-
-//-----------------------------------------------------------------------------
-// WalletConfig
-
-type WalletConfig struct {
-       Enable bool `mapstructure:"enable"`
-}
-
+// Default configurable wallet parameters.
 func DefaultWalletConfig() *WalletConfig {
        return &WalletConfig{
-               Enable: false,
+               Disable: false,
+               Rescan:  false,
        }
 }
 
-func TestWalletConfig() *WalletConfig {
-       conf := DefaultWalletConfig()
-       return conf
-}
-
 //-----------------------------------------------------------------------------
 // Utils
 
@@ -252,3 +192,31 @@ func rootify(path, root string) string {
        }
        return filepath.Join(root, path)
 }
+
+// DefaultDataDir is the default data directory to use for the databases and other
+// persistence requirements.
+func DefaultDataDir() string {
+       // Try to place the data folder in the user's home dir
+       home := homeDir()
+       if home == "" {
+               return "./.bytom"
+       }
+       switch runtime.GOOS {
+       case "darwin":
+               return filepath.Join(home, "Library", "Bytom")
+       case "windows":
+               return filepath.Join(home, "AppData", "Roaming", "Bytom")
+       default:
+               return filepath.Join(home, ".bytom")
+       }
+}
+
+func homeDir() string {
+       if home := os.Getenv("HOME"); home != "" {
+               return home
+       }
+       if usr, err := user.Current(); err == nil {
+               return usr.HomeDir
+       }
+       return ""
+}