This repository has been archived by the owner on May 16, 2019. It is now read-only.
forked from cjepson/dcrticketbuyer
-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.go
470 lines (419 loc) · 18.1 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
// Copyright (c) 2016 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 (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
flags "github.com/btcsuite/go-flags"
"github.com/decred/dcrutil"
"github.com/decred/dcrwallet/netparams"
)
const (
defaultConfigFilename = "ticketbuyer.conf"
defaultDataDirname = "data"
defaultLogLevel = "info"
defaultLogDirname = "logs"
defaultLogFilename = "dcrticketbuyer.log"
)
var activeNet = &netparams.MainNetParams
// csvPath is the default path for web server CSV files to be
// held in. It is set to the DataDir after loading the
// configuration.
var csvPath = ""
var (
dcrdHomeDir = dcrutil.AppDataDir("dcrd", false)
dcrwalletHomeDir = dcrutil.AppDataDir("dcrwallet", false)
dcrticketbuyerHomeDir = dcrutil.AppDataDir("dcrticketbuyer", false)
defaultDaemonRPCKeyFile = filepath.Join(dcrdHomeDir, "rpc.key")
defaultDaemonRPCCertFile = filepath.Join(dcrdHomeDir, "rpc.cert")
defaultConfigFile = filepath.Join(dcrticketbuyerHomeDir, defaultConfigFilename)
defaultDataDir = filepath.Join(dcrticketbuyerHomeDir, defaultDataDirname)
defaultWalletRPCKeyFile = filepath.Join(dcrwalletHomeDir, "rpc.key")
defaultWalletRPCCertFile = filepath.Join(dcrwalletHomeDir, "rpc.cert")
defaultLogDir = filepath.Join(dcrticketbuyerHomeDir, defaultLogDirname)
defaultHost = "localhost"
defaultHTTPServerBind = "localhost"
defaultHTTPServerPort = 0
defaultAccountName = "default"
defaultTicketAddress = ""
defaultPoolAddress = ""
defaultMaxFee = 1.0
defaultMinFee = 0.01
defaultFeeSource = "mean"
defaultTxFee = 0.01
defaultMaxPriceAbsolute = 100.0
defaultMaxPriceScale = 2.0
defaultMinPriceScale = 0.7
defaultPriceTarget = 0.0
defaultAvgPriceMode = "vwap"
defaultAvgVWAPPriceDelta = 2880
defaultMaxPerBlock = 3
defaultBalanceToMaintain = 0.0
defaultHighPricePenalty = 1.3
defaultBlocksToAvg = 11
defaultFeeTargetScaling = 1.05
defaultDontWaitForTickets = false
defaultMaxInMempool = 0
defaultExpiryDelta = 16
)
type config struct {
// General application behavior
ConfigFile string `short:"C" long:"configfile" description:"Path to configuration file"`
DataDir string `short:"b" long:"datadir" description:"Directory to store data"`
ShowVersion bool `short:"V" long:"version" description:"Display version information and exit"`
TestNet bool `long:"testnet" description:"Use the test network (default mainnet)"`
SimNet bool `long:"simnet" description:"Use the simulation test network (default mainnet)"`
DebugLevel string `short:"d" long:"debuglevel" description:"Logging level {trace, debug, info, warn, error, critical}"`
LogDir string `long:"logdir" description:"Directory to log output"`
HTTPSvrBind string `long:"httpsvrbind" description:"IP to bind for the HTTP server that tracks ticket purchase metrics (default: \"\" or localhost)"`
HTTPSvrPort int `long:"httpsvrport" description:"Server port for the HTTP server that tracks ticket purchase metrics; disabled if 0 (default: 0)"`
HTTPUIPath string `long:"httpuipath" description:"Deprecated and unused option for backwards compatibility."`
// RPC client options
DcrdUser string `long:"dcrduser" description:"Daemon RPC user name"`
DcrdPass string `long:"dcrdpass" description:"Daemon RPC password"`
DcrdServ string `long:"dcrdserv" description:"Hostname/IP and port of dcrd RPC server to connect to (default localhost:9109, testnet: localhost:19109, simnet: localhost:19556)"`
DcrdCert string `long:"dcrdcert" description:"File containing the dcrd certificate file"`
DcrwUser string `long:"dcrwuser" description:"Wallet RPC user name"`
DcrwPass string `long:"dcrwpass" description:"Wallet RPC password"`
DcrwServ string `long:"dcrwserv" description:"Hostname/IP and port of dcrwallet RPC server to connect to (default localhost:9110, testnet: localhost:19110, simnet: localhost:19557)"`
DcrwCert string `long:"dcrwcert" description:"File containing the dcrwallet certificate file"`
DisableClientTLS bool `long:"noclienttls" description:"Disable TLS for the RPC client -- NOTE: This is only allowed if the RPC client is connecting to localhost"`
// Automatic ticket buyer settings
AccountName string `long:"accountname" description:"Name of the account to buy tickets from (default: default)"`
TicketAddress string `long:"ticketaddress" description:"Address to give ticket voting rights to"`
PoolAddress string `long:"pooladdress" description:"Address to give pool fees rights to"`
PoolFees float64 `long:"poolfees" description:"The pool fee base rate for a given pool as a percentage (0.01 to 100.00%)"`
MaxPriceAbsolute float64 `long:"maxpriceabsolute" description:"The absolute maximum price to pay for a ticket (default: 100.0 Coin)"`
MaxPriceScale float64 `long:"maxpricescale" description:"Attempt to prevent the stake difficulty from going above this multiplier (>1.0) by manipulation (default: 2.0, 0.0 to disable)"`
MinPriceScale float64 `long:"minpricescale" description:"Attempt to prevent the stake difficulty from going below this multiplier (<1.0) by manipulation (default: 0.7, 0.0 to disable)"`
PriceTarget float64 `long:"pricetarget" description:"A target to try to seek setting the stake price to rather than meeting the average price (default: 0.0, 0.0 to disable)"`
AvgPriceMode string `long:"avgpricemode" description:"The mode to use for calculating the average price if pricetarget is disabled (default: dual)"`
AvgPriceVWAPDelta int `long:"avgpricevwapdelta" description:"The number of blocks to use from the current block to calculate the VWAP (default: 2880)"`
MaxFee float64 `long:"maxfee" description:"Maximum ticket fee per KB (default: 1.0 Coin/KB)"`
MinFee float64 `long:"minfee" description:"Minimum ticket fee per KB (default: 0.01 Coin/KB)"`
FeeSource string `long:"feesource" description:"The fee source to use for ticket fee per KB (median or mean, default: mean)"`
TxFee float64 `long:"txfee" description:"Default regular tx fee per KB, for consolidations (default: 0.01 Coin/KB)"`
MaxPerBlock int `long:"maxperblock" description:"Maximum tickets per block, with negative numbers indicating buy one ticket every 1-in-n blocks (default: 3)"`
BalanceToMaintain float64 `long:"balancetomaintain" description:"Balance to try to maintain in the wallet"`
HighPricePenalty float64 `long:"highpricepenalty" description:"The exponential penalty to apply to the number of tickets to purchase above the ideal ticket pool price (default: 1.3)"`
BlocksToAvg int `long:"blockstoavg" description:"Number of blocks to average for fees calculation (default: 11)"`
FeeTargetScaling float64 `long:"feetargetscaling" description:"The amount above the mean fee in the previous blocks to purchase tickets with, proportional e.g. 1.05 = 105% (default: 1.05)"`
DontWaitForTickets bool `long:"dontwaitfortickets" description:"Don't wait until your last round of tickets have entered the blockchain to attempt to purchase more"`
MaxInMempool int `long:"maxinmempool" description:"The maximum number of your tickets allowed in mempool before purchasing more tickets (default: 0)"`
ExpiryDelta int `long:"expirydelta" description:"Number of blocks in the future before the ticket expires (default: 16)"`
}
// cleanAndExpandPath expands environement variables and leading ~ in the
// passed path, cleans the result, and returns it.
func cleanAndExpandPath(path string) string {
// Expand initial ~ to OS specific home directory.
if strings.HasPrefix(path, "~") {
homeDir := filepath.Dir(dcrwalletHomeDir)
path = strings.Replace(path, "~", homeDir, 1)
}
// NOTE: The os.ExpandEnv doesn't work with Windows cmd.exe-style
// %VARIABLE%, but they variables can still be expanded via POSIX-style
// $VARIABLE.
return filepath.Clean(os.ExpandEnv(path))
}
// validLogLevel returns whether or not logLevel is a valid debug log level.
func validLogLevel(logLevel string) bool {
switch logLevel {
case "trace":
fallthrough
case "debug":
fallthrough
case "info":
fallthrough
case "warn":
fallthrough
case "error":
fallthrough
case "critical":
return true
}
return false
}
// 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 subsytems 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 delimters, 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
}
// loadConfig initializes and parses the config using a config file and command
// line options.
func loadConfig() (*config, error) {
loadConfigError := func(err error) (*config, error) {
return nil, err
}
// Default config.
cfg := config{
DebugLevel: defaultLogLevel,
ConfigFile: defaultConfigFile,
DataDir: defaultDataDir,
LogDir: defaultLogDir,
HTTPSvrBind: defaultHTTPServerBind,
HTTPSvrPort: defaultHTTPServerPort,
DcrdCert: defaultDaemonRPCCertFile,
DcrwCert: defaultWalletRPCCertFile,
AccountName: defaultAccountName,
TicketAddress: defaultTicketAddress,
PoolAddress: defaultPoolAddress,
MaxFee: defaultMaxFee,
MinFee: defaultMinFee,
FeeSource: defaultFeeSource,
TxFee: defaultTxFee,
MaxPriceAbsolute: defaultMaxPriceAbsolute,
MaxPriceScale: defaultMaxPriceScale,
MinPriceScale: defaultMinPriceScale,
PriceTarget: defaultPriceTarget,
AvgPriceMode: defaultAvgPriceMode,
AvgPriceVWAPDelta: defaultAvgVWAPPriceDelta,
MaxPerBlock: defaultMaxPerBlock,
BalanceToMaintain: defaultBalanceToMaintain,
HighPricePenalty: defaultHighPricePenalty,
BlocksToAvg: defaultBlocksToAvg,
FeeTargetScaling: defaultFeeTargetScaling,
DontWaitForTickets: defaultDontWaitForTickets,
MaxInMempool: defaultMaxInMempool,
ExpiryDelta: defaultExpiryDelta,
}
// A config file in the current directory takes precedence.
exists := false
if _, err := os.Stat(defaultConfigFilename); !os.IsNotExist(err) {
exists = true
}
if exists {
cfg.ConfigFile = defaultConfigFile
}
// Pre-parse the command line options to see if an alternative config
// file or the version flag was specified.
preCfg := cfg
preParser := flags.NewParser(&preCfg, flags.Default)
_, err := preParser.Parse()
if err != nil {
e, ok := err.(*flags.Error)
if !ok || e.Type != flags.ErrHelp {
preParser.WriteHelp(os.Stderr)
}
if e.Type == flags.ErrHelp {
os.Exit(0)
}
return loadConfigError(err)
}
// Show the version and exit if the version flag was specified.
appName := filepath.Base(os.Args[0])
appName = strings.TrimSuffix(appName, filepath.Ext(appName))
if preCfg.ShowVersion {
fmt.Println(appName, "version", version())
os.Exit(0)
}
// Load additional config from file.
var configFileError error
parser := flags.NewParser(&cfg, flags.Default)
err = flags.NewIniParser(parser).ParseFile(preCfg.ConfigFile)
if err != nil {
if _, ok := err.(*os.PathError); !ok {
fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr)
return loadConfigError(err)
}
configFileError = err
}
// Parse command line options again to ensure they take precedence.
_, err = parser.Parse()
if err != nil {
if e, ok := err.(*flags.Error); !ok || e.Type != flags.ErrHelp {
parser.WriteHelp(os.Stderr)
}
return loadConfigError(err)
}
// Warn about missing config file after the final command line parse
// succeeds. This prevents the warning on help messages and invalid
// options.
if configFileError != nil {
log.Warnf("%v", configFileError)
}
// Create the home directory if it doesn't already exist.
funcName := "loadConfig"
err = os.MkdirAll(dcrticketbuyerHomeDir, 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 loadConfigError(err)
}
// Choose the active network params based on the selected network.
// Multiple networks can't be selected simultaneously.
numNets := 0
activeNet = &netparams.MainNetParams
if cfg.TestNet {
activeNet = &netparams.TestNetParams
numNets++
}
if cfg.SimNet {
activeNet = &netparams.SimNetParams
numNets++
}
if numNets > 1 {
str := "%s: The testnet and simnet params can't be used " +
"together -- choose one"
err := fmt.Errorf(str, "loadConfig")
fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr)
return loadConfigError(err)
}
// Append the network type to the data directory so it is "namespaced"
// per network. The CSV files for the web UI will go here.
// All data is specific to a network, so namespacing the data directory
// means each individual piece of serialized data does not have to
// worry about changing names per network and such.
cfg.DataDir = cleanAndExpandPath(cfg.DataDir)
cfg.DataDir = filepath.Join(cfg.DataDir, activeNet.Name)
// Create the data dir if it does not exist.
err = os.MkdirAll(cfg.DataDir, 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 data directory: %v"
err := fmt.Errorf(str, funcName, err)
fmt.Fprintln(os.Stderr, err)
return loadConfigError(err)
}
// If the user has set a pool address, the pool fees for the
// pool can not be zero.
if cfg.PoolAddress != "" && cfg.PoolFees == 0.0 {
str := "%s: Pool address is set but pool fees are unset or 0.00%%"
err := fmt.Errorf(str, "loadConfig")
fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr)
return loadConfigError(err)
}
// Set the host names and ports to the default if the
// user does not specify them.
if cfg.DcrdServ == "" {
cfg.DcrdServ = defaultHost + ":" + activeNet.RPCClientPort
}
if cfg.DcrwServ == "" {
cfg.DcrwServ = defaultHost + ":" + activeNet.RPCServerPort
}
// The HTTP server port can not be beyond a uint16's size in value.
if cfg.HTTPSvrPort > 0xffff {
str := "%s: Invalid HTTP port number for HTTP server"
err := fmt.Errorf(str, "loadConfig")
fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr)
return loadConfigError(err)
}
// Make sure the fee source type given is valid.
switch cfg.FeeSource {
case useMeanStr:
case useMedianStr:
default:
str := "%s: Invalid fee source '%s'"
err := fmt.Errorf(str, "loadConfig", cfg.FeeSource)
fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr)
return loadConfigError(err)
}
// Make sure a valid average price mode is given.
switch cfg.AvgPriceMode {
case useVWAPStr:
case usePoolPriceStr:
case useDualPriceStr:
default:
str := "%s: Invalid average price mode '%s'"
err := fmt.Errorf(str, "loadConfig", cfg.AvgPriceMode)
fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr)
return loadConfigError(err)
}
// Append the network type to the log directory so it is "namespaced"
// per network.
cfg.LogDir = cleanAndExpandPath(cfg.LogDir)
cfg.LogDir = filepath.Join(cfg.LogDir, activeNet.Name)
// Special show command to list supported subsystems and exit.
if cfg.DebugLevel == "show" {
fmt.Println("Supported subsystems", supportedSubsystems())
os.Exit(0)
}
// Initialize logging at the default logging level.
initSeelogLogger(filepath.Join(cfg.LogDir, defaultLogFilename))
setLogLevels(defaultLogLevel)
// Parse, validate, and set debug log level(s).
if err := parseAndSetDebugLevels(cfg.DebugLevel); err != nil {
err := fmt.Errorf("%s: %v", "loadConfig", err.Error())
fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr)
return loadConfigError(err)
}
csvPath = cfg.DataDir
return &cfg, nil
}