| 2393 | } |
| 2394 | |
| 2395 | bool InitBlockIndex() { |
| 2396 | LOCK(cs_main); |
| 2397 | // Check whether we're already initialized |
| 2398 | if (chainActive.Genesis() != nullptr) |
| 2399 | return true; |
| 2400 | |
| 2401 | // Use the provided setting for -txindex in the new database |
| 2402 | SysCfg().SetTxIndex(SysCfg().GetBoolArg("-txindex", true)); |
| 2403 | pCdMan->pBlockCache->WriteFlag("txindex", SysCfg().IsTxIndex()); |
| 2404 | LogPrint(BCLog::INFO, "Initializing databases...\n"); |
| 2405 | |
| 2406 | // Only add the genesis block if not reindexing (in which case we reuse the one already on disk) |
| 2407 | if (!SysCfg().IsReindex()) { |
| 2408 | try { |
| 2409 | CBlock &block = const_cast<CBlock &>(SysCfg().GenesisBlock()); |
| 2410 | // Start new block file |
| 2411 | uint32_t nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION); |
| 2412 | CDiskBlockPos blockPos; |
| 2413 | CValidationState state; |
| 2414 | if (!FindBlockPos(state, blockPos, nBlockSize + 8, 0, block.GetTime())) |
| 2415 | return ERRORMSG("FindBlockPos failed"); |
| 2416 | |
| 2417 | if (!WriteBlockToDisk(block, blockPos)) |
| 2418 | return ERRORMSG("writing genesis block to disk failed"); |
| 2419 | |
| 2420 | if (!AddToBlockIndex(block, state, blockPos)) |
| 2421 | return ERRORMSG("genesis block not accepted"); |
| 2422 | |
| 2423 | } catch (runtime_error &e) { |
| 2424 | return ERRORMSG("failed to initialize block database: %s", e.what()); |
| 2425 | } |
| 2426 | } |
| 2427 | |
| 2428 | return true; |
| 2429 | } |
| 2430 | |
| 2431 | void PrintBlockTree() { |
| 2432 | AssertLockHeld(cs_main); |
no test coverage detected