From c29b6296ed1d8d03205e1ad6aa6ad62792da1d10 Mon Sep 17 00:00:00 2001 From: smk762 Date: Sat, 2 Sep 2023 19:06:35 +0800 Subject: [PATCH] add progress logs for loading block/index --- src/main.cpp | 13 +++++++++++-- src/txdb.cpp | 15 +++++++++++++-- src/txdb.h | 2 +- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index a3468c3d636..d4593ea7c7e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6019,10 +6019,11 @@ CBlockIndex * InsertBlockIndex(uint256 hash) bool static LoadBlockIndexDB() { const CChainParams& chainparams = Params(); + int nHighest = 1; LogPrintf("%s: start loading guts\n", __func__); { LOCK(cs_main); - if (!pblocktree->LoadBlockIndexGuts()) + if (!pblocktree->LoadBlockIndexGuts(nHighest)) return false; } LogPrintf("%s: loaded guts\n", __func__); @@ -6037,10 +6038,18 @@ bool static LoadBlockIndexDB() vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex)); } sort(vSortedByHeight.begin(), vSortedByHeight.end()); - + int nHeight = 0; + int nLastPercent = -1; for(const auto& item : vSortedByHeight) { + int nPercent = 100 * nHeight / nHighest; + if (nPercent > nLastPercent && nPercent % 10 == 0) { + LogPrintf("Indexing blocks... %d%%\n", (100 * nHeight) / nHighest); + nLastPercent = nPercent; + } CBlockIndex* pindex = item.second; + nHeight = pindex-> nHeight; + pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + GetBlockProof(*pindex); // We can link the chain of blocks for which we've received transactions at some point. // Pruned nodes may have deleted the block. diff --git a/src/txdb.cpp b/src/txdb.cpp index b87d3487439..46237aecd0c 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -707,14 +707,23 @@ bool CBlockTreeDB::blockOnchainActive(const uint256 &hash) { return true; } -bool CBlockTreeDB::LoadBlockIndexGuts() +bool CBlockTreeDB::LoadBlockIndexGuts(int& nHighest) { boost::scoped_ptr pcursor(NewIterator()); pcursor->Seek(make_pair(DB_BLOCK_INDEX, uint256())); + int nCount = 0; + int nLastPercent = -1; + // Load mapBlockIndex while (pcursor->Valid()) { + int nPercent = 100 * nCount / nHighest; + if (nPercent > nLastPercent && nPercent % 10 == 0) { + LogPrintf("Loading blocks... %d%%\n", (100 * nCount) / nHighest); + nLastPercent = nPercent; + } + nCount++; boost::this_thread::interruption_point(); std::pair key; if (pcursor->GetKey(key) && key.first == DB_BLOCK_INDEX) { @@ -723,7 +732,9 @@ bool CBlockTreeDB::LoadBlockIndexGuts() // Construct block index object CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash()); pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev); - pindexNew->nHeight = diskindex.nHeight; + pindexNew->nHeight = diskindex.nHeight; + if (diskindex.nHeight > nHighest) + nHighest = diskindex.nHeight; pindexNew->nFile = diskindex.nFile; pindexNew->nDataPos = diskindex.nDataPos; pindexNew->nUndoPos = diskindex.nUndoPos; diff --git a/src/txdb.h b/src/txdb.h index 0e7908c7d88..7ab9ba4f232 100644 --- a/src/txdb.h +++ b/src/txdb.h @@ -279,7 +279,7 @@ class CBlockTreeDB : public CDBWrapper * NOTE: this does no consistency check beyond verifying records exist * @returns true on success */ - bool LoadBlockIndexGuts(); + bool LoadBlockIndexGuts(int& nHighest); /**** * Check if a block is on the active chain * @param hash the block hash