src/wallet/wallet.cpp
CBlockIndex* CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, CBlockIndex* pindexStop, const WalletRescanReserver &reserver, bool fUpdate)
{
    int64_t nNow = GetTime();
    const CChainParams& chainParams = Params();

    assert(reserver.isReserved());
    if (pindexStop) {
        assert(pindexStop->nHeight >= pindexStart->nHeight);
    }

    CBlockIndex* pindex = pindexStart;
    CBlockIndex* ret = nullptr;

    if (pindex) LogPrintf("Rescan started from block %d...\n", pindex->nHeight);

    {
        fAbortRescan = false;
        ShowProgress(_("Rescanning..."), 0); // show rescan progress in GUI as dialog or on splashscreen, if -rescan on startup
        CBlockIndex* tip = nullptr;
        double dProgressStart;
        double dProgressTip;
        {
            LOCK(cs_main);
            tip = chainActive.Tip();
            dProgressStart = GuessVerificationProgress(chainParams.TxData(), pindex);
            dProgressTip = GuessVerificationProgress(chainParams.TxData(), tip);
        }
        double gvp = dProgressStart;
        while (pindex && !fAbortRescan)
        {
            if (pindex->nHeight % 100 == 0 && dProgressTip - dProgressStart > 0.0) {
                ShowProgress(_("Rescanning..."), std::max(1, std::min(99, (int)((gvp - dProgressStart) / (dProgressTip - dProgressStart) * 100))));
            }
            if (GetTime() >= nNow + 60) {
                nNow = GetTime();
                LogPrintf("Still rescanning. At block %d. Progress=%f\n", pindex->nHeight, gvp);
            }

            CBlock block;
            if (ReadBlockFromDisk(block, pindex, Params().GetConsensus())) {
                LOCK2(cs_main, cs_wallet);
                if (pindex && !chainActive.Contains(pindex)) {
                    // Abort scan if current block is no longer active, to prevent
                    // marking transactions as coming from the wrong block.
                    ret = pindex;
                    break;
                }
                for (size_t posInBlock = 0; posInBlock < block.vtx.size(); ++posInBlock) {
                    AddToWalletIfInvolvingMe(block.vtx[posInBlock], pindex, posInBlock, fUpdate);
                }
            } else {
                ret = pindex;
            }
            if (pindex == pindexStop) {
                break;
            }
            {
                LOCK(cs_main);
                pindex = chainActive.Next(pindex);
                gvp = GuessVerificationProgress(chainParams.TxData(), pindex);
                if (tip != chainActive.Tip()) {
                    tip = chainActive.Tip();
                    // in case the tip has changed, update progress max
                    dProgressTip = GuessVerificationProgress(chainParams.TxData(), tip);
                }
            }
        }
        if (pindex && fAbortRescan) {
            LogPrintf("Rescan aborted at block %d. Progress=%f\n", pindex->nHeight, gvp);
        }
        ShowProgress(_("Rescanning..."), 100); // hide progress dialog in GUI
    }
    return ret;
}
/**
 * Scan the block chain (starting in pindexStart) for transactions
 * from or to us. If fUpdate is true, found transactions that already
 * exist in the wallet will be updated.
 *
 * Returns null if scan was successful. Otherwise, if a complete rescan was not
 * possible (due to pruning or corruption), returns pointer to the most recent
 * block that could not be scanned.
 *
 * If pindexStop is not a nullptr, the scan will stop at the block-index
 * defined by pindexStop
 *
 * Caller needs to make sure pindexStop (and the optional pindexStart) are on
 * the main chain after to the addition of any new keys you want to detect
 * transactions for.
 */
Scan the block chain (starting in pindexStart) for transactions
from or to us. If fUpdate is true, found transactions that already
exist in the wallet will be updated.

Returns null if scan was successful. Otherwise, if a complete rescan was not
possible (due to pruning or corruption), returns pointer to the most recent
block that could not be scanned.

If pindexStop is not a nullptr, the scan will stop at the block-index
defined by pindexStop

Caller needs to make sure pindexStop (and the optional pindexStart) are on
the main chain after to the addition of any new keys you want to detect
transactions for.
Scan the block chain ( starting in pindexStart ) for transactions from or to us . If fUpdate is true found transactions that already exist in the wallet will be updated . Returns null if scan was successful . Otherwise if a complete rescan was not possible ( due to pruning or corruption ) returns pointer to the most recent block that could not be scanned . If pindexStop is not a nullptr the scan will stop at the block - index defined by pindexStop Caller needs to make sure pindexStop ( and the optional pindexStart ) are on the main chain after to the addition of any new keys you want to detect transactions for .
CBlockIndex* CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, CBlockIndex* pindexStop, const WalletRescanReserver &reserver, bool fUpdate)
{
    int64_t nNow = GetTime();
    const CChainParams& chainParams = Params();
    assert(reserver.isReserved());
    if (pindexStop) {
        assert(pindexStop->nHeight >= pindexStart->nHeight);
    }
    CBlockIndex* pindex = pindexStart;
    CBlockIndex* ret = nullptr;
    if (pindex) LogPrintf("Rescan started from block %d...\n", pindex->nHeight);
    {
        fAbortRescan = false;
        ShowProgress(_("Rescanning..."), 0); 
        CBlockIndex* tip = nullptr;
        double dProgressStart;
        double dProgressTip;
        {
            LOCK(cs_main);
            tip = chainActive.Tip();
            dProgressStart = GuessVerificationProgress(chainParams.TxData(), pindex);
            dProgressTip = GuessVerificationProgress(chainParams.TxData(), tip);
        }
        double gvp = dProgressStart;
        while (pindex && !fAbortRescan)
        {
            if (pindex->nHeight % 100 == 0 && dProgressTip - dProgressStart > 0.0) {
                ShowProgress(_("Rescanning..."), std::max(1, std::min(99, (int)((gvp - dProgressStart) / (dProgressTip - dProgressStart) * 100))));
            }
            if (GetTime() >= nNow + 60) {
                nNow = GetTime();
                LogPrintf("Still rescanning. At block %d. Progress=%f\n", pindex->nHeight, gvp);
            }
            CBlock block;
            if (ReadBlockFromDisk(block, pindex, Params().GetConsensus())) {
                LOCK2(cs_main, cs_wallet);
                if (pindex && !chainActive.Contains(pindex)) {
                    ret = pindex;
                    break;
                }
                for (size_t posInBlock = 0; posInBlock < block.vtx.size(); ++posInBlock) {
                    AddToWalletIfInvolvingMe(block.vtx[posInBlock], pindex, posInBlock, fUpdate);
                }
            } else {
                ret = pindex;
            }
            if (pindex == pindexStop) {
                break;
            }
            {
                LOCK(cs_main);
                pindex = chainActive.Next(pindex);
                gvp = GuessVerificationProgress(chainParams.TxData(), pindex);
                if (tip != chainActive.Tip()) {
                    tip = chainActive.Tip();
                    dProgressTip = GuessVerificationProgress(chainParams.TxData(), tip);
                }
            }
        }
        if (pindex && fAbortRescan) {
            LogPrintf("Rescan aborted at block %d. Progress=%f\n", pindex->nHeight, gvp);
        }
        ShowProgress(_("Rescanning..."), 100); 
    }
    return ret;
}
CBlockIndex * CWallet :: ScanForWalletTransactions ( CBlockIndex * pindexStart , CBlockIndex * pindexStop , const WalletRescanReserver & reserver , bool fUpdate ) { int64_t nNow = GetTime ( ) ; const CChainParams & chainParams = Params ( ) ; assert ( reserver . isReserved ( ) ) ; if ( pindexStop ) { assert ( pindexStop -> nHeight >= pindexStart -> nHeight ) ; } CBlockIndex * pindex = pindexStart ; CBlockIndex * ret = nullptr ; if ( pindex ) LogPrintf ( " \n " , pindex -> nHeight ) ; { fAbortRescan = false ; ShowProgress ( _ ( " " ) , 0 ) ; CBlockIndex * tip = nullptr ; double dProgressStart ; double dProgressTip ; { LOCK ( cs_main ) ; tip = chainActive . Tip ( ) ; dProgressStart = GuessVerificationProgress ( chainParams . TxData ( ) , pindex ) ; dProgressTip = GuessVerificationProgress ( chainParams . TxData ( ) , tip ) ; } double gvp = dProgressStart ; while ( pindex && ! fAbortRescan ) { if ( pindex -> nHeight % 100 == 0 && dProgressTip - dProgressStart > 0.0 ) { ShowProgress ( _ ( " " ) , std :: max ( 1 , std :: min ( 99 , ( int ) ( ( gvp - dProgressStart ) / ( dProgressTip - dProgressStart ) * 100 ) ) ) ) ; } if ( GetTime ( ) >= nNow + 60 ) { nNow = GetTime ( ) ; LogPrintf ( " \n " , pindex -> nHeight , gvp ) ; } CBlock block ; if ( ReadBlockFromDisk ( block , pindex , Params ( ) . GetConsensus ( ) ) ) { LOCK2 ( cs_main , cs_wallet ) ; if ( pindex && ! chainActive . Contains ( pindex ) ) { ret = pindex ; break ; } for ( size_t posInBlock = 0 ; posInBlock < block . vtx . size ( ) ; ++ posInBlock ) { AddToWalletIfInvolvingMe ( block . vtx [ posInBlock ] , pindex , posInBlock , fUpdate ) ; } } else { ret = pindex ; } if ( pindex == pindexStop ) { break ; } { LOCK ( cs_main ) ; pindex = chainActive . Next ( pindex ) ; gvp = GuessVerificationProgress ( chainParams . TxData ( ) , pindex ) ; if ( tip != chainActive . Tip ( ) ) { tip = chainActive . Tip ( ) ; dProgressTip = GuessVerificationProgress ( chainParams . TxData ( ) , tip ) ; } } } if ( pindex && fAbortRescan ) { LogPrintf ( " \n " , pindex -> nHeight , gvp ) ; } ShowProgress ( _ ( " " ) , 100 ) ; } return ret ; }
Scan the block chain (starting in pindexStart) for transactions
 from or to us.
Scan the block chain ( starting in pindexStart ) for transactions from or to us .
// show rescan progress in GUI as dialog or on splashscreen, if -rescan on startup // Abort scan if current block is no longer active, to prevent // marking transactions as coming from the wrong block. // in case the tip has changed, update progress max // hide progress dialog in GUI