Auto merge of #2899 - str4d:2829-alertnotify-deprecation, r=str4d

Notify users about auto-senescence via -alertnotify

Closes #2829.
This commit is contained in:
Homu 2018-04-03 06:07:08 -07:00
commit 395ed619a0
3 changed files with 59 additions and 10 deletions

View File

@ -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();
@ -32,10 +33,11 @@ void EnforceNodeDeprecation(int nHeight, bool forceLogging) {
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);
uiInterface.ThreadSafeMessageBox(msg, "", CClientUIInterface::MSG_ERROR);
}
if (!disableDeprecation) {
@ -52,10 +54,11 @@ void EnforceNodeDeprecation(int nHeight, bool forceLogging) {
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);
uiInterface.ThreadSafeMessageBox(msg, "", CClientUIInterface::MSG_WARNING);
}
}
}

View File

@ -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

View File

@ -1,12 +1,15 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "chainparams.h"
#include "clientversion.h"
#include "deprecation.h"
#include "init.h"
#include "ui_interface.h"
#include "util.h"
#include "chainparams.h"
#include <boost/filesystem/operations.hpp>
#include <fstream>
using ::testing::StrictMock;
@ -43,6 +46,18 @@ protected:
}
StrictMock<MockUIInterface> mock_;
static std::vector<std::string> read_lines(boost::filesystem::path filepath) {
std::vector<std::string> 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());
}
}
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<std::string> 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 to %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);
}