<p>Block tree is stored on-disk. During startup, is read into memory. When read, <code>BlockManager::m_block_index</code> field is filled with data - CBlockIndexes. Every CBlockIndex contains memory-only field <code>arith_uint256 nChainWork</code>, which is a total amount of work in the chain up to (and including) current block.</p>

<p>Best chain is selected as maximum among all <code>nChainWork</code> of all tips and saved to <code>CChainState::m_chain</code> - currently active chain.</p>

CC BY-SA 4.0
2020-01-25T13:49:42.820Z
2020-01-25T13:49:42.820Z
warchantua
<p>I'm trying to understand how Bitcoin Core works in-depth. Can someone tell me where is the code executed during a blockchain reorganization in Bitcoin Core?</p>

<p>Any explanation would also be greatly appreciated.</p>

CC BY-SA 4.0
2019-05-29T14:09:03.573Z
Possible duplicate of [Where in Bitcoin Core does it do X?](https://bitcoin.stackexchange.com/questions/87450/where-in-bitcoin-core-does-it-do-x)
Ugam Kamat
CC BY-SA 4.0
2019-06-01T12:46:06.550Z
When you say reorganization, do you mean a spontaneous fork in the blockchain? or are you referring to the suggestion of Binance recently after their hack, to “reorganize” and rollback the blockchain, and you are wondereing if there is code in place to do the reorg of the blockchain? (spoiler alert: there isn’t any).
rxon
CC BY-SA 4.0
2019-06-02T12:54:20.567Z
@bitcoinero I mean spontaneous fork. How the best chain is selected.
SystemGlitch
<bitcoin-core><chain-reorganization>
Where is the code of chain reorganization in Bitcoin Core
SystemGlitch
# Where is the code of chain reorganization in Bitcoin Core

(Asked by: username_0 on 2019-05-29)

I'm trying to understand how Bitcoin Core works in-depth. Can someone tell me where is the code executed during a blockchain reorganization in Bitcoin Core?

Any explanation would also be greatly appreciated.

**Tags:** bitcoin-core, chain-reorganization

> **Comments:**
> **username_1**: Possible duplicate of [Where in Bitcoin Core does it do X?](https://bitcoin.stackexchange.com/questions/87450/where-in-bitcoin-core-does-it-do-x)  
> **username_2**: When you say reorganization, do you mean a spontaneous fork in the blockchain? or are you referring to the suggestion of Binance recently after their hack, to “reorganize” and rollback the blockchain, and you are wondereing if there is code in place to do the reorg of the blockchain? (spoiler alert: there isn’t any).  
> **username_0**: @bitcoinero I mean spontaneous fork. How the best chain is selected.  

---

## 1 answers

### Answer by username_3 on 2020-01-25. Score: 1 | Accepted answer

Block tree is stored on-disk. During startup, is read into memory. When read, `BlockManager::m_block_index` field is filled with data - CBlockIndexes. Every CBlockIndex contains memory-only field `arith_uint256 nChainWork`, which is a total amount of work in the chain up to (and including) current block.

Best chain is selected as maximum among all `nChainWork` of all tips and saved to `CChainState::m_chain` - currently active chain.


---

