Skip to content

Commit

Permalink
add progress logs for loading block/index
Browse files Browse the repository at this point in the history
  • Loading branch information
smk762 committed Sep 2, 2023
1 parent 657cc69 commit c29b629
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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__);
Expand All @@ -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.
Expand Down
15 changes: 13 additions & 2 deletions src/txdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,14 +707,23 @@ bool CBlockTreeDB::blockOnchainActive(const uint256 &hash) {
return true;
}

bool CBlockTreeDB::LoadBlockIndexGuts()
bool CBlockTreeDB::LoadBlockIndexGuts(int& nHighest)
{
boost::scoped_ptr<CDBIterator> 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<char, uint256> key;
if (pcursor->GetKey(key) && key.first == DB_BLOCK_INDEX) {
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/txdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c29b629

Please sign in to comment.