Skip to content

Commit 316623f

Browse files
committed
Switch reindexing to AcceptBlock in-loop and ActivateBestChain afterwards
1 parent d253ec4 commit 316623f

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

src/init.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ std::string HelpMessage(HelpMessageMode mode)
404404
strUsage += HelpMessageOpt("-limitdescendantcount=<n>", strprintf("Do not accept transactions if any ancestor would have <n> or more in-mempool descendants (default: %u)", DEFAULT_DESCENDANT_LIMIT));
405405
strUsage += HelpMessageOpt("-limitdescendantsize=<n>", strprintf("Do not accept transactions if any ancestor would have more than <n> kilobytes of in-mempool descendants (default: %u).", DEFAULT_DESCENDANT_SIZE_LIMIT));
406406
}
407-
string debugCategories = "addrman, alert, bench, coindb, db, lock, rand, rpc, selectcoins, mempool, mempoolrej, net, proxy, prune, http, libevent, tor, zmq"; // Don't translate these and qt below
407+
string debugCategories = "addrman, alert, bench, coindb, db, http, libevent, lock, mempool, mempoolrej, net, proxy, prune, rand, reindex, rpc, selectcoins, tor, zmq"; // Don't translate these and qt below
408408
if (mode == HMM_BITCOIN_QT)
409409
debugCategories += ", qt";
410410
strUsage += HelpMessageOpt("-debug=<category>", strprintf(_("Output debugging information (default: %u, supplying <category> is optional)"), 0) + ". " +
@@ -554,9 +554,10 @@ void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
554554
{
555555
const CChainParams& chainparams = Params();
556556
RenameThread("bitcoin-loadblk");
557+
CImportingNow imp;
558+
557559
// -reindex
558560
if (fReindex) {
559-
CImportingNow imp;
560561
int nFile = 0;
561562
while (true) {
562563
CDiskBlockPos pos(nFile, 0);
@@ -581,7 +582,6 @@ void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
581582
if (boost::filesystem::exists(pathBootstrap)) {
582583
FILE *file = fopen(pathBootstrap.string().c_str(), "rb");
583584
if (file) {
584-
CImportingNow imp;
585585
boost::filesystem::path pathBootstrapOld = GetDataDir() / "bootstrap.dat.old";
586586
LogPrintf("Importing bootstrap.dat...\n");
587587
LoadExternalBlockFile(chainparams, file);
@@ -595,14 +595,20 @@ void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
595595
BOOST_FOREACH(const boost::filesystem::path& path, vImportFiles) {
596596
FILE *file = fopen(path.string().c_str(), "rb");
597597
if (file) {
598-
CImportingNow imp;
599598
LogPrintf("Importing blocks file %s...\n", path.string());
600599
LoadExternalBlockFile(chainparams, file);
601600
} else {
602601
LogPrintf("Warning: Could not open blocks file %s\n", path.string());
603602
}
604603
}
605604

605+
// scan for better chains in the block chain database, that are not yet connected in the active best chain
606+
CValidationState state;
607+
if (!ActivateBestChain(state, chainparams)) {
608+
LogPrintf("Failed to connect best block");
609+
StartShutdown();
610+
}
611+
606612
if (GetBoolArg("-stopafterblockimport", DEFAULT_STOPAFTERBLOCKIMPORT)) {
607613
LogPrintf("Stopping after block import\n");
608614
StartShutdown();
@@ -1358,12 +1364,6 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
13581364
if (mapArgs.count("-blocknotify"))
13591365
uiInterface.NotifyBlockTip.connect(BlockNotifyCallback);
13601366

1361-
uiInterface.InitMessage(_("Activating best chain..."));
1362-
// scan for better chains in the block chain database, that are not yet connected in the active best chain
1363-
CValidationState state;
1364-
if (!ActivateBestChain(state, chainparams))
1365-
strErrors << "Failed to connect best block";
1366-
13671367
std::vector<boost::filesystem::path> vImportFiles;
13681368
if (mapArgs.count("-loadblock"))
13691369
{

src/main.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3402,7 +3402,8 @@ static bool AcceptBlock(const CBlock& block, CValidationState& state, const CCha
34023402
{
34033403
AssertLockHeld(cs_main);
34043404

3405-
CBlockIndex *&pindex = *ppindex;
3405+
CBlockIndex *pindexDummy = NULL;
3406+
CBlockIndex *&pindex = ppindex ? *ppindex : pindexDummy;
34063407

34073408
if (!AcceptBlockHeader(block, state, chainparams, &pindex))
34083409
return false;
@@ -4037,13 +4038,14 @@ bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskB
40374038

40384039
// process in case the block isn't known yet
40394040
if (mapBlockIndex.count(hash) == 0 || (mapBlockIndex[hash]->nStatus & BLOCK_HAVE_DATA) == 0) {
4041+
LOCK(cs_main);
40404042
CValidationState state;
4041-
if (ProcessNewBlock(state, chainparams, NULL, &block, true, dbp))
4043+
if (AcceptBlock(block, state, chainparams, NULL, true, dbp))
40424044
nLoaded++;
40434045
if (state.IsError())
40444046
break;
40454047
} else if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex[hash]->nHeight % 1000 == 0) {
4046-
LogPrintf("Block Import: already had block %s at height %d\n", hash.ToString(), mapBlockIndex[hash]->nHeight);
4048+
LogPrint("reindex", "Block Import: already had block %s at height %d\n", hash.ToString(), mapBlockIndex[hash]->nHeight);
40474049
}
40484050

40494051
// Recursively process earlier encountered successors of this block
@@ -4057,10 +4059,11 @@ bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskB
40574059
std::multimap<uint256, CDiskBlockPos>::iterator it = range.first;
40584060
if (ReadBlockFromDisk(block, it->second, chainparams.GetConsensus()))
40594061
{
4060-
LogPrintf("%s: Processing out of order child %s of %s\n", __func__, block.GetHash().ToString(),
4062+
LogPrint("reindex", "%s: Processing out of order child %s of %s\n", __func__, block.GetHash().ToString(),
40614063
head.ToString());
4064+
LOCK(cs_main);
40624065
CValidationState dummy;
4063-
if (ProcessNewBlock(dummy, chainparams, NULL, &block, true, &it->second))
4066+
if (AcceptBlock(block, dummy, chainparams, NULL, true, &it->second))
40644067
{
40654068
nLoaded++;
40664069
queue.push_back(block.GetHash());

0 commit comments

Comments
 (0)