<p>You're looking for <code>validation.h</code> and its corresponding .cpp source file.</p>

<p>You should start at <code>CChainState::ActivateBestChain</code>. This subroutine calls <code>CChainState::ActivateBestChainStep</code> which runs the logic you're interested in - stepping one by one of the blocks, cleaning up and reorg'ing from lowest to highest PoW. </p>

<p>First, <code>ActivateBestChainStep</code> walks the chain in a <code>while</code> loop: <code>while (m_chain.Tip() &amp;&amp; m_chain.Tip() != pindexFork)</code> cleaning up blocks which are not in the best chain using <code>DisconnectTip(state, chainparams, &amp;disconnectpool)</code>. <strong>This partly answers your question - all the blocks which aren't in the best chain are weeded out here.</strong></p>

<p>It then walks through the current <code>CBlockIndex</code> organizing blocks using the next while loop: <code>while (fContinue &amp;&amp; nHeight != pindexMostWork-&gt;nHeight)</code>. <code>pindexMostWork</code> is a <code>CBlockIndex</code> that contains the blocks with most work. Inside there is a loop which selects the blocks it should connect to the best chain: <code>while (pindexIter &amp;&amp; pindexIter-&gt;nHeight != nHeight)</code>. It pushes the candidate blocks onto a locally allocated vector <code>std::vector&lt;CBlockIndex*&gt; vpindexToConnect;</code>.</p>

<p>Next it iterates this new vector in reverse, pushing the blocks with lowest work first: <code>for (CBlockIndex *pindexConnect : reverse_iterate(vpindexToConnect))</code> connecting each to the tip. The last connected block will be the block with the highest work index. This builds a chain sorted from lowest to highest work.</p>

<p>Several places in the code call <code>ActivateBestChain</code>, among them:</p>

<ul>
<li><code>invalidateblock</code> and <code>reconsiderblock</code> in <code>rpc/blockchain.cpp</code></li>
<li><code>ThreadImport</code> in <code>init.cpp</code></li>
<li><code>ProcessGetBlockData</code> and <code>Processmessage</code> in net_processing.cpp</li>
<li>Several subroutines in <code>validation.cpp</code> itself</li>
</ul>

<p>You can use any of the above to trace how and when a new best chain is activated. I think the most interesting ones are in <code>net_processing.cpp</code> as those are the ones triggered by messages received from the network.</p>

CC BY-SA 4.0
2019-10-20T14:24:26.680Z
2019-10-20T14:24:26.680Z
Jose Fonseca
<p>Bitcoin Core stores blocks in files in local disk. And it uses LevelDb to form an index on top of block storage to make it faster to retrieve a block (basically a simple key-value pair db). See <a href="https://en.bitcoin.it/wiki/Bitcoin_Core_0.11_(ch_2):_Data_Storage#Block_index_.28leveldb.29" rel="nofollow noreferrer">this</a> for more details.</p>

<p>Also Bitcoin Core stores all the blocks that are not that are not in the main chain in case a fork happens and one of them becomes part of the main chain.</p>

<p>How does Bitcoin Core scan the local storage to find the chaintip of the main chain which have most number of blocks? Does is keep a list of heads in memory? It would be extremely nice if you guys can redirect me to the location in the <a href="https://github.com/bitcoin/bitcoin/tree/master/src" rel="nofollow noreferrer">Bitcoin Core</a> source code that handles that process.</p>

<bitcoin-core-development><leveldb><best-chain><blockchain-data-storage>
How Does Bitcoin Core Client Keep Track of Longest Chain/Strongest PoW Chains?
user3299116
# How Does Bitcoin Core Client Keep Track of Longest Chain/Strongest PoW Chains?

(Asked by: username_0 on 2019-10-20)

Bitcoin Core stores blocks in files in local disk. And it uses LevelDb to form an index on top of block storage to make it faster to retrieve a block (basically a simple key-value pair db). See [this](https://en.bitcoin.it/wiki/Bitcoin_Core_0.11_(ch_2):_Data_Storage#Block_index_.28leveldb.29) for more details.

Also Bitcoin Core stores all the blocks that are not that are not in the main chain in case a fork happens and one of them becomes part of the main chain.

How does Bitcoin Core scan the local storage to find the chaintip of the main chain which have most number of blocks? Does is keep a list of heads in memory? It would be extremely nice if you guys can redirect me to the location in the [Bitcoin Core](https://github.com/bitcoin/bitcoin/tree/master/src) source code that handles that process.

**Tags:** bitcoin-core-development, leveldb, best-chain, blockchain-data-storage


---

## 1 answers

### Answer by username_1 on 2019-10-20. Score: 2

You're looking for `validation.h` and its corresponding .cpp source file.

You should start at `CChainState::ActivateBestChain`. This subroutine calls `CChainState::ActivateBestChainStep` which runs the logic you're interested in - stepping one by one of the blocks, cleaning up and reorg'ing from lowest to highest PoW.

First, `ActivateBestChainStep` walks the chain in a `while` loop: `while (m_chain.Tip() && m_chain.Tip() != pindexFork)` cleaning up blocks which are not in the best chain using `DisconnectTip(state, chainparams, &disconnectpool)`. **This partly answers your question - all the blocks which aren't in the best chain are weeded out here.**

It then walks through the current `CBlockIndex` organizing blocks using the next while loop: `while (fContinue && nHeight != pindexMostWork->nHeight)`. `pindexMostWork` is a `CBlockIndex` that contains the blocks with most work. Inside there is a loop which selects the blocks it should connect to the best chain: `while (pindexIter && pindexIter->nHeight != nHeight)`. It pushes the candidate blocks onto a locally allocated vector `std::vector vpindexToConnect;`.

Next it iterates this new vector in reverse, pushing the blocks with lowest work first: `for (CBlockIndex *pindexConnect : reverse_iterate(vpindexToConnect))` connecting each to the tip. The last connected block will be the block with the highest work index. This builds a chain sorted from lowest to highest work.

Several places in the code call `ActivateBestChain`, among them:

* `invalidateblock` and `reconsiderblock` in `rpc/blockchain.cpp`
* `ThreadImport` in `init.cpp`
* `ProcessGetBlockData` and `Processmessage` in net\_processing.cpp
* Several subroutines in `validation.cpp` itself

You can use any of the above to trace how and when a new best chain is activated. I think the most interesting ones are in `net_processing.cpp` as those are the ones triggered by messages received from the network.


---

