github.com/lbryio/lbcd@v0.22.119/database/cmd/dbtool/fetchblockregion.go (about)

     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  	"encoding/hex"
     9  	"errors"
    10  	"strconv"
    11  	"time"
    12  
    13  	"github.com/lbryio/lbcd/chaincfg/chainhash"
    14  	"github.com/lbryio/lbcd/database"
    15  )
    16  
    17  // blockRegionCmd defines the configuration options for the fetchblockregion
    18  // command.
    19  type blockRegionCmd struct{}
    20  
    21  var (
    22  	// blockRegionCfg defines the configuration options for the command.
    23  	blockRegionCfg = blockRegionCmd{}
    24  )
    25  
    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 {
    30  		return err
    31  	}
    32  
    33  	// Ensure expected arguments.
    34  	if len(args) < 1 {
    35  		return errors.New("required block hash parameter not specified")
    36  	}
    37  	if len(args) < 2 {
    38  		return errors.New("required start offset parameter not " +
    39  			"specified")
    40  	}
    41  	if len(args) < 3 {
    42  		return errors.New("required region length parameter not " +
    43  			"specified")
    44  	}
    45  
    46  	// Parse arguments.
    47  	blockHash, err := chainhash.NewHashFromStr(args[0])
    48  	if err != nil {
    49  		return err
    50  	}
    51  	startOffset, err := strconv.ParseUint(args[1], 10, 32)
    52  	if err != nil {
    53  		return err
    54  	}
    55  	regionLen, err := strconv.ParseUint(args[2], 10, 32)
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	// Load the block database.
    61  	db, err := loadBlockDB()
    62  	if err != nil {
    63  		return err
    64  	}
    65  	defer db.Close()
    66  
    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{
    71  			Hash:   blockHash,
    72  			Offset: uint32(startOffset),
    73  			Len:    uint32(regionLen),
    74  		}
    75  		startTime := time.Now()
    76  		regionBytes, err := tx.FetchBlockRegion(&region)
    77  		if err != nil {
    78  			return err
    79  		}
    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))
    83  		return nil
    84  	})
    85  }
    86  
    87  // Usage overrides the usage display for the command.
    88  func (cmd *blockRegionCmd) Usage() string {
    89  	return "<block-hash> <start-offset> <length-of-region>"
    90  }