I've been going through main.cpp in order to roughly find out which function calls the best block and I found out depending on situation there are several similar-sounding function names and code extracts that I suspect of calling the best block. Here's my list:

  • CBlockIndex *pindexBestKnownBlock
  • uint256 CCoinsView::GetBestBlock () const
  • CBlockIndex* pindexBestHeader
  • CBlockIndex* pindexBestForkTip
  • pindexBestInvalid
  • // move best block pointer to prevout block
  • chainActive.Tip()->GetBlockHash().ToString()

Now my question: Which one of the list items above can I use and modify in a way that calls a block that is n blocks deeper than the best block on the same blockchain?


The chainActive variable stores the best known block chain. But this is essentially just the chain of block headers, not the actual blocks themselves. To load the actual block from disk, use the ReadBlockFromDisk() function. But, be aware that this is an expensive function to use, it does not return immediately.

chainActive[0] or chainActive.Genesis() [1]

Gives the genesis block header.

chainActive[chainActive.Height()] or chainActive.Tip() [2]

Gives the header for the most recent block in the active chain.


Gives the block header for the block with 10 blocks built on top of it.

Use any of these CBlockIndex pointers as below to get the block.

CBlock block;
if (ReadBlockFromDisk(block, pindex)) {
    // do something
  • Thanks! Does ReadBlockFromDisc() access blk?????.dat? Is there any software or app for opening and reading blk?????.dat? – Aliakbar Ahmadi May 20 '15 at 16:00
  • 1
    I believe it does access the blk___.dat files. Those files just contain raw concatenated block bytes, so you would probably need a blockchain parser to access those. What exactly are you trying to do? I might be able to point you at a good tool for the job. – morsecoder May 20 '15 at 16:15
  • I'm going to simulate and analyse outcome of a "block mining race". I want to make a rebuild of bitcoin-0.10.0 with modified code that prescribes selfish mining algorithm. For this part I got the hint that bitcoin code must work with a pointer (speaking on processor level) that is shifted back and forth according to the state of a node. And here I want to implement the selfish behaviour: modified code shall "point to blocks" according to the algorithm of the attack. With the idea of a pointer I hope to be able to "hide" blocks - this would mean a backshifted pointer to a publicly known block. – Aliakbar Ahmadi May 20 '15 at 16:34
  • For the simulation, for example I'm planning to run 1 selfish node (modified node) alongside with 4 other honest nodes (original code), fork the blockchain and start mining on both branches. – Aliakbar Ahmadi May 20 '15 at 16:36
  • 1
    I think the code is pretty much the best documentation at this point. Just read main.cpp, chainparams.cpp, and chainparamsbase.cpp. Look up code elements that you're not sure about to see their documentation (e.g. std::vector or std::map, or anything related to boost). Best of luck! – morsecoder May 20 '15 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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