Skip to content

Commit

Permalink
config: Introduce NetworkFromName.
Browse files Browse the repository at this point in the history
This moves the mapping logic to a more obvious home and enables it to be
reused later.
  • Loading branch information
jholdstock committed May 16, 2024
1 parent 92a0eb7 commit ea6f5e8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
16 changes: 6 additions & 10 deletions cmd/vspd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var (
defaultMaxLogSize = int64(10)
defaultLogsToKeep = 20
defaultVSPFee = 3.0
defaultNetwork = "testnet"
defaultNetworkName = "testnet"
defaultHomeDir = dcrutil.AppDataDir(appName, false)
defaultConfigFilename = fmt.Sprintf("%s.conf", appName)
defaultDBFilename = fmt.Sprintf("%s.db", appName)
Expand All @@ -52,7 +52,7 @@ type vspdConfig struct {
LogLevel string `long:"loglevel" ini-name:"loglevel" description:"Logging level." choice:"trace" choice:"debug" choice:"info" choice:"warn" choice:"error" choice:"critical"`
MaxLogSize int64 `long:"maxlogsize" ini-name:"maxlogsize" description:"File size threshold for log file rotation (MB)."`
LogsToKeep int `long:"logstokeep" ini-name:"logstokeep" description:"The number of rotated log files to keep."`
Network string `long:"network" ini-name:"network" description:"Decred network to use." choice:"testnet" choice:"mainnet" choice:"simnet"`
NetworkName string `long:"network" ini-name:"network" description:"Decred network to use." choice:"testnet" choice:"mainnet" choice:"simnet"`
VSPFee float64 `long:"vspfee" ini-name:"vspfee" description:"Fee percentage charged for VSP use. eg. 2.0 (2%), 0.5 (0.5%)."`
DcrdHost string `long:"dcrdhost" ini-name:"dcrdhost" description:"The ip:port to establish a JSON-RPC connection with dcrd. Should be the same host where vspd is running."`
DcrdUser string `long:"dcrduser" ini-name:"dcrduser" description:"Username for dcrd RPC connections."`
Expand Down Expand Up @@ -178,7 +178,7 @@ func loadConfig() (*vspdConfig, error) {
LogLevel: defaultLogLevel,
MaxLogSize: defaultMaxLogSize,
LogsToKeep: defaultLogsToKeep,
Network: defaultNetwork,
NetworkName: defaultNetworkName,
VSPFee: defaultVSPFee,
HomeDir: defaultHomeDir,
ConfigFile: defaultConfigFile,
Expand Down Expand Up @@ -279,13 +279,9 @@ func loadConfig() (*vspdConfig, error) {
}

// Set the active network.
switch cfg.Network {
case "testnet":
cfg.network = &config.TestNet3
case "mainnet":
cfg.network = &config.MainNet
case "simnet":
cfg.network = &config.SimNet
cfg.network, err = config.NetworkFromName(cfg.NetworkName)
if err != nil {
return nil, err
}

// Ensure backup interval is greater than 30 seconds.
Expand Down
17 changes: 16 additions & 1 deletion internal/config/network.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Copyright (c) 2020-2023 The Decred developers
// Copyright (c) 2020-2024 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package config

import (
"fmt"

"github.com/decred/dcrd/chaincfg/v3"
)

Expand Down Expand Up @@ -76,6 +78,19 @@ var SimNet = Network{
DCP0012Height: 1,
}

func NetworkFromName(name string) (*Network, error) {
switch name {
case "mainnet":
return &MainNet, nil
case "testnet":
return &TestNet3, nil
case "simnet":
return &SimNet, nil
default:
return nil, fmt.Errorf("%q is not a supported network", name)
}
}

// DCP5Active returns true if the DCP-0005 block header commitments agenda is
// active on this network at the provided height, otherwise false.
func (n *Network) DCP5Active(height int64) bool {
Expand Down

0 comments on commit ea6f5e8

Please sign in to comment.