diff --git a/cmd/vspd/config.go b/cmd/vspd/config.go index 3946b034..ecc8324e 100644 --- a/cmd/vspd/config.go +++ b/cmd/vspd/config.go @@ -59,14 +59,34 @@ type vspdConfig struct { HomeDir string `long:"homedir" no-ini:"true" description:"Path to application home directory. Used for storing VSP database and logs."` 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."` - logPath string - dbPath string + // The following fields are derived from the above fields by loadConfig(). + dataDir string network *config.Network dcrdCert []byte walletHosts, walletUsers, walletPasswords []string walletCerts [][]byte } +func (cfg *vspdConfig) Network() *config.Network { + return cfg.network +} + +func (cfg *vspdConfig) LogDir() string { + return filepath.Join(cfg.HomeDir, "logs", cfg.network.Name) +} + +func (cfg *vspdConfig) DatabaseFile() string { + return filepath.Join(cfg.dataDir, dbFilename) +} + +func (cfg *vspdConfig) DcrdDetails() (string, string, string, []byte) { + return cfg.DcrdUser, cfg.DcrdPass, cfg.DcrdHost, cfg.dcrdCert +} + +func (cfg *vspdConfig) WalletDetails() ([]string, []string, []string, [][]byte) { + return cfg.walletUsers, cfg.walletPasswords, cfg.walletHosts, cfg.walletCerts +} + var defaultConfig = vspdConfig{ Listen: ":8800", LogLevel: "debug", @@ -370,24 +390,20 @@ func loadConfig() (*vspdConfig, error) { cfg.DcrdHost = normalizeAddress(cfg.DcrdHost, cfg.network.DcrdRPCServerPort) // Create the data directory. - dataDir := filepath.Join(cfg.HomeDir, "data", cfg.network.Name) - err = os.MkdirAll(dataDir, 0700) + cfg.dataDir = filepath.Join(cfg.HomeDir, "data", cfg.network.Name) + err = os.MkdirAll(cfg.dataDir, 0700) if err != nil { return nil, fmt.Errorf("failed to create data directory: %w", err) } - // Set the log path. - cfg.logPath = filepath.Join(cfg.HomeDir, "logs", cfg.network.Name) - - // Set the database path. - cfg.dbPath = filepath.Join(dataDir, dbFilename) + dbPath := cfg.DatabaseFile() // If xpub has been provided, create a new database and exit. if cfg.FeeXPub != "" { // If database already exists, return error. - if fileExists(cfg.dbPath) { + if fileExists(dbPath) { return nil, fmt.Errorf("database already initialized at %s, "+ - "--feexpub option is not needed", cfg.dbPath) + "--feexpub option is not needed", dbPath) } // Ensure provided value is a valid key for the selected network. @@ -397,10 +413,10 @@ func loadConfig() (*vspdConfig, error) { } // Create new database. - fmt.Printf("Initializing new database at %s\n", cfg.dbPath) - err = database.CreateNew(cfg.dbPath, cfg.FeeXPub) + fmt.Printf("Initializing new database at %s\n", dbPath) + err = database.CreateNew(dbPath, cfg.FeeXPub) if err != nil { - return nil, fmt.Errorf("error creating db file %s: %w", cfg.dbPath, err) + return nil, fmt.Errorf("error creating db file %s: %w", dbPath, err) } // Exit with success @@ -409,9 +425,9 @@ func loadConfig() (*vspdConfig, error) { } // If database does not exist, return error. - if !fileExists(cfg.dbPath) { + if !fileExists(dbPath) { return nil, fmt.Errorf("no database exists in %s. Run vspd with the"+ - " --feexpub option to initialize one", dataDir) + " --feexpub option to initialize one", cfg.dataDir) } return &cfg, nil diff --git a/cmd/vspd/main.go b/cmd/vspd/main.go index f3e4dbac..0bf2c45e 100644 --- a/cmd/vspd/main.go +++ b/cmd/vspd/main.go @@ -38,7 +38,7 @@ func main() { // returns a function which can be used to create ready-to-use subsystem // loggers. func initLogging(cfg *vspdConfig) (func(subsystem string) slog.Logger, error) { - backend, err := newLogBackend(cfg.logPath, "vspd", cfg.MaxLogSize, cfg.LogsToKeep) + backend, err := newLogBackend(cfg.LogDir(), "vspd", cfg.MaxLogSize, cfg.LogsToKeep) if err != nil { return nil, fmt.Errorf("failed to initialize logger: %w", err) } @@ -82,7 +82,9 @@ func run() int { log.Criticalf("Version %s (Go version %s %s/%s)", version.String(), runtime.Version(), runtime.GOOS, runtime.GOARCH) - if cfg.network == &config.MainNet && version.IsPreRelease() { + network := cfg.Network() + + if network == &config.MainNet && version.IsPreRelease() { log.Warnf("") log.Warnf("\tWARNING: This is a pre-release version of vspd which should not be used on mainnet") log.Warnf("") @@ -101,7 +103,7 @@ func run() int { } // Open database. - db, err := database.Open(cfg.dbPath, makeLogger(" DB"), maxVoteChangeRecords) + db, err := database.Open(cfg.DatabaseFile(), makeLogger(" DB"), maxVoteChangeRecords) if err != nil { log.Errorf("Failed to open database: %v", err) return 1 @@ -116,18 +118,21 @@ func run() int { // Create RPC client for local dcrd instance (used for broadcasting and // checking the status of fee transactions). - dcrd := rpc.SetupDcrd(cfg.DcrdUser, cfg.DcrdPass, cfg.DcrdHost, cfg.dcrdCert, cfg.network.Params, rpcLog, blockNotifChan) + dUser, dPass, dHost, dCert := cfg.DcrdDetails() + dcrd := rpc.SetupDcrd(dUser, dPass, dHost, dCert, network.Params, rpcLog, blockNotifChan) + defer dcrd.Close() // Create RPC client for remote dcrwallet instances (used for voting). - wallets := rpc.SetupWallet(cfg.walletUsers, cfg.walletPasswords, cfg.walletHosts, cfg.walletCerts, cfg.network.Params, rpcLog) + wUsers, wPasswords, wHosts, wCerts := cfg.WalletDetails() + wallets := rpc.SetupWallet(wUsers, wPasswords, wHosts, wCerts, network.Params, rpcLog) defer wallets.Close() // Create webapi server. apiCfg := webapi.Config{ Listen: cfg.Listen, VSPFee: cfg.VSPFee, - Network: cfg.network, + Network: network, SupportEmail: cfg.SupportEmail, VspClosed: cfg.VspClosed, VspClosedMsg: cfg.VspClosedMsg, @@ -154,7 +159,7 @@ func run() int { }() // Start vspd. - vspd := vspd.New(cfg.network, log, db, dcrd, wallets, blockNotifChan) + vspd := vspd.New(network, log, db, dcrd, wallets, blockNotifChan) wg.Add(1) go func() { vspd.Run(ctx)