mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00
[DEV-68] Backport 'blockchain: Backport optimize exported header access'
This commit is contained in:
parent
176aad5dcd
commit
6475e03b93
@ -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.
|
||||
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user