@@ -3743,45 +3743,56 @@ void UnloadBlockIndex()
37433743
37443744bool LoadBlockIndex (std::string& strError)
37453745{
3746- // Load block index from databases
3747- if (!fReindex && !LoadBlockIndexDB (strError))
3748- return false ;
3746+ bool needs_init = fReindex ;
3747+ if (!fReindex ) {
3748+ if (!LoadBlockIndexDB (strError))
3749+ return false ;
3750+ needs_init = mapBlockIndex.empty ();
3751+ }
3752+
3753+ if (needs_init) {
3754+ // Everything here is for *new* reindex/DBs. Thus, though
3755+ // LoadBlockIndexDB may have set fReindex if we shut down
3756+ // mid-reindex previously, we don't check fReindex and
3757+ // instead only check it prior to LoadBlockIndexDB to set
3758+ // needs_init.
3759+
3760+ LogPrintf (" Initializing databases...\n " );
3761+ // Use the provided setting for -txindex in the new database
3762+ fTxIndex = gArgs .GetBoolArg (" -txindex" , DEFAULT_TXINDEX);
3763+ pblocktree->WriteFlag (" txindex" , fTxIndex );
3764+ }
37493765 return true ;
37503766}
37513767
37523768
3753- bool InitBlockIndex ()
3769+ bool LoadGenesisBlock ()
37543770{
37553771 LOCK (cs_main);
37563772
3757- // Check whether we're already initialized
3758- if (chainActive.Genesis () != NULL )
3773+ // Check whether we're already initialized by checking for genesis in
3774+ // mapBlockIndex. Note that we can't use chainActive here, since it is
3775+ // set based on the coins db, not the block index db, which is the only
3776+ // thing loaded at this point.
3777+ if (mapBlockIndex.count (Params ().GenesisBlock ().GetHash ()))
37593778 return true ;
37603779
3761- // Use the provided setting for -txindex in the new database
3762- fTxIndex = gArgs .GetBoolArg (" -txindex" , true );
3763- pblocktree->WriteFlag (" txindex" , fTxIndex );
3764- LogPrintf (" Initializing databases...\n " );
3765-
3766- // Only add the genesis block if not reindexing (in which case we reuse the one already on disk)
3767- if (!fReindex ) {
3768- try {
3769- CBlock& block = const_cast <CBlock&>(Params ().GenesisBlock ());
3770- // Start new block file
3771- unsigned int nBlockSize = ::GetSerializeSize (block, SER_DISK, CLIENT_VERSION);
3772- CDiskBlockPos blockPos;
3773- CValidationState state;
3774- if (!FindBlockPos (state, blockPos, nBlockSize + 8 , 0 , block.GetBlockTime ()))
3775- return error (" LoadBlockIndex() : FindBlockPos failed" );
3776- if (!WriteBlockToDisk (block, blockPos))
3777- return error (" LoadBlockIndex() : writing genesis block to disk failed" );
3778- CBlockIndex* pindex = AddToBlockIndex (block);
3779- if (!ReceivedBlockTransactions (block, state, pindex, blockPos))
3780- return error (" LoadBlockIndex() : genesis block not accepted" );
3781- } catch (const std::runtime_error& e) {
3782- return error (" LoadBlockIndex() : failed to initialize block database: %s" , e.what ());
3783- }
3784- }
3780+ try {
3781+ CBlock& block = const_cast <CBlock&>(Params ().GenesisBlock ());
3782+ // Start new block file
3783+ unsigned int nBlockSize = ::GetSerializeSize (block, SER_DISK, CLIENT_VERSION);
3784+ CDiskBlockPos blockPos;
3785+ CValidationState state;
3786+ if (!FindBlockPos (state, blockPos, nBlockSize + 8 , 0 , block.GetBlockTime ()))
3787+ return error (" %s: FindBlockPos failed" , __func__);
3788+ if (!WriteBlockToDisk (block, blockPos))
3789+ return error (" %s: writing genesis block to disk failed" , __func__);
3790+ CBlockIndex *pindex = AddToBlockIndex (block);
3791+ if (!ReceivedBlockTransactions (block, state, pindex, blockPos))
3792+ return error (" %s: genesis block not accepted" , __func__);
3793+ } catch (const std::runtime_error& e) {
3794+ return error (" %s: failed to write genesis block: %s" , __func__, e.what ());
3795+ }
37853796
37863797 return true ;
37873798}
0 commit comments