Skip to content

Commit 0fd2a33

Browse files
committed
Use a signal to continue init after genesis activation
1 parent bbcb8fd commit 0fd2a33

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

src/init.cpp

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,21 @@ static void BlockNotifyCallback(bool initialSync, const CBlockIndex *pBlockIndex
510510
boost::thread t(runCommand, strCmd); // thread runs free
511511
}
512512

513+
static bool fHaveGenesis = false;
514+
static boost::mutex cs_GenesisWait;
515+
static CConditionVariable condvar_GenesisWait;
516+
517+
static void BlockNotifyGenesisWait(bool, const CBlockIndex *pBlockIndex)
518+
{
519+
if (pBlockIndex != NULL) {
520+
{
521+
boost::unique_lock<boost::mutex> lock_GenesisWait(cs_GenesisWait);
522+
fHaveGenesis = true;
523+
}
524+
condvar_GenesisWait.notify_all();
525+
}
526+
}
527+
513528
struct CImportingNow
514529
{
515530
CImportingNow() {
@@ -1286,7 +1301,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
12861301
break;
12871302
}
12881303

1289-
if (!fReindex) {
1304+
if (!fReindex && chainActive.Tip() != NULL) {
12901305
uiInterface.InitMessage(_("Rewinding blocks..."));
12911306
if (!RewindBlockIndex(chainparams)) {
12921307
strLoadError = _("Unable to rewind the database to a pre-fork state. You will need to redownload the blockchain");
@@ -1403,6 +1418,14 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
14031418

14041419
// ********************************************************* Step 10: import blocks
14051420

1421+
// Either install a handler to notify us when genesis activates, or set fHaveGenesis directly.
1422+
// No locking, as this happens before any background thread is started.
1423+
if (chainActive.Tip() == NULL) {
1424+
uiInterface.NotifyBlockTip.connect(BlockNotifyGenesisWait);
1425+
} else {
1426+
fHaveGenesis = true;
1427+
}
1428+
14061429
if (mapArgs.count("-blocknotify"))
14071430
uiInterface.NotifyBlockTip.connect(BlockNotifyCallback);
14081431

@@ -1412,19 +1435,16 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
14121435
BOOST_FOREACH(const std::string& strFile, mapMultiArgs["-loadblock"])
14131436
vImportFiles.push_back(strFile);
14141437
}
1438+
14151439
threadGroup.create_thread(boost::bind(&ThreadImport, vImportFiles));
14161440

14171441
// Wait for genesis block to be processed
1418-
bool fHaveGenesis = false;
1419-
while (!fHaveGenesis && !fRequestShutdown) {
1420-
{
1421-
LOCK(cs_main);
1422-
fHaveGenesis = (chainActive.Tip() != NULL);
1423-
}
1424-
1425-
if (!fHaveGenesis) {
1426-
MilliSleep(10);
1442+
{
1443+
boost::unique_lock<boost::mutex> lock(cs_GenesisWait);
1444+
while (!fHaveGenesis) {
1445+
condvar_GenesisWait.wait(lock);
14271446
}
1447+
uiInterface.NotifyBlockTip.disconnect(BlockNotifyGenesisWait);
14281448
}
14291449

14301450
// ********************************************************* Step 11: start node

src/main.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4331,8 +4331,6 @@ bool InitBlockIndex(const CChainParams& chainparams)
43314331
CBlockIndex *pindex = AddToBlockIndex(block);
43324332
if (!ReceivedBlockTransactions(block, state, pindex, blockPos))
43334333
return error("LoadBlockIndex(): genesis block not accepted");
4334-
if (!ActivateBestChain(state, chainparams, &block))
4335-
return error("LoadBlockIndex(): genesis block cannot be activated");
43364334
// Force a chainstate write so that when we VerifyDB in a moment, it doesn't check stale data
43374335
return FlushStateToDisk(state, FLUSH_STATE_ALWAYS);
43384336
} catch (const std::runtime_error& e) {

src/test/test_bitcoin.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
6060
pcoinsdbview = new CCoinsViewDB(1 << 23, true);
6161
pcoinsTip = new CCoinsViewCache(pcoinsdbview);
6262
InitBlockIndex(chainparams);
63+
{
64+
CValidationState state;
65+
bool ok = ActivateBestChain(state, chainparams);
66+
BOOST_CHECK(ok);
67+
}
6368
nScriptCheckThreads = 3;
6469
for (int i=0; i < nScriptCheckThreads-1; i++)
6570
threadGroup.create_thread(&ThreadScriptCheck);

0 commit comments

Comments
 (0)