MCPcopy Create free account
hub / github.com/WaykiChain/WaykiChain / LoadBlockIndexDB

Function LoadBlockIndexDB

src/main.cpp:2225–2287  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2223}
2224
2225bool static LoadBlockIndexDB() {
2226 if (!pCdMan->pBlockIndexDb->LoadBlockIndexes())
2227 return ERRORMSG("%s(), LoadBlockIndexes from db failed", __FUNCTION__);
2228
2229 boost::this_thread::interruption_point();
2230
2231 vector<pair<int32_t, CBlockIndex *> > vSortedByHeight;
2232 vSortedByHeight.reserve(mapBlockIndex.size());
2233 for (const auto &item : mapBlockIndex) {
2234 CBlockIndex *pIndex = item.second;
2235 vSortedByHeight.push_back(make_pair(pIndex->height, pIndex));
2236 }
2237 sort(vSortedByHeight.begin(), vSortedByHeight.end());
2238 for (const auto &item : vSortedByHeight) {
2239 CBlockIndex *pIndex = item.second;
2240 if ((pIndex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TRANSACTIONS && !(pIndex->nStatus & BLOCK_FAILED_MASK))
2241 setBlockIndexValid.insert(pIndex);
2242 if (pIndex->nStatus & BLOCK_FAILED_MASK &&
2243 (pIndexBestInvalid == nullptr || pIndex->height > pIndexBestInvalid->height))
2244 pIndexBestInvalid = pIndex;
2245 if (pIndex->pprev)
2246 pIndex->BuildSkip();
2247 }
2248
2249 // Load block file info
2250 pCdMan->pBlockCache->ReadLastBlockFile(nLastBlockFile);
2251 LogPrint(BCLog::INFO, "last block file = %i\n", nLastBlockFile);
2252 if (pCdMan->pBlockIndexDb->ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile))
2253 LogPrint(BCLog::INFO, "last block file info: %s\n", infoLastBlockFile.ToString());
2254
2255 // Check whether we need to continue reindexing
2256 bool fReindexing = false;
2257 pCdMan->pBlockCache->ReadReindexing(fReindexing);
2258
2259 bool fCurReindex = SysCfg().IsReindex();
2260 SysCfg().SetReIndex(fCurReindex |= fReindexing);
2261
2262 // Check whether we have a transaction index
2263 bool bTxIndex = SysCfg().IsTxIndex();
2264 pCdMan->pBlockCache->ReadFlag("txindex", bTxIndex);
2265 SysCfg().SetTxIndex(bTxIndex);
2266 LogPrint(BCLog::INFO, "transaction index %s\n", bTxIndex ? "enabled" : "disabled");
2267
2268 // Load pointer to end of best chain
2269 uint256 bestBlockHash = pCdMan->pBlockCache->GetBestBlockHash();
2270 const auto &it = mapBlockIndex.find(bestBlockHash);
2271 if (it == mapBlockIndex.end()) {
2272 return ERRORMSG("%s(), the best block hash in db not found in block index! hash=%s\n",
2273 __FUNCTION__, bestBlockHash.ToString());
2274 }
2275 auto tipIndex = it->second;
2276 CBlock block;
2277 if (!ReadBlockFromDisk(tipIndex, block)) {
2278 return ERRORMSG("Failed to read block=%s from disk", tipIndex->GetIdString());
2279 }
2280 chainActive.SetTip(tipIndex, &block);
2281 // chainActive.UpdateFinalityBlock();
2282 LogPrint(BCLog::INFO, "LoadBlockIndexDB(): hashBestChain=%s height=%d date=%s\n",

Callers 1

LoadBlockIndexFunction · 0.85

Calls 15

sortFunction · 0.85
ReadBlockFromDiskFunction · 0.85
DateTimeStrFormatFunction · 0.85
LoadBlockIndexesMethod · 0.80
reserveMethod · 0.80
push_backMethod · 0.80
BuildSkipMethod · 0.80
ReadLastBlockFileMethod · 0.80
ReadBlockFileInfoMethod · 0.80
ReadReindexingMethod · 0.80
IsReindexMethod · 0.80
SetReIndexMethod · 0.80

Tested by

no test coverage detected