From 7b4d0c77620888faf9f2d656b866a9fcc0ebe250 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Tue, 23 Jan 2018 00:29:08 +0000 Subject: [PATCH 1/2] Notify users about auto-senescence via -alertnotify Closes #2829. --- src/deprecation.cpp | 7 +++-- src/deprecation.h | 7 +++-- src/gtest/test_deprecation.cpp | 47 ++++++++++++++++++++++++++++++++-- 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/src/deprecation.cpp b/src/deprecation.cpp index 9f394e720..b2e980bca 100644 --- a/src/deprecation.cpp +++ b/src/deprecation.cpp @@ -4,6 +4,7 @@ #include "deprecation.h" +#include "alert.h" #include "clientversion.h" #include "init.h" #include "ui_interface.h" @@ -12,7 +13,7 @@ static const std::string CLIENT_VERSION_STR = FormatVersion(CLIENT_VERSION); -void EnforceNodeDeprecation(int nHeight, bool forceLogging) { +void EnforceNodeDeprecation(int nHeight, bool forceLogging, bool fThread) { // Do not enforce deprecation in regtest or on testnet std::string networkID = Params().NetworkIDString(); @@ -36,6 +37,7 @@ void EnforceNodeDeprecation(int nHeight, bool forceLogging) { "-disabledeprecation=", CLIENT_VERSION_STR); } LogPrintf("*** %s\n", msg); + CAlert::Notify(msg, fThread); uiInterface.ThreadSafeMessageBox(msg, "", CClientUIInterface::MSG_ERROR); } if (!disableDeprecation) { @@ -56,6 +58,7 @@ void EnforceNodeDeprecation(int nHeight, bool forceLogging) { "-disabledeprecation=", CLIENT_VERSION_STR); } LogPrintf("*** %s\n", msg); + CAlert::Notify(msg, fThread); uiInterface.ThreadSafeMessageBox(msg, "", CClientUIInterface::MSG_WARNING); } -} \ No newline at end of file +} diff --git a/src/deprecation.h b/src/deprecation.h index 3e361cab7..bd68b42ce 100644 --- a/src/deprecation.h +++ b/src/deprecation.h @@ -18,8 +18,11 @@ static const int DEPRECATION_WARN_LIMIT = 14 * 24 * 24; // 2 weeks /** * Checks whether the node is deprecated based on the current block height, and * shuts down the node with an error if so (and deprecation is not disabled for - * the current client version). + * the current client version). Warning and error messages are sent to the debug + * log, the metrics UI, and (if configured) -alertnofity. + * + * fThread means run -alertnotify in a free-running thread. */ -void EnforceNodeDeprecation(int nHeight, bool forceLogging=false); +void EnforceNodeDeprecation(int nHeight, bool forceLogging=false, bool fThread=true); #endif // ZCASH_DEPRECATION_H diff --git a/src/gtest/test_deprecation.cpp b/src/gtest/test_deprecation.cpp index eb2d7bfc6..4f2526d65 100644 --- a/src/gtest/test_deprecation.cpp +++ b/src/gtest/test_deprecation.cpp @@ -1,12 +1,15 @@ #include #include +#include "chainparams.h" #include "clientversion.h" #include "deprecation.h" #include "init.h" #include "ui_interface.h" #include "util.h" -#include "chainparams.h" + +#include +#include using ::testing::StrictMock; @@ -43,6 +46,18 @@ protected: } StrictMock mock_; + + static std::vector read_lines(boost::filesystem::path filepath) { + std::vector result; + + std::ifstream f(filepath.string().c_str()); + std::string line; + while (std::getline(f,line)) { + result.push_back(line); + } + + return result; + } }; TEST_F(DeprecationTest, NonDeprecatedNodeKeepsRunning) { @@ -119,4 +134,32 @@ TEST_F(DeprecationTest, DeprecatedNodeIgnoredOnTestnet) { EXPECT_FALSE(ShutdownRequested()); EnforceNodeDeprecation(DEPRECATION_HEIGHT+1); EXPECT_FALSE(ShutdownRequested()); -} \ No newline at end of file +} + +TEST_F(DeprecationTest, AlertNotify) { + boost::filesystem::path temp = GetTempPath() / + boost::filesystem::unique_path("alertnotify-%%%%.txt"); + + mapArgs["-alertnotify"] = std::string("echo %s >> ") + temp.string(); + + EXPECT_CALL(mock_, ThreadSafeMessageBox(::testing::_, "", CClientUIInterface::MSG_WARNING)); + EnforceNodeDeprecation(DEPRECATION_HEIGHT - DEPRECATION_WARN_LIMIT, false, false); + + std::vector r = read_lines(temp); + EXPECT_EQ(r.size(), 1u); + + // -alertnotify restricts the message to safe characters. + auto expectedMsg = strprintf( + "This version will be deprecated at block height %d, and will automatically shut down. You should upgrade to the latest version of Zcash. To disable deprecation for this version, set disabledeprecation%s.", + DEPRECATION_HEIGHT, + CLIENT_VERSION_STR); + + // Windows built-in echo semantics are different than posixy shells. Quotes and + // whitespace are printed literally. +#ifndef WIN32 + EXPECT_EQ(r[0], expectedMsg); +#else + EXPECT_EQ(r[0], strprintf("'%s' ", expectedMsg)); +#endif + boost::filesystem::remove(temp); +} From 6c444243224b3ff1b456cd1a310170b998ac0342 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Tue, 3 Apr 2018 12:17:30 +0100 Subject: [PATCH 2/2] Adjust deprecation message to work in both UI and -alertnotify --- src/deprecation.cpp | 8 ++++---- src/gtest/test_deprecation.cpp | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/deprecation.cpp b/src/deprecation.cpp index b2e980bca..4c197cce1 100644 --- a/src/deprecation.cpp +++ b/src/deprecation.cpp @@ -33,8 +33,8 @@ void EnforceNodeDeprecation(int nHeight, bool forceLogging, bool fThread) { DEPRECATION_HEIGHT) + " " + _("You should upgrade to the latest version of Zcash."); if (!disableDeprecation) { - msg += " " + strprintf(_("To disable deprecation for this version, set %s%s."), - "-disabledeprecation=", CLIENT_VERSION_STR); + msg += " " + strprintf(_("To disable deprecation for this version, set '%s' to '%s'."), + "disabledeprecation", CLIENT_VERSION_STR); } LogPrintf("*** %s\n", msg); CAlert::Notify(msg, fThread); @@ -54,8 +54,8 @@ void EnforceNodeDeprecation(int nHeight, bool forceLogging, bool fThread) { msg = strprintf(_("This version will be deprecated at block height %d, and will automatically shut down."), DEPRECATION_HEIGHT) + " " + _("You should upgrade to the latest version of Zcash.") + " " + - strprintf(_("To disable deprecation for this version, set %s%s."), - "-disabledeprecation=", CLIENT_VERSION_STR); + strprintf(_("To disable deprecation for this version, set '%s' to '%s'."), + "disabledeprecation", CLIENT_VERSION_STR); } LogPrintf("*** %s\n", msg); CAlert::Notify(msg, fThread); diff --git a/src/gtest/test_deprecation.cpp b/src/gtest/test_deprecation.cpp index 4f2526d65..1a9ffc6ae 100644 --- a/src/gtest/test_deprecation.cpp +++ b/src/gtest/test_deprecation.cpp @@ -150,7 +150,7 @@ TEST_F(DeprecationTest, AlertNotify) { // -alertnotify restricts the message to safe characters. auto expectedMsg = strprintf( - "This version will be deprecated at block height %d, and will automatically shut down. You should upgrade to the latest version of Zcash. To disable deprecation for this version, set disabledeprecation%s.", + "This version will be deprecated at block height %d, and will automatically shut down. You should upgrade to the latest version of Zcash. To disable deprecation for this version, set disabledeprecation to %s.", DEPRECATION_HEIGHT, CLIENT_VERSION_STR);