Skip to content

Commit 10457e6

Browse files
committed
vspd: Accessors for derived config values.
This enables the derived config values, which are not exported, to still be accessed after config.go is moved to the internal vspd package.
1 parent ba5b1b2 commit 10457e6

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

cmd/vspd/config.go

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,34 @@ type vspdConfig struct {
5959
HomeDir string `long:"homedir" no-ini:"true" description:"Path to application home directory. Used for storing VSP database and logs."`
6060
ConfigFile string `long:"configfile" no-ini:"true" description:"DEPRECATED: This behavior is no longer available and this option will be removed in a future version of the software."`
6161

62-
logPath string
63-
dbPath string
62+
// The following fields are derived from the above fields by loadConfig().
63+
dataDir string
6464
network *config.Network
6565
dcrdCert []byte
6666
walletHosts, walletUsers, walletPasswords []string
6767
walletCerts [][]byte
6868
}
6969

70+
func (cfg *vspdConfig) Network() *config.Network {
71+
return cfg.network
72+
}
73+
74+
func (cfg *vspdConfig) LogDir() string {
75+
return filepath.Join(cfg.HomeDir, "logs", cfg.network.Name)
76+
}
77+
78+
func (cfg *vspdConfig) DatabaseFile() string {
79+
return filepath.Join(cfg.dataDir, dbFilename)
80+
}
81+
82+
func (cfg *vspdConfig) DcrdDetails() (string, string, string, []byte) {
83+
return cfg.DcrdUser, cfg.DcrdPass, cfg.DcrdHost, cfg.dcrdCert
84+
}
85+
86+
func (cfg *vspdConfig) WalletDetails() ([]string, []string, []string, [][]byte) {
87+
return cfg.walletUsers, cfg.walletPasswords, cfg.walletHosts, cfg.walletCerts
88+
}
89+
7090
var defaultConfig = vspdConfig{
7191
Listen: ":8800",
7292
LogLevel: "debug",
@@ -370,24 +390,20 @@ func loadConfig() (*vspdConfig, error) {
370390
cfg.DcrdHost = normalizeAddress(cfg.DcrdHost, cfg.network.DcrdRPCServerPort)
371391

372392
// Create the data directory.
373-
dataDir := filepath.Join(cfg.HomeDir, "data", cfg.network.Name)
374-
err = os.MkdirAll(dataDir, 0700)
393+
cfg.dataDir = filepath.Join(cfg.HomeDir, "data", cfg.network.Name)
394+
err = os.MkdirAll(cfg.dataDir, 0700)
375395
if err != nil {
376396
return nil, fmt.Errorf("failed to create data directory: %w", err)
377397
}
378398

379-
// Set the log path.
380-
cfg.logPath = filepath.Join(cfg.HomeDir, "logs", cfg.network.Name)
381-
382-
// Set the database path.
383-
cfg.dbPath = filepath.Join(dataDir, dbFilename)
399+
dbPath := cfg.DatabaseFile()
384400

385401
// If xpub has been provided, create a new database and exit.
386402
if cfg.FeeXPub != "" {
387403
// If database already exists, return error.
388-
if fileExists(cfg.dbPath) {
404+
if fileExists(dbPath) {
389405
return nil, fmt.Errorf("database already initialized at %s, "+
390-
"--feexpub option is not needed", cfg.dbPath)
406+
"--feexpub option is not needed", dbPath)
391407
}
392408

393409
// Ensure provided value is a valid key for the selected network.
@@ -397,10 +413,10 @@ func loadConfig() (*vspdConfig, error) {
397413
}
398414

399415
// Create new database.
400-
fmt.Printf("Initializing new database at %s\n", cfg.dbPath)
401-
err = database.CreateNew(cfg.dbPath, cfg.FeeXPub)
416+
fmt.Printf("Initializing new database at %s\n", dbPath)
417+
err = database.CreateNew(dbPath, cfg.FeeXPub)
402418
if err != nil {
403-
return nil, fmt.Errorf("error creating db file %s: %w", cfg.dbPath, err)
419+
return nil, fmt.Errorf("error creating db file %s: %w", dbPath, err)
404420
}
405421

406422
// Exit with success
@@ -409,9 +425,9 @@ func loadConfig() (*vspdConfig, error) {
409425
}
410426

411427
// If database does not exist, return error.
412-
if !fileExists(cfg.dbPath) {
428+
if !fileExists(dbPath) {
413429
return nil, fmt.Errorf("no database exists in %s. Run vspd with the"+
414-
" --feexpub option to initialize one", dataDir)
430+
" --feexpub option to initialize one", cfg.dataDir)
415431
}
416432

417433
return &cfg, nil

cmd/vspd/main.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func main() {
3838
// returns a function which can be used to create ready-to-use subsystem
3939
// loggers.
4040
func initLogging(cfg *vspdConfig) (func(subsystem string) slog.Logger, error) {
41-
backend, err := newLogBackend(cfg.logPath, "vspd", cfg.MaxLogSize, cfg.LogsToKeep)
41+
backend, err := newLogBackend(cfg.LogDir(), "vspd", cfg.MaxLogSize, cfg.LogsToKeep)
4242
if err != nil {
4343
return nil, fmt.Errorf("failed to initialize logger: %w", err)
4444
}
@@ -82,7 +82,9 @@ func run() int {
8282
log.Criticalf("Version %s (Go version %s %s/%s)", version.String(),
8383
runtime.Version(), runtime.GOOS, runtime.GOARCH)
8484

85-
if cfg.network == &config.MainNet && version.IsPreRelease() {
85+
network := cfg.Network()
86+
87+
if network == &config.MainNet && version.IsPreRelease() {
8688
log.Warnf("")
8789
log.Warnf("\tWARNING: This is a pre-release version of vspd which should not be used on mainnet")
8890
log.Warnf("")
@@ -101,7 +103,7 @@ func run() int {
101103
}
102104

103105
// Open database.
104-
db, err := database.Open(cfg.dbPath, makeLogger(" DB"), maxVoteChangeRecords)
106+
db, err := database.Open(cfg.DatabaseFile(), makeLogger(" DB"), maxVoteChangeRecords)
105107
if err != nil {
106108
log.Errorf("Failed to open database: %v", err)
107109
return 1
@@ -116,18 +118,21 @@ func run() int {
116118

117119
// Create RPC client for local dcrd instance (used for broadcasting and
118120
// checking the status of fee transactions).
119-
dcrd := rpc.SetupDcrd(cfg.DcrdUser, cfg.DcrdPass, cfg.DcrdHost, cfg.dcrdCert, cfg.network.Params, rpcLog, blockNotifChan)
121+
dUser, dPass, dHost, dCert := cfg.DcrdDetails()
122+
dcrd := rpc.SetupDcrd(dUser, dPass, dHost, dCert, network.Params, rpcLog, blockNotifChan)
123+
120124
defer dcrd.Close()
121125

122126
// Create RPC client for remote dcrwallet instances (used for voting).
123-
wallets := rpc.SetupWallet(cfg.walletUsers, cfg.walletPasswords, cfg.walletHosts, cfg.walletCerts, cfg.network.Params, rpcLog)
127+
wUsers, wPasswords, wHosts, wCerts := cfg.WalletDetails()
128+
wallets := rpc.SetupWallet(wUsers, wPasswords, wHosts, wCerts, network.Params, rpcLog)
124129
defer wallets.Close()
125130

126131
// Create webapi server.
127132
apiCfg := webapi.Config{
128133
Listen: cfg.Listen,
129134
VSPFee: cfg.VSPFee,
130-
Network: cfg.network,
135+
Network: network,
131136
SupportEmail: cfg.SupportEmail,
132137
VspClosed: cfg.VspClosed,
133138
VspClosedMsg: cfg.VspClosedMsg,
@@ -154,7 +159,7 @@ func run() int {
154159
}()
155160

156161
// Start vspd.
157-
vspd := vspd.New(cfg.network, log, db, dcrd, wallets, blockNotifChan)
162+
vspd := vspd.New(network, log, db, dcrd, wallets, blockNotifChan)
158163
wg.Add(1)
159164
go func() {
160165
vspd.Run(ctx)

0 commit comments

Comments
 (0)