| 5380 | } |
| 5381 | |
| 5382 | bool static LoadBlockIndexDB() |
| 5383 | { |
| 5384 | const CChainParams& chainparams = Params(); |
| 5385 | if (!pblocktree->LoadBlockIndexGuts()) |
| 5386 | return false; |
| 5387 | |
| 5388 | boost::this_thread::interruption_point(); |
| 5389 | |
| 5390 | // Calculate nChainWork |
| 5391 | vector<pair<int, CBlockIndex*> > vSortedByHeight; |
| 5392 | vSortedByHeight.reserve(mapBlockIndex.size()); |
| 5393 | for (auto const &item : mapBlockIndex) { |
| 5394 | if (fRequestShutdown) return false; |
| 5395 | CBlockIndex* pindex = item.second; |
| 5396 | vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex)); |
| 5397 | } |
| 5398 | sort(vSortedByHeight.begin(), vSortedByHeight.end()); |
| 5399 | for (auto const &item : vSortedByHeight) { |
| 5400 | if (fRequestShutdown) return false; |
| 5401 | CBlockIndex* pindex = item.second; |
| 5402 | pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + GetBlockProof(*pindex); |
| 5403 | pindex->nTimeMax = (pindex->pprev ? std::max(pindex->pprev->nTimeMax, pindex->nTime) : pindex->nTime); |
| 5404 | |
| 5405 | // We can link the chain of blocks for which we've received transactions at some point. |
| 5406 | // Pruned nodes may have deleted the block. |
| 5407 | if (pindex->nTx > 0) { |
| 5408 | if (pindex->pprev) { |
| 5409 | if (pindex->pprev->nChainTx) { |
| 5410 | pindex->nChainTx = pindex->pprev->nChainTx + pindex->nTx; |
| 5411 | } else { |
| 5412 | pindex->nChainTx = 0; |
| 5413 | mapBlocksUnlinked.insert(std::make_pair(pindex->pprev, pindex)); |
| 5414 | } |
| 5415 | } else { |
| 5416 | pindex->nChainTx = pindex->nTx; |
| 5417 | } |
| 5418 | } |
| 5419 | if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && (pindex->nChainTx || pindex->pprev == NULL)) |
| 5420 | setBlockIndexCandidates.insert(pindex); |
| 5421 | if (pindex->nStatus & BLOCK_FAILED_MASK && (!pindexBestInvalid || pindex->nChainWork > pindexBestInvalid->nChainWork)) |
| 5422 | pindexBestInvalid = pindex; |
| 5423 | if (pindex->pprev) |
| 5424 | pindex->BuildSkip(); |
| 5425 | if (pindex->IsValid(BLOCK_VALID_TREE) && (pindexBestHeader == NULL || CBlockIndexWorkComparator()(pindexBestHeader, pindex))) |
| 5426 | pindexBestHeader = pindex; |
| 5427 | } |
| 5428 | |
| 5429 | // Load block file info |
| 5430 | pblocktree->ReadLastBlockFile(nLastBlockFile); |
| 5431 | vinfoBlockFile.resize(nLastBlockFile + 1); |
| 5432 | LogPrintf("%s: last block file = %i\n", __func__, nLastBlockFile); |
| 5433 | for (int nFile = 0; nFile <= nLastBlockFile; nFile++) { |
| 5434 | if (fRequestShutdown) return false; |
| 5435 | pblocktree->ReadBlockFileInfo(nFile, vinfoBlockFile[nFile]); |
| 5436 | } |
| 5437 | LogPrintf("%s: last block file info: %s\n", __func__, vinfoBlockFile[nLastBlockFile].ToString()); |
| 5438 | for (int nFile = nLastBlockFile + 1; true; nFile++) { |
| 5439 | if (fRequestShutdown) return false; |
no test coverage detected