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.
13 "github.com/btcsuite/btcd/chaincfg/chainhash"
14 "github.com/btcsuite/btcd/database"
17 // blockRegionCmd defines the configuration options for the fetchblockregion
19 type blockRegionCmd struct{}
22 // blockRegionCfg defines the configuration options for the command.
23 blockRegionCfg = blockRegionCmd{}
26 // Execute is the main entry point for the command. It's invoked by the parser.
27 func (cmd *blockRegionCmd) Execute(args []string) error {
28 // Setup the global config options and ensure they are valid.
29 if err := setupGlobalConfig(); err != nil {
33 // Ensure expected arguments.
35 return errors.New("required block hash parameter not specified")
38 return errors.New("required start offset parameter not " +
42 return errors.New("required region length parameter not " +
47 blockHash, err := chainhash.NewHashFromStr(args[0])
51 startOffset, err := strconv.ParseUint(args[1], 10, 32)
55 regionLen, err := strconv.ParseUint(args[2], 10, 32)
60 // Load the block database.
61 db, err := loadBlockDB()
67 return db.View(func(tx database.Tx) error {
68 log.Infof("Fetching block region %s<%d:%d>", blockHash,
69 startOffset, startOffset+regionLen-1)
70 region := database.BlockRegion{
72 Offset: uint32(startOffset),
73 Len: uint32(regionLen),
75 startTime := time.Now()
76 regionBytes, err := tx.FetchBlockRegion(®ion)
80 log.Infof("Loaded block region in %v", time.Since(startTime))
81 log.Infof("Double Hash: %s", chainhash.DoubleHashH(regionBytes))
82 log.Infof("Region Hex: %s", hex.EncodeToString(regionBytes))
87 // Usage overrides the usage display for the command.
88 func (cmd *blockRegionCmd) Usage() string {
89 return "<block-hash> <start-offset> <length-of-region>"