| 4110 | |
| 4111 | |
| 4112 | bool InitBlockIndex() { |
| 4113 | const CChainParams& chainparams = Params(); |
| 4114 | LOCK(cs_main); |
| 4115 | |
| 4116 | // Initialize global variables that cannot be constructed at startup. |
| 4117 | recentRejects.reset(new CRollingBloomFilter(120000, 0.000001)); |
| 4118 | |
| 4119 | // Check whether we're already initialized |
| 4120 | if (chainActive.Genesis() != NULL) |
| 4121 | return true; |
| 4122 | |
| 4123 | // Use the provided setting for -txindex in the new database |
| 4124 | fTxIndex = GetBoolArg("-txindex", false); |
| 4125 | pblocktree->WriteFlag("txindex", fTxIndex); |
| 4126 | LogPrintf("Initializing databases...\n"); |
| 4127 | |
| 4128 | // Only add the genesis block if not reindexing (in which case we reuse the one already on disk) |
| 4129 | if (!fReindex) { |
| 4130 | try { |
| 4131 | CBlock &block = const_cast<CBlock&>(Params().GenesisBlock()); |
| 4132 | // Start new block file |
| 4133 | unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION); |
| 4134 | CDiskBlockPos blockPos; |
| 4135 | CValidationState state; |
| 4136 | if (!FindBlockPos(state, blockPos, nBlockSize+8, 0, block.GetBlockTime())) |
| 4137 | return error("LoadBlockIndex(): FindBlockPos failed"); |
| 4138 | if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart())) |
| 4139 | return error("LoadBlockIndex(): writing genesis block to disk failed"); |
| 4140 | CBlockIndex *pindex = AddToBlockIndex(block); |
| 4141 | if (!ReceivedBlockTransactions(block, state, pindex, blockPos)) |
| 4142 | return error("LoadBlockIndex(): genesis block not accepted"); |
| 4143 | if (!ActivateBestChain(state, &block)) |
| 4144 | return error("LoadBlockIndex(): genesis block cannot be activated"); |
| 4145 | // Force a chainstate write so that when we VerifyDB in a moment, it doesn't check stale data |
| 4146 | return FlushStateToDisk(state, FLUSH_STATE_ALWAYS); |
| 4147 | } catch (const std::runtime_error& e) { |
| 4148 | return error("LoadBlockIndex(): failed to initialize block database: %s", e.what()); |
| 4149 | } |
| 4150 | } |
| 4151 | |
| 4152 | return true; |
| 4153 | } |
| 4154 | |
| 4155 | |
| 4156 | |