OSDN Git Service

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