Skip to content

Commit ea6f5e8

Browse files
committedMay 16, 2024·
config: Introduce NetworkFromName.
This moves the mapping logic to a more obvious home and enables it to be reused later.
1 parent 92a0eb7 commit ea6f5e8

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed
 

‎cmd/vspd/config.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var (
3333
defaultMaxLogSize = int64(10)
3434
defaultLogsToKeep = 20
3535
defaultVSPFee = 3.0
36-
defaultNetwork = "testnet"
36+
defaultNetworkName = "testnet"
3737
defaultHomeDir = dcrutil.AppDataDir(appName, false)
3838
defaultConfigFilename = fmt.Sprintf("%s.conf", appName)
3939
defaultDBFilename = fmt.Sprintf("%s.db", appName)
@@ -52,7 +52,7 @@ type vspdConfig struct {
5252
LogLevel string `long:"loglevel" ini-name:"loglevel" description:"Logging level." choice:"trace" choice:"debug" choice:"info" choice:"warn" choice:"error" choice:"critical"`
5353
MaxLogSize int64 `long:"maxlogsize" ini-name:"maxlogsize" description:"File size threshold for log file rotation (MB)."`
5454
LogsToKeep int `long:"logstokeep" ini-name:"logstokeep" description:"The number of rotated log files to keep."`
55-
Network string `long:"network" ini-name:"network" description:"Decred network to use." choice:"testnet" choice:"mainnet" choice:"simnet"`
55+
NetworkName string `long:"network" ini-name:"network" description:"Decred network to use." choice:"testnet" choice:"mainnet" choice:"simnet"`
5656
VSPFee float64 `long:"vspfee" ini-name:"vspfee" description:"Fee percentage charged for VSP use. eg. 2.0 (2%), 0.5 (0.5%)."`
5757
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."`
5858
DcrdUser string `long:"dcrduser" ini-name:"dcrduser" description:"Username for dcrd RPC connections."`
@@ -178,7 +178,7 @@ func loadConfig() (*vspdConfig, error) {
178178
LogLevel: defaultLogLevel,
179179
MaxLogSize: defaultMaxLogSize,
180180
LogsToKeep: defaultLogsToKeep,
181-
Network: defaultNetwork,
181+
NetworkName: defaultNetworkName,
182182
VSPFee: defaultVSPFee,
183183
HomeDir: defaultHomeDir,
184184
ConfigFile: defaultConfigFile,
@@ -279,13 +279,9 @@ func loadConfig() (*vspdConfig, error) {
279279
}
280280

281281
// Set the active network.
282-
switch cfg.Network {
283-
case "testnet":
284-
cfg.network = &config.TestNet3
285-
case "mainnet":
286-
cfg.network = &config.MainNet
287-
case "simnet":
288-
cfg.network = &config.SimNet
282+
cfg.network, err = config.NetworkFromName(cfg.NetworkName)
283+
if err != nil {
284+
return nil, err
289285
}
290286

291287
// Ensure backup interval is greater than 30 seconds.

‎internal/config/network.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
// Copyright (c) 2020-2023 The Decred developers
1+
// Copyright (c) 2020-2024 The Decred developers
22
// Use of this source code is governed by an ISC
33
// license that can be found in the LICENSE file.
44

55
package config
66

77
import (
8+
"fmt"
9+
810
"github.com/decred/dcrd/chaincfg/v3"
911
)
1012

@@ -76,6 +78,19 @@ var SimNet = Network{
7678
DCP0012Height: 1,
7779
}
7880

81+
func NetworkFromName(name string) (*Network, error) {
82+
switch name {
83+
case "mainnet":
84+
return &MainNet, nil
85+
case "testnet":
86+
return &TestNet3, nil
87+
case "simnet":
88+
return &SimNet, nil
89+
default:
90+
return nil, fmt.Errorf("%q is not a supported network", name)
91+
}
92+
}
93+
7994
// DCP5Active returns true if the DCP-0005 block header commitments agenda is
8095
// active on this network at the provided height, otherwise false.
8196
func (n *Network) DCP5Active(height int64) bool {

0 commit comments

Comments
 (0)
Please sign in to comment.