OSDN Git Service

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