@ -3864,42 +3864,55 @@ void UnloadBlockIndex()
bool LoadBlockIndex ( const CChainParams & chainparams )
bool LoadBlockIndex ( const CChainParams & chainparams )
{
{
// Load block index from databases
// Load block index from databases
if ( ! fReindex & & ! LoadBlockIndexDB ( chainparams ) )
bool needs_init = fReindex ;
return false ;
if ( ! fReindex ) {
bool ret = LoadBlockIndexDB ( chainparams ) ;
if ( ! ret ) return false ;
needs_init = mapBlockIndex . empty ( ) ;
}
if ( needs_init ) {
// Everything here is for *new* reindex/DBs. Thus, though
// LoadBlockIndexDB may have set fReindex if we shut down
// mid-reindex previously, we don't check fReindex and
// instead only check it prior to LoadBlockIndexDB to set
// needs_init.
LogPrintf ( " Initializing databases... \n " ) ;
// Use the provided setting for -txindex in the new database
fTxIndex = GetBoolArg ( " -txindex " , DEFAULT_TXINDEX ) ;
pblocktree - > WriteFlag ( " txindex " , fTxIndex ) ;
}
return true ;
return true ;
}
}
bool InitBlockIndex ( const CChainParams & chainparams )
bool LoadGenesisBlock ( const CChainParams & chainparams )
{
{
LOCK ( cs_main ) ;
LOCK ( cs_main ) ;
// Check whether we're already initialized
// Check whether we're already initialized by checking for genesis in
if ( chainActive . Genesis ( ) ! = NULL )
// mapBlockIndex. Note that we can't use chainActive here, since it is
// set based on the coins db, not the block index db, which is the only
// thing loaded at this point.
if ( mapBlockIndex . count ( chainparams . GenesisBlock ( ) . GetHash ( ) ) )
return true ;
return true ;
// Use the provided setting for -txindex in the new database
fTxIndex = GetBoolArg ( " -txindex " , DEFAULT_TXINDEX ) ;
pblocktree - > WriteFlag ( " txindex " , fTxIndex ) ;
LogPrintf ( " Initializing databases... \n " ) ;
// Only add the genesis block if not reindexing (in which case we reuse the one already on disk)
// Only add the genesis block if not reindexing (in which case we reuse the one already on disk)
if ( ! fReindex ) {
try {
try {
CBlock & block = const_cast < CBlock & > ( chainparams . GenesisBlock ( ) ) ;
CBlock & block = const_cast < CBlock & > ( chainparams . GenesisBlock ( ) ) ;
// Start new block file
// Start new block file
unsigned int nBlockSize = : : GetSerializeSize ( block , SER_DISK , CLIENT_VERSION ) ;
unsigned int nBlockSize = : : GetSerializeSize ( block , SER_DISK , CLIENT_VERSION ) ;
CDiskBlockPos blockPos ;
CDiskBlockPos blockPos ;
CValidationState state ;
CValidationState state ;
if ( ! FindBlockPos ( state , blockPos , nBlockSize + 8 , 0 , block . GetBlockTime ( ) ) )
if ( ! FindBlockPos ( state , blockPos , nBlockSize + 8 , 0 , block . GetBlockTime ( ) ) )
return error ( " %s: FindBlockPos failed " , __func__ ) ;
return error ( " LoadBlockIndex() : FindBlockPos failed " ) ;
if ( ! WriteBlockToDisk ( block , blockPos , chainparams . MessageStart ( ) ) )
if ( ! WriteBlockToDisk ( block , blockPos , chainparams . MessageStart ( ) ) )
return error ( " %s: writing genesis block to disk failed " , __func__ ) ;
return error ( " LoadBlockIndex() : writing genesis block to disk failed " ) ;
CBlockIndex * pindex = AddToBlockIndex ( block ) ;
CBlockIndex * pindex = AddToBlockIndex ( block ) ;
if ( ! ReceivedBlockTransactions ( block , state , pindex , blockPos , chainparams . GetConsensus ( ) ) )
if ( ! ReceivedBlockTransactions ( block , state , pindex , blockPos , chainparams . GetConsensus ( ) ) )
return error ( " %s: genesis block not accepted " , __func__ ) ;
return error ( " LoadBlockIndex() : genesis block not accepted " ) ;
} catch ( const std : : runtime_error & e ) {
} catch ( const std : : runtime_error & e ) {
return error ( " %s: failed to write genesis block: %s " , __func__ , e . what ( ) ) ;
return error ( " LoadBlockIndex() : failed to initialize block database : % s " , e.what()) ;
}
}
}
return true ;
return true ;