This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 358
/
config.go
284 lines (241 loc) · 7.06 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package config
import (
"io"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"runtime"
log "github.com/sirupsen/logrus"
"github.com/bytom/crypto/ed25519"
)
var (
// CommonConfig means config object
CommonConfig *Config
)
type Config struct {
// Top level options use an anonymous struct
BaseConfig `mapstructure:",squash"`
// Options for services
P2P *P2PConfig `mapstructure:"p2p"`
Wallet *WalletConfig `mapstructure:"wallet"`
Auth *RPCAuthConfig `mapstructure:"auth"`
Web *WebConfig `mapstructure:"web"`
Simd *SimdConfig `mapstructure:"simd"`
Websocket *WebsocketConfig `mapstructure:"ws"`
}
// Default configurable parameters.
func DefaultConfig() *Config {
return &Config{
BaseConfig: DefaultBaseConfig(),
P2P: DefaultP2PConfig(),
Wallet: DefaultWalletConfig(),
Auth: DefaultRPCAuthConfig(),
Web: DefaultWebConfig(),
Simd: DefaultSimdConfig(),
Websocket: DefaultWebsocketConfig(),
}
}
// Set the RootDir for all Config structs
func (cfg *Config) SetRoot(root string) *Config {
cfg.BaseConfig.RootDir = root
return cfg
}
// NodeKey retrieves the currently configured private key of the node, checking
// first any manually set key, falling back to the one found in the configured
// data folder. If no key can be found, a new one is generated.
func (cfg *Config) NodeKey() (string, error) {
// Use any specifically configured key.
if cfg.P2P.PrivateKey != "" {
return cfg.P2P.PrivateKey, nil
}
keyFile := rootify(cfg.P2P.NodeKeyFile, cfg.BaseConfig.RootDir)
buf := make([]byte, ed25519.PrivateKeySize*2)
fd, err := os.Open(keyFile)
defer fd.Close()
if err == nil {
if _, err = io.ReadFull(fd, buf); err == nil {
return string(buf), nil
}
}
log.WithField("err", err).Warning("key file access failed")
_, privKey, err := ed25519.GenerateKey(nil)
if err != nil {
return "", err
}
if err = ioutil.WriteFile(keyFile, []byte(privKey.String()), 0600); err != nil {
return "", err
}
return privKey.String(), nil
}
//-----------------------------------------------------------------------------
// BaseConfig
type BaseConfig struct {
// The root directory for all data.
// This should be set in viper so it can unmarshal into this struct
RootDir string `mapstructure:"home"`
//The ID of the network to json
ChainID string `mapstructure:"chain_id"`
//log level to set
LogLevel string `mapstructure:"log_level"`
// A custom human readable name for this node
Moniker string `mapstructure:"moniker"`
// TCP or UNIX socket address for the profiling server to listen on
ProfListenAddress string `mapstructure:"prof_laddr"`
Mining bool `mapstructure:"mining"`
// Database backend: leveldb | memdb
DBBackend string `mapstructure:"db_backend"`
// Database directory
DBPath string `mapstructure:"db_dir"`
// Keystore directory
KeysPath string `mapstructure:"keys_dir"`
ApiAddress string `mapstructure:"api_addr"`
VaultMode bool `mapstructure:"vault_mode"`
// log file name
LogFile string `mapstructure:"log_file"`
}
// Default configurable base parameters.
func DefaultBaseConfig() BaseConfig {
return BaseConfig{
Moniker: "anonymous",
ProfListenAddress: "",
Mining: false,
DBBackend: "leveldb",
DBPath: "data",
KeysPath: "keystore",
}
}
func (b BaseConfig) DBDir() string {
return rootify(b.DBPath, b.RootDir)
}
func (b BaseConfig) KeysDir() string {
return rootify(b.KeysPath, b.RootDir)
}
// P2PConfig
type P2PConfig struct {
ListenAddress string `mapstructure:"laddr"`
Seeds string `mapstructure:"seeds"`
PrivateKey string `mapstructure:"node_key"`
NodeKeyFile string `mapstructure:"node_key_file"`
SkipUPNP bool `mapstructure:"skip_upnp"`
MaxNumPeers int `mapstructure:"max_num_peers"`
HandshakeTimeout int `mapstructure:"handshake_timeout"`
DialTimeout int `mapstructure:"dial_timeout"`
ProxyAddress string `mapstructure:"proxy_address"`
ProxyUsername string `mapstructure:"proxy_username"`
ProxyPassword string `mapstructure:"proxy_password"`
}
// Default configurable p2p parameters.
func DefaultP2PConfig() *P2PConfig {
return &P2PConfig{
ListenAddress: "tcp://0.0.0.0:46656",
NodeKeyFile: "nodekey",
SkipUPNP: false,
MaxNumPeers: 50,
HandshakeTimeout: 30,
DialTimeout: 3,
ProxyAddress: "",
ProxyUsername: "",
ProxyPassword: "",
}
}
//-----------------------------------------------------------------------------
type WalletConfig struct {
Disable bool `mapstructure:"disable"`
Rescan bool `mapstructure:"rescan"`
MaxTxFee uint64 `mapstructure:"max_tx_fee"`
}
type RPCAuthConfig struct {
Disable bool `mapstructure:"disable"`
}
type WebConfig struct {
Closed bool `mapstructure:"closed"`
}
type SimdConfig struct {
Enable bool `mapstructure:"enable"`
}
type WebsocketConfig struct {
MaxNumWebsockets int `mapstructure:"max_num_websockets"`
MaxNumConcurrentReqs int `mapstructure:"max_num_concurrent_reqs"`
}
// Default configurable rpc's auth parameters.
func DefaultRPCAuthConfig() *RPCAuthConfig {
return &RPCAuthConfig{
Disable: false,
}
}
// Default configurable web parameters.
func DefaultWebConfig() *WebConfig {
return &WebConfig{
Closed: false,
}
}
// Default configurable wallet parameters.
func DefaultWalletConfig() *WalletConfig {
return &WalletConfig{
Disable: false,
Rescan: false,
MaxTxFee: uint64(1000000000),
}
}
// Default configurable web parameters.
func DefaultSimdConfig() *SimdConfig {
return &SimdConfig{
Enable: false,
}
}
func DefaultWebsocketConfig() *WebsocketConfig {
return &WebsocketConfig{
MaxNumWebsockets: 25,
MaxNumConcurrentReqs: 20,
}
}
//-----------------------------------------------------------------------------
// Utils
// helper function to make config creation independent of root dir
func rootify(path, root string) string {
if filepath.IsAbs(path) {
return path
}
return filepath.Join(root, path)
}
// DefaultDataDir is the default data directory to use for the databases and other
// persistence requirements.
func DefaultDataDir() string {
// Try to place the data folder in the user's home dir
home := homeDir()
if home == "" {
return "./.bytom"
}
switch runtime.GOOS {
case "darwin":
// In order to be compatible with old data path,
// copy the data from the old path to the new path
oldPath := filepath.Join(home, "Library", "Bytom")
newPath := filepath.Join(home, "Library", "Application Support", "Bytom")
if !isFolderNotExists(oldPath) && isFolderNotExists(newPath) {
if err := os.Rename(oldPath, newPath); err != nil {
log.Errorf("DefaultDataDir: %v", err)
return oldPath
}
}
return newPath
case "windows":
return filepath.Join(home, "AppData", "Roaming", "Bytom")
default:
return filepath.Join(home, ".bytom")
}
}
func isFolderNotExists(path string) bool {
_, err := os.Stat(path)
return os.IsNotExist(err)
}
func homeDir() string {
if home := os.Getenv("HOME"); home != "" {
return home
}
if usr, err := user.Current(); err == nil {
return usr.HomeDir
}
return ""
}