11 log "github.com/sirupsen/logrus"
13 "github.com/vapor/crypto/ed25519"
17 // CommonConfig means config object
22 // Top level options use an anonymous struct
23 BaseConfig `mapstructure:",squash"`
24 // Options for services
25 P2P *P2PConfig `mapstructure:"p2p"`
26 Wallet *WalletConfig `mapstructure:"wallet"`
27 Auth *RPCAuthConfig `mapstructure:"auth"`
28 Web *WebConfig `mapstructure:"web"`
29 Simd *SimdConfig `mapstructure:"simd"`
30 Websocket *WebsocketConfig `mapstructure:"ws"`
33 // Default configurable parameters.
34 func DefaultConfig() *Config {
36 BaseConfig: DefaultBaseConfig(),
37 P2P: DefaultP2PConfig(),
38 Wallet: DefaultWalletConfig(),
39 Auth: DefaultRPCAuthConfig(),
40 Web: DefaultWebConfig(),
41 Simd: DefaultSimdConfig(),
42 Websocket: DefaultWebsocketConfig(),
46 // Set the RootDir for all Config structs
47 func (cfg *Config) SetRoot(root string) *Config {
48 cfg.BaseConfig.RootDir = root
52 // NodeKey retrieves the currently configured private key of the node, checking
53 // first any manually set key, falling back to the one found in the configured
54 // data folder. If no key can be found, a new one is generated.
55 func (cfg *Config) NodeKey() (string, error) {
56 // Use any specifically configured key.
57 if cfg.P2P.PrivateKey != "" {
58 return cfg.P2P.PrivateKey, nil
61 keyFile := rootify(cfg.P2P.NodeKeyFile, cfg.BaseConfig.RootDir)
62 buf := make([]byte, ed25519.PrivateKeySize*2)
63 fd, err := os.Open(keyFile)
66 if _, err = io.ReadFull(fd, buf); err == nil {
67 return string(buf), nil
71 log.WithField("err", err).Warning("key file access failed")
72 _, privKey, err := ed25519.GenerateKey(nil)
77 if err = ioutil.WriteFile(keyFile, []byte(privKey.String()), 0600); err != nil {
80 return privKey.String(), nil
83 //-----------------------------------------------------------------------------
85 type BaseConfig struct {
86 // The root directory for all data.
87 // This should be set in viper so it can unmarshal into this struct
88 RootDir string `mapstructure:"home"`
90 //The alias of the node
91 NodeAlias string `mapstructure:"node_alias"`
93 //The ID of the network to json
94 ChainID string `mapstructure:"chain_id"`
97 LogLevel string `mapstructure:"log_level"`
99 // A custom human readable name for this node
100 Moniker string `mapstructure:"moniker"`
102 // TCP or UNIX socket address for the profiling server to listen on
103 ProfListenAddress string `mapstructure:"prof_laddr"`
105 Mining bool `mapstructure:"mining"`
107 // Database backend: leveldb | memdb
108 DBBackend string `mapstructure:"db_backend"`
110 // Database directory
111 DBPath string `mapstructure:"db_dir"`
113 // Keystore directory
114 KeysPath string `mapstructure:"keys_dir"`
116 ApiAddress string `mapstructure:"api_addr"`
118 VaultMode bool `mapstructure:"vault_mode"`
121 LogFile string `mapstructure:"log_file"`
124 // Default configurable base parameters.
125 func DefaultBaseConfig() BaseConfig {
127 Moniker: "anonymous",
128 ProfListenAddress: "",
130 DBBackend: "leveldb",
132 KeysPath: "keystore",
137 func (b BaseConfig) DBDir() string {
138 return rootify(b.DBPath, b.RootDir)
141 func (b BaseConfig) KeysDir() string {
142 return rootify(b.KeysPath, b.RootDir)
146 type P2PConfig struct {
147 ListenAddress string `mapstructure:"laddr"`
148 Seeds string `mapstructure:"seeds"`
149 PrivateKey string `mapstructure:"node_key"`
150 NodeKeyFile string `mapstructure:"node_key_file"`
151 SkipUPNP bool `mapstructure:"skip_upnp"`
152 LANDiscover bool `mapstructure:"lan_discoverable"`
153 MaxNumPeers int `mapstructure:"max_num_peers"`
154 HandshakeTimeout int `mapstructure:"handshake_timeout"`
155 DialTimeout int `mapstructure:"dial_timeout"`
156 ProxyAddress string `mapstructure:"proxy_address"`
157 ProxyUsername string `mapstructure:"proxy_username"`
158 ProxyPassword string `mapstructure:"proxy_password"`
159 KeepDial string `mapstructure:"keep_dial"`
162 // Default configurable p2p parameters.
163 func DefaultP2PConfig() *P2PConfig {
165 ListenAddress: "tcp://0.0.0.0:46656",
166 NodeKeyFile: "nodekey",
170 HandshakeTimeout: 30,
178 //-----------------------------------------------------------------------------
179 type WalletConfig struct {
180 Disable bool `mapstructure:"disable"`
181 Rescan bool `mapstructure:"rescan"`
182 TxIndex bool `mapstructure:"txindex"`
183 MaxTxFee uint64 `mapstructure:"max_tx_fee"`
186 type RPCAuthConfig struct {
187 Disable bool `mapstructure:"disable"`
190 type WebConfig struct {
191 Closed bool `mapstructure:"closed"`
194 type SimdConfig struct {
195 Enable bool `mapstructure:"enable"`
198 type WebsocketConfig struct {
199 MaxNumWebsockets int `mapstructure:"max_num_websockets"`
200 MaxNumConcurrentReqs int `mapstructure:"max_num_concurrent_reqs"`
203 // Default configurable rpc's auth parameters.
204 func DefaultRPCAuthConfig() *RPCAuthConfig {
205 return &RPCAuthConfig{
210 // Default configurable web parameters.
211 func DefaultWebConfig() *WebConfig {
217 // Default configurable wallet parameters.
218 func DefaultWalletConfig() *WalletConfig {
219 return &WalletConfig{
223 MaxTxFee: uint64(1000000000),
227 // Default configurable web parameters.
228 func DefaultSimdConfig() *SimdConfig {
234 func DefaultWebsocketConfig() *WebsocketConfig {
235 return &WebsocketConfig{
236 MaxNumWebsockets: 25,
237 MaxNumConcurrentReqs: 20,
241 //-----------------------------------------------------------------------------
244 // helper function to make config creation independent of root dir
245 func rootify(path, root string) string {
246 if filepath.IsAbs(path) {
249 return filepath.Join(root, path)
252 // DefaultDataDir is the default data directory to use for the databases and other
253 // persistence requirements.
254 func DefaultDataDir() string {
255 // Try to place the data folder in the user's home dir
260 switch runtime.GOOS {
262 // In order to be compatible with old data path,
263 // copy the data from the old path to the new path
264 oldPath := filepath.Join(home, "Library", "Bytom")
265 newPath := filepath.Join(home, "Library", "Application Support", "Bytom")
266 if !isFolderNotExists(oldPath) && isFolderNotExists(newPath) {
267 if err := os.Rename(oldPath, newPath); err != nil {
268 log.Errorf("DefaultDataDir: %v", err)
274 return filepath.Join(home, "AppData", "Roaming", "Bytom")
276 return filepath.Join(home, ".bytom")
280 func isFolderNotExists(path string) bool {
281 _, err := os.Stat(path)
282 return os.IsNotExist(err)
285 func homeDir() string {
286 if home := os.Getenv("HOME"); home != "" {
289 if usr, err := user.Current(); err == nil {