OSDN Git Service

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