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