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)
This commit is contained in:
Wladimir J. van der Laan 2016-12-19 12:38:35 +01:00
commit 7f72568e6b
No known key found for this signature in database
GPG key ID: 74810B012346C9A6
10 changed files with 138 additions and 64 deletions

View file

@ -155,6 +155,7 @@ BITCOIN_CORE_H = \
wallet/rpcwallet.h \ wallet/rpcwallet.h \
wallet/wallet.h \ wallet/wallet.h \
wallet/walletdb.h \ wallet/walletdb.h \
warnings.h \
zmq/zmqabstractnotifier.h \ zmq/zmqabstractnotifier.h \
zmq/zmqconfig.h\ zmq/zmqconfig.h\
zmq/zmqnotificationinterface.h \ zmq/zmqnotificationinterface.h \
@ -306,6 +307,7 @@ libbitcoin_common_a_SOURCES = \
scheduler.cpp \ scheduler.cpp \
script/sign.cpp \ script/sign.cpp \
script/standard.cpp \ script/standard.cpp \
warnings.cpp \
$(BITCOIN_CORE_H) $(BITCOIN_CORE_H)
# util: shared between all executables. # util: shared between all executables.

View file

@ -41,6 +41,7 @@
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
#include "wallet/wallet.h" #include "wallet/wallet.h"
#endif #endif
#include "warnings.h"
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <memory> #include <memory>

View file

@ -30,6 +30,7 @@
#include "scheduler.h" #include "scheduler.h"
#include "ui_interface.h" #include "ui_interface.h"
#include "util.h" #include "util.h"
#include "warnings.h"
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
#include "wallet/wallet.h" #include "wallet/wallet.h"
@ -260,7 +261,7 @@ BitcoinCore::BitcoinCore():
void BitcoinCore::handleRunawayException(const std::exception *e) void BitcoinCore::handleRunawayException(const std::exception *e)
{ {
PrintExceptionContinue(e, "Runaway exception"); PrintExceptionContinue(e, "Runaway exception");
Q_EMIT runawayException(QString::fromStdString(strMiscWarning)); Q_EMIT runawayException(QString::fromStdString(GetWarnings("gui")));
} }
void BitcoinCore::initialize() void BitcoinCore::initialize()
@ -691,10 +692,10 @@ int main(int argc, char *argv[])
app.exec(); app.exec();
} catch (const std::exception& e) { } catch (const std::exception& e) {
PrintExceptionContinue(&e, "Runaway exception"); PrintExceptionContinue(&e, "Runaway exception");
app.handleRunawayException(QString::fromStdString(strMiscWarning)); app.handleRunawayException(QString::fromStdString(GetWarnings("gui")));
} catch (...) { } catch (...) {
PrintExceptionContinue(NULL, "Runaway exception"); PrintExceptionContinue(NULL, "Runaway exception");
app.handleRunawayException(QString::fromStdString(strMiscWarning)); app.handleRunawayException(QString::fromStdString(GetWarnings("gui")));
} }
return app.getReturnValue(); return app.getReturnValue();
} }

View file

@ -13,6 +13,7 @@
#include "ui_interface.h" #include "ui_interface.h"
#include "util.h" #include "util.h"
#include "utilstrencodings.h" #include "utilstrencodings.h"
#include "warnings.h"
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
@ -103,8 +104,8 @@ void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
if (!fMatch) if (!fMatch)
{ {
fDone = true; fDone = true;
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)); 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));
strMiscWarning = strMessage; SetMiscWarning(strMessage);
uiInterface.ThreadSafeMessageBox(strMessage, "", CClientUIInterface::MSG_WARNING); uiInterface.ThreadSafeMessageBox(strMessage, "", CClientUIInterface::MSG_WARNING);
} }
} }

View file

@ -107,7 +107,7 @@ map<string, vector<string> > mapMultiArgs;
bool fDebug = false; bool fDebug = false;
bool fPrintToConsole = false; bool fPrintToConsole = false;
bool fPrintToDebugLog = true; bool fPrintToDebugLog = true;
string strMiscWarning;
bool fLogTimestamps = DEFAULT_LOGTIMESTAMPS; bool fLogTimestamps = DEFAULT_LOGTIMESTAMPS;
bool fLogTimeMicros = DEFAULT_LOGTIMEMICROS; bool fLogTimeMicros = DEFAULT_LOGTIMEMICROS;
bool fLogIPs = DEFAULT_LOGIPS; bool fLogIPs = DEFAULT_LOGIPS;

View file

@ -46,7 +46,7 @@ extern std::map<std::string, std::vector<std::string> > mapMultiArgs;
extern bool fDebug; extern bool fDebug;
extern bool fPrintToConsole; extern bool fPrintToConsole;
extern bool fPrintToDebugLog; extern bool fPrintToDebugLog;
extern std::string strMiscWarning;
extern bool fLogTimestamps; extern bool fLogTimestamps;
extern bool fLogTimeMicros; extern bool fLogTimeMicros;
extern bool fLogIPs; extern bool fLogIPs;

View file

@ -34,6 +34,7 @@
#include "utilstrencodings.h" #include "utilstrencodings.h"
#include "validationinterface.h" #include "validationinterface.h"
#include "versionbits.h" #include "versionbits.h"
#include "warnings.h"
#include <atomic> #include <atomic>
#include <sstream> #include <sstream>
@ -1141,8 +1142,6 @@ bool IsInitialBlockDownload()
return false; return false;
} }
bool fLargeWorkForkFound = false;
bool fLargeWorkInvalidChainFound = false;
CBlockIndex *pindexBestForkTip = NULL, *pindexBestForkBase = NULL; CBlockIndex *pindexBestForkTip = NULL, *pindexBestForkBase = NULL;
static void AlertNotify(const std::string& strMessage) static void AlertNotify(const std::string& strMessage)
@ -1177,7 +1176,7 @@ void CheckForkWarningConditions()
if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > chainActive.Tip()->nChainWork + (GetBlockProof(*chainActive.Tip()) * 6))) if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > chainActive.Tip()->nChainWork + (GetBlockProof(*chainActive.Tip()) * 6)))
{ {
if (!fLargeWorkForkFound && pindexBestForkBase) if (!GetfLargeWorkForkFound() && pindexBestForkBase)
{ {
std::string warning = std::string("'Warning: Large-work fork detected, forking after block ") + std::string warning = std::string("'Warning: Large-work fork detected, forking after block ") +
pindexBestForkBase->phashBlock->ToString() + std::string("'"); pindexBestForkBase->phashBlock->ToString() + std::string("'");
@ -1188,18 +1187,18 @@ void CheckForkWarningConditions()
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__, 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__,
pindexBestForkBase->nHeight, pindexBestForkBase->phashBlock->ToString(), pindexBestForkBase->nHeight, pindexBestForkBase->phashBlock->ToString(),
pindexBestForkTip->nHeight, pindexBestForkTip->phashBlock->ToString()); pindexBestForkTip->nHeight, pindexBestForkTip->phashBlock->ToString());
fLargeWorkForkFound = true; SetfLargeWorkForkFound(true);
} }
else else
{ {
LogPrintf("%s: Warning: Found invalid chain at least ~6 blocks longer than our best chain.\nChain state database corruption likely.\n", __func__); LogPrintf("%s: Warning: Found invalid chain at least ~6 blocks longer than our best chain.\nChain state database corruption likely.\n", __func__);
fLargeWorkInvalidChainFound = true; SetfLargeWorkInvalidChainFound(true);
} }
} }
else else
{ {
fLargeWorkForkFound = false; SetfLargeWorkForkFound(false);
fLargeWorkInvalidChainFound = false; SetfLargeWorkInvalidChainFound(false);
} }
} }
@ -1475,7 +1474,7 @@ bool UndoReadFromDisk(CBlockUndo& blockundo, const CDiskBlockPos& pos, const uin
/** Abort with a message */ /** Abort with a message */
bool AbortNode(const std::string& strMessage, const std::string& userMessage="") bool AbortNode(const std::string& strMessage, const std::string& userMessage="")
{ {
strMiscWarning = strMessage; SetMiscWarning(strMessage);
LogPrintf("*** %s\n", strMessage); LogPrintf("*** %s\n", strMessage);
uiInterface.ThreadSafeMessageBox( uiInterface.ThreadSafeMessageBox(
userMessage.empty() ? _("Error: A fatal internal error occurred, see debug.log for details") : userMessage, 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) {
ThresholdState state = checker.GetStateFor(pindex, chainParams.GetConsensus(), warningcache[bit]); ThresholdState state = checker.GetStateFor(pindex, chainParams.GetConsensus(), warningcache[bit]);
if (state == THRESHOLD_ACTIVE || state == THRESHOLD_LOCKED_IN) { if (state == THRESHOLD_ACTIVE || state == THRESHOLD_LOCKED_IN) {
if (state == THRESHOLD_ACTIVE) { if (state == THRESHOLD_ACTIVE) {
strMiscWarning = strprintf(_("Warning: unknown new rules activated (versionbit %i)"), bit); std::string strWarning = strprintf(_("Warning: unknown new rules activated (versionbit %i)"), bit);
SetMiscWarning(strWarning);
if (!fWarned) { if (!fWarned) {
AlertNotify(strMiscWarning); AlertNotify(strWarning);
fWarned = true; fWarned = true;
} }
} else { } else {
@ -2066,10 +2066,11 @@ void static UpdateTip(CBlockIndex *pindexNew, const CChainParams& chainParams) {
warningMessages.push_back(strprintf("%d of last 100 blocks have unexpected version", nUpgraded)); warningMessages.push_back(strprintf("%d of last 100 blocks have unexpected version", nUpgraded));
if (nUpgraded > 100/2) if (nUpgraded > 100/2)
{ {
// strMiscWarning is read by GetWarnings(), called by Qt and the JSON-RPC code to warn the user: std::string strWarning = _("Warning: Unknown block versions being mined! It's possible unknown rules are in effect");
strMiscWarning = _("Warning: Unknown block versions being mined! It's possible unknown rules are in effect"); // notify GetWarnings(), called by Qt and the JSON-RPC code to warn the user:
SetMiscWarning(strWarning);
if (!fWarned) { if (!fWarned) {
AlertNotify(strMiscWarning); AlertNotify(strWarning);
fWarned = true; fWarned = true;
} }
} }
@ -4010,51 +4011,10 @@ void static CheckBlockIndex(const Consensus::Params& consensusParams)
assert(nNodes == forward.size()); assert(nNodes == forward.size());
} }
std::string GetWarnings(const std::string& strFor) std::string CBlockFileInfo::ToString() const
{ {
string strStatusBar; 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));
string strRPC;
string strGUI;
const string uiAlertSeperator = "<hr />";
if (!CLIENT_VERSION_IS_RELEASE) {
strStatusBar = "This is a pre-release test build - use at your own risk - do not use for mining or merchant applications";
strGUI = _("This is a pre-release test build - use at your own risk - do not use for mining or merchant applications");
}
if (GetBoolArg("-testsafemode", DEFAULT_TESTSAFEMODE))
strStatusBar = strRPC = strGUI = "testsafemode enabled";
// Misc warnings like out of disk space and clock is wrong
if (strMiscWarning != "")
{
strStatusBar = strMiscWarning;
strGUI += (strGUI.empty() ? "" : uiAlertSeperator) + strMiscWarning;
}
if (fLargeWorkForkFound)
{
strStatusBar = strRPC = "Warning: The network does not appear to fully agree! Some miners appear to be experiencing issues.";
strGUI += (strGUI.empty() ? "" : uiAlertSeperator) + _("Warning: The network does not appear to fully agree! Some miners appear to be experiencing issues.");
}
else if (fLargeWorkInvalidChainFound)
{
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.";
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.");
}
if (strFor == "gui")
return strGUI;
else if (strFor == "statusbar")
return strStatusBar;
else if (strFor == "rpc")
return strRPC;
assert(!"GetWarnings(): invalid parameter");
return "error";
} }
std::string CBlockFileInfo::ToString() const {
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));
}
ThresholdState VersionBitsTipState(const Consensus::Params& params, Consensus::DeploymentPos pos) ThresholdState VersionBitsTipState(const Consensus::Params& params, Consensus::DeploymentPos pos)
{ {

View file

@ -137,7 +137,6 @@ static const bool DEFAULT_CHECKPOINTS_ENABLED = true;
static const bool DEFAULT_TXINDEX = false; static const bool DEFAULT_TXINDEX = false;
static const unsigned int DEFAULT_BANSCORE_THRESHOLD = 100; static const unsigned int DEFAULT_BANSCORE_THRESHOLD = 100;
static const bool DEFAULT_TESTSAFEMODE = false;
/** Default for -mempoolreplacement */ /** Default for -mempoolreplacement */
static const bool DEFAULT_ENABLE_REPLACEMENT = true; static const bool DEFAULT_ENABLE_REPLACEMENT = true;
/** Default for using fee filter */ /** Default for using fee filter */

89
src/warnings.cpp Normal file
View file

@ -0,0 +1,89 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2016 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "sync.h"
#include "clientversion.h"
#include "util.h"
#include "warnings.h"
CCriticalSection cs_warnings;
std::string strMiscWarning;
bool fLargeWorkForkFound = false;
bool fLargeWorkInvalidChainFound = false;
void SetMiscWarning(const std::string& strWarning)
{
LOCK(cs_warnings);
strMiscWarning = strWarning;
}
void SetfLargeWorkForkFound(bool flag)
{
LOCK(cs_warnings);
fLargeWorkForkFound = flag;
}
bool GetfLargeWorkForkFound()
{
LOCK(cs_warnings);
return fLargeWorkForkFound;
}
void SetfLargeWorkInvalidChainFound(bool flag)
{
LOCK(cs_warnings);
fLargeWorkInvalidChainFound = flag;
}
bool GetfLargeWorkInvalidChainFound()
{
LOCK(cs_warnings);
return fLargeWorkInvalidChainFound;
}
std::string GetWarnings(const std::string& strFor)
{
std::string strStatusBar;
std::string strRPC;
std::string strGUI;
const std::string uiAlertSeperator = "<hr />";
LOCK(cs_warnings);
if (!CLIENT_VERSION_IS_RELEASE) {
strStatusBar = "This is a pre-release test build - use at your own risk - do not use for mining or merchant applications";
strGUI = _("This is a pre-release test build - use at your own risk - do not use for mining or merchant applications");
}
if (GetBoolArg("-testsafemode", DEFAULT_TESTSAFEMODE))
strStatusBar = strRPC = strGUI = "testsafemode enabled";
// Misc warnings like out of disk space and clock is wrong
if (strMiscWarning != "")
{
strStatusBar = strMiscWarning;
strGUI += (strGUI.empty() ? "" : uiAlertSeperator) + strMiscWarning;
}
if (fLargeWorkForkFound)
{
strStatusBar = strRPC = "Warning: The network does not appear to fully agree! Some miners appear to be experiencing issues.";
strGUI += (strGUI.empty() ? "" : uiAlertSeperator) + _("Warning: The network does not appear to fully agree! Some miners appear to be experiencing issues.");
}
else if (fLargeWorkInvalidChainFound)
{
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.";
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.");
}
if (strFor == "gui")
return strGUI;
else if (strFor == "statusbar")
return strStatusBar;
else if (strFor == "rpc")
return strRPC;
assert(!"GetWarnings(): invalid parameter");
return "error";
}

21
src/warnings.h Normal file
View file

@ -0,0 +1,21 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2016 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_WARNINGS_H
#define BITCOIN_WARNINGS_H
#include <stdlib.h>
#include <string>
void SetMiscWarning(const std::string& strWarning);
void SetfLargeWorkForkFound(bool flag);
bool GetfLargeWorkForkFound();
void SetfLargeWorkInvalidChainFound(bool flag);
bool GetfLargeWorkInvalidChainFound();
std::string GetWarnings(const std::string& strFor);
static const bool DEFAULT_TESTSAFEMODE = false;
#endif // BITCOIN_WARNINGS_H