Skip to content

Commit e074feb

Browse files
committed
Eliminate data races for strMiscWarning and fLargeWork*Found.
This moves all access to these datastructures through accessor functions and protects them with a lock. Coming from btc@e3ba0ef95636290a3bb597ddd25d13ea13b034aa
1 parent 5fdd73e commit e074feb

File tree

4 files changed

+52
-15
lines changed

4 files changed

+52
-15
lines changed

src/timedata.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample, int nOffsetLimit)
7474
// Only let other nodes change our time by so much
7575
if (abs64(nMedian) < nOffsetLimit) {
7676
nTimeOffset = nMedian;
77-
strMiscWarning = "";
77+
SetMiscWarning("");
7878
} else {
7979
nTimeOffset = (nMedian > 0 ? 1 : -1) * nOffsetLimit;
8080
std::string strMessage = _("Warning: Please check that your computer's date and time are correct! If your clock is wrong PIVX Core will not work properly.");
81-
strMiscWarning = strMessage;
81+
SetMiscWarning(strMessage);
8282
LogPrintf("*** %s\n", strMessage);
8383
uiInterface.ThreadSafeMessageBox(strMessage, "", CClientUIInterface::MSG_ERROR);
8484
}

src/util.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ ArgsManager gArgs;
100100
bool fDaemon = false;
101101
CTranslationInterface translationInterface;
102102

103+
RecursiveMutex cs_warnings;
103104
std::string strMiscWarning;
104105
bool fLargeWorkForkFound = false;
105106
bool fLargeWorkInvalidChainFound = false;
@@ -843,11 +844,43 @@ int GetNumCores()
843844
return std::thread::hardware_concurrency();
844845
}
845846

847+
void SetMiscWarning(const std::string& strWarning)
848+
{
849+
LOCK(cs_warnings);
850+
strMiscWarning = strWarning;
851+
}
852+
853+
void SetfLargeWorkForkFound(bool flag)
854+
{
855+
LOCK(cs_warnings);
856+
fLargeWorkForkFound = flag;
857+
}
858+
859+
bool GetfLargeWorkForkFound()
860+
{
861+
LOCK(cs_warnings);
862+
return fLargeWorkForkFound;
863+
}
864+
865+
void SetfLargeWorkInvalidChainFound(bool flag)
866+
{
867+
LOCK(cs_warnings);
868+
fLargeWorkInvalidChainFound = flag;
869+
}
870+
871+
bool GetfLargeWorkInvalidChainFound()
872+
{
873+
LOCK(cs_warnings);
874+
return fLargeWorkInvalidChainFound;
875+
}
876+
846877
std::string GetWarnings(const std::string& strFor)
847878
{
848879
std::string strStatusBar;
849880
std::string strRPC;
850881

882+
LOCK(cs_warnings);
883+
851884
if (!CLIENT_VERSION_IS_RELEASE)
852885
strStatusBar = _("This is a pre-release test build - use at your own risk - do not use for staking or merchant applications!");
853886

src/util.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ extern std::string strBudgetMode;
5858
extern CTranslationInterface translationInterface;
5959

6060
static const bool DEFAULT_TESTSAFEMODE = false;
61-
extern std::string strMiscWarning;
62-
extern bool fLargeWorkForkFound;
63-
extern bool fLargeWorkInvalidChainFound;
6461

6562
/**
6663
* Translation function: Call Translate signal on UI interface, which returns a Optional result.
@@ -266,6 +263,11 @@ void TraceThread(const char* name, Callable func)
266263

267264
fs::path AbsPathForConfigVal(const fs::path& path, bool net_specific = true);
268265

266+
void SetMiscWarning(const std::string& strWarning);
267+
void SetfLargeWorkForkFound(bool flag);
268+
bool GetfLargeWorkForkFound();
269+
void SetfLargeWorkInvalidChainFound(bool flag);
270+
bool GetfLargeWorkInvalidChainFound();
269271
std::string GetWarnings(const std::string& strFor);
270272

271273
#endif // BITCOIN_UTIL_H

src/validation.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ void CheckForkWarningConditions()
874874
pindexBestForkTip = nullptr;
875875

876876
if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > pChainTip->nChainWork + (GetBlockProof(*pChainTip) * 6))) {
877-
if (!fLargeWorkForkFound && pindexBestForkBase) {
877+
if (!GetfLargeWorkForkFound() && pindexBestForkBase) {
878878
if (pindexBestForkBase->phashBlock) {
879879
std::string warning = std::string("'Warning: Large-work fork detected, forking after block ") +
880880
pindexBestForkBase->phashBlock->ToString() + std::string("'");
@@ -886,15 +886,15 @@ void CheckForkWarningConditions()
886886
LogPrintf("CheckForkWarningConditions: Warning: Large valid fork found\n forking the chain at height %d (%s)\n lasting to height %d (%s).\nChain state database corruption likely.\n",
887887
pindexBestForkBase->nHeight, pindexBestForkBase->phashBlock->ToString(),
888888
pindexBestForkTip->nHeight, pindexBestForkTip->phashBlock->ToString());
889-
fLargeWorkForkFound = true;
889+
SetfLargeWorkForkFound(true);
890890
}
891891
} else {
892892
LogPrintf("CheckForkWarningConditions: Warning: Found invalid chain at least ~6 blocks longer than our best chain.\nChain state database corruption likely.\n");
893-
fLargeWorkInvalidChainFound = true;
893+
SetfLargeWorkInvalidChainFound(true);
894894
}
895895
} else {
896-
fLargeWorkForkFound = false;
897-
fLargeWorkInvalidChainFound = false;
896+
SetfLargeWorkForkFound(false);
897+
SetfLargeWorkInvalidChainFound(false);
898898
}
899899
}
900900

@@ -1184,7 +1184,7 @@ bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsVi
11841184
/** Abort with a message */
11851185
static bool AbortNode(const std::string& strMessage, const std::string& userMessage="")
11861186
{
1187-
strMiscWarning = strMessage;
1187+
SetMiscWarning(strMessage);
11881188
LogPrintf("*** %s\n", strMessage);
11891189
uiInterface.ThreadSafeMessageBox(
11901190
userMessage.empty() ? _("Error: A fatal internal error occured, see debug.log for details") : userMessage,
@@ -1922,10 +1922,12 @@ void static UpdateTip(CBlockIndex* pindexNew)
19221922
if (nUpgraded > 0)
19231923
LogPrintf("SetBestChain: %d of last 100 blocks above version %d\n", nUpgraded, (int)CBlock::CURRENT_VERSION);
19241924
if (nUpgraded > 100 / 2) {
1925-
// strMiscWarning is read by GetWarnings(), called by Qt and the JSON-RPC code to warn the user:
1926-
strMiscWarning = _("Warning: This version is obsolete, upgrade required!");
1927-
AlertNotify(strMiscWarning);
1928-
fWarned = true;
1925+
std::string strWarning = _("Warning: This version is obsolete, upgrade required!");
1926+
SetMiscWarning(strWarning);
1927+
if (!fWarned) {
1928+
AlertNotify(strWarning);
1929+
fWarned = true;
1930+
}
19291931
}
19301932
}
19311933
}

0 commit comments

Comments
 (0)