OSDN Git Service

Merge pull request #657 from Bytom/coinbase-tx-test
[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         Time time.Time
85 }
86
87 // Default configurable base parameters.
88 func DefaultBaseConfig() BaseConfig {
89         return BaseConfig{
90                 Moniker:           "anonymous",
91                 ProfListenAddress: "",
92                 FastSync:          true,
93                 FilterPeers:       false,
94                 Mining:            false,
95                 TxIndex:           "kv",
96                 DBBackend:         "leveldb",
97                 DBPath:            "data",
98                 KeysPath:          "keystore",
99                 HsmUrl:            "",
100         }
101 }
102
103 func (b BaseConfig) DBDir() string {
104         return rootify(b.DBPath, b.RootDir)
105 }
106
107 func (b BaseConfig) KeysDir() string {
108         return rootify(b.KeysPath, b.RootDir)
109 }
110
111 // P2PConfig
112 type P2PConfig struct {
113         RootDir          string `mapstructure:"home"`
114         ListenAddress    string `mapstructure:"laddr"`
115         Seeds            string `mapstructure:"seeds"`
116         SkipUPNP         bool   `mapstructure:"skip_upnp"`
117         AddrBook         string `mapstructure:"addr_book_file"`
118         AddrBookStrict   bool   `mapstructure:"addr_book_strict"`
119         PexReactor       bool   `mapstructure:"pex"`
120         MaxNumPeers      int    `mapstructure:"max_num_peers"`
121         HandshakeTimeout int    `mapstructure:"handshake_timeout"`
122         DialTimeout      int    `mapstructure:"dial_timeout"`
123 }
124
125 // Default configurable p2p parameters.
126 func DefaultP2PConfig() *P2PConfig {
127         return &P2PConfig{
128                 ListenAddress:    "tcp://0.0.0.0:46656",
129                 AddrBook:         "addrbook.json",
130                 AddrBookStrict:   true,
131                 SkipUPNP:         false,
132                 MaxNumPeers:      50,
133                 HandshakeTimeout: 30,
134                 DialTimeout:      3,
135                 PexReactor:       true,
136         }
137 }
138
139 func (p *P2PConfig) AddrBookFile() string {
140         return rootify(p.AddrBook, p.RootDir)
141 }
142
143 //-----------------------------------------------------------------------------
144 type WalletConfig struct {
145         Disable bool `mapstructure:"disable"`
146 }
147
148 type RPCAuthConfig struct {
149         Disable bool `mapstructure:"disable"`
150 }
151
152 type WebConfig struct {
153         Closed bool `mapstructure:"closed"`
154 }
155
156 // Default configurable rpc's auth parameters.
157 func DefaultRPCAuthConfig() *RPCAuthConfig {
158         return &RPCAuthConfig{
159                 Disable: false,
160         }
161 }
162
163 // Default configurable web parameters.
164 func DefaultWebConfig() *WebConfig {
165         return &WebConfig{
166                 Closed: false,
167         }
168 }
169
170 // Default configurable wallet parameters.
171 func DefaultWalletConfig() *WalletConfig {
172         return &WalletConfig{
173                 Disable: false,
174         }
175 }
176
177 //-----------------------------------------------------------------------------
178 // Utils
179
180 // helper function to make config creation independent of root dir
181 func rootify(path, root string) string {
182         if filepath.IsAbs(path) {
183                 return path
184         }
185         return filepath.Join(root, path)
186 }
187
188 // DefaultDataDir is the default data directory to use for the databases and other
189 // persistence requirements.
190 func DefaultDataDir() string {
191         // Try to place the data folder in the user's home dir
192         home := homeDir()
193         dataDir := "./.Bytom"
194         if home != "" {
195                 switch runtime.GOOS {
196                 case "darwin":
197                         dataDir = filepath.Join(home, "Library", "Bytom")
198                 case "windows":
199                         dataDir = filepath.Join(home, "AppData", "Roaming", "Bytom")
200                 default:
201                         dataDir = filepath.Join(home, ".Bytom")
202                 }
203         }
204         return dataDir
205 }
206
207 func homeDir() string {
208         if home := os.Getenv("HOME"); home != "" {
209                 return home
210         }
211         if usr, err := user.Current(); err == nil {
212                 return usr.HomeDir
213         }
214         return ""
215 }