github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/database/cmd/dbtool/main.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  	"os"
    10  	"path/filepath"
    11  	"runtime"
    12  	"strings"
    13  
    14  	"github.com/btcsuite/btclog"
    15  	flags "github.com/btcsuite/go-flags"
    16  	"github.com/BlockABC/godash/database"
    17  )
    18  
    19  const (
    20  	// blockDbNamePrefix is the prefix for the btcd block database.
    21  	blockDbNamePrefix = "blocks"
    22  )
    23  
    24  var (
    25  	log             btclog.Logger
    26  	shutdownChannel = make(chan error)
    27  )
    28  
    29  // loadBlockDB opens the block database and returns a handle to it.
    30  func loadBlockDB() (database.DB, error) {
    31  	// The database name is based on the database type.
    32  	dbName := blockDbNamePrefix + "_" + cfg.DbType
    33  	dbPath := filepath.Join(cfg.DataDir, dbName)
    34  
    35  	log.Infof("Loading block database from '%s'", dbPath)
    36  	db, err := database.Open(cfg.DbType, dbPath, activeNetParams.Net)
    37  	if err != nil {
    38  		// Return the error if it's not because the database doesn't
    39  		// exist.
    40  		if dbErr, ok := err.(database.Error); !ok || dbErr.ErrorCode !=
    41  			database.ErrDbDoesNotExist {
    42  
    43  			return nil, err
    44  		}
    45  
    46  		// Create the db if it does not exist.
    47  		err = os.MkdirAll(cfg.DataDir, 0700)
    48  		if err != nil {
    49  			return nil, err
    50  		}
    51  		db, err = database.Create(cfg.DbType, dbPath, activeNetParams.Net)
    52  		if err != nil {
    53  			return nil, err
    54  		}
    55  	}
    56  
    57  	log.Info("Block database loaded")
    58  	return db, nil
    59  }
    60  
    61  // realMain is the real main function for the utility.  It is necessary to work
    62  // around the fact that deferred functions do not run when os.Exit() is called.
    63  func realMain() error {
    64  	// Setup logging.
    65  	backendLogger := btclog.NewDefaultBackendLogger()
    66  	defer backendLogger.Flush()
    67  	log = btclog.NewSubsystemLogger(backendLogger, "")
    68  	dbLog := btclog.NewSubsystemLogger(backendLogger, "BCDB: ")
    69  	dbLog.SetLevel(btclog.DebugLvl)
    70  	database.UseLogger(dbLog)
    71  
    72  	// Setup the parser options and commands.
    73  	appName := filepath.Base(os.Args[0])
    74  	appName = strings.TrimSuffix(appName, filepath.Ext(appName))
    75  	parserFlags := flags.Options(flags.HelpFlag | flags.PassDoubleDash)
    76  	parser := flags.NewNamedParser(appName, parserFlags)
    77  	parser.AddGroup("Global Options", "", cfg)
    78  	parser.AddCommand("insecureimport",
    79  		"Insecurely import bulk block data from bootstrap.dat",
    80  		"Insecurely import bulk block data from bootstrap.dat.  "+
    81  			"WARNING: This is NOT secure because it does NOT "+
    82  			"verify chain rules.  It is only provided for testing "+
    83  			"purposes.", &importCfg)
    84  	parser.AddCommand("loadheaders",
    85  		"Time how long to load headers for all blocks in the database",
    86  		"", &headersCfg)
    87  	parser.AddCommand("fetchblock",
    88  		"Fetch the specific block hash from the database", "",
    89  		&fetchBlockCfg)
    90  	parser.AddCommand("fetchblockregion",
    91  		"Fetch the specified block region from the database", "",
    92  		&blockRegionCfg)
    93  
    94  	// Parse command line and invoke the Execute function for the specified
    95  	// command.
    96  	if _, err := parser.Parse(); err != nil {
    97  		if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
    98  			parser.WriteHelp(os.Stderr)
    99  		} else {
   100  			log.Error(err)
   101  		}
   102  
   103  		return err
   104  	}
   105  
   106  	return nil
   107  }
   108  
   109  func main() {
   110  	// Use all processor cores.
   111  	runtime.GOMAXPROCS(runtime.NumCPU())
   112  
   113  	// Work around defer not working after os.Exit()
   114  	if err := realMain(); err != nil {
   115  		os.Exit(1)
   116  	}
   117  }