-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathconfig.go
353 lines (311 loc) · 9.5 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package config
import (
"crypto/ecdsa"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/idena-network/idena-go/common"
"github.com/idena-network/idena-go/crypto"
"github.com/idena-network/idena-go/log"
"github.com/idena-network/idena-go/rpc"
"github.com/pkg/errors"
"github.com/urfave/cli"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
)
const (
datadirPrivateKey = "nodekey" // Path within the datadir to the node's private key
apiKeyFileName = "api.key"
LowPowerProfile = "lowpower"
SharedNodeProfile = "shared"
DefaultProfile = "default"
)
type Config struct {
DataDir string
Network uint32
Consensus *ConsensusConf
P2P P2P
RPC *rpc.Config
GenesisConf *GenesisConf
IpfsConf *IpfsConfig
Validation *ValidationConfig
Sync *SyncConfig
OfflineDetection *OfflineDetectionConfig
Blockchain *BlockchainConfig
Mempool *Mempool
}
func (c *Config) ProvideNodeKey(key string, password string, withBackup bool) error {
instanceDir := filepath.Join(c.DataDir, "keystore")
if err := os.MkdirAll(instanceDir, 0700); err != nil {
return err
}
keyfile := filepath.Join(instanceDir, datadirPrivateKey)
currentKey, err := crypto.LoadECDSA(keyfile)
if !withBackup && err == nil {
return errors.New("key already exists")
}
keyBytes, err := hex.DecodeString(key)
if err != nil {
return errors.Errorf("error while decoding key, err: %v", err.Error())
}
decrypted, err := crypto.Decrypt(keyBytes, password)
if err != nil {
return errors.Errorf("error while decrypting key, err: %v", err.Error())
}
ecdsaKey, err := crypto.ToECDSA(decrypted)
if err != nil {
return errors.Errorf("key is not valid ECDSA key, err: %v", err.Error())
}
if withBackup && currentKey != nil {
backupFile := filepath.Join(instanceDir, fmt.Sprintf("backup-%v", time.Now().Unix()))
if err := crypto.SaveECDSA(backupFile, currentKey); err != nil {
return errors.Errorf("failed to backup key, err: %v", err.Error())
}
}
if err := crypto.SaveECDSA(keyfile, ecdsaKey); err != nil {
return errors.Errorf("failed to persist key, err: %v", err.Error())
}
return nil
}
func (c *Config) NodeKey() (*ecdsa.PrivateKey, error) {
// Generate ephemeral key if no datadir is being used.
if c.DataDir == "" {
key, err := crypto.GenerateKey()
return key, errors.Wrap(err, "failed to generate ephemeral node key")
}
instanceDir := filepath.Join(c.DataDir, "keystore")
if err := os.MkdirAll(instanceDir, 0700); err != nil {
return nil, errors.Wrap(err, "failed to persist node key")
}
keyfile := filepath.Join(instanceDir, datadirPrivateKey)
if _, err := os.Stat(keyfile); os.IsNotExist(err) {
// No persistent key found, generate and store a new one.
key, err := crypto.GenerateKey()
if err != nil {
return nil, errors.Wrap(err, "failed to generate node key")
}
if err := crypto.SaveECDSA(keyfile, key); err != nil {
return nil, errors.Wrap(err, "failed to persist node key")
}
return key, nil
} else if err != nil {
return nil, errors.Wrap(err, "failed to check node key file")
}
key, err := crypto.LoadECDSA(keyfile)
return key, errors.Wrap(err, "failed to load node key")
}
// NodeDB returns the path to the discovery node database.
func (c *Config) NodeDB() string {
if c.DataDir == "" {
return "" // ephemeral
}
return filepath.Join(c.DataDir, "nodes")
}
func (c *Config) KeyStoreDataDir() (string, error) {
instanceDir := filepath.Join(c.DataDir, "keystore")
if err := os.MkdirAll(instanceDir, 0700); err != nil {
log.Error(fmt.Sprintf("Failed to create keystore datadir: %v", err))
return "", err
}
return instanceDir, nil
}
func (c *Config) SetApiKey() error {
shouldSaveKey := true
if c.RPC.APIKey == "" {
apiKeyFile := filepath.Join(c.DataDir, apiKeyFileName)
data, _ := ioutil.ReadFile(apiKeyFile)
key := strings.TrimSpace(string(data))
if key == "" {
randomKey, _ := crypto.GenerateKey()
key = hex.EncodeToString(crypto.FromECDSA(randomKey)[:16])
} else {
shouldSaveKey = false
}
c.RPC.APIKey = key
}
if shouldSaveKey {
f, err := os.OpenFile(filepath.Join(c.DataDir, apiKeyFileName), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(c.RPC.APIKey)
return err
}
return nil
}
func MakeMobileConfig(path string, cfg string) (*Config, error) {
conf := getDefaultConfig(filepath.Join(path, DefaultDataDir))
if cfg != "" {
log.Info("using custom configuration")
bytes := []byte(cfg)
err := json.Unmarshal(bytes, &conf)
if err != nil {
return nil, errors.Errorf("Cannot parse JSON config")
}
} else {
log.Info("using default config")
}
return conf, nil
}
func MakeConfig(ctx *cli.Context, cfgTransform func(cfg *Config)) (*Config, error) {
cfg, err := MakeConfigFromFile(ctx.String(CfgFileFlag.Name))
if err != nil {
return nil, err
}
if ctx.IsSet(DataDirFlag.Name) {
cfg.DataDir = ctx.String(DataDirFlag.Name)
}
cfgTransform(cfg)
applyFlags(ctx, cfg)
return cfg, nil
}
func applyProfile(ctx *cli.Context, cfg *Config) {
if ctx.IsSet(ProfileFlag.Name) {
switch ctx.String(ProfileFlag.Name) {
case LowPowerProfile:
applyLowPowerProfile(cfg)
case SharedNodeProfile:
applySharedNodeProfile(cfg)
case DefaultProfile:
applyDefaultProfile(cfg)
default:
println("unknown node profile")
}
}
if cfg.IpfsConf.GracePeriod == "" {
cfg.IpfsConf.GracePeriod = "40s"
}
if cfg.IpfsConf.ReproviderInterval == "" {
cfg.IpfsConf.ReproviderInterval = "12h"
}
if cfg.IpfsConf.Routing == "" {
cfg.IpfsConf.Routing = "dht"
}
}
func MakeConfigFromFile(file string) (*Config, error) {
cfg := getDefaultConfig(DefaultDataDir)
if file != "" {
if err := loadConfig(file, cfg); err != nil {
log.Error(err.Error())
return nil, err
}
}
return cfg, nil
}
func getDefaultConfig(dataDir string) *Config {
ipfsConfig := GetDefaultIpfsConfig()
ipfsConfig.DataDir = filepath.Join(dataDir, DefaultIpfsDataDir)
ipfsConfig.IpfsPort = DefaultIpfsPort
ipfsConfig.BootNodes = DefaultIpfsBootstrapNodes
ipfsConfig.SwarmKey = DefaultSwarmKey
return &Config{
DataDir: dataDir,
Network: 0x1, // testnet
P2P: P2P{
MaxInboundPeers: DefaultMaxInboundNotOwnShardPeers,
MaxOutboundPeers: DefaultMaxOutboundNotOwnShardPeers,
MaxInboundOwnShardPeers: DefaultMaxInboundOwnShardPeers,
MaxOutboundOwnShardPeers: DefaultMaxOutboundOwnShardPeers,
DisableMetrics: false,
},
Consensus: GetDefaultConsensusConfig(),
RPC: rpc.GetDefaultRPCConfig(DefaultRpcHost, DefaultRpcPort),
GenesisConf: &GenesisConf{
FirstCeremonyTime: DefaultCeremonyTime,
GodAddress: common.HexToAddress(DefaultGodAddress),
},
IpfsConf: ipfsConfig,
Validation: &ValidationConfig{},
Sync: &SyncConfig{
FastSync: true,
ForceFullSync: DefaultForceFullSync,
AllFlipsLoadingTime: time.Hour * 2,
},
OfflineDetection: GetDefaultOfflineDetectionConfig(),
Blockchain: &BlockchainConfig{
StoreCertRange: DefaultStoreCertRange,
BurnTxRange: DefaultBurntTxRange,
},
Mempool: GetDefaultMempoolConfig(),
}
}
func applyFlags(ctx *cli.Context, cfg *Config) {
applyProfile(ctx, cfg)
applyP2PFlags(ctx, cfg)
applyConsensusFlags(ctx, cfg)
applyRpcFlags(ctx, cfg)
applyGenesisFlags(ctx, cfg)
applyIpfsFlags(ctx, cfg)
applyValidationFlags(ctx, cfg)
applySyncFlags(ctx, cfg)
}
func applySyncFlags(ctx *cli.Context, cfg *Config) {
if ctx.IsSet(FastSyncFlag.Name) {
cfg.Sync.FastSync = ctx.Bool(FastSyncFlag.Name)
}
if ctx.IsSet(ForceFullSyncFlag.Name) {
cfg.Sync.ForceFullSync = ctx.Uint64(ForceFullSyncFlag.Name)
}
}
func applyP2PFlags(ctx *cli.Context, cfg *Config) {
if ctx.IsSet(MaxNetworkDelayFlag.Name) {
cfg.P2P.MaxDelay = ctx.Int(MaxNetworkDelayFlag.Name)
}
}
func applyConsensusFlags(ctx *cli.Context, cfg *Config) {
if ctx.IsSet(AutomineFlag.Name) {
cfg.Consensus.Automine = ctx.Bool(AutomineFlag.Name)
}
}
func applyRpcFlags(ctx *cli.Context, cfg *Config) {
if ctx.IsSet(RpcHostFlag.Name) {
cfg.RPC.HTTPHost = ctx.String(RpcHostFlag.Name)
}
if ctx.IsSet(RpcPortFlag.Name) {
cfg.RPC.HTTPPort = ctx.Int(RpcPortFlag.Name)
}
if ctx.IsSet(ApiKeyFlag.Name) {
cfg.RPC.APIKey = ctx.String(ApiKeyFlag.Name)
}
}
func applyGenesisFlags(ctx *cli.Context, cfg *Config) {
if ctx.IsSet(GodAddressFlag.Name) {
cfg.GenesisConf.GodAddress = common.HexToAddress(ctx.String(GodAddressFlag.Name))
}
if ctx.IsSet(CeremonyTimeFlag.Name) {
cfg.GenesisConf.FirstCeremonyTime = ctx.Int64(CeremonyTimeFlag.Name)
}
}
func applyIpfsFlags(ctx *cli.Context, cfg *Config) {
cfg.IpfsConf.DataDir = filepath.Join(cfg.DataDir, DefaultIpfsDataDir)
if ctx.IsSet(IpfsPortFlag.Name) {
cfg.IpfsConf.IpfsPort = ctx.Int(IpfsPortFlag.Name)
}
if ctx.IsSet(IpfsPortStaticFlag.Name) {
cfg.IpfsConf.StaticPort = ctx.Bool(IpfsPortStaticFlag.Name)
}
if ctx.IsSet(IpfsBootNodeFlag.Name) {
cfg.IpfsConf.BootNodes = []string{ctx.String(IpfsBootNodeFlag.Name)}
}
}
func applyValidationFlags(ctx *cli.Context, cfg *Config) {
}
func loadConfig(configPath string, conf *Config) error {
if _, err := os.Stat(configPath); err != nil {
return errors.Errorf("Config file cannot be found, path: %v", configPath)
}
if jsonFile, err := os.Open(configPath); err != nil {
return errors.Errorf("Config file cannot be opened, path: %v", configPath)
} else {
byteValue, _ := ioutil.ReadAll(jsonFile)
err := json.Unmarshal(byteValue, &conf)
if err != nil {
return errors.Wrap(err, errors.Errorf("Cannot parse JSON config, path: %v", configPath).Error())
}
return nil
}
}