Skip to content

Commit f97d335

Browse files
committed
Merge #8392: Fix several node initialization issues
9d4eb9a Do diskspace check before import thread is started (Pieter Wuille) aa59f2e Add extra message to avoid a long 'Loading banlist' (Pieter Wuille) 0fd2a33 Use a signal to continue init after genesis activation (Pieter Wuille)
2 parents 37d83bb + 9d4eb9a commit f97d335

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

src/init.cpp

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

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

1325-
if (!fReindex) {
1340+
if (!fReindex && chainActive.Tip() != NULL) {
13261341
uiInterface.InitMessage(_("Rewinding blocks..."));
13271342
if (!RewindBlockIndex(chainparams)) {
13281343
strLoadError = _("Unable to rewind the database to a pre-fork state. You will need to redownload the blockchain");
@@ -1439,6 +1454,17 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
14391454

14401455
// ********************************************************* Step 10: import blocks
14411456

1457+
if (!CheckDiskSpace())
1458+
return false;
1459+
1460+
// Either install a handler to notify us when genesis activates, or set fHaveGenesis directly.
1461+
// No locking, as this happens before any background thread is started.
1462+
if (chainActive.Tip() == NULL) {
1463+
uiInterface.NotifyBlockTip.connect(BlockNotifyGenesisWait);
1464+
} else {
1465+
fHaveGenesis = true;
1466+
}
1467+
14421468
if (mapArgs.count("-blocknotify"))
14431469
uiInterface.NotifyBlockTip.connect(BlockNotifyCallback);
14441470

@@ -1448,26 +1474,20 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
14481474
BOOST_FOREACH(const std::string& strFile, mapMultiArgs["-loadblock"])
14491475
vImportFiles.push_back(strFile);
14501476
}
1477+
14511478
threadGroup.create_thread(boost::bind(&ThreadImport, vImportFiles));
14521479

14531480
// Wait for genesis block to be processed
1454-
bool fHaveGenesis = false;
1455-
while (!fHaveGenesis && !fRequestShutdown) {
1456-
{
1457-
LOCK(cs_main);
1458-
fHaveGenesis = (chainActive.Tip() != NULL);
1459-
}
1460-
1461-
if (!fHaveGenesis) {
1462-
MilliSleep(10);
1481+
{
1482+
boost::unique_lock<boost::mutex> lock(cs_GenesisWait);
1483+
while (!fHaveGenesis) {
1484+
condvar_GenesisWait.wait(lock);
14631485
}
1486+
uiInterface.NotifyBlockTip.disconnect(BlockNotifyGenesisWait);
14641487
}
14651488

14661489
// ********************************************************* Step 11: start node
14671490

1468-
if (!CheckDiskSpace())
1469-
return false;
1470-
14711491
if (!strErrors.str().empty())
14721492
return InitError(strErrors.str());
14731493

src/main.cpp

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

src/net.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,6 +2050,8 @@ void StartNode(boost::thread_group& threadGroup, CScheduler& scheduler)
20502050
DumpBanlist();
20512051
}
20522052

2053+
uiInterface.InitMessage(_("Starting network threads..."));
2054+
20532055
fAddressesInitialized = true;
20542056

20552057
if (semOutbound == NULL) {

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)