1 // Copyright (c) 2015-2016 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
13 "github.com/btcsuite/btcd/chaincfg"
14 "github.com/btcsuite/btcd/database"
15 _ "github.com/btcsuite/btcd/database/ffldb"
16 "github.com/btcsuite/btcd/wire"
17 "github.com/btcsuite/btcutil"
21 btcdHomeDir = btcutil.AppDataDir("btcd", false)
22 knownDbTypes = database.SupportedDrivers()
23 activeNetParams = &chaincfg.MainNetParams
25 // Default global config.
27 DataDir: filepath.Join(btcdHomeDir, "data"),
32 // config defines the global configuration options.
34 DataDir string `short:"b" long:"datadir" description:"Location of the btcd data directory"`
35 DbType string `long:"dbtype" description:"Database backend to use for the Block Chain"`
36 TestNet3 bool `long:"testnet" description:"Use the test network"`
37 RegressionTest bool `long:"regtest" description:"Use the regression test network"`
38 SimNet bool `long:"simnet" description:"Use the simulation test network"`
41 // fileExists reports whether the named file or directory exists.
42 func fileExists(name string) bool {
43 if _, err := os.Stat(name); err != nil {
44 if os.IsNotExist(err) {
51 // validDbType returns whether or not dbType is a supported database type.
52 func validDbType(dbType string) bool {
53 for _, knownType := range knownDbTypes {
54 if dbType == knownType {
62 // netName returns the name used when referring to a bitcoin network. At the
63 // time of writing, btcd currently places blocks for testnet version 3 in the
64 // data and log directory "testnet", which does not match the Name field of the
65 // chaincfg parameters. This function can be used to override this directory name
66 // as "testnet" when the passed active network matches wire.TestNet3.
68 // A proper upgrade to move the data and log directories for this network to
69 // "testnet3" is planned for the future, at which point this function can be
70 // removed and the network parameter's name used instead.
71 func netName(chainParams *chaincfg.Params) string {
72 switch chainParams.Net {
76 return chainParams.Name
80 // setupGlobalConfig examine the global configuration options for any conditions
81 // which are invalid as well as performs any addition setup necessary after the
83 func setupGlobalConfig() error {
84 // Multiple networks can't be selected simultaneously.
85 // Count number of network flags passed; assign active network params
90 activeNetParams = &chaincfg.TestNet3Params
92 if cfg.RegressionTest {
94 activeNetParams = &chaincfg.RegressionNetParams
98 activeNetParams = &chaincfg.SimNetParams
101 return errors.New("The testnet, regtest, and simnet params " +
102 "can't be used together -- choose one of the three")
105 // Validate database type.
106 if !validDbType(cfg.DbType) {
107 str := "The specified database type [%v] is invalid -- " +
109 return fmt.Errorf(str, cfg.DbType, knownDbTypes)
112 // Append the network type to the data directory so it is "namespaced"
113 // per network. In addition to the block database, there are other
114 // pieces of data that are saved to disk such as address manager state.
115 // All data is specific to a network, so namespacing the data directory
116 // means each individual piece of serialized data does not have to
117 // worry about changing names per network and such.
118 cfg.DataDir = filepath.Join(cfg.DataDir, netName(activeNetParams))