<p>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. </p>

<p><code>chainActive[0]</code> or <code>chainActive.Genesis()</code> <a href="https://dev.visucore.com/bitcoin/doxygen/class_c_chain.html#a1eee0372da596a8fc68369e480adc2f1" rel="nofollow">[1]</a></p>

<p>Gives the genesis block header. </p>

<p><code>chainActive[chainActive.Height()]</code> or <code>chainActive.Tip()</code> <a href="https://dev.visucore.com/bitcoin/doxygen/class_c_chain.html#a7482d06d8e8143f46326cf9c12c06daf" rel="nofollow">[2]</a></p>

<p>Gives the header for the most recent block in the active chain. </p>

<pre><code>chainActive[chainActive.Height()-10]
</code></pre>

<p>Gives the block header for the block with 10 blocks built on top of it. </p>

<p>Use any of these CBlockIndex pointers as below to get the block. </p>

<pre><code>CBlock block;
if (ReadBlockFromDisk(block, pindex)) {
    // do something
}
</code></pre>

CC BY-SA 3.0
2015-05-20T16:00:44.933Z
Thanks! Does `ReadBlockFromDisc()` access _blk?????.dat_? Is there any software or app for opening and reading _blk?????.dat_?
Aliakbar Ahmadi
CC BY-SA 3.0
2015-05-20T16:15:29.367Z
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
CC BY-SA 3.0
2015-05-20T16:34:25.800Z
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
CC BY-SA 3.0
2015-05-20T16:36:26.273Z
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
CC BY-SA 3.0
2015-05-20T16:47:13.233Z
In that case, what you probably need to do is make a new set of parameters in one of the chainparams files, and program your node to selfish mine when it is started with some flag. This doesn't require digging into the blk___.dat files at all, mainly some modification of what/when things are shared in main.cpp. Then start up the various nodes, connect them to each other, and have them all mine. If the selfish one gains more coins in the long run, then the strategy is working.
morsecoder
CC BY-SA 3.0
2015-05-20T17:08:25.507Z
Thanks a lot! Can you maybe give me a good source where I can read about what each chainparams file does or do you recommend to go through the files and just try to understand? Unfortunately, I'm very new to C++ but I'm eager to learn new stuff. Is there a good approach for getting a rough idea what those chainparams files do? (I hope I'm speaking of same files as you: chainparams.cpp, chainparamsbase.cpp,  chainparamsseeds.h etc.)
Aliakbar Ahmadi
CC BY-SA 3.0
2015-05-20T18:39:36.970Z
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
CC BY-SA 3.0
2015-06-05T20:02:27.317Z
Thanks for the review! Could you please explain why chainActive[] works with genesis block but not with any other block? Is it because a block can have multiple valid "twins"?
Aliakbar Ahmadi
CC BY-SA 3.0
2015-06-05T22:10:23.930Z
@AliakbarAhmadi The operator chainActive[] may be confusing to people without intimate C++ knowledege, so I left that part out of the suggested edits. But in general, 'twins', as you say, are not an issue because the chainActive only has the headers of the main chain in it.
morsecoder
CC BY-SA 3.0
2015-05-20T12:05:07.817Z
2015-06-05T17:17:16.717Z
2015-06-05T17:17:16.717Z
morsecoder
<p>I've been going through <code>main.cpp</code> 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:</p>

<ul>
<li>CBlockIndex *pindexBestKnownBlock</li>
<li>uint256 CCoinsView::GetBestBlock () const</li>
<li>CBlockIndex* pindexBestHeader</li>
<li>CBlockIndex* pindexBestForkTip </li>
<li>pindexBestInvalid</li>
<li>// move best block pointer to prevout block<br>
view.SetBestBlock(pindex->pprev->GetBlockHash());</li>
<li>chainActive.Tip()->GetBlockHash().ToString()</li>
</ul>

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

<blockchain><block><bitcoin-core-development><c++>
Call function for block n on blockchain
Aliakbar Ahmadi
# Call function for block n on blockchain

(Asked by: username_0 on 2015-05-19)

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  
  view.SetBestBlock(pindex->pprev->GetBlockHash());
* 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**?

**Tags:** blockchain, block, bitcoin-core-development, c++


---

## 1 answers

### Answer by username_1 on 2015-05-20. Score: 2 | Accepted answer

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]](https://dev.visucore.com/bitcoin/doxygen/class_c_chain.html#a1eee0372da596a8fc68369e480adc2f1)

Gives the genesis block header.

`chainActive[chainActive.Height()]` or `chainActive.Tip()` [[2]](https://dev.visucore.com/bitcoin/doxygen/class_c_chain.html#a7482d06d8e8143f46326cf9c12c06daf)

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

```
chainActive[chainActive.Height()-10]

```

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
}

```

> **Comments:**
> **username_0**: Thanks! Does `ReadBlockFromDisc()` access _blk?????.dat_? Is there any software or app for opening and reading _blk?????.dat_?  
> **username_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.  
> **username_0**: 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.  
> **username_0**: 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.  
> **username_1**: In that case, what you probably need to do is make a new set of parameters in one of the chainparams files, and program your node to selfish mine when it is started with some flag. This doesn't require digging into the blk___.dat files at all, mainly some modification of what/when things are shared in main.cpp. Then start up the various nodes, connect them to each other, and have them all mine. If the selfish one gains more coins in the long run, then the strategy is working.  
> **username_0**: Thanks a lot! Can you maybe give me a good source where I can read about what each chainparams file does or do you recommend to go through the files and just try to understand? Unfortunately, I'm very new to C++ but I'm eager to learn new stuff. Is there a good approach for getting a rough idea what those chainparams files do? (I hope I'm speaking of same files as you: chainparams.cpp, chainparamsbase.cpp,  chainparamsseeds.h etc.)  
> **username_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!  
> **username_0**: Thanks for the review! Could you please explain why chainActive[] works with genesis block but not with any other block? Is it because a block can have multiple valid "twins"?  
> **username_1**: @username_0 The operator chainActive[] may be confusing to people without intimate C++ knowledege, so I left that part out of the suggested edits. But in general, 'twins', as you say, are not an issue because the chainActive only has the headers of the main chain in it.  

---

