github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/database/cmd/dbtool/globalconfig.go (about)

     1  // Copyright (c) 2015-2016 The btcsuite developers
     2  // Copyright (c) 2016 The Dash developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  package main
     7  
     8  import (
     9  	"errors"
    10  	"fmt"
    11  	"os"
    12  	"path/filepath"
    13  
    14  	"github.com/BlockABC/godash/chaincfg"
    15  	"github.com/BlockABC/godash/database"
    16  	_ "github.com/BlockABC/godash/database/ffldb"
    17  	"github.com/BlockABC/godash/wire"
    18  	"github.com/BlockABC/godashutil"
    19  )
    20  
    21  var (
    22  	btcdHomeDir     = godashutil.AppDataDir("btcd", false)
    23  	knownDbTypes    = database.SupportedDrivers()
    24  	activeNetParams = &chaincfg.MainNetParams
    25  
    26  	// Default global config.
    27  	cfg = &config{
    28  		DataDir: filepath.Join(btcdHomeDir, "data"),
    29  		DbType:  "ffldb",
    30  	}
    31  )
    32  
    33  // config defines the global configuration options.
    34  type config struct {
    35  	DataDir        string `short:"b" long:"datadir" description:"Location of the btcd data directory"`
    36  	DbType         string `long:"dbtype" description:"Database backend to use for the Block Chain"`
    37  	TestNet3       bool   `long:"testnet" description:"Use the test network"`
    38  	RegressionTest bool   `long:"regtest" description:"Use the regression test network"`
    39  	SimNet         bool   `long:"simnet" description:"Use the simulation test network"`
    40  }
    41  
    42  // fileExists reports whether the named file or directory exists.
    43  func fileExists(name string) bool {
    44  	if _, err := os.Stat(name); err != nil {
    45  		if os.IsNotExist(err) {
    46  			return false
    47  		}
    48  	}
    49  	return true
    50  }
    51  
    52  // validDbType returns whether or not dbType is a supported database type.
    53  func validDbType(dbType string) bool {
    54  	for _, knownType := range knownDbTypes {
    55  		if dbType == knownType {
    56  			return true
    57  		}
    58  	}
    59  
    60  	return false
    61  }
    62  
    63  // netName returns the name used when referring to a bitcoin network.  At the
    64  // time of writing, btcd currently places blocks for testnet version 3 in the
    65  // data and log directory "testnet", which does not match the Name field of the
    66  // chaincfg parameters.  This function can be used to override this directory name
    67  // as "testnet" when the passed active network matches wire.TestNet3.
    68  //
    69  // A proper upgrade to move the data and log directories for this network to
    70  // "testnet3" is planned for the future, at which point this function can be
    71  // removed and the network parameter's name used instead.
    72  func netName(chainParams *chaincfg.Params) string {
    73  	switch chainParams.Net {
    74  	case wire.TestNet3:
    75  		return "testnet"
    76  	default:
    77  		return chainParams.Name
    78  	}
    79  }
    80  
    81  // setupGlobalConfig examine the global configuration options for any conditions
    82  // which are invalid as well as performs any addition setup necessary after the
    83  // initial parse.
    84  func setupGlobalConfig() error {
    85  	// Multiple networks can't be selected simultaneously.
    86  	// Count number of network flags passed; assign active network params
    87  	// while we're at it
    88  	numNets := 0
    89  	if cfg.TestNet3 {
    90  		numNets++
    91  		activeNetParams = &chaincfg.TestNet3Params
    92  	}
    93  	if cfg.RegressionTest {
    94  		numNets++
    95  		activeNetParams = &chaincfg.RegressionNetParams
    96  	}
    97  	if cfg.SimNet {
    98  		numNets++
    99  		activeNetParams = &chaincfg.SimNetParams
   100  	}
   101  	if numNets > 1 {
   102  		return errors.New("The testnet, regtest, and simnet params " +
   103  			"can't be used together -- choose one of the three")
   104  	}
   105  
   106  	// Validate database type.
   107  	if !validDbType(cfg.DbType) {
   108  		str := "The specified database type [%v] is invalid -- " +
   109  			"supported types %v"
   110  		return fmt.Errorf(str, cfg.DbType, knownDbTypes)
   111  	}
   112  
   113  	// Append the network type to the data directory so it is "namespaced"
   114  	// per network.  In addition to the block database, there are other
   115  	// pieces of data that are saved to disk such as address manager state.
   116  	// All data is specific to a network, so namespacing the data directory
   117  	// means each individual piece of serialized data does not have to
   118  	// worry about changing names per network and such.
   119  	cfg.DataDir = filepath.Join(cfg.DataDir, netName(activeNetParams))
   120  
   121  	return nil
   122  }