Skip to content

Commit 7f72568

Browse files
committed
Merge #9236: Fix races for strMiscWarning and fLargeWork*Found, make QT runawayException use GetWarnings
749be01 Move GetWarnings() into its own file. (Gregory Maxwell) e3ba0ef Eliminate data races for strMiscWarning and fLargeWork*Found. (Gregory Maxwell) c63198f Make QT runawayException call GetWarnings instead of directly access strMiscWarning. (Gregory Maxwell)
2 parents a336d13 + 749be01 commit 7f72568

File tree

10 files changed

+138
-64
lines changed

10 files changed

+138
-64
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ BITCOIN_CORE_H = \
155155
wallet/rpcwallet.h \
156156
wallet/wallet.h \
157157
wallet/walletdb.h \
158+
warnings.h \
158159
zmq/zmqabstractnotifier.h \
159160
zmq/zmqconfig.h\
160161
zmq/zmqnotificationinterface.h \
@@ -306,6 +307,7 @@ libbitcoin_common_a_SOURCES = \
306307
scheduler.cpp \
307308
script/sign.cpp \
308309
script/standard.cpp \
310+
warnings.cpp \
309311
$(BITCOIN_CORE_H)
310312

311313
# util: shared between all executables.

src/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#ifdef ENABLE_WALLET
4242
#include "wallet/wallet.h"
4343
#endif
44+
#include "warnings.h"
4445
#include <stdint.h>
4546
#include <stdio.h>
4647
#include <memory>

src/qt/bitcoin.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "scheduler.h"
3131
#include "ui_interface.h"
3232
#include "util.h"
33+
#include "warnings.h"
3334

3435
#ifdef ENABLE_WALLET
3536
#include "wallet/wallet.h"
@@ -260,7 +261,7 @@ BitcoinCore::BitcoinCore():
260261
void BitcoinCore::handleRunawayException(const std::exception *e)
261262
{
262263
PrintExceptionContinue(e, "Runaway exception");
263-
Q_EMIT runawayException(QString::fromStdString(strMiscWarning));
264+
Q_EMIT runawayException(QString::fromStdString(GetWarnings("gui")));
264265
}
265266

266267
void BitcoinCore::initialize()
@@ -691,10 +692,10 @@ int main(int argc, char *argv[])
691692
app.exec();
692693
} catch (const std::exception& e) {
693694
PrintExceptionContinue(&e, "Runaway exception");
694-
app.handleRunawayException(QString::fromStdString(strMiscWarning));
695+
app.handleRunawayException(QString::fromStdString(GetWarnings("gui")));
695696
} catch (...) {
696697
PrintExceptionContinue(NULL, "Runaway exception");
697-
app.handleRunawayException(QString::fromStdString(strMiscWarning));
698+
app.handleRunawayException(QString::fromStdString(GetWarnings("gui")));
698699
}
699700
return app.getReturnValue();
700701
}

src/timedata.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "ui_interface.h"
1414
#include "util.h"
1515
#include "utilstrencodings.h"
16+
#include "warnings.h"
1617

1718
#include <boost/foreach.hpp>
1819

@@ -103,8 +104,8 @@ void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
103104
if (!fMatch)
104105
{
105106
fDone = true;
106-
string strMessage = strprintf(_("Please check that your computer's date and time are correct! If your clock is wrong, %s will not work properly."), _(PACKAGE_NAME));
107-
strMiscWarning = strMessage;
107+
std::string strMessage = strprintf(_("Please check that your computer's date and time are correct! If your clock is wrong, %s will not work properly."), _(PACKAGE_NAME));
108+
SetMiscWarning(strMessage);
108109
uiInterface.ThreadSafeMessageBox(strMessage, "", CClientUIInterface::MSG_WARNING);
109110
}
110111
}

src/util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ map<string, vector<string> > mapMultiArgs;
107107
bool fDebug = false;
108108
bool fPrintToConsole = false;
109109
bool fPrintToDebugLog = true;
110-
string strMiscWarning;
110+
111111
bool fLogTimestamps = DEFAULT_LOGTIMESTAMPS;
112112
bool fLogTimeMicros = DEFAULT_LOGTIMEMICROS;
113113
bool fLogIPs = DEFAULT_LOGIPS;

src/util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ extern std::map<std::string, std::vector<std::string> > mapMultiArgs;
4646
extern bool fDebug;
4747
extern bool fPrintToConsole;
4848
extern bool fPrintToDebugLog;
49-
extern std::string strMiscWarning;
49+
5050
extern bool fLogTimestamps;
5151
extern bool fLogTimeMicros;
5252
extern bool fLogIPs;

src/validation.cpp

Lines changed: 16 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "utilstrencodings.h"
3535
#include "validationinterface.h"
3636
#include "versionbits.h"
37+
#include "warnings.h"
3738

3839
#include <atomic>
3940
#include <sstream>
@@ -1141,8 +1142,6 @@ bool IsInitialBlockDownload()
11411142
return false;
11421143
}
11431144

1144-
bool fLargeWorkForkFound = false;
1145-
bool fLargeWorkInvalidChainFound = false;
11461145
CBlockIndex *pindexBestForkTip = NULL, *pindexBestForkBase = NULL;
11471146

11481147
static void AlertNotify(const std::string& strMessage)
@@ -1177,7 +1176,7 @@ void CheckForkWarningConditions()
11771176

11781177
if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > chainActive.Tip()->nChainWork + (GetBlockProof(*chainActive.Tip()) * 6)))
11791178
{
1180-
if (!fLargeWorkForkFound && pindexBestForkBase)
1179+
if (!GetfLargeWorkForkFound() && pindexBestForkBase)
11811180
{
11821181
std::string warning = std::string("'Warning: Large-work fork detected, forking after block ") +
11831182
pindexBestForkBase->phashBlock->ToString() + std::string("'");
@@ -1188,18 +1187,18 @@ void CheckForkWarningConditions()
11881187
LogPrintf("%s: 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", __func__,
11891188
pindexBestForkBase->nHeight, pindexBestForkBase->phashBlock->ToString(),
11901189
pindexBestForkTip->nHeight, pindexBestForkTip->phashBlock->ToString());
1191-
fLargeWorkForkFound = true;
1190+
SetfLargeWorkForkFound(true);
11921191
}
11931192
else
11941193
{
11951194
LogPrintf("%s: Warning: Found invalid chain at least ~6 blocks longer than our best chain.\nChain state database corruption likely.\n", __func__);
1196-
fLargeWorkInvalidChainFound = true;
1195+
SetfLargeWorkInvalidChainFound(true);
11971196
}
11981197
}
11991198
else
12001199
{
1201-
fLargeWorkForkFound = false;
1202-
fLargeWorkInvalidChainFound = false;
1200+
SetfLargeWorkForkFound(false);
1201+
SetfLargeWorkInvalidChainFound(false);
12031202
}
12041203
}
12051204

@@ -1475,7 +1474,7 @@ bool UndoReadFromDisk(CBlockUndo& blockundo, const CDiskBlockPos& pos, const uin
14751474
/** Abort with a message */
14761475
bool AbortNode(const std::string& strMessage, const std::string& userMessage="")
14771476
{
1478-
strMiscWarning = strMessage;
1477+
SetMiscWarning(strMessage);
14791478
LogPrintf("*** %s\n", strMessage);
14801479
uiInterface.ThreadSafeMessageBox(
14811480
userMessage.empty() ? _("Error: A fatal internal error occurred, see debug.log for details") : userMessage,
@@ -2044,9 +2043,10 @@ void static UpdateTip(CBlockIndex *pindexNew, const CChainParams& chainParams) {
20442043
ThresholdState state = checker.GetStateFor(pindex, chainParams.GetConsensus(), warningcache[bit]);
20452044
if (state == THRESHOLD_ACTIVE || state == THRESHOLD_LOCKED_IN) {
20462045
if (state == THRESHOLD_ACTIVE) {
2047-
strMiscWarning = strprintf(_("Warning: unknown new rules activated (versionbit %i)"), bit);
2046+
std::string strWarning = strprintf(_("Warning: unknown new rules activated (versionbit %i)"), bit);
2047+
SetMiscWarning(strWarning);
20482048
if (!fWarned) {
2049-
AlertNotify(strMiscWarning);
2049+
AlertNotify(strWarning);
20502050
fWarned = true;
20512051
}
20522052
} else {
@@ -2066,10 +2066,11 @@ void static UpdateTip(CBlockIndex *pindexNew, const CChainParams& chainParams) {
20662066
warningMessages.push_back(strprintf("%d of last 100 blocks have unexpected version", nUpgraded));
20672067
if (nUpgraded > 100/2)
20682068
{
2069-
// strMiscWarning is read by GetWarnings(), called by Qt and the JSON-RPC code to warn the user:
2070-
strMiscWarning = _("Warning: Unknown block versions being mined! It's possible unknown rules are in effect");
2069+
std::string strWarning = _("Warning: Unknown block versions being mined! It's possible unknown rules are in effect");
2070+
// notify GetWarnings(), called by Qt and the JSON-RPC code to warn the user:
2071+
SetMiscWarning(strWarning);
20712072
if (!fWarned) {
2072-
AlertNotify(strMiscWarning);
2073+
AlertNotify(strWarning);
20732074
fWarned = true;
20742075
}
20752076
}
@@ -4010,51 +4011,10 @@ void static CheckBlockIndex(const Consensus::Params& consensusParams)
40104011
assert(nNodes == forward.size());
40114012
}
40124013

4013-
std::string GetWarnings(const std::string& strFor)
4014+
std::string CBlockFileInfo::ToString() const
40144015
{
4015-
string strStatusBar;
4016-
string strRPC;
4017-
string strGUI;
4018-
const string uiAlertSeperator = "<hr />";
4019-
4020-
if (!CLIENT_VERSION_IS_RELEASE) {
4021-
strStatusBar = "This is a pre-release test build - use at your own risk - do not use for mining or merchant applications";
4022-
strGUI = _("This is a pre-release test build - use at your own risk - do not use for mining or merchant applications");
4023-
}
4024-
4025-
if (GetBoolArg("-testsafemode", DEFAULT_TESTSAFEMODE))
4026-
strStatusBar = strRPC = strGUI = "testsafemode enabled";
4027-
4028-
// Misc warnings like out of disk space and clock is wrong
4029-
if (strMiscWarning != "")
4030-
{
4031-
strStatusBar = strMiscWarning;
4032-
strGUI += (strGUI.empty() ? "" : uiAlertSeperator) + strMiscWarning;
4033-
}
4034-
4035-
if (fLargeWorkForkFound)
4036-
{
4037-
strStatusBar = strRPC = "Warning: The network does not appear to fully agree! Some miners appear to be experiencing issues.";
4038-
strGUI += (strGUI.empty() ? "" : uiAlertSeperator) + _("Warning: The network does not appear to fully agree! Some miners appear to be experiencing issues.");
4039-
}
4040-
else if (fLargeWorkInvalidChainFound)
4041-
{
4042-
strStatusBar = strRPC = "Warning: We do not appear to fully agree with our peers! You may need to upgrade, or other nodes may need to upgrade.";
4043-
strGUI += (strGUI.empty() ? "" : uiAlertSeperator) + _("Warning: We do not appear to fully agree with our peers! You may need to upgrade, or other nodes may need to upgrade.");
4044-
}
4045-
4046-
if (strFor == "gui")
4047-
return strGUI;
4048-
else if (strFor == "statusbar")
4049-
return strStatusBar;
4050-
else if (strFor == "rpc")
4051-
return strRPC;
4052-
assert(!"GetWarnings(): invalid parameter");
4053-
return "error";
4016+
return strprintf("CBlockFileInfo(blocks=%u, size=%u, heights=%u...%u, time=%s...%s)", nBlocks, nSize, nHeightFirst, nHeightLast, DateTimeStrFormat("%Y-%m-%d", nTimeFirst), DateTimeStrFormat("%Y-%m-%d", nTimeLast));
40544017
}
4055-
std::string CBlockFileInfo::ToString() const {
4056-
return strprintf("CBlockFileInfo(blocks=%u, size=%u, heights=%u...%u, time=%s...%s)", nBlocks, nSize, nHeightFirst, nHeightLast, DateTimeStrFormat("%Y-%m-%d", nTimeFirst), DateTimeStrFormat("%Y-%m-%d", nTimeLast));
4057-
}
40584018

40594019
ThresholdState VersionBitsTipState(const Consensus::Params& params, Consensus::DeploymentPos pos)
40604020
{

src/validation.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ static const bool DEFAULT_CHECKPOINTS_ENABLED = true;
137137
static const bool DEFAULT_TXINDEX = false;
138138
static const unsigned int DEFAULT_BANSCORE_THRESHOLD = 100;
139139

140-
static const bool DEFAULT_TESTSAFEMODE = false;
141140
/** Default for -mempoolreplacement */
142141
static const bool DEFAULT_ENABLE_REPLACEMENT = true;
143142
/** Default for using fee filter */

src/warnings.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright (c) 2009-2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2016 The Bitcoin Core developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#include "sync.h"
7+
#include "clientversion.h"
8+
#include "util.h"
9+
#include "warnings.h"
10+
11+
CCriticalSection cs_warnings;
12+
std::string strMiscWarning;
13+
bool fLargeWorkForkFound = false;
14+
bool fLargeWorkInvalidChainFound = false;
15+
16+
void SetMiscWarning(const std::string& strWarning)
17+
{
18+
LOCK(cs_warnings);
19+
strMiscWarning = strWarning;
20+
}
21+
22+
void SetfLargeWorkForkFound(bool flag)
23+
{
24+
LOCK(cs_warnings);
25+
fLargeWorkForkFound = flag;
26+
}
27+
28+
bool GetfLargeWorkForkFound()
29+
{
30+
LOCK(cs_warnings);
31+
return fLargeWorkForkFound;
32+
}
33+
34+
void SetfLargeWorkInvalidChainFound(bool flag)
35+
{
36+
LOCK(cs_warnings);
37+
fLargeWorkInvalidChainFound = flag;
38+
}
39+
40+
bool GetfLargeWorkInvalidChainFound()
41+
{
42+
LOCK(cs_warnings);
43+
return fLargeWorkInvalidChainFound;
44+
}
45+
46+
std::string GetWarnings(const std::string& strFor)
47+
{
48+
std::string strStatusBar;
49+
std::string strRPC;
50+
std::string strGUI;
51+
const std::string uiAlertSeperator = "<hr />";
52+
53+
LOCK(cs_warnings);
54+
55+
if (!CLIENT_VERSION_IS_RELEASE) {
56+
strStatusBar = "This is a pre-release test build - use at your own risk - do not use for mining or merchant applications";
57+
strGUI = _("This is a pre-release test build - use at your own risk - do not use for mining or merchant applications");
58+
}
59+
60+
if (GetBoolArg("-testsafemode", DEFAULT_TESTSAFEMODE))
61+
strStatusBar = strRPC = strGUI = "testsafemode enabled";
62+
63+
// Misc warnings like out of disk space and clock is wrong
64+
if (strMiscWarning != "")
65+
{
66+
strStatusBar = strMiscWarning;
67+
strGUI += (strGUI.empty() ? "" : uiAlertSeperator) + strMiscWarning;
68+
}
69+
70+
if (fLargeWorkForkFound)
71+
{
72+
strStatusBar = strRPC = "Warning: The network does not appear to fully agree! Some miners appear to be experiencing issues.";
73+
strGUI += (strGUI.empty() ? "" : uiAlertSeperator) + _("Warning: The network does not appear to fully agree! Some miners appear to be experiencing issues.");
74+
}
75+
else if (fLargeWorkInvalidChainFound)
76+
{
77+
strStatusBar = strRPC = "Warning: We do not appear to fully agree with our peers! You may need to upgrade, or other nodes may need to upgrade.";
78+
strGUI += (strGUI.empty() ? "" : uiAlertSeperator) + _("Warning: We do not appear to fully agree with our peers! You may need to upgrade, or other nodes may need to upgrade.");
79+
}
80+
81+
if (strFor == "gui")
82+
return strGUI;
83+
else if (strFor == "statusbar")
84+
return strStatusBar;
85+
else if (strFor == "rpc")
86+
return strRPC;
87+
assert(!"GetWarnings(): invalid parameter");
88+
return "error";
89+
}

src/warnings.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) 2009-2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2016 The Bitcoin Core developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#ifndef BITCOIN_WARNINGS_H
7+
#define BITCOIN_WARNINGS_H
8+
9+
#include <stdlib.h>
10+
#include <string>
11+
12+
void SetMiscWarning(const std::string& strWarning);
13+
void SetfLargeWorkForkFound(bool flag);
14+
bool GetfLargeWorkForkFound();
15+
void SetfLargeWorkInvalidChainFound(bool flag);
16+
bool GetfLargeWorkInvalidChainFound();
17+
std::string GetWarnings(const std::string& strFor);
18+
19+
static const bool DEFAULT_TESTSAFEMODE = false;
20+
21+
#endif // BITCOIN_WARNINGS_H

0 commit comments

Comments
 (0)