From 049f5b357200406c01f7bf2d2e2936d1d28a319b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felf=C3=B6ldi=20Zsolt?= Date: Tue, 12 Jun 2018 15:52:54 +0200 Subject: core, eth, les: more efficient hash-based header chain retrieval (#16946) --- core/blockchain.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'core/blockchain.go') diff --git a/core/blockchain.go b/core/blockchain.go index ea26fa034..34832252a 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1524,6 +1524,18 @@ func (bc *BlockChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []com return bc.hc.GetBlockHashesFromHash(hash, max) } +// GetAncestor retrieves the Nth ancestor of a given block. It assumes that either the given block or +// a close ancestor of it is canonical. maxNonCanonical points to a downwards counter limiting the +// number of blocks to be individually checked before we reach the canonical chain. +// +// Note: ancestor == 0 returns the same block, 1 returns its parent and so on. +func (bc *BlockChain) GetAncestor(hash common.Hash, number, ancestor uint64, maxNonCanonical *uint64) (common.Hash, uint64) { + bc.chainmu.Lock() + defer bc.chainmu.Unlock() + + return bc.hc.GetAncestor(hash, number, ancestor, maxNonCanonical) +} + // GetHeaderByNumber retrieves a block header from the database by number, // caching it (associated with its hash) if found. func (bc *BlockChain) GetHeaderByNumber(number uint64) *types.Header { -- cgit v1.2.3