github.com/lbryio/lbcd@v0.22.119/database/cmd/dbtool/globalconfig.go (about)

     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.
     4  
     5  package main
     6  
     7  import (
     8  	"errors"
     9  	"fmt"
    10  	"os"
    11  	"path/filepath"
    12  
    13  	"github.com/lbryio/lbcd/chaincfg"
    14  	"github.com/lbryio/lbcd/database"
    15  	_ "github.com/lbryio/lbcd/database/ffldb"
    16  	"github.com/lbryio/lbcd/wire"
    17  	btcutil "github.com/lbryio/lbcutil"
    18  )
    19  
    20  var (
    21  	btcdHomeDir     = btcutil.AppDataDir("lbcd", false)
    22  	knownDbTypes    = database.SupportedDrivers()
    23  	activeNetParams = &chaincfg.MainNetParams
    24  
    25  	// Default global config.
    26  	cfg = &config{
    27  		DataDir: filepath.Join(btcdHomeDir, "data"),
    28  		DbType:  "ffldb",
    29  	}
    30  )
    31  
    32  // config defines the global configuration options.
    33  type config struct {
    34  	DataDir        string `short:"b" long:"datadir" description:"Location of the lbcd data directory"`
    35  	DbType         string `long:"dbtype" description:"Database backend to use for the Block Chain"`
    36  	RegressionTest bool   `long:"regtest" description:"Use the regression test network"`
    37  	SimNet         bool   `long:"simnet" description:"Use the simulation test network"`
    38  	TestNet3       bool   `long:"testnet" description:"Use the test network"`
    39  }
    40  
    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) {
    45  			return false
    46  		}
    47  	}
    48  	return true
    49  }
    50  
    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 {
    55  			return true
    56  		}
    57  	}
    58  
    59  	return false
    60  }
    61  
    62  // netName returns the name used when referring to a bitcoin network.  At the
    63  // time of writing, lbcd 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.
    67  //
    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 {
    73  	case wire.TestNet3:
    74  		return "testnet"
    75  	default:
    76  		return chainParams.Name
    77  	}
    78  }
    79  
    80  // setupGlobalConfig examine the global configuration options for any conditions
    81  // which are invalid as well as performs any addition setup necessary after the
    82  // initial parse.
    83  func setupGlobalConfig() error {
    84  	// Multiple networks can't be selected simultaneously.
    85  	// Count number of network flags passed; assign active network params
    86  	// while we're at it
    87  	numNets := 0
    88  	if cfg.TestNet3 {
    89  		numNets++
    90  		activeNetParams = &chaincfg.TestNet3Params
    91  	}
    92  	if cfg.RegressionTest {
    93  		numNets++
    94  		activeNetParams = &chaincfg.RegressionNetParams
    95  	}
    96  	if cfg.SimNet {
    97  		numNets++
    98  		activeNetParams = &chaincfg.SimNetParams
    99  	}
   100  	if numNets > 1 {
   101  		return errors.New("The testnet, regtest, and simnet params " +
   102  			"can't be used together -- choose one of the three")
   103  	}
   104  
   105  	// Validate database type.
   106  	if !validDbType(cfg.DbType) {
   107  		str := "The specified database type [%v] is invalid -- " +
   108  			"supported types %v"
   109  		return fmt.Errorf(str, cfg.DbType, knownDbTypes)
   110  	}
   111  
   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))
   119  
   120  	return nil
   121  }