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.
10 "github.com/btcsuite/btcd/chaincfg/chainhash"
11 "github.com/btcsuite/btcd/database"
14 // headersCmd defines the configuration options for the loadheaders command.
15 type headersCmd struct {
16 Bulk bool `long:"bulk" description:"Use bulk loading of headers instead of one at a time"`
20 // headersCfg defines the configuration options for the command.
21 headersCfg = headersCmd{
26 // Execute is the main entry point for the command. It's invoked by the parser.
27 func (cmd *headersCmd) Execute(args []string) error {
28 // Setup the global config options and ensure they are valid.
29 if err := setupGlobalConfig(); err != nil {
33 // Load the block database.
34 db, err := loadBlockDB()
40 // NOTE: This code will only work for ffldb. Ideally the package using
41 // the database would keep a metadata index of its own.
42 blockIdxName := []byte("ffldb-blockidx")
44 err = db.View(func(tx database.Tx) error {
46 blockIdxBucket := tx.Metadata().Bucket(blockIdxName)
47 blockIdxBucket.ForEach(func(k, v []byte) error {
51 log.Infof("Loading headers for %d blocks...", totalHdrs)
53 startTime := time.Now()
54 blockIdxBucket.ForEach(func(k, v []byte) error {
55 var hash chainhash.Hash
57 _, err := tx.FetchBlockHeader(&hash)
64 log.Infof("Loaded %d headers in %v", numLoaded,
65 time.Since(startTime))
72 err = db.View(func(tx database.Tx) error {
73 blockIdxBucket := tx.Metadata().Bucket(blockIdxName)
74 hashes := make([]chainhash.Hash, 0, 500000)
75 blockIdxBucket.ForEach(func(k, v []byte) error {
76 var hash chainhash.Hash
78 hashes = append(hashes, hash)
82 log.Infof("Loading headers for %d blocks...", len(hashes))
83 startTime := time.Now()
84 hdrs, err := tx.FetchBlockHeaders(hashes)
88 log.Infof("Loaded %d headers in %v", len(hdrs),
89 time.Since(startTime))