Skip to content

Commit

Permalink
blockchain: Backport optimize exported header access.
Browse files Browse the repository at this point in the history
backport of decred/dcrd#1273

Notable difference being that btcd mainline currenlty
doesn't have a blockchain/blockindex_test.go file, so
those changes are omitted.

Great work @davecgh :)
  • Loading branch information
cfromknecht committed Aug 1, 2018
1 parent 9a2f952 commit bb633e6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
26 changes: 9 additions & 17 deletions blockchain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1242,25 +1242,17 @@ func (b *BlockChain) BestSnapshot() *BestState {
return snapshot
}

// FetchHeader returns the block header identified by the given hash or an error
// if it doesn't exist.
func (b *BlockChain) FetchHeader(hash *chainhash.Hash) (wire.BlockHeader, error) {
// Reconstruct the header from the block index if possible.
if node := b.index.LookupNode(hash); node != nil {
return node.Header(), nil
}

// Fall back to loading it from the database.
var header *wire.BlockHeader
err := b.db.View(func(dbTx database.Tx) error {
var err error
header, err = dbFetchHeaderByHash(dbTx, hash)
return err
})
if err != nil {
// HeaderByHash returns the block header identified by the given hash or an
// error if it doesn't exist. Note that this will return headers from both the
// main and side chains.
func (b *BlockChain) HeaderByHash(hash *chainhash.Hash) (wire.BlockHeader, error) {
node := b.index.Lookup(hash)
if node == nil {
err := fmt.Errorf("block %s is not known", hash)
return wire.BlockHeader{}, err
}
return *header, nil

return node.Header(), nil
}

// MainChainHasBlock returns whether or not the block with the given hash is in
Expand Down
8 changes: 4 additions & 4 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1320,7 +1320,7 @@ func handleGetBlockHeader(s *rpcServer, cmd interface{}, closeChan <-chan struct
if err != nil {
return nil, rpcDecodeHexError(c.Hash)
}
blockHeader, err := s.cfg.Chain.FetchHeader(hash)
blockHeader, err := s.cfg.Chain.HeaderByHash(hash)
if err != nil {
return nil, &btcjson.RPCError{
Code: btcjson.ErrRPCBlockNotFound,
Expand Down Expand Up @@ -2442,7 +2442,7 @@ func handleGetNetworkHashPS(s *rpcServer, cmd interface{}, closeChan <-chan stru
}

// Fetch the header from chain.
header, err := s.cfg.Chain.FetchHeader(hash)
header, err := s.cfg.Chain.HeaderByHash(hash)
if err != nil {
context := "Failed to fetch block header"
return nil, internalRPCError(err.Error(), context)
Expand Down Expand Up @@ -2634,7 +2634,7 @@ func handleGetRawTransaction(s *rpcServer, cmd interface{}, closeChan <-chan str
var chainHeight int32
if blkHash != nil {
// Fetch the header from chain.
header, err := s.cfg.Chain.FetchHeader(blkHash)
header, err := s.cfg.Chain.HeaderByHash(blkHash)
if err != nil {
context := "Failed to fetch block header"
return nil, internalRPCError(err.Error(), context)
Expand Down Expand Up @@ -3262,7 +3262,7 @@ func handleSearchRawTransactions(s *rpcServer, cmd interface{}, closeChan <-chan
var blkHeight int32
if blkHash := rtx.blkHash; blkHash != nil {
// Fetch the header from chain.
header, err := s.cfg.Chain.FetchHeader(blkHash)
header, err := s.cfg.Chain.HeaderByHash(blkHash)
if err != nil {
return nil, &btcjson.RPCError{
Code: btcjson.ErrRPCBlockNotFound,
Expand Down

0 comments on commit bb633e6

Please sign in to comment.