github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/cmd/addblock/config.go (about)

     1  // Copyright (c) 2013-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  	"fmt"
    10  	"os"
    11  	"path/filepath"
    12  
    13  	flags "github.com/btcsuite/go-flags"
    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  const (
    22  	defaultDbType   = "ffldb"
    23  	defaultDataFile = "bootstrap.dat"
    24  	defaultProgress = 10
    25  )
    26  
    27  var (
    28  	btcdHomeDir     = godashutil.AppDataDir("btcd", false)
    29  	defaultDataDir  = filepath.Join(btcdHomeDir, "data")
    30  	knownDbTypes    = database.SupportedDrivers()
    31  	activeNetParams = &chaincfg.MainNetParams
    32  )
    33  
    34  // config defines the configuration options for findcheckpoint.
    35  //
    36  // See loadConfig for details on the configuration load process.
    37  type config struct {
    38  	DataDir        string `short:"b" long:"datadir" description:"Location of the btcd data directory"`
    39  	DbType         string `long:"dbtype" description:"Database backend to use for the Block Chain"`
    40  	TestNet3       bool   `long:"testnet" description:"Use the test network"`
    41  	RegressionTest bool   `long:"regtest" description:"Use the regression test network"`
    42  	SimNet         bool   `long:"simnet" description:"Use the simulation test network"`
    43  	InFile         string `short:"i" long:"infile" description:"File containing the block(s)"`
    44  	TxIndex        bool   `long:"txindex" description:"Build a full hash-based transaction index which makes all transactions available via the getrawtransaction RPC"`
    45  	AddrIndex      bool   `long:"addrindex" description:"Build a full address-based transaction index which makes the searchrawtransactions RPC available"`
    46  	Progress       int    `short:"p" long:"progress" description:"Show a progress message each time this number of seconds have passed -- Use 0 to disable progress announcements"`
    47  }
    48  
    49  // filesExists reports whether the named file or directory exists.
    50  func fileExists(name string) bool {
    51  	if _, err := os.Stat(name); err != nil {
    52  		if os.IsNotExist(err) {
    53  			return false
    54  		}
    55  	}
    56  	return true
    57  }
    58  
    59  // validDbType returns whether or not dbType is a supported database type.
    60  func validDbType(dbType string) bool {
    61  	for _, knownType := range knownDbTypes {
    62  		if dbType == knownType {
    63  			return true
    64  		}
    65  	}
    66  
    67  	return false
    68  }
    69  
    70  // netName returns the name used when referring to a bitcoin network.  At the
    71  // time of writing, btcd currently places blocks for testnet version 3 in the
    72  // data and log directory "testnet", which does not match the Name field of the
    73  // chaincfg parameters.  This function can be used to override this directory name
    74  // as "testnet" when the passed active network matches wire.TestNet3.
    75  //
    76  // A proper upgrade to move the data and log directories for this network to
    77  // "testnet3" is planned for the future, at which point this function can be
    78  // removed and the network parameter's name used instead.
    79  func netName(chainParams *chaincfg.Params) string {
    80  	switch chainParams.Net {
    81  	case wire.TestNet3:
    82  		return "testnet"
    83  	default:
    84  		return chainParams.Name
    85  	}
    86  }
    87  
    88  // loadConfig initializes and parses the config using command line options.
    89  func loadConfig() (*config, []string, error) {
    90  	// Default config.
    91  	cfg := config{
    92  		DataDir:  defaultDataDir,
    93  		DbType:   defaultDbType,
    94  		InFile:   defaultDataFile,
    95  		Progress: defaultProgress,
    96  	}
    97  
    98  	// Parse command line options.
    99  	parser := flags.NewParser(&cfg, flags.Default)
   100  	remainingArgs, err := parser.Parse()
   101  	if err != nil {
   102  		if e, ok := err.(*flags.Error); !ok || e.Type != flags.ErrHelp {
   103  			parser.WriteHelp(os.Stderr)
   104  		}
   105  		return nil, nil, err
   106  	}
   107  
   108  	// Multiple networks can't be selected simultaneously.
   109  	funcName := "loadConfig"
   110  	numNets := 0
   111  	// Count number of network flags passed; assign active network params
   112  	// while we're at it
   113  	if cfg.TestNet3 {
   114  		numNets++
   115  		activeNetParams = &chaincfg.TestNet3Params
   116  	}
   117  	if cfg.RegressionTest {
   118  		numNets++
   119  		activeNetParams = &chaincfg.RegressionNetParams
   120  	}
   121  	if cfg.SimNet {
   122  		numNets++
   123  		activeNetParams = &chaincfg.SimNetParams
   124  	}
   125  	if numNets > 1 {
   126  		str := "%s: The testnet, regtest, and simnet params can't be " +
   127  			"used together -- choose one of the three"
   128  		err := fmt.Errorf(str, funcName)
   129  		fmt.Fprintln(os.Stderr, err)
   130  		parser.WriteHelp(os.Stderr)
   131  		return nil, nil, err
   132  	}
   133  
   134  	// Validate database type.
   135  	if !validDbType(cfg.DbType) {
   136  		str := "%s: The specified database type [%v] is invalid -- " +
   137  			"supported types %v"
   138  		err := fmt.Errorf(str, "loadConfig", cfg.DbType, knownDbTypes)
   139  		fmt.Fprintln(os.Stderr, err)
   140  		parser.WriteHelp(os.Stderr)
   141  		return nil, nil, err
   142  	}
   143  
   144  	// Append the network type to the data directory so it is "namespaced"
   145  	// per network.  In addition to the block database, there are other
   146  	// pieces of data that are saved to disk such as address manager state.
   147  	// All data is specific to a network, so namespacing the data directory
   148  	// means each individual piece of serialized data does not have to
   149  	// worry about changing names per network and such.
   150  	cfg.DataDir = filepath.Join(cfg.DataDir, netName(activeNetParams))
   151  
   152  	// Ensure the specified block file exists.
   153  	if !fileExists(cfg.InFile) {
   154  		str := "%s: The specified block file [%v] does not exist"
   155  		err := fmt.Errorf(str, "loadConfig", cfg.InFile)
   156  		fmt.Fprintln(os.Stderr, err)
   157  		parser.WriteHelp(os.Stderr)
   158  		return nil, nil, err
   159  	}
   160  
   161  	return &cfg, remainingArgs, nil
   162  }