OSDN Git Service

Merge pull request #935 from Bytom/dev
[bytom/bytom.git] / config / config.go
1 package config
2
3 import (
4         "os"
5         "os/user"
6         "path/filepath"
7         "runtime"
8         "time"
9 )
10
11 type Config struct {
12         // Top level options use an anonymous struct
13         BaseConfig `mapstructure:",squash"`
14         // Options for services
15         P2P    *P2PConfig     `mapstructure:"p2p"`
16         Wallet *WalletConfig  `mapstructure:"wallet"`
17         Auth   *RPCAuthConfig `mapstructure:"auth"`
18         Web    *WebConfig     `mapstructure:"web"`
19 }
20
21 // Default configurable parameters.
22 func DefaultConfig() *Config {
23         return &Config{
24                 BaseConfig: DefaultBaseConfig(),
25                 P2P:        DefaultP2PConfig(),
26                 Wallet:     DefaultWalletConfig(),
27                 Auth:       DefaultRPCAuthConfig(),
28                 Web:        DefaultWebConfig(),
29         }
30 }
31
32 // Set the RootDir for all Config structs
33 func (cfg *Config) SetRoot(root string) *Config {
34         cfg.BaseConfig.RootDir = root
35         cfg.P2P.RootDir = root
36         return cfg
37 }
38
39 //-----------------------------------------------------------------------------
40 // BaseConfig
41 type BaseConfig struct {
42         // The root directory for all data.
43         // This should be set in viper so it can unmarshal into this struct
44         RootDir string `mapstructure:"home"`
45
46         //The ID of the network to json
47         ChainID string `mapstructure:"chain_id"`
48
49         // A JSON file containing the private key to use as a validator in the consensus protocol
50         PrivateKey string `mapstructure:"private_key"`
51
52         // A custom human readable name for this node
53         Moniker string `mapstructure:"moniker"`
54
55         // TCP or UNIX socket address for the profiling server to listen on
56         ProfListenAddress string `mapstructure:"prof_laddr"`
57
58         // If this node is many blocks behind the tip of the chain, FastSync
59         // allows them to catchup quickly by downloading blocks in parallel
60         // and verifying their commits
61         FastSync bool `mapstructure:"fast_sync"`
62
63         Mining bool `mapstructure:"mining"`
64
65         FilterPeers bool `mapstructure:"filter_peers"` // false
66
67         // What indexer to use for transactions
68         TxIndex string `mapstructure:"tx_index"`
69
70         // Database backend: leveldb | memdb
71         DBBackend string `mapstructure:"db_backend"`
72
73         // Database directory
74         DBPath string `mapstructure:"db_dir"`
75
76         // Keystore directory
77         KeysPath string `mapstructure:"keys_dir"`
78
79         // remote HSM url
80         HsmUrl string `mapstructure:"hsm_url"`
81
82         ApiAddress string `mapstructure:"api_addr"`
83
84         VaultMode bool `mapstructure:"vault_mode"`
85
86         Time time.Time
87
88         // log file name
89         LogName string `mapstructure:"log_name"`
90 }
91
92 // Default configurable base parameters.
93 func DefaultBaseConfig() BaseConfig {
94         return BaseConfig{
95                 Moniker:           "anonymous",
96                 ProfListenAddress: "",
97                 FastSync:          true,
98                 FilterPeers:       false,
99                 Mining:            false,
100                 TxIndex:           "kv",
101                 DBBackend:         "leveldb",
102                 DBPath:            "data",
103                 KeysPath:          "keystore",
104                 HsmUrl:            "",
105                 LogName:           "bytom.log",
106         }
107 }
108
109 func (b BaseConfig) DBDir() string {
110         return rootify(b.DBPath, b.RootDir)
111 }
112
113 func (b BaseConfig) KeysDir() string {
114         return rootify(b.KeysPath, b.RootDir)
115 }
116
117 // P2PConfig
118 type P2PConfig struct {
119         RootDir          string `mapstructure:"home"`
120         ListenAddress    string `mapstructure:"laddr"`
121         Seeds            string `mapstructure:"seeds"`
122         SkipUPNP         bool   `mapstructure:"skip_upnp"`
123         AddrBook         string `mapstructure:"addr_book_file"`
124         AddrBookStrict   bool   `mapstructure:"addr_book_strict"`
125         PexReactor       bool   `mapstructure:"pex"`
126         MaxNumPeers      int    `mapstructure:"max_num_peers"`
127         HandshakeTimeout int    `mapstructure:"handshake_timeout"`
128         DialTimeout      int    `mapstructure:"dial_timeout"`
129 }
130
131 // Default configurable p2p parameters.
132 func DefaultP2PConfig() *P2PConfig {
133         return &P2PConfig{
134                 ListenAddress:    "tcp://0.0.0.0:46656",
135                 AddrBook:         "addrbook.json",
136                 AddrBookStrict:   true,
137                 SkipUPNP:         false,
138                 MaxNumPeers:      50,
139                 HandshakeTimeout: 30,
140                 DialTimeout:      3,
141                 PexReactor:       true,
142         }
143 }
144
145 func (p *P2PConfig) AddrBookFile() string {
146         return rootify(p.AddrBook, p.RootDir)
147 }
148
149 //-----------------------------------------------------------------------------
150 type WalletConfig struct {
151         Disable bool `mapstructure:"disable"`
152         Rescan  bool `mapstructure:"rescan"`
153 }
154
155 type RPCAuthConfig struct {
156         Disable bool `mapstructure:"disable"`
157 }
158
159 type WebConfig struct {
160         Closed bool `mapstructure:"closed"`
161 }
162
163 // Default configurable rpc's auth parameters.
164 func DefaultRPCAuthConfig() *RPCAuthConfig {
165         return &RPCAuthConfig{
166                 Disable: false,
167         }
168 }
169
170 // Default configurable web parameters.
171 func DefaultWebConfig() *WebConfig {
172         return &WebConfig{
173                 Closed: false,
174         }
175 }
176
177 // Default configurable wallet parameters.
178 func DefaultWalletConfig() *WalletConfig {
179         return &WalletConfig{
180                 Disable: false,
181                 Rescan:  false,
182         }
183 }
184
185 //-----------------------------------------------------------------------------
186 // Utils
187
188 // helper function to make config creation independent of root dir
189 func rootify(path, root string) string {
190         if filepath.IsAbs(path) {
191                 return path
192         }
193         return filepath.Join(root, path)
194 }
195
196 // DefaultDataDir is the default data directory to use for the databases and other
197 // persistence requirements.
198 func DefaultDataDir() string {
199         // Try to place the data folder in the user's home dir
200         home := homeDir()
201         if home == "" {
202                 return "./.bytom"
203         }
204         switch runtime.GOOS {
205         case "darwin":
206                 return filepath.Join(home, "Library", "Bytom")
207         case "windows":
208                 return filepath.Join(home, "AppData", "Roaming", "Bytom")
209         default:
210                 return filepath.Join(home, ".bytom")
211         }
212 }
213
214 func homeDir() string {
215         if home := os.Getenv("HOME"); home != "" {
216                 return home
217         }
218         if usr, err := user.Current(); err == nil {
219                 return usr.HomeDir
220         }
221         return ""
222 }