github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/database/cmd/dbtool/fetchblock.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  	"time"
    12  
    13  	"github.com/BlockABC/godash/database"
    14  	"github.com/BlockABC/godash/wire"
    15  )
    16  
    17  // fetchBlockCmd defines the configuration options for the fetchblock command.
    18  type fetchBlockCmd struct{}
    19  
    20  var (
    21  	// fetchBlockCfg defines the configuration options for the command.
    22  	fetchBlockCfg = fetchBlockCmd{}
    23  )
    24  
    25  // Execute is the main entry point for the command.  It's invoked by the parser.
    26  func (cmd *fetchBlockCmd) Execute(args []string) error {
    27  	// Setup the global config options and ensure they are valid.
    28  	if err := setupGlobalConfig(); err != nil {
    29  		return err
    30  	}
    31  
    32  	if len(args) < 1 {
    33  		return errors.New("required block hash parameter not specified")
    34  	}
    35  	blockHash, err := wire.NewShaHashFromStr(args[0])
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	// Load the block database.
    41  	db, err := loadBlockDB()
    42  	if err != nil {
    43  		return err
    44  	}
    45  	defer db.Close()
    46  
    47  	return db.View(func(tx database.Tx) error {
    48  		log.Infof("Fetching block %s", blockHash)
    49  		startTime := time.Now()
    50  		blockBytes, err := tx.FetchBlock(blockHash)
    51  		if err != nil {
    52  			return err
    53  		}
    54  		log.Infof("Loaded block in %v", time.Now().Sub(startTime))
    55  		log.Infof("Block Hex: %s", hex.EncodeToString(blockBytes))
    56  		return nil
    57  	})
    58  }
    59  
    60  // Usage overrides the usage display for the command.
    61  func (cmd *fetchBlockCmd) Usage() string {
    62  	return "<block-hash>"
    63  }