| 5741 | |
| 5742 | |
| 5743 | bool InitBlockIndex(const CChainParams& chainparams) |
| 5744 | { |
| 5745 | LOCK(cs_main); |
| 5746 | // Check whether we're already initialized |
| 5747 | if (chainActive.Genesis() != NULL) |
| 5748 | return true; |
| 5749 | |
| 5750 | // Use the provided setting for -txindex in the new database |
| 5751 | fTxIndex = GetBoolArg("-txindex", DEFAULT_TXINDEX); |
| 5752 | pblocktree->WriteFlag("txindex", fTxIndex); |
| 5753 | |
| 5754 | // Use the provided setting for -addressindex in the new database |
| 5755 | fAddressIndex = GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX); |
| 5756 | pblocktree->WriteFlag("addressindex", fAddressIndex); |
| 5757 | |
| 5758 | // Use the provided setting for -spentindex in the new database |
| 5759 | fSpentIndex = GetBoolArg("-spentindex", DEFAULT_SPENTINDEX); |
| 5760 | pblocktree->WriteFlag("spentindex", fSpentIndex); |
| 5761 | |
| 5762 | LogPrintf("Initializing databases...\n"); |
| 5763 | |
| 5764 | // Only add the genesis block if not reindexing (in which case we reuse the one already on disk) |
| 5765 | if (!fReindex) { |
| 5766 | try { |
| 5767 | CBlock& block = const_cast<CBlock&>(chainparams.GenesisBlock()); |
| 5768 | // Start new block file |
| 5769 | unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION); |
| 5770 | CDiskBlockPos blockPos; |
| 5771 | CValidationState state; |
| 5772 | if (!FindBlockPos(state, blockPos, nBlockSize + 8, 0, block.GetBlockTime())) |
| 5773 | return error("InitBlockIndex() : FindBlockPos failed"); |
| 5774 | if (!WriteBlockToDisk(block, blockPos)) |
| 5775 | return error("InitBlockIndex() : writing genesis block to disk failed"); |
| 5776 | CBlockIndex* pindex = AddToBlockIndex(block); |
| 5777 | if (!ReceivedBlockTransactions(block, state, pindex, blockPos)) |
| 5778 | return error("InitBlockIndex() : genesis block not accepted"); |
| 5779 | if (!ActivateBestChain(state, chainparams, &block)) |
| 5780 | return error("InitBlockIndex() : genesis block cannot be activated"); |
| 5781 | // Force a chainstate write so that when we VerifyDB in a moment, it doesnt check stale data |
| 5782 | return FlushStateToDisk(state, FLUSH_STATE_ALWAYS); |
| 5783 | } catch (std::runtime_error& e) { |
| 5784 | return error("InitBlockIndex() : failed to initialize block database: %s", e.what()); |
| 5785 | } |
| 5786 | } |
| 5787 | |
| 5788 | return true; |
| 5789 | } |
| 5790 | |
| 5791 | bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskBlockPos* dbp) |
| 5792 | { |