Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev 95 backport optimize exported header access #46

Merged
merged 2 commits into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions blockdag/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,25 +746,16 @@ func (dag *BlockDAG) setDAGState(dagState *DAGState) {
dag.dagState = dagState
}

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

// Fall back to loading it from the database.
var header *wire.BlockHeader
err := dag.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.
func (dag *BlockDAG) HeaderByHash(hash *daghash.Hash) (wire.BlockHeader, error) {
node := dag.index.LookupNode(hash)
if node == nil {
err := fmt.Errorf("block %s is not known", hash)
return wire.BlockHeader{}, err
}
return *header, nil

return node.Header(), nil
}

// BlockLocatorFromHash returns a block locator for the passed block hash.
Expand Down
6 changes: 3 additions & 3 deletions server/rpcserver/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,7 @@ func handleGetBlockHeader(s *Server, cmd interface{}, closeChan <-chan struct{})
if err != nil {
return nil, rpcDecodeHexError(c.Hash)
}
blockHeader, err := s.cfg.DAG.FetchHeader(hash)
blockHeader, err := s.cfg.DAG.HeaderByHash(hash)
if err != nil {
return nil, &btcjson.RPCError{
Code: btcjson.ErrRPCBlockNotFound,
Expand Down Expand Up @@ -2465,7 +2465,7 @@ func handleGetRawTransaction(s *Server, cmd interface{}, closeChan <-chan struct
var chainHeight int32
if blkHash != nil {
// Fetch the header from chain.
header, err := s.cfg.DAG.FetchHeader(blkHash)
header, err := s.cfg.DAG.HeaderByHash(blkHash)
if err != nil {
context := "Failed to fetch block header"
return nil, internalRPCError(err.Error(), context)
Expand Down Expand Up @@ -3089,7 +3089,7 @@ func handleSearchRawTransactions(s *Server, cmd interface{}, closeChan <-chan st
var blkHeight int32
if blkHash := rtx.blkHash; blkHash != nil {
// Fetch the header from chain.
header, err := s.cfg.DAG.FetchHeader(blkHash)
header, err := s.cfg.DAG.HeaderByHash(blkHash)
if err != nil {
return nil, &btcjson.RPCError{
Code: btcjson.ErrRPCBlockNotFound,
Expand Down