src/wallet/wallet.cpp
/**
 * 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.
 */
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.
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 .
int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
{
    int ret = 0;
    int64_t nNow = GetTime();
    const CChainParams& chainParams = Params();
    CBlockIndex* pindex = pindexStart;
    std::vector<uint256> myTxHashes;
    {
        LOCK2(cs_main, cs_wallet);
        while (pindex && nTimeFirstKey && (pindex->GetBlockTime() < (nTimeFirstKey - 7200)))
            pindex = chainActive.Next(pindex);
        ShowProgress(_("Rescanning..."), 0); 
        double dProgressStart = Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), pindex, false);
        double dProgressTip = Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), chainActive.Tip(), false);
        while (pindex)
        {
            if (pindex->nHeight % 100 == 0 && dProgressTip - dProgressStart > 0.0)
                ShowProgress(_("Rescanning..."), std::max(1, std::min(99, (int)((Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), pindex, false) - dProgressStart) / (dProgressTip - dProgressStart) * 100))));
            CBlock block;
            ReadBlockFromDisk(block, pindex, Params().GetConsensus());
            BOOST_FOREACH(CTransaction& tx, block.vtx)
            {
                if (AddToWalletIfInvolvingMe(tx, &block, fUpdate)) {
                    myTxHashes.push_back(tx.GetHash());
                    ret++;
                }
            }
            SproutMerkleTree sproutTree;
            SaplingMerkleTree saplingTree;
            assert(pcoinsTip->GetSproutAnchorAt(pindex->hashSproutAnchor, sproutTree));
            if (pindex->pprev) {
                if (NetworkUpgradeActive(pindex->pprev->nHeight, Params().GetConsensus(), Consensus::UPGRADE_ACADIA)) {
                    assert(pcoinsTip->GetSaplingAnchorAt(pindex->pprev->hashFinalSaplingRoot, saplingTree));
                }
            }
            ChainTipAdded(pindex, &block, sproutTree, saplingTree);
            pindex = chainActive.Next(pindex);
            if (GetTime() >= nNow + 60) {
                nNow = GetTime();
                LogPrintf("Still rescanning. At block %d. Progress=%f\n", pindex->nHeight, Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), pindex));
            }
        }
        CWalletDB walletdb(strWalletFile, "r+", false);
        for (auto hash : myTxHashes) {
            CWalletTx wtx = mapWallet[hash];
            if (!wtx.mapSaplingNoteData.empty()) {
                if (!wtx.WriteToDisk(&walletdb)) {
                    LogPrintf("Rescanning... WriteToDisk failed to update Sapling note data for: %s\n", hash.ToString());
                }
            }
        }
        ShowProgress(_("Rescanning..."), 100); 
    }
    return ret;
}
int CWallet :: ScanForWalletTransactions ( CBlockIndex * pindexStart , bool fUpdate ) { int ret = 0 ; int64_t nNow = GetTime ( ) ; const CChainParams & chainParams = Params ( ) ; CBlockIndex * pindex = pindexStart ; std :: vector < uint256 > myTxHashes ; { LOCK2 ( cs_main , cs_wallet ) ; while ( pindex && nTimeFirstKey && ( pindex -> GetBlockTime ( ) < ( nTimeFirstKey - 7200 ) ) ) pindex = chainActive . Next ( pindex ) ; ShowProgress ( _ ( " " ) , 0 ) ; double dProgressStart = Checkpoints :: GuessVerificationProgress ( chainParams . Checkpoints ( ) , pindex , false ) ; double dProgressTip = Checkpoints :: GuessVerificationProgress ( chainParams . Checkpoints ( ) , chainActive . Tip ( ) , false ) ; while ( pindex ) { if ( pindex -> nHeight % 100 == 0 && dProgressTip - dProgressStart > 0.0 ) ShowProgress ( _ ( " " ) , std :: max ( 1 , std :: min ( 99 , ( int ) ( ( Checkpoints :: GuessVerificationProgress ( chainParams . Checkpoints ( ) , pindex , false ) - dProgressStart ) / ( dProgressTip - dProgressStart ) * 100 ) ) ) ) ; CBlock block ; ReadBlockFromDisk ( block , pindex , Params ( ) . GetConsensus ( ) ) ; BOOST_FOREACH ( CTransaction & tx , block . vtx ) { if ( AddToWalletIfInvolvingMe ( tx , & block , fUpdate ) ) { myTxHashes . push_back ( tx . GetHash ( ) ) ; ret ++ ; } } SproutMerkleTree sproutTree ; SaplingMerkleTree saplingTree ; assert ( pcoinsTip -> GetSproutAnchorAt ( pindex -> hashSproutAnchor , sproutTree ) ) ; if ( pindex -> pprev ) { if ( NetworkUpgradeActive ( pindex -> pprev -> nHeight , Params ( ) . GetConsensus ( ) , Consensus :: UPGRADE_ACADIA ) ) { assert ( pcoinsTip -> GetSaplingAnchorAt ( pindex -> pprev -> hashFinalSaplingRoot , saplingTree ) ) ; } } ChainTipAdded ( pindex , & block , sproutTree , saplingTree ) ; pindex = chainActive . Next ( pindex ) ; if ( GetTime ( ) >= nNow + 60 ) { nNow = GetTime ( ) ; LogPrintf ( " \n " , pindex -> nHeight , Checkpoints :: GuessVerificationProgress ( chainParams . Checkpoints ( ) , pindex ) ) ; } } CWalletDB walletdb ( strWalletFile , " " , false ) ; for ( auto hash : myTxHashes ) { CWalletTx wtx = mapWallet [ hash ] ; if ( ! wtx . mapSaplingNoteData . empty ( ) ) { if ( ! wtx . WriteToDisk ( & walletdb ) ) { LogPrintf ( " \n " , hash . ToString ( ) ) ; } } } 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 .
// no need to read and scan block, if block was created before // our wallet birthday (as adjusted for block time variability) // show rescan progress in GUI as dialog or on splashscreen, if -rescan on startup // This should never fail: we should always be able to get the tree // state on the path to the tip of our chain // Increment note witness caches // After rescanning, persist Sapling note data that might have changed, e.g. nullifiers. // Do not flush the wallet here for performance reasons. // hide progress dialog in GUI