0

Does the getblock(hash) RPC call always return a "previousblockhash" field and if not: under what conditions does it not return this?

Background: many algorithms suggest using this field as a way to step back in the blockchain when an orphan or reorganization occurs but the C++ code would indicate this field isn't always present.

if (blockindex->pprev)
        result.push_back(Pair("previousblockhash", blockindex->pprev->GetBlockHash().GetHex()));

Would that be due to chain reorganizations and orphans? Can anyone explain what dynamics would make this check fail and what the next step would be if it does?

5
  • Do you really mean orphan blocks, or rather stale blocks? A block can never become an orphan, Bitcoin Core can not connect a block without its previous existing.
    – Claris
    Aug 8, 2015 at 10:53
  • I guess stale block might be the term I'm after then. Good observation Aug 8, 2015 at 11:05
  • It won't return information on previousblockhash for the genesis block, for instance.
    – user11221
    Aug 8, 2015 at 14:14
  • 1
    @Bitcoin Nice analysis, but it's not correct. getblockheader was indeed added because getblock can fail when a block is pruned, but even then, the index entries remain. See my answer. Aug 10, 2015 at 14:06
  • @PieterWuille Right, that makes a lot more sense.
    – Claris
    Aug 10, 2015 at 14:10

1 Answer 1

2

It will only ever not be present for the genesis block.

A block is not added to the block index until all its ancestors are in there too.

The block index is conceptually a tree: every block can have multiple decendents but only one ancestor. A reorganization is switching from one branch of the tree to another, and does not affect this.

Orphan blocks (in the meaning of blocks without known parent) don't exist anymore since Bitcoin Core 0.10 (we only download blocks after validating their headers, so the parent is never unknown), and even before that, orphan blocks were just stored in a queue in memory and not actually added to the index until their parents were found.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.