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