Repository: bitcoin/bitcoin
File: src/validation.cpp (C++)

Code before:
        for (CBlockIndex* pindex : vSortedByHeight) {
            if (m_interrupt) return false;
            // If we have an assumeutxo-based chainstate, then the snapshot
            // block will be a candidate for the tip, but it may not be
            // VALID_TRANSACTIONS (eg if we haven't yet downloaded the block),
            // so we special-case the snapshot block as a potential candidate
            // here.
            if (pindex == GetSnapshotBaseBlock() ||
                    (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) &&
                     (pindex->HaveNumChainTxs() || pindex->pprev == nullptr))) {

                for (Chainstate* chainstate : GetAll()) {
                    chainstate->TryAddBlockIndexCandidate(pindex);
                }
            }
            if (pindex->nStatus & BLOCK_FAILED_MASK && (!m_best_invalid || pindex->nChainWork > m_best_invalid->nChainWork)) {
                m_best_invalid = pindex;
            }
            if (pindex->IsValid(BLOCK_VALID_TREE) && (m_best_header == nullptr || CBlockIndexWorkComparator()(m_best_header, pindex)))
                m_best_header = pindex;
        }

        needs_init = m_blockman.m_block_index.empty();
    }

    if (needs_init) {
        // Everything here is for *new* reindex/DBs. Thus, though
        // LoadBlockIndexDB may have set fReindex if we shut down
        // mid-reindex previously, we don't check fReindex and
        // instead only check it prior to LoadBlockIndexDB to set
        // needs_init.

        LogPrintf("Initializing databases...\n");
    }
    return true;
}

bool Chainstate::LoadGenesisBlock()
{
    LOCK(cs_main);

    const CChainParams& params{m_chainman.GetParams()};

    // Check whether we're already initialized by checking for genesis in
    // m_blockman.m_block_index. Note that we can't use m_chain here, since it is
    // set based on the coins db, not the block index db, which is the only
    // thing loaded at this point.
    if (m_blockman.m_block_index.count(params.GenesisBlock().GetHash()))
        return true;

    try {

Review comment (suggestion):
Unrelated, but this comment and the `needs_init` variable are confusing. Originally (https://github.com/bitcoin/bitcoin/commit/eda888e57352037ab2e60f6ef90098b3ce23a157) it may have made sense, but now there is just a log message left which doesn't even seem to belong here (no db initialization is happening here). So I think this could be cleaned up, but no need to do it here. 

Code after:
            if (m_interrupt) return false;
            // If we have an assumeutxo-based chainstate, then the snapshot
            // block will be a candidate for the tip, but it may not be
            // VALID_TRANSACTIONS (eg if we haven't yet downloaded the block),
            // so we special-case the snapshot block as a potential candidate
            // here.
            if (pindex == GetSnapshotBaseBlock() ||
                    (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) &&
                     (pindex->HaveNumChainTxs() || pindex->pprev == nullptr))) {

                for (Chainstate* chainstate : GetAll()) {
                    chainstate->TryAddBlockIndexCandidate(pindex);
                }
            }
            if (pindex->nStatus & BLOCK_FAILED_MASK && (!m_best_invalid || pindex->nChainWork > m_best_invalid->nChainWork)) {
                m_best_invalid = pindex;
            }
            if (pindex->IsValid(BLOCK_VALID_TREE) && (m_best_header == nullptr || CBlockIndexWorkComparator()(m_best_header, pindex)))
                m_best_header = pindex;
        }

        needs_init = m_blockman.m_block_index.empty();
    }

    if (needs_init) {
        // Everything here is for *new* reindex/DBs. Thus, though
        // LoadBlockIndexDB may have set m_reindexing if we shut down
        // mid-reindex previously, we don't check m_reindexing and
        // instead only check it prior to LoadBlockIndexDB to set
        // needs_init.

        LogPrintf("Initializing databases...\n");
    }
    return true;
}

bool Chainstate::LoadGenesisBlock()
{
    LOCK(cs_main);

    const CChainParams& params{m_chainman.GetParams()};

    // Check whether we're already initialized by checking for genesis in
    // m_blockman.m_block_index. Note that we can't use m_chain here, since it is
    // set based on the coins db, not the block index db, which is the only
    // thing loaded at this point.
    if (m_blockman.m_block_index.count(params.GenesisBlock().GetHash()))
        return true;

    try {
        const CBlock& block = params.GenesisBlock();