This repository was archived by the owner on Aug 7, 2023. It is now read-only.
forked from decred/dcrpool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
507 lines (452 loc) · 18.4 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
// Copyright (c) 2018 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"crypto/elliptic"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"time"
flags "github.com/jessevdk/go-flags"
"github.com/decred/dcrd/certgen"
"github.com/decred/dcrd/chaincfg"
"github.com/decred/dcrd/dcrutil"
"github.com/decred/dcrpool/dividend"
"github.com/decred/dcrpool/util"
"github.com/decred/slog"
)
const (
defaultConfigFilename = "dcrpool.conf"
defaultDataDirname = "data"
defaultLogLevel = "debug"
defaultLogDirname = "log"
defaultLogFilename = "dcrpool.log"
defaultDBFilename = "dcrpool.kv"
defaultTLSCertFilename = "dcrpool.cert"
defaultTLSKeyFilename = "dcrpool.key"
defaultRPCCertFilename = "rpc.cert"
defaultRPCUser = "dcrp"
defaultRPCPass = "dcrppass"
defaultDcrdRPCHost = "127.0.0.1:19109"
defaultWalletGRPCHost = "127.0.0.1:51028"
defaultPoolFeeAddr = ""
defaultMaxGenTime = 15
defaultPoolFee = 0.01
defaultLastNPeriod = 86400 // 1 day
defaultWalletPass = ""
defaultMaxTxFeeReserve = 0.1
defaultSoloPool = false
defaultGUIPort = 8080
defaultGUIDir = "gui"
)
var (
defaultActiveNet = chaincfg.SimNetParams.Name
defaultPaymentMethod = dividend.PPS
defaultMinPayment = 0.2
dcrpoolHomeDir = dcrutil.AppDataDir("dcrpool", false)
dcrwalletHomeDir = dcrutil.AppDataDir("dcrwallet", false)
dcrdHomeDir = dcrutil.AppDataDir("dcrd", false)
defaultConfigFile = filepath.Join(dcrpoolHomeDir, defaultConfigFilename)
defaultDataDir = filepath.Join(dcrpoolHomeDir, defaultDataDirname)
defaultDBFile = filepath.Join(defaultDataDir, defaultDBFilename)
dcrdRPCCertFile = filepath.Join(dcrdHomeDir, defaultRPCCertFilename)
walletRPCCertFile = filepath.Join(dcrwalletHomeDir, defaultRPCCertFilename)
defaultLogDir = filepath.Join(dcrpoolHomeDir, defaultLogDirname)
defaultTLSCertFile = filepath.Join(dcrpoolHomeDir, defaultTLSCertFilename)
defaultTLSKeyFile = filepath.Join(dcrpoolHomeDir, defaultTLSKeyFilename)
)
// runServiceCommand is only set to a real function on Windows. It is used
// to parse and execute service commands specified via the -s flag.
var runServiceCommand func(string) error
// config defines the configuration options for the pool.
type config struct {
HomeDir string `long:"homedir" ini-name:"homedir" description:"Path to application home directory."`
ConfigFile string `long:"configfile" ini-name:"configfile" description:"Path to configuration file."`
DataDir string `long:"datadir" ini-name:"datadir" description:"The data directory."`
ActiveNet string `long:"activenet" ini-name:"activenet" description:"The active network being mined on. {testnet3, mainnet, simnet}"`
GUIPort uint32 `long:"guiport" ini-name:"guiport" description:"The pool GUI port."`
DebugLevel string `long:"debuglevel" ini-name:"debuglevel" description:"Logging level for all subsystems. {trace, debug, info, warn, error, critical} -- You may also specify <subsystem>=<level>,<subsystem2>=<level>,... to set the log level for individual subsystems -- Use show to list available subsystems"`
LogDir string `long:"logdir" ini-name:"logdir" description:"Directory to log output."`
DBFile string `long:"dbfile" ini-name:"dbfile" description:"Path to the database file."`
DcrdRPCHost string `long:"dcrdrpchost" ini-name:"dcrdrpchost" description:"The ip:port to establish an RPC connection for dcrd."`
DcrdRPCCert string `long:"dcrdrpccert" ini-name:"dcrdrpccert" description:"The dcrd RPC certificate."`
WalletGRPCHost string `long:"walletgrpchost" ini-name:"walletgrpchost" description:"The ip:port to establish a GRPC connection for the wallet."`
WalletRPCCert string `long:"walletrpccert" ini-name:"walletrpccert" description:"The wallet RPC certificate."`
RPCUser string `long:"rpcuser" ini-name:"rpcuser" description:"Username for RPC connections."`
RPCPass string `long:"rpcpass" ini-name:"rpcpass" default-mask:"-" description:"Password for RPC connections."`
PoolFeeAddrs []string `long:"poolfeeaddrs" ini-name:"poolfeeaddrs" description:"Payment addresses to use for pool fee transactions. These addresses should be generated from a dedicated wallet account for pool fees."`
PoolFee float64 `long:"poolfee" ini-name:"poolfee" description:"The fee charged for pool participation. eg. 0.01 (1%), 0.05 (5%)."`
MaxTxFeeReserve float64 `long:"maxtxfeereserve" ini-name:"maxtxfeereserve" description:"The maximum amount reserved for transaction fees, in DCR."`
MaxGenTime uint64 `long:"maxgentime" ini-name:"maxgentime" description:"The share creation target time for the pool in seconds."`
PaymentMethod string `long:"paymentmethod" ini-name:"paymentmethod" description:"The payment method of the pool. {pps, pplns}"`
LastNPeriod uint32 `long:"lastnperiod" ini-name:"lastnperiod" description:"The period of interest when using the PPLNS payment scheme."`
WalletPass string `long:"walletpass" ini-name:"walletpass" description:"The wallet passphrase."`
MinPayment float64 `long:"minpayment" ini-name:"minpayment" description:"The minimum payment to process for an account."`
SoloPool bool `long:"solopool" ini-name:"solopool" description:"Solo pool mode. This disables payment processing when enabled."`
BackupPass string `long:"backuppass" ini-name:"backuppass" description:"The admin password, required for database backup."`
GUIDir string `long:"guidir" ini-name:"guidir" description:"The path to the directory containing the pool's user interface assets (templates, css etc.)"`
poolFeeAddrs []dcrutil.Address
dcrdRPCCerts []byte
net *chaincfg.Params
}
// serviceOptions defines the configuration options for the daemon as a service on
// Windows.
type serviceOptions struct {
ServiceCommand string `short:"s" long:"service" description:"Service command {install, remove, start, stop}"`
}
// validLogLevel returns whether or not logLevel is a valid debug log level.
func validLogLevel(logLevel string) bool {
_, ok := slog.LevelFromString(logLevel)
return ok
}
// supportedSubsystems returns a sorted slice of the supported subsystems for
// logging purposes.
func supportedSubsystems() []string {
// Convert the subsystemLoggers map keys to a slice.
subsystems := make([]string, 0, len(subsystemLoggers))
for subsysID := range subsystemLoggers {
subsystems = append(subsystems, subsysID)
}
// Sort the subsystems for stable display.
sort.Strings(subsystems)
return subsystems
}
// parseAndSetDebugLevels attempts to parse the specified debug level and set
// the levels accordingly. An appropriate error is returned if anything is
// invalid.
func parseAndSetDebugLevels(debugLevel string) error {
// When the specified string doesn't have any delimiters, treat it as
// the log level for all subsystems.
if !strings.Contains(debugLevel, ",") && !strings.Contains(debugLevel, "=") {
// Validate debug log level.
if !validLogLevel(debugLevel) {
str := "the specified debug level [%v] is invalid"
return fmt.Errorf(str, debugLevel)
}
// Change the logging level for all subsystems.
setLogLevels(debugLevel)
return nil
}
// Split the specified string into subsystem/level pairs while detecting
// issues and update the log levels accordingly.
for _, logLevelPair := range strings.Split(debugLevel, ",") {
if !strings.Contains(logLevelPair, "=") {
str := "the specified debug level contains an invalid " +
"subsystem/level pair [%v]"
return fmt.Errorf(str, logLevelPair)
}
// Extract the specified subsystem and log level.
fields := strings.Split(logLevelPair, "=")
subsysID, logLevel := fields[0], fields[1]
// Validate subsystem.
if _, exists := subsystemLoggers[subsysID]; !exists {
str := "the specified subsystem [%v] is invalid -- " +
"supported subsytems %v"
return fmt.Errorf(str, subsysID, supportedSubsystems())
}
// Validate log level.
if !validLogLevel(logLevel) {
str := "the specified debug level [%v] is invalid"
return fmt.Errorf(str, logLevel)
}
setLogLevel(subsysID, logLevel)
}
return nil
}
// fileExists reports whether the named file or directory exists.
func fileExists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
// genCertPair generates a key/cert pair to the paths provided.
func genCertPair(certFile, keyFile string) error {
org := "dcrpool autogenerated cert"
validUntil := time.Now().Add(10 * 365 * 24 * time.Hour)
cert, key, err := certgen.NewTLSCertPair(elliptic.P256(), org,
validUntil, nil)
if err != nil {
return err
}
// Write cert and key files.
if err = ioutil.WriteFile(certFile, cert, 0644); err != nil {
return err
}
if err = ioutil.WriteFile(keyFile, key, 0600); err != nil {
os.Remove(certFile)
return err
}
return nil
}
// newConfigParser returns a new command line flags parser.
func newConfigParser(cfg *config, so *serviceOptions, options flags.Options) *flags.Parser {
parser := flags.NewParser(cfg, options)
if runtime.GOOS == "windows" {
parser.AddGroup("Service Options", "Service Options", so)
}
return parser
}
// loadConfig initializes and parses the config using a config file and command
// line options.
//
// The configuration proceeds as follows:
// 1) Start with a default config with sane settings
// 2) Pre-parse the command line to check for an alternative config file
// 3) Load configuration file overwriting defaults with any specified options
// 4) Parse CLI options and overwrite/add any specified options
//
// The above results in dcrpool functioning properly without any config settings
// while still allowing the user to override settings with config files and
// command line options. Command line options always take precedence.
func loadConfig() (*config, []string, error) {
// Default config.
cfg := config{
HomeDir: dcrpoolHomeDir,
ConfigFile: defaultConfigFile,
DataDir: defaultDataDir,
DBFile: defaultDBFile,
DebugLevel: defaultLogLevel,
LogDir: defaultLogDir,
RPCUser: defaultRPCUser,
RPCPass: defaultRPCPass,
DcrdRPCHost: defaultDcrdRPCHost,
DcrdRPCCert: dcrdRPCCertFile,
WalletRPCCert: walletRPCCertFile,
WalletGRPCHost: defaultWalletGRPCHost,
PoolFeeAddrs: []string{defaultPoolFeeAddr},
PoolFee: defaultPoolFee,
MaxTxFeeReserve: defaultMaxTxFeeReserve,
MaxGenTime: defaultMaxGenTime,
ActiveNet: defaultActiveNet,
PaymentMethod: defaultPaymentMethod,
LastNPeriod: defaultLastNPeriod,
WalletPass: defaultWalletPass,
MinPayment: defaultMinPayment,
SoloPool: defaultSoloPool,
GUIPort: defaultGUIPort,
GUIDir: defaultGUIDir,
}
// Service options which are only added on Windows.
serviceOpts := serviceOptions{}
// Pre-parse the command line options to see if an alternative config
// file or the version flag was specified. Any errors aside from the
// help message error can be ignored here since they will be caught by
// the final parse below.
preCfg := cfg
preParser := newConfigParser(&preCfg, &serviceOpts, flags.HelpFlag)
_, err := preParser.Parse()
if err != nil {
if e, ok := err.(*flags.Error); ok && e.Type != flags.ErrHelp {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
} else if ok && e.Type == flags.ErrHelp {
fmt.Fprintln(os.Stdout, err)
os.Exit(0)
}
}
appName := filepath.Base(os.Args[0])
appName = strings.TrimSuffix(appName, filepath.Ext(appName))
usageMessage := fmt.Sprintf("Use %s -h to show usage", appName)
// Perform service command and exit if specified. Invalid service
// commands show an appropriate error. Only runs on Windows since
// the runServiceCommand function will be nil when not on Windows.
if serviceOpts.ServiceCommand != "" && runServiceCommand != nil {
err := runServiceCommand(serviceOpts.ServiceCommand)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
os.Exit(0)
}
// Update the home directory for dcrpool if specified. Since the home
// directory is updated, other variables need to be updated to
// reflect the new changes.
if preCfg.HomeDir != "" {
cfg.HomeDir, _ = filepath.Abs(preCfg.HomeDir)
if preCfg.ConfigFile == defaultConfigFile {
defaultConfigFile = filepath.Join(cfg.HomeDir,
defaultConfigFilename)
preCfg.ConfigFile = defaultConfigFile
cfg.ConfigFile = defaultConfigFile
} else {
cfg.ConfigFile = preCfg.ConfigFile
}
if preCfg.DataDir == defaultDataDir {
cfg.DataDir = filepath.Join(cfg.HomeDir, defaultDataDirname)
} else {
cfg.DataDir = preCfg.DataDir
}
if preCfg.LogDir == defaultLogDir {
cfg.LogDir = filepath.Join(cfg.HomeDir, defaultLogDirname)
} else {
cfg.LogDir = preCfg.LogDir
}
if preCfg.DBFile == defaultDBFile {
cfg.DBFile = filepath.Join(cfg.DataDir, defaultDBFilename)
} else {
cfg.DBFile = preCfg.DBFile
}
}
// Create the home directory if it doesn't already exist.
funcName := "loadConfig"
err = os.MkdirAll(cfg.HomeDir, 0700)
if err != nil {
// Show a nicer error message if it's because a symlink is
// linked to a directory that does not exist (probably because
// it's not mounted).
if e, ok := err.(*os.PathError); ok && os.IsExist(err) {
if link, lerr := os.Readlink(e.Path); lerr == nil {
str := "is symlink %s -> %s mounted?"
err = fmt.Errorf(str, e.Path, link)
}
}
str := "%s: failed to create home directory: %v"
err := fmt.Errorf(str, funcName, err)
fmt.Fprintln(os.Stderr, err)
return nil, nil, err
}
// Create a default config file when one does not exist and the user did
// not specify an override.
if !fileExists(preCfg.ConfigFile) {
preIni := flags.NewIniParser(preParser)
err = preIni.WriteFile(preCfg.ConfigFile,
flags.IniIncludeComments|flags.IniIncludeDefaults)
if err != nil {
return nil, nil, fmt.Errorf("error creating a default "+
"config file: %v", err)
}
}
// Load additional config from file.
var configFileError error
parser := newConfigParser(&cfg, &serviceOpts, flags.Default)
if preCfg.ConfigFile != defaultConfigFile {
err := flags.NewIniParser(parser).ParseFile(preCfg.ConfigFile)
if err != nil {
if _, ok := err.(*os.PathError); !ok {
fmt.Fprintf(os.Stderr, "error parsing config file: %v\n", err)
fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err
}
configFileError = err
}
}
// Parse command line options again to ensure they take precedence.
remainingArgs, err := parser.Parse()
if err != nil {
if e, ok := err.(*flags.Error); !ok || e.Type != flags.ErrHelp {
fmt.Fprintln(os.Stderr, usageMessage)
}
return nil, nil, err
}
cfg.DataDir = util.CleanAndExpandPath(cfg.DataDir)
cfg.LogDir = util.CleanAndExpandPath(cfg.LogDir)
logRotator = nil
// Initialize log rotation. After log rotation has been initialized, the
// logger variables may be used.
initLogRotator(filepath.Join(cfg.LogDir, defaultLogFilename))
// Ensure the backup password is set.
if cfg.BackupPass == "" {
str := "%s: pool backup password is not set"
err := fmt.Errorf(str, funcName)
return nil, nil, err
}
// Create the data directory.
err = os.MkdirAll(cfg.DataDir, 0700)
if err != nil {
str := "%s: failed to create data directory: %v"
err := fmt.Errorf(str, funcName, err)
return nil, nil, err
}
// Special show command to list supported subsystems and exit.
if cfg.DebugLevel == "show" {
fmt.Println("Supported subsystems", supportedSubsystems())
os.Exit(0)
}
// Parse, validate, and set debug log level(s).
if err := parseAndSetDebugLevels(cfg.DebugLevel); err != nil {
err := fmt.Errorf("%s: %v", funcName, err.Error())
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err
}
// Set the mining active network. Testnet proof of work
// parameters are modified here to mirror that of mainnet in order to
// generate reasonable difficulties for asics. Simnet is currently reserved
// for cpu miner tests only.
switch cfg.ActiveNet {
case chaincfg.TestNet3Params.Name:
cfg.net = &chaincfg.TestNet3Params
cfg.net.PowLimit = chaincfg.MainNetParams.PowLimit
case chaincfg.MainNetParams.Name:
cfg.net = &chaincfg.MainNetParams
case chaincfg.SimNetParams.Name:
cfg.net = &chaincfg.SimNetParams
default:
return nil, nil, fmt.Errorf("unknown network provided %v",
cfg.ActiveNet)
}
if !cfg.SoloPool {
// Ensure a valid payment method is set.
if cfg.PaymentMethod != dividend.PPS && cfg.PaymentMethod != dividend.PPLNS {
str := "%s: paymentmethod must be either %s or %s"
err := fmt.Errorf(str, funcName, dividend.PPS, dividend.PPLNS)
return nil, nil, err
}
for _, pAddr := range cfg.PoolFeeAddrs {
addr, err := dcrutil.DecodeAddress(pAddr)
if err != nil {
str := "%s: pool fee address '%v' failed to decode: %v"
err := fmt.Errorf(str, funcName, pAddr, err)
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err
}
// Ensure pool fee address is valid and on the active network.
if !addr.IsForNet(cfg.net) {
return nil, nil,
fmt.Errorf("pool fee address (%v) not on the active network "+
"(%s)", addr, cfg.ActiveNet)
}
cfg.poolFeeAddrs = append(cfg.poolFeeAddrs, addr)
}
}
// Warn about missing config file only after all other configuration is
// done. This prevents the warning on help messages and invalid
// options. Note this should go directly before the return.
if configFileError != nil {
pLog.Warnf("%v", configFileError)
}
// Generate the TLS cert and key if they not already exist.
if !fileExists(defaultTLSCertFile) || !fileExists(defaultTLSKeyFile) {
err := genCertPair(defaultTLSCertFile, defaultTLSKeyFile)
if err != nil {
return nil, nil,
fmt.Errorf("failed to generate dcrpool's TLS cert/key: %v", err)
}
}
// Load Dcrd RPC Certificate.
if !fileExists(cfg.DcrdRPCCert) {
return nil, nil,
fmt.Errorf("dcrd RPC certificate (%v) not found", cfg.DcrdRPCCert)
}
cfg.dcrdRPCCerts, err = ioutil.ReadFile(dcrdRPCCertFile)
if err != nil {
return nil, nil, err
}
if !cfg.SoloPool {
// Load the wallet RPC certificate.
if !fileExists(cfg.WalletRPCCert) {
return nil, nil,
fmt.Errorf("wallet RPC certificate (%v) not found",
cfg.WalletRPCCert)
}
}
return &cfg, remainingArgs, nil
}