diff --git a/blockdag/dag.go b/blockdag/dag.go index 4b34fcd99a..498bf19072 100644 --- a/blockdag/dag.go +++ b/blockdag/dag.go @@ -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. diff --git a/server/rpcserver/rpcserver.go b/server/rpcserver/rpcserver.go index 28751697b7..30471e1163 100644 --- a/server/rpcserver/rpcserver.go +++ b/server/rpcserver/rpcserver.go @@ -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, @@ -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) @@ -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,