@@ -3864,42 +3864,55 @@ void UnloadBlockIndex()
3864
3864
bool LoadBlockIndex (const CChainParams& chainparams)
3865
3865
{
3866
3866
// Load block index from databases
3867
- if (!fReindex && !LoadBlockIndexDB (chainparams))
3868
- return false ;
3867
+ bool needs_init = fReindex ;
3868
+ if (!fReindex ) {
3869
+ bool ret = LoadBlockIndexDB (chainparams);
3870
+ if (!ret) return false ;
3871
+ needs_init = mapBlockIndex.empty ();
3872
+ }
3873
+
3874
+ if (needs_init) {
3875
+ // Everything here is for *new* reindex/DBs. Thus, though
3876
+ // LoadBlockIndexDB may have set fReindex if we shut down
3877
+ // mid-reindex previously, we don't check fReindex and
3878
+ // instead only check it prior to LoadBlockIndexDB to set
3879
+ // needs_init.
3880
+
3881
+ LogPrintf (" Initializing databases...\n " );
3882
+ // Use the provided setting for -txindex in the new database
3883
+ fTxIndex = GetBoolArg (" -txindex" , DEFAULT_TXINDEX);
3884
+ pblocktree->WriteFlag (" txindex" , fTxIndex );
3885
+ }
3869
3886
return true ;
3870
3887
}
3871
3888
3872
- bool InitBlockIndex (const CChainParams& chainparams)
3889
+ bool LoadGenesisBlock (const CChainParams& chainparams)
3873
3890
{
3874
3891
LOCK (cs_main);
3875
3892
3876
- // Check whether we're already initialized
3877
- if (chainActive.Genesis () != NULL )
3893
+ // Check whether we're already initialized by checking for genesis in
3894
+ // mapBlockIndex. Note that we can't use chainActive here, since it is
3895
+ // set based on the coins db, not the block index db, which is the only
3896
+ // thing loaded at this point.
3897
+ if (mapBlockIndex.count (chainparams.GenesisBlock ().GetHash ()))
3878
3898
return true ;
3879
3899
3880
- // Use the provided setting for -txindex in the new database
3881
- fTxIndex = GetBoolArg (" -txindex" , DEFAULT_TXINDEX);
3882
- pblocktree->WriteFlag (" txindex" , fTxIndex );
3883
- LogPrintf (" Initializing databases...\n " );
3884
-
3885
3900
// Only add the genesis block if not reindexing (in which case we reuse the one already on disk)
3886
- if (!fReindex ) {
3887
- try {
3888
- CBlock &block = const_cast <CBlock&>(chainparams.GenesisBlock ());
3889
- // Start new block file
3890
- unsigned int nBlockSize = ::GetSerializeSize (block, SER_DISK, CLIENT_VERSION);
3891
- CDiskBlockPos blockPos;
3892
- CValidationState state;
3893
- if (!FindBlockPos (state, blockPos, nBlockSize+8 , 0 , block.GetBlockTime ()))
3894
- return error (" LoadBlockIndex(): FindBlockPos failed" );
3895
- if (!WriteBlockToDisk (block, blockPos, chainparams.MessageStart ()))
3896
- return error (" LoadBlockIndex(): writing genesis block to disk failed" );
3897
- CBlockIndex *pindex = AddToBlockIndex (block);
3898
- if (!ReceivedBlockTransactions (block, state, pindex, blockPos, chainparams.GetConsensus ()))
3899
- return error (" LoadBlockIndex(): genesis block not accepted" );
3900
- } catch (const std::runtime_error& e) {
3901
- return error (" LoadBlockIndex(): failed to initialize block database: %s" , e.what ());
3902
- }
3901
+ try {
3902
+ CBlock &block = const_cast <CBlock&>(chainparams.GenesisBlock ());
3903
+ // Start new block file
3904
+ unsigned int nBlockSize = ::GetSerializeSize (block, SER_DISK, CLIENT_VERSION);
3905
+ CDiskBlockPos blockPos;
3906
+ CValidationState state;
3907
+ if (!FindBlockPos (state, blockPos, nBlockSize+8 , 0 , block.GetBlockTime ()))
3908
+ return error (" %s: FindBlockPos failed" , __func__);
3909
+ if (!WriteBlockToDisk (block, blockPos, chainparams.MessageStart ()))
3910
+ return error (" %s: writing genesis block to disk failed" , __func__);
3911
+ CBlockIndex *pindex = AddToBlockIndex (block);
3912
+ if (!ReceivedBlockTransactions (block, state, pindex, blockPos, chainparams.GetConsensus ()))
3913
+ return error (" %s: genesis block not accepted" , __func__);
3914
+ } catch (const std::runtime_error& e) {
3915
+ return error (" %s: failed to write genesis block: %s" , __func__, e.what ());
3903
3916
}
3904
3917
3905
3918
return true ;
0 commit comments