OSDN Git Service

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