OSDN Git Service

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