github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/database/cmd/dbtool/fetchblockregion.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  	"encoding/hex"
    10  	"errors"
    11  	"strconv"
    12  	"time"
    13  
    14  	"github.com/dashpay/godash/database"
    15  	"github.com/dashpay/godash/wire"
    16  )
    17  
    18  // blockRegionCmd defines the configuration options for the fetchblockregion
    19  // command.
    20  type blockRegionCmd struct{}
    21  
    22  var (
    23  	// blockRegionCfg defines the configuration options for the command.
    24  	blockRegionCfg = blockRegionCmd{}
    25  )
    26  
    27  // Execute is the main entry point for the command.  It's invoked by the parser.
    28  func (cmd *blockRegionCmd) Execute(args []string) error {
    29  	// Setup the global config options and ensure they are valid.
    30  	if err := setupGlobalConfig(); err != nil {
    31  		return err
    32  	}
    33  
    34  	// Ensure expected arguments.
    35  	if len(args) < 1 {
    36  		return errors.New("required block hash parameter not specified")
    37  	}
    38  	if len(args) < 2 {
    39  		return errors.New("required start offset parameter not " +
    40  			"specified")
    41  	}
    42  	if len(args) < 3 {
    43  		return errors.New("required region length parameter not " +
    44  			"specified")
    45  	}
    46  
    47  	// Parse arguments.
    48  	blockHash, err := wire.NewShaHashFromStr(args[0])
    49  	if err != nil {
    50  		return err
    51  	}
    52  	startOffset, err := strconv.ParseUint(args[1], 10, 32)
    53  	if err != nil {
    54  		return err
    55  	}
    56  	regionLen, err := strconv.ParseUint(args[2], 10, 32)
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	// Load the block database.
    62  	db, err := loadBlockDB()
    63  	if err != nil {
    64  		return err
    65  	}
    66  	defer db.Close()
    67  
    68  	return db.View(func(tx database.Tx) error {
    69  		log.Infof("Fetching block region %s<%d:%d>", blockHash,
    70  			startOffset, startOffset+regionLen-1)
    71  		region := database.BlockRegion{
    72  			Hash:   blockHash,
    73  			Offset: uint32(startOffset),
    74  			Len:    uint32(regionLen),
    75  		}
    76  		startTime := time.Now()
    77  		regionBytes, err := tx.FetchBlockRegion(&region)
    78  		if err != nil {
    79  			return err
    80  		}
    81  		log.Infof("Loaded block region in %v", time.Now().Sub(startTime))
    82  		log.Infof("Double SHA256: %s", wire.DoubleSha256SH(regionBytes))
    83  		log.Infof("Region Hex: %s", hex.EncodeToString(regionBytes))
    84  		return nil
    85  	})
    86  }
    87  
    88  // Usage overrides the usage display for the command.
    89  func (cmd *blockRegionCmd) Usage() string {
    90  	return "<block-hash> <start-offset> <length-of-region>"
    91  }