From f08e31382b63cd8841624be6280e5ae3f12786ed Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Fri, 27 Nov 2015 13:35:49 +0100 Subject: [PATCH 1/4] util: Don't set strMiscWarning on every exception Fixes bitcoin/bitcoin#6809 - run-of-the-mill exceptions should not get into strMiscWarning (which is reported by `getinfo`). --- src/util.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/util.cpp b/src/util.cpp index d7d5a1cbf..c8a1e5626 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -449,7 +449,6 @@ void PrintExceptionContinue(const std::exception* pex, const char* pszThread) std::string message = FormatException(pex, pszThread); LogPrintf("\n\n************************\n%s\n", message); fprintf(stderr, "\n\n************************\n%s\n", message.c_str()); - strMiscWarning = message; } boost::filesystem::path GetDefaultDataDir() From 05882459c06e626236c54ac01cf2c4cda4f53cf0 Mon Sep 17 00:00:00 2001 From: Gregory Maxwell Date: Tue, 29 Nov 2016 01:00:11 +0000 Subject: [PATCH 2/4] Move GetWarnings and related globals to util. This is a first step in avoiding racy accesses to strMiscWarning. By itself this commit causes a link error because global variables relating to alerts are only accessible in the zcashd server, but util is used in other binaries. Signed-off-by: Daira Hopwood --- src/alert.h | 3 +++ src/main.cpp | 67 ---------------------------------------------------- src/main.h | 4 ---- src/util.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++++ src/util.h | 7 ++++++ 5 files changed, 70 insertions(+), 71 deletions(-) diff --git a/src/alert.h b/src/alert.h index bca2f8de1..8cc1165b1 100644 --- a/src/alert.h +++ b/src/alert.h @@ -14,6 +14,9 @@ #include #include +/** Minimum alert priority for enabling safe mode. */ +static const int ALERT_PRIORITY_SAFE_MODE = 4000; + class CAlert; class CNode; class uint256; diff --git a/src/main.cpp b/src/main.cpp index 5ac46cb7c..ade1ee1e0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1795,8 +1795,6 @@ bool IsInitialBlockDownload(const CChainParams& chainParams) return false; } -static bool fLargeWorkForkFound = false; -static bool fLargeWorkInvalidChainFound = false; static CBlockIndex *pindexBestForkTip = NULL; static CBlockIndex *pindexBestForkBase = NULL; @@ -5083,71 +5081,6 @@ void static CheckBlockIndex(const Consensus::Params& consensusParams) assert(nNodes == forward.size()); } -////////////////////////////////////////////////////////////////////////////// -// -// CAlert -// - -std::string GetWarnings(const std::string& strFor) -{ - int nPriority = 0; - string strStatusBar; - string strRPC; - - 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"); - - if (GetBoolArg("-testsafemode", DEFAULT_TESTSAFEMODE)) - strStatusBar = strRPC = "testsafemode enabled"; - - // Misc warnings like out of disk space and clock is wrong - if (strMiscWarning != "") - { - nPriority = 1000; - strStatusBar = strMiscWarning; - } - - if (fLargeWorkForkFound) - { - nPriority = 2000; - strStatusBar = strRPC = _("Warning: The network does not appear to fully agree! Some miners appear to be experiencing issues."); - } - else if (fLargeWorkInvalidChainFound) - { - nPriority = 2000; - 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."); - } - - // Alerts - { - LOCK(cs_mapAlerts); - BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts) - { - const CAlert& alert = item.second; - if (alert.AppliesToMe() && alert.nPriority > nPriority) - { - nPriority = alert.nPriority; - strStatusBar = alert.strStatusBar; - if (alert.nPriority >= ALERT_PRIORITY_SAFE_MODE) { - strRPC = alert.strRPCError; - } - } - } - } - - if (strFor == "statusbar") - return strStatusBar; - else if (strFor == "rpc") - return strRPC; - assert(!"GetWarnings(): invalid parameter"); - return "error"; -} - - - - - - ////////////////////////////////////////////////////////////////////////////// diff --git a/src/main.h b/src/main.h index 8427fdf07..6bdb06d43 100644 --- a/src/main.h +++ b/src/main.h @@ -55,8 +55,6 @@ struct CNodeStateStats; /** Default for accepting alerts from the P2P network. */ static const bool DEFAULT_ALERTS = true; -/** Minimum alert priority for enabling safe mode. */ -static const int ALERT_PRIORITY_SAFE_MODE = 4000; /** Maximum reorg length we will accept before we shut down and alert the user. */ static const unsigned int MAX_REORG_LENGTH = COINBASE_MATURITY - 1; /** Default for -minrelaytxfee, minimum relay fee for transactions */ @@ -113,8 +111,6 @@ static const bool DEFAULT_CHECKPOINTS_ENABLED = true; static const bool DEFAULT_TXINDEX = false; static const unsigned int DEFAULT_BANSCORE_THRESHOLD = 100; -static const bool DEFAULT_TESTSAFEMODE = false; - #define equihash_parameters_acceptable(N, K) \ ((CBlockHeader::HEADER_SIZE + equihash_solution_size(N, K))*MAX_HEADERS_RESULTS < \ MAX_PROTOCOL_MESSAGE_LENGTH-1000) diff --git a/src/util.cpp b/src/util.cpp index c8a1e5626..2dc4511cc 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1,5 +1,6 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2014 The Bitcoin Core developers +// Copyright (c) 2015-2020 The Zcash developers // Distributed under the MIT software license, see the accompanying // file COPYING or https://www.opensource.org/licenses/mit-license.php . @@ -15,6 +16,7 @@ #include "sync.h" #include "utilstrencodings.h" #include "utiltime.h" +#include "alert.h" #include #include @@ -109,7 +111,11 @@ bool fPrintToConsole = false; bool fPrintToDebugLog = true; bool fDaemon = false; bool fServer = false; + string strMiscWarning; +bool fLargeWorkForkFound = false; +bool fLargeWorkInvalidChainFound = false; + bool fLogTimestamps = DEFAULT_LOGTIMESTAMPS; bool fLogTimeMicros = DEFAULT_LOGTIMEMICROS; bool fLogIPs = DEFAULT_LOGIPS; @@ -923,3 +929,57 @@ int GetNumCores() return boost::thread::physical_concurrency(); } +std::string GetWarnings(const std::string& strFor) +{ + int nPriority = 0; + string strStatusBar; + string strRPC; + + 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"); + + if (GetBoolArg("-testsafemode", DEFAULT_TESTSAFEMODE)) + strStatusBar = strRPC = "testsafemode enabled"; + + // Misc warnings like out of disk space and clock is wrong + if (strMiscWarning != "") + { + nPriority = 1000; + strStatusBar = strMiscWarning; + } + + if (fLargeWorkForkFound) + { + nPriority = 2000; + strStatusBar = strRPC = _("Warning: The network does not appear to fully agree! Some miners appear to be experiencing issues."); + } + else if (fLargeWorkInvalidChainFound) + { + nPriority = 2000; + 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."); + } + + // Alerts + { + LOCK(cs_mapAlerts); + BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts) + { + const CAlert& alert = item.second; + if (alert.AppliesToMe() && alert.nPriority > nPriority) + { + nPriority = alert.nPriority; + strStatusBar = alert.strStatusBar; + if (alert.nPriority >= ALERT_PRIORITY_SAFE_MODE) { + strRPC = alert.strRPCError; + } + } + } + } + + if (strFor == "statusbar") + return strStatusBar; + else if (strFor == "rpc") + return strRPC; + assert(!"GetWarnings(): invalid parameter"); + return "error"; +} diff --git a/src/util.h b/src/util.h index 9485581cf..3aa1376a3 100644 --- a/src/util.h +++ b/src/util.h @@ -47,7 +47,12 @@ extern bool fDebug; extern bool fPrintToConsole; extern bool fPrintToDebugLog; extern bool fServer; + +static const bool DEFAULT_TESTSAFEMODE = false; extern std::string strMiscWarning; +extern bool fLargeWorkForkFound; +extern bool fLargeWorkInvalidChainFound; + extern bool fLogTimestamps; extern bool fLogIPs; extern std::atomic fReopenDebugLog; @@ -262,4 +267,6 @@ template void TraceThread(const char* name, Callable func) } } +std::string GetWarnings(const std::string& strFor); + #endif // BITCOIN_UTIL_H From d6a48adb760608183284dbfa4725caa97737b5eb Mon Sep 17 00:00:00 2001 From: Gregory Maxwell Date: Tue, 29 Nov 2016 09:46:19 +0000 Subject: [PATCH 3/4] Eliminate data races for strMiscWarning and fLargeWork*Found. This moves all access to these datastructures through accessor functions and protects them with a lock. Relative to the upstream commit, we also add GetMiscWarning() to make this accessible to tests. Signed-off-by: Daira Hopwood --- src/main.cpp | 14 +++++++------- src/test/alert_tests.cpp | 22 +++++++++++----------- src/timedata.cpp | 4 ++-- src/util.cpp | 39 +++++++++++++++++++++++++++++++++++++++ src/util.h | 9 ++++++--- 5 files changed, 65 insertions(+), 23 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index ade1ee1e0..1449d0e02 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -116,7 +116,7 @@ namespace { /** Abort with a message */ bool AbortNode(const std::string& strMessage, const std::string& userMessage="") { - strMiscWarning = strMessage; + SetMiscWarning(strMessage); LogPrintf("*** %s\n", strMessage); uiInterface.ThreadSafeMessageBox( userMessage.empty() ? _("Error: A fatal internal error occurred, see debug.log for details") : userMessage, @@ -1813,7 +1813,7 @@ void CheckForkWarningConditions(const CChainParams& chainParams) 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 ") + pindexBestForkBase->phashBlock->ToString() + std::string("'"); @@ -1824,20 +1824,20 @@ void CheckForkWarningConditions(const CChainParams& chainParams) 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(), pindexBestForkTip->nHeight, pindexBestForkTip->phashBlock->ToString()); - fLargeWorkForkFound = true; + SetfLargeWorkForkFound(true); } else { std::string warning = std::string("Warning: Found invalid chain at least ~6 blocks longer than our best chain.\nChain state database corruption likely."); LogPrintf("%s: %s\n", warning.c_str(), __func__); CAlert::Notify(warning, true); - fLargeWorkInvalidChainFound = true; + SetfLargeWorkInvalidChainFound(true); } } else { - fLargeWorkForkFound = false; - fLargeWorkInvalidChainFound = false; + SetfLargeWorkForkFound(false); + SetfLargeWorkInvalidChainFound(false); } } @@ -2482,7 +2482,7 @@ void PartitionCheck(bool (*initialDownloadCheck)(const CChainParams&), } if (!strWarning.empty()) { - strMiscWarning = strWarning; + SetMiscWarning(strWarning); CAlert::Notify(strWarning, true); lastAlertTime = now; } diff --git a/src/test/alert_tests.cpp b/src/test/alert_tests.cpp index 1362917a9..f23d28478 100644 --- a/src/test/alert_tests.cpp +++ b/src/test/alert_tests.cpp @@ -435,26 +435,26 @@ void PartitionAlertTestImpl(const Consensus::Params& params, int startTime, int // Test 1: chain with blocks every nPowTargetSpacing seconds, // as normal, no worries: - strMiscWarning = ""; + SetMiscWarning(""); PartitionCheck(falseFunc, csDummy, &indexDummy[799]); - BOOST_CHECK_EQUAL("", strMiscWarning); + BOOST_CHECK_EQUAL("", GetMiscWarning()); // Test 2: go 3.5 hours without a block, expect a warning: now += 3*60*60+30*60; SetMockTime(now); - strMiscWarning = ""; + SetMiscWarning(""); PartitionCheck(falseFunc, csDummy, &indexDummy[799]); std::string expectedSlowErr = strprintf("WARNING: check your network connection, %d blocks received in the last 4 hours (%d expected)", expectedSlow, expectedTotal); - BOOST_CHECK_EQUAL(expectedSlowErr, strMiscWarning); - BOOST_TEST_MESSAGE(std::string("Got alert text: ")+strMiscWarning); + BOOST_CHECK_EQUAL(expectedSlowErr, GetMiscWarning()); + BOOST_TEST_MESSAGE(std::string("Got alert text: ")+GetMiscWarning()); // Test 3: test the "partition alerts only go off once per day" // code: now += 60*10; SetMockTime(now); - strMiscWarning = ""; + SetMiscWarning(""); PartitionCheck(falseFunc, csDummy, &indexDummy[799]); - BOOST_CHECK_EQUAL("", strMiscWarning); + BOOST_CHECK_EQUAL("", GetMiscWarning()); // Test 4: get 2.5 times as many blocks as expected: start = now + 60*60*24; // Pretend it is a day later @@ -465,12 +465,12 @@ void PartitionAlertTestImpl(const Consensus::Params& params, int startTime, int now = indexDummy[799].nTime + params.PoWTargetSpacing(0) * 2/5; SetMockTime(now); - strMiscWarning = ""; + SetMiscWarning(""); PartitionCheck(falseFunc, csDummy, &indexDummy[799]); std::string expectedFastErr = strprintf("WARNING: abnormally high number of blocks generated, %d blocks received in the last 4 hours (%d expected)", expectedFast, expectedTotal); - BOOST_CHECK_EQUAL(expectedFastErr, strMiscWarning); - BOOST_TEST_MESSAGE(std::string("Got alert text: ")+strMiscWarning); - strMiscWarning = ""; + BOOST_CHECK_EQUAL(expectedFastErr, GetMiscWarning()); + BOOST_TEST_MESSAGE(std::string("Got alert text: ")+GetMiscWarning()); + SetMiscWarning(""); SetMockTime(0); } diff --git a/src/timedata.cpp b/src/timedata.cpp index 26665730b..204728948 100644 --- a/src/timedata.cpp +++ b/src/timedata.cpp @@ -99,8 +99,8 @@ void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample) if (!fMatch) { fDone = true; - string strMessage = _("Warning: Please check that your computer's date and time are correct! If your clock is wrong Zcash will not work properly."); - strMiscWarning = strMessage; + std::string strMessage = _("Warning: Please check that your computer's date and time are correct! If your clock is wrong Zcash will not work properly."); + SetMiscWarning(strMessage); LogPrintf("*** %s\n", strMessage); uiInterface.ThreadSafeMessageBox(strMessage, "", CClientUIInterface::MSG_WARNING); } diff --git a/src/util.cpp b/src/util.cpp index 2dc4511cc..c6357c9be 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -112,6 +112,7 @@ bool fPrintToDebugLog = true; bool fDaemon = false; bool fServer = false; +CCriticalSection cs_warnings; string strMiscWarning; bool fLargeWorkForkFound = false; bool fLargeWorkInvalidChainFound = false; @@ -929,12 +930,50 @@ int GetNumCores() return boost::thread::physical_concurrency(); } +void SetMiscWarning(const std::string& strWarning) +{ + LOCK(cs_warnings); + strMiscWarning = strWarning; +} + +std::string GetMiscWarning() +{ + LOCK(cs_warnings); + return strMiscWarning; +} + +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) { int nPriority = 0; string strStatusBar; string strRPC; + 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"); diff --git a/src/util.h b/src/util.h index 3aa1376a3..df7c6755b 100644 --- a/src/util.h +++ b/src/util.h @@ -49,9 +49,6 @@ extern bool fPrintToDebugLog; extern bool fServer; static const bool DEFAULT_TESTSAFEMODE = false; -extern std::string strMiscWarning; -extern bool fLargeWorkForkFound; -extern bool fLargeWorkInvalidChainFound; extern bool fLogTimestamps; extern bool fLogIPs; @@ -267,6 +264,12 @@ template void TraceThread(const char* name, Callable func) } } +void SetMiscWarning(const std::string& strWarning); +std::string GetMiscWarning(); +void SetfLargeWorkForkFound(bool flag); +bool GetfLargeWorkForkFound(); +void SetfLargeWorkInvalidChainFound(bool flag); +bool GetfLargeWorkInvalidChainFound(); std::string GetWarnings(const std::string& strFor); #endif // BITCOIN_UTIL_H From 9f4ad7e1c6063724af064bb6d4284e40549aec0b Mon Sep 17 00:00:00 2001 From: Gregory Maxwell Date: Wed, 30 Nov 2016 06:07:42 +0000 Subject: [PATCH 4/4] Move GetWarnings() into its own file. Signed-off-by: Daira Hopwood --- src/Makefile.am | 2 + src/init.cpp | 1 + src/main.cpp | 1 + src/test/alert_tests.cpp | 1 + src/timedata.cpp | 1 + src/util.cpp | 99 ----------------------------------- src/util.h | 10 ---- src/warnings.cpp | 109 +++++++++++++++++++++++++++++++++++++++ src/warnings.h | 22 ++++++++ 9 files changed, 137 insertions(+), 109 deletions(-) create mode 100644 src/warnings.cpp create mode 100644 src/warnings.h diff --git a/src/Makefile.am b/src/Makefile.am index c1c1961f7..7bc8cccc7 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -220,6 +220,7 @@ BITCOIN_CORE_H = \ wallet/rpcwallet.h \ wallet/wallet.h \ wallet/walletdb.h \ + warnings.h \ zmq/zmqabstractnotifier.h \ zmq/zmqconfig.h\ zmq/zmqnotificationinterface.h \ @@ -383,6 +384,7 @@ libbitcoin_common_a_SOURCES = \ script/standard.cpp \ transaction_builder.cpp \ utiltest.cpp \ + warnings.cpp \ $(BITCOIN_CORE_H) \ $(LIBZCASH_H) diff --git a/src/init.cpp b/src/init.cpp index 19c4d8f45..39065a88d 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -44,6 +44,7 @@ #include "wallet/wallet.h" #include "wallet/walletdb.h" #endif +#include "warnings.h" #include #include diff --git a/src/main.cpp b/src/main.cpp index 1449d0e02..dfb1ba736 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,6 +31,7 @@ #include "validationinterface.h" #include "wallet/asyncrpcoperation_sendmany.h" #include "wallet/asyncrpcoperation_shieldcoinbase.h" +#include "warnings.h" #include #include diff --git a/src/test/alert_tests.cpp b/src/test/alert_tests.cpp index f23d28478..c4ba04c37 100644 --- a/src/test/alert_tests.cpp +++ b/src/test/alert_tests.cpp @@ -20,6 +20,7 @@ #include "util.h" #include "utilstrencodings.h" #include "utiltest.h" +#include "warnings.h" #include "test/test_bitcoin.h" diff --git a/src/timedata.cpp b/src/timedata.cpp index 204728948..0cb967726 100644 --- a/src/timedata.cpp +++ b/src/timedata.cpp @@ -9,6 +9,7 @@ #include "ui_interface.h" #include "util.h" #include "utilstrencodings.h" +#include "warnings.h" #include diff --git a/src/util.cpp b/src/util.cpp index c6357c9be..54a17123b 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -16,7 +16,6 @@ #include "sync.h" #include "utilstrencodings.h" #include "utiltime.h" -#include "alert.h" #include #include @@ -112,11 +111,6 @@ bool fPrintToDebugLog = true; bool fDaemon = false; bool fServer = false; -CCriticalSection cs_warnings; -string strMiscWarning; -bool fLargeWorkForkFound = false; -bool fLargeWorkInvalidChainFound = false; - bool fLogTimestamps = DEFAULT_LOGTIMESTAMPS; bool fLogTimeMicros = DEFAULT_LOGTIMEMICROS; bool fLogIPs = DEFAULT_LOGIPS; @@ -929,96 +923,3 @@ int GetNumCores() { return boost::thread::physical_concurrency(); } - -void SetMiscWarning(const std::string& strWarning) -{ - LOCK(cs_warnings); - strMiscWarning = strWarning; -} - -std::string GetMiscWarning() -{ - LOCK(cs_warnings); - return strMiscWarning; -} - -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) -{ - int nPriority = 0; - string strStatusBar; - string strRPC; - - 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"); - - if (GetBoolArg("-testsafemode", DEFAULT_TESTSAFEMODE)) - strStatusBar = strRPC = "testsafemode enabled"; - - // Misc warnings like out of disk space and clock is wrong - if (strMiscWarning != "") - { - nPriority = 1000; - strStatusBar = strMiscWarning; - } - - if (fLargeWorkForkFound) - { - nPriority = 2000; - strStatusBar = strRPC = _("Warning: The network does not appear to fully agree! Some miners appear to be experiencing issues."); - } - else if (fLargeWorkInvalidChainFound) - { - nPriority = 2000; - 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."); - } - - // Alerts - { - LOCK(cs_mapAlerts); - BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts) - { - const CAlert& alert = item.second; - if (alert.AppliesToMe() && alert.nPriority > nPriority) - { - nPriority = alert.nPriority; - strStatusBar = alert.strStatusBar; - if (alert.nPriority >= ALERT_PRIORITY_SAFE_MODE) { - strRPC = alert.strRPCError; - } - } - } - } - - if (strFor == "statusbar") - return strStatusBar; - else if (strFor == "rpc") - return strRPC; - assert(!"GetWarnings(): invalid parameter"); - return "error"; -} diff --git a/src/util.h b/src/util.h index df7c6755b..614a6bc01 100644 --- a/src/util.h +++ b/src/util.h @@ -48,8 +48,6 @@ extern bool fPrintToConsole; extern bool fPrintToDebugLog; extern bool fServer; -static const bool DEFAULT_TESTSAFEMODE = false; - extern bool fLogTimestamps; extern bool fLogIPs; extern std::atomic fReopenDebugLog; @@ -264,12 +262,4 @@ template void TraceThread(const char* name, Callable func) } } -void SetMiscWarning(const std::string& strWarning); -std::string GetMiscWarning(); -void SetfLargeWorkForkFound(bool flag); -bool GetfLargeWorkForkFound(); -void SetfLargeWorkInvalidChainFound(bool flag); -bool GetfLargeWorkInvalidChainFound(); -std::string GetWarnings(const std::string& strFor); - #endif // BITCOIN_UTIL_H diff --git a/src/warnings.cpp b/src/warnings.cpp new file mode 100644 index 000000000..80d7f2755 --- /dev/null +++ b/src/warnings.cpp @@ -0,0 +1,109 @@ +// 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" +#include "alert.h" +#include "uint256.h" + +CCriticalSection cs_warnings; +std::string strMiscWarning; +bool fLargeWorkForkFound = false; +bool fLargeWorkInvalidChainFound = false; + +void SetMiscWarning(const std::string& strWarning) +{ + LOCK(cs_warnings); + strMiscWarning = strWarning; +} + +std::string GetMiscWarning() +{ + LOCK(cs_warnings); + return strMiscWarning; +} + +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) +{ + int nPriority = 0; + std::string strStatusBar; + std::string strRPC; + + 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"); + + if (GetBoolArg("-testsafemode", DEFAULT_TESTSAFEMODE)) + strStatusBar = strRPC = "testsafemode enabled"; + + // Misc warnings like out of disk space and clock is wrong + if (strMiscWarning != "") + { + nPriority = 1000; + strStatusBar = strMiscWarning; + } + + if (fLargeWorkForkFound) + { + nPriority = 2000; + strStatusBar = strRPC = _("Warning: The network does not appear to fully agree! Some miners appear to be experiencing issues."); + } + else if (fLargeWorkInvalidChainFound) + { + nPriority = 2000; + 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."); + } + + // Alerts + { + LOCK(cs_mapAlerts); + for (const std::pair& item : mapAlerts) + { + const CAlert& alert = item.second; + if (alert.AppliesToMe() && alert.nPriority > nPriority) + { + nPriority = alert.nPriority; + strStatusBar = alert.strStatusBar; + if (alert.nPriority >= ALERT_PRIORITY_SAFE_MODE) { + strRPC = alert.strRPCError; + } + } + } + } + + if (strFor == "statusbar") + return strStatusBar; + else if (strFor == "rpc") + return strRPC; + assert(!"GetWarnings(): invalid parameter"); + return "error"; +} diff --git a/src/warnings.h b/src/warnings.h new file mode 100644 index 000000000..220b49504 --- /dev/null +++ b/src/warnings.h @@ -0,0 +1,22 @@ +// 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 +#include + +void SetMiscWarning(const std::string& strWarning); +std::string GetMiscWarning(); +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