src/main.cpp
/**
 * Try to make some progress towards making pindexMostWork the active block.
 * pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork.
 */
Try to make some progress towards making pindexMostWork the active block.
pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork.
Try to make some progress towards making pindexMostWork the active block . pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork .
static bool ActivateBestChainStep(CValidationState& state, CBlockIndex* pindexMostWork, CBlock* pblock)
{
    AssertLockHeld(cs_main);
    bool fInvalidFound = false;
    const CBlockIndex* pindexOldTip = chainActive.Tip();
    const CBlockIndex* pindexFork = chainActive.FindFork(pindexMostWork);
    while (chainActive.Tip() && chainActive.Tip() != pindexFork) {
        if (!DisconnectTip(state))
            return false;
    }
    std::vector<CBlockIndex*> vpindexToConnect;
    bool fContinue = true;
    int nHeight = pindexFork ? pindexFork->nHeight : -1;
    while (fContinue && nHeight != pindexMostWork->nHeight) {
        int nTargetHeight = std::min(nHeight + 32, pindexMostWork->nHeight);
        vpindexToConnect.clear();
        vpindexToConnect.reserve(nTargetHeight - nHeight);
        CBlockIndex* pindexIter = pindexMostWork->GetAncestor(nTargetHeight);
        while (pindexIter && pindexIter->nHeight != nHeight) {
            vpindexToConnect.push_back(pindexIter);
            pindexIter = pindexIter->pprev;
        }
        nHeight = nTargetHeight;
        BOOST_REVERSE_FOREACH (CBlockIndex* pindexConnect, vpindexToConnect) {
            if (!ConnectTip(state, pindexConnect, pindexConnect == pindexMostWork ? pblock : NULL)) {
                if (state.IsInvalid()) {
                    if (!state.CorruptionPossible())
                        InvalidChainFound(vpindexToConnect.back());
                    state = CValidationState();
                    fInvalidFound = true;
                    fContinue = false;
                    break;
                } else {
                    return false;
                }
            } else {
                PruneBlockIndexCandidates();
                if (!pindexOldTip || chainActive.Tip()->nChainWork > pindexOldTip->nChainWork) {
                    fContinue = false;
                    break;
                }
            }
        }
    }
    if (fInvalidFound)
        CheckForkWarningConditionsOnNewFork(vpindexToConnect.back());
    else
        CheckForkWarningConditions();
    return true;
}
static bool ActivateBestChainStep ( CValidationState & state , CBlockIndex * pindexMostWork , CBlock * pblock ) { AssertLockHeld ( cs_main ) ; bool fInvalidFound = false ; const CBlockIndex * pindexOldTip = chainActive . Tip ( ) ; const CBlockIndex * pindexFork = chainActive . FindFork ( pindexMostWork ) ; while ( chainActive . Tip ( ) && chainActive . Tip ( ) != pindexFork ) { if ( ! DisconnectTip ( state ) ) return false ; } std :: vector < CBlockIndex * > vpindexToConnect ; bool fContinue = true ; int nHeight = pindexFork ? pindexFork -> nHeight : -1 ; while ( fContinue && nHeight != pindexMostWork -> nHeight ) { int nTargetHeight = std :: min ( nHeight + 32 , pindexMostWork -> nHeight ) ; vpindexToConnect . clear ( ) ; vpindexToConnect . reserve ( nTargetHeight - nHeight ) ; CBlockIndex * pindexIter = pindexMostWork -> GetAncestor ( nTargetHeight ) ; while ( pindexIter && pindexIter -> nHeight != nHeight ) { vpindexToConnect . push_back ( pindexIter ) ; pindexIter = pindexIter -> pprev ; } nHeight = nTargetHeight ; BOOST_REVERSE_FOREACH ( CBlockIndex * pindexConnect , vpindexToConnect ) { if ( ! ConnectTip ( state , pindexConnect , pindexConnect == pindexMostWork ? pblock : NULL ) ) { if ( state . IsInvalid ( ) ) { if ( ! state . CorruptionPossible ( ) ) InvalidChainFound ( vpindexToConnect . back ( ) ) ; state = CValidationState ( ) ; fInvalidFound = true ; fContinue = false ; break ; } else { return false ; } } else { PruneBlockIndexCandidates ( ) ; if ( ! pindexOldTip || chainActive . Tip ( ) -> nChainWork > pindexOldTip -> nChainWork ) { fContinue = false ; break ; } } } } if ( fInvalidFound ) CheckForkWarningConditionsOnNewFork ( vpindexToConnect . back ( ) ) ; else CheckForkWarningConditions ( ) ; return true ; }
Try to make some progress towards making pindexMostWork the active block.
Try to make some progress towards making pindexMostWork the active block .
// Disconnect active blocks which are no longer in the best chain. // Build list of new blocks to connect. // Don't iterate the entire list of potential improvements toward the best tip, as we likely only need // a few blocks along the way. // Connect new blocks. // The block violates a consensus rule. // A system error occurred (disk space, database error, ...). // We're in a better position than we were. Return temporarily to release the lock. // Callbacks/notifications for a new best chain.