9 log "github.com/sirupsen/logrus"
13 // CommonConfig means config object
18 // Top level options use an anonymous struct
19 BaseConfig `mapstructure:",squash"`
20 // Options for services
21 P2P *P2PConfig `mapstructure:"p2p"`
22 Wallet *WalletConfig `mapstructure:"wallet"`
23 Auth *RPCAuthConfig `mapstructure:"auth"`
24 Web *WebConfig `mapstructure:"web"`
25 Simd *SimdConfig `mapstructure:"simd"`
28 // Default configurable parameters.
29 func DefaultConfig() *Config {
31 BaseConfig: DefaultBaseConfig(),
32 P2P: DefaultP2PConfig(),
33 Wallet: DefaultWalletConfig(),
34 Auth: DefaultRPCAuthConfig(),
35 Web: DefaultWebConfig(),
36 Simd: DefaultSimdConfig(),
40 // Set the RootDir for all Config structs
41 func (cfg *Config) SetRoot(root string) *Config {
42 cfg.BaseConfig.RootDir = root
46 //-----------------------------------------------------------------------------
48 type BaseConfig struct {
49 // The root directory for all data.
50 // This should be set in viper so it can unmarshal into this struct
51 RootDir string `mapstructure:"home"`
53 //The ID of the network to json
54 ChainID string `mapstructure:"chain_id"`
57 LogLevel string `mapstructure:"log_level"`
59 // A custom human readable name for this node
60 Moniker string `mapstructure:"moniker"`
62 // TCP or UNIX socket address for the profiling server to listen on
63 ProfListenAddress string `mapstructure:"prof_laddr"`
65 Mining bool `mapstructure:"mining"`
67 // Database backend: leveldb | memdb
68 DBBackend string `mapstructure:"db_backend"`
71 DBPath string `mapstructure:"db_dir"`
74 KeysPath string `mapstructure:"keys_dir"`
76 ApiAddress string `mapstructure:"api_addr"`
78 VaultMode bool `mapstructure:"vault_mode"`
81 LogFile string `mapstructure:"log_file"`
84 // Default configurable base parameters.
85 func DefaultBaseConfig() BaseConfig {
88 ProfListenAddress: "",
96 func (b BaseConfig) DBDir() string {
97 return rootify(b.DBPath, b.RootDir)
100 func (b BaseConfig) KeysDir() string {
101 return rootify(b.KeysPath, b.RootDir)
105 type P2PConfig struct {
106 ListenAddress string `mapstructure:"laddr"`
107 Seeds string `mapstructure:"seeds"`
108 SkipUPNP bool `mapstructure:"skip_upnp"`
109 MaxNumPeers int `mapstructure:"max_num_peers"`
110 HandshakeTimeout int `mapstructure:"handshake_timeout"`
111 DialTimeout int `mapstructure:"dial_timeout"`
114 // Default configurable p2p parameters.
115 func DefaultP2PConfig() *P2PConfig {
117 ListenAddress: "tcp://0.0.0.0:46656",
120 HandshakeTimeout: 30,
125 //-----------------------------------------------------------------------------
126 type WalletConfig struct {
127 Disable bool `mapstructure:"disable"`
128 Rescan bool `mapstructure:"rescan"`
129 MaxTxFee uint64 `mapstructure:"max_tx_fee"`
132 type RPCAuthConfig struct {
133 Disable bool `mapstructure:"disable"`
136 type WebConfig struct {
137 Closed bool `mapstructure:"closed"`
140 type SimdConfig struct {
141 Enable bool `mapstructure:"enable"`
144 // Default configurable rpc's auth parameters.
145 func DefaultRPCAuthConfig() *RPCAuthConfig {
146 return &RPCAuthConfig{
151 // Default configurable web parameters.
152 func DefaultWebConfig() *WebConfig {
158 // Default configurable wallet parameters.
159 func DefaultWalletConfig() *WalletConfig {
160 return &WalletConfig{
163 MaxTxFee: uint64(1000000000),
167 // Default configurable web parameters.
168 func DefaultSimdConfig() *SimdConfig {
174 //-----------------------------------------------------------------------------
177 // helper function to make config creation independent of root dir
178 func rootify(path, root string) string {
179 if filepath.IsAbs(path) {
182 return filepath.Join(root, path)
185 // DefaultDataDir is the default data directory to use for the databases and other
186 // persistence requirements.
187 func DefaultDataDir() string {
188 // Try to place the data folder in the user's home dir
193 switch runtime.GOOS {
195 // In order to be compatible with old data path,
196 // copy the data from the old path to the new path
197 oldPath := filepath.Join(home, "Library", "Bytom")
198 newPath := filepath.Join(home, "Library", "Application Support", "Bytom")
199 if !isFolderNotExists(oldPath) && isFolderNotExists(newPath) {
200 if err := os.Rename(oldPath, newPath); err != nil {
201 log.Errorf("DefaultDataDir: %v", err)
207 return filepath.Join(home, "AppData", "Roaming", "Bytom")
209 return filepath.Join(home, ".bytom")
213 func isFolderNotExists(path string) bool {
214 _, err := os.Stat(path)
215 return os.IsNotExist(err)
218 func homeDir() string {
219 if home := os.Getenv("HOME"); home != "" {
222 if usr, err := user.Current(); err == nil {