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 Websocket *WebsocketConfig `mapstructure:"ws"`
32 // Default configurable parameters.
33 func DefaultConfig() *Config {
35 BaseConfig: DefaultBaseConfig(),
36 P2P: DefaultP2PConfig(),
37 Wallet: DefaultWalletConfig(),
38 Auth: DefaultRPCAuthConfig(),
39 Web: DefaultWebConfig(),
40 Websocket: DefaultWebsocketConfig(),
44 // Set the RootDir for all Config structs
45 func (cfg *Config) SetRoot(root string) *Config {
46 cfg.BaseConfig.RootDir = root
50 // NodeKey retrieves the currently configured private key of the node, checking
51 // first any manually set key, falling back to the one found in the configured
52 // data folder. If no key can be found, a new one is generated.
53 func (cfg *Config) NodeKey() (string, error) {
54 // Use any specifically configured key.
55 if cfg.P2P.PrivateKey != "" {
56 return cfg.P2P.PrivateKey, nil
59 keyFile := rootify(cfg.P2P.NodeKeyFile, cfg.BaseConfig.RootDir)
60 buf := make([]byte, ed25519.PrivateKeySize*2)
61 fd, err := os.Open(keyFile)
64 if _, err = io.ReadFull(fd, buf); err == nil {
65 return string(buf), nil
69 log.WithField("err", err).Warning("key file access failed")
70 _, privKey, err := ed25519.GenerateKey(nil)
75 if err = ioutil.WriteFile(keyFile, []byte(privKey.String()), 0600); err != nil {
78 return privKey.String(), nil
81 //-----------------------------------------------------------------------------
83 type BaseConfig struct {
84 // The root directory for all data.
85 // This should be set in viper so it can unmarshal into this struct
86 RootDir string `mapstructure:"home"`
88 //The alias of the node
89 NodeAlias string `mapstructure:"node_alias"`
91 //The ID of the network to json
92 ChainID string `mapstructure:"chain_id"`
95 LogLevel string `mapstructure:"log_level"`
97 // A custom human readable name for this node
98 Moniker string `mapstructure:"moniker"`
100 // TCP or UNIX socket address for the profiling server to listen on
101 ProfListenAddress string `mapstructure:"prof_laddr"`
103 Mining bool `mapstructure:"mining"`
105 // Database backend: leveldb | memdb
106 DBBackend string `mapstructure:"db_backend"`
108 // Database directory
109 DBPath string `mapstructure:"db_dir"`
111 // Keystore directory
112 KeysPath string `mapstructure:"keys_dir"`
114 ApiAddress string `mapstructure:"api_addr"`
116 VaultMode bool `mapstructure:"vault_mode"`
119 LogFile string `mapstructure:"log_file"`
122 // Default configurable base parameters.
123 func DefaultBaseConfig() BaseConfig {
125 Moniker: "anonymous",
126 ProfListenAddress: "",
128 DBBackend: "leveldb",
130 KeysPath: "keystore",
135 func (b BaseConfig) DBDir() string {
136 return rootify(b.DBPath, b.RootDir)
139 func (b BaseConfig) KeysDir() string {
140 return rootify(b.KeysPath, b.RootDir)
144 type P2PConfig struct {
145 ListenAddress string `mapstructure:"laddr"`
146 Seeds string `mapstructure:"seeds"`
147 PrivateKey string `mapstructure:"node_key"`
148 NodeKeyFile string `mapstructure:"node_key_file"`
149 SkipUPNP bool `mapstructure:"skip_upnp"`
150 LANDiscover bool `mapstructure:"lan_discoverable"`
151 MaxNumPeers int `mapstructure:"max_num_peers"`
152 HandshakeTimeout int `mapstructure:"handshake_timeout"`
153 DialTimeout int `mapstructure:"dial_timeout"`
154 ProxyAddress string `mapstructure:"proxy_address"`
155 ProxyUsername string `mapstructure:"proxy_username"`
156 ProxyPassword string `mapstructure:"proxy_password"`
157 KeepDial string `mapstructure:"keep_dial"`
160 // Default configurable p2p parameters.
161 func DefaultP2PConfig() *P2PConfig {
163 ListenAddress: "tcp://0.0.0.0:46656",
164 NodeKeyFile: "nodekey",
168 HandshakeTimeout: 30,
176 //-----------------------------------------------------------------------------
177 type WalletConfig struct {
178 Disable bool `mapstructure:"disable"`
179 Rescan bool `mapstructure:"rescan"`
180 TxIndex bool `mapstructure:"txindex"`
181 MaxTxFee uint64 `mapstructure:"max_tx_fee"`
184 type RPCAuthConfig struct {
185 Disable bool `mapstructure:"disable"`
188 type WebConfig struct {
189 Closed bool `mapstructure:"closed"`
192 type WebsocketConfig struct {
193 MaxNumWebsockets int `mapstructure:"max_num_websockets"`
194 MaxNumConcurrentReqs int `mapstructure:"max_num_concurrent_reqs"`
197 // Default configurable rpc's auth parameters.
198 func DefaultRPCAuthConfig() *RPCAuthConfig {
199 return &RPCAuthConfig{
204 // Default configurable web parameters.
205 func DefaultWebConfig() *WebConfig {
211 // Default configurable wallet parameters.
212 func DefaultWalletConfig() *WalletConfig {
213 return &WalletConfig{
217 MaxTxFee: uint64(1000000000),
221 func DefaultWebsocketConfig() *WebsocketConfig {
222 return &WebsocketConfig{
223 MaxNumWebsockets: 25,
224 MaxNumConcurrentReqs: 20,
228 //-----------------------------------------------------------------------------
231 // helper function to make config creation independent of root dir
232 func rootify(path, root string) string {
233 if filepath.IsAbs(path) {
236 return filepath.Join(root, path)
239 // DefaultDataDir is the default data directory to use for the databases and other
240 // persistence requirements.
241 func DefaultDataDir() string {
242 // Try to place the data folder in the user's home dir
247 switch runtime.GOOS {
249 // In order to be compatible with old data path,
250 // copy the data from the old path to the new path
251 oldPath := filepath.Join(home, "Library", "Bytom")
252 newPath := filepath.Join(home, "Library", "Application Support", "Bytom")
253 if !isFolderNotExists(oldPath) && isFolderNotExists(newPath) {
254 if err := os.Rename(oldPath, newPath); err != nil {
255 log.Errorf("DefaultDataDir: %v", err)
261 return filepath.Join(home, "AppData", "Roaming", "Bytom")
263 return filepath.Join(home, ".bytom")
267 func isFolderNotExists(path string) bool {
268 _, err := os.Stat(path)
269 return os.IsNotExist(err)
272 func homeDir() string {
273 if home := os.Getenv("HOME"); home != "" {
276 if usr, err := user.Current(); err == nil {