Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util, rpc, gui: Changes for snapshotdownload and add feature sync from zero #2093

Merged
merged 5 commits into from
Apr 11, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/gridcoin/gridcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,12 @@ void ScheduleUpdateChecks(CScheduler& scheduler)
LogPrintf("Gridcoin: checking for updates every %" PRId64 " hours", hours);

scheduler.scheduleEvery([]{
g_UpdateChecker->CheckForLatestUpdate();
g_UpdateChecker->ScheduledUpdateCheck();
}, hours * 60 * 60 * 1000);

// Schedule a start-up check one minute from now:
scheduler.scheduleFromNow([]{
g_UpdateChecker->CheckForLatestUpdate();
g_UpdateChecker->ScheduledUpdateCheck();
}, 60 * 1000);
}

Expand All @@ -379,6 +379,7 @@ void ScheduleBeaconDBPassivation(CScheduler& scheduler)

std::unique_ptr<Upgrade> g_UpdateChecker;
bool fSnapshotRequest = false;
bool fSyncfromzeroRequest = false;

// -----------------------------------------------------------------------------
// Functions
Expand Down
49 changes: 37 additions & 12 deletions src/gridcoin/upgrade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ Upgrade::Upgrade()
ExtractStatus.SnapshotExtractProgress = 0;
}

bool Upgrade::CheckForLatestUpdate(bool ui_dialog, std::string client_message_out)
void Upgrade::ScheduledUpdateCheck()
{
std::string VersionResponse = "";

CheckForLatestUpdate(VersionResponse);
}

bool Upgrade::CheckForLatestUpdate(std::string& client_message_out, bool ui_dialog, bool snapshotrequest)
{
// If testnet skip this || If the user changes this to disable while wallet running just drop out of here now. (need a way to remove items from scheduler)
if (fTestNet || GetBoolArg("-disableupdatecheck", false))
iFoggz marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -48,6 +55,7 @@ bool Upgrade::CheckForLatestUpdate(bool ui_dialog, std::string client_message_ou

std::string GithubResponse = "";
std::string VersionResponse = "";
std::string UpdateCheckType = snapshotrequest ? "Snapshot Request" : "Update Checker";
iFoggz marked this conversation as resolved.
Show resolved Hide resolved

// We receive the response and it's in a json reply
UniValue Response(UniValue::VOBJ);
Expand All @@ -59,14 +67,14 @@ bool Upgrade::CheckForLatestUpdate(bool ui_dialog, std::string client_message_ou

catch (const std::runtime_error& e)
{
LogPrintf("Update Checker: Exception occurred while checking for latest update. (%s)", e.what());
LogPrintf("%s: Exception occurred while checking for latest update. (%s)", UpdateCheckType, e.what());

return false;
iFoggz marked this conversation as resolved.
Show resolved Hide resolved
}

if (VersionResponse.empty())
{
LogPrintf("Update Checker: No Response from github");
LogPrintf("%s: No Response from github", UpdateCheckType);

return false;
}
Expand All @@ -91,7 +99,7 @@ bool Upgrade::CheckForLatestUpdate(bool ui_dialog, std::string client_message_ou

catch (std::exception& ex)
{
LogPrintf("Update Checker: Exception occurred while parsing json response (%s)", ex.what());
LogPrintf("%s: Exception occurred while parsing json response (%s)", UpdateCheckType, ex.what());

return false;
}
Expand All @@ -118,7 +126,7 @@ bool Upgrade::CheckForLatestUpdate(bool ui_dialog, std::string client_message_ou

if (GithubVersion.size() != 4)
{
LogPrintf("Update Check: Got malformed version (%s)", GithubReleaseData);
LogPrintf("%s: Got malformed version (%s)", UpdateCheckType, GithubReleaseData);

return false;
}
Expand All @@ -144,7 +152,7 @@ bool Upgrade::CheckForLatestUpdate(bool ui_dialog, std::string client_message_ou
}
catch (std::exception& ex)
{
LogPrintf("Update Check: Exception occurred checking client version against github version (%s)", ToString(ex.what()));
LogPrintf("%s: Exception occurred checking client version against github version (%s)", UpdateCheckType, ToString(ex.what()));

return false;
}
Expand All @@ -156,17 +164,17 @@ bool Upgrade::CheckForLatestUpdate(bool ui_dialog, std::string client_message_ou
client_message_out.append(_("Github version: ") + GithubReleaseData + "\r\n");
client_message_out.append(_("This update is ") + GithubReleaseType + "\r\n\r\n");

// For snapshot requests we will handle things differently after this point
if (snapshotrequest && NewMandatory)
return NewVersion;

if (NewMandatory)
{
client_message_out.append(_("WARNING: A mandatory release is available. Please upgrade as soon as possible.") + "\n");
}

std::string ChangeLog = GithubReleaseBody;

if (ui_dialog)
{
uiInterface.UpdateMessageBox(client_message_out, ChangeLog);
}

return NewVersion;
}
Expand All @@ -177,6 +185,18 @@ void Upgrade::SnapshotMain()
std::cout << _("Snapshot Process Has Begun.") << std::endl;
std::cout << _("Warning: Ending this process after Stage 2 will result in syncing from 0 or an incomplete/corrupted blockchain.") << std::endl << std::endl;

// Verify a mandatory release is not available before we continue to snapshot download.
std::string VersionResponse = "";

if (CheckForLatestUpdate(VersionResponse, false, true))
{
std::cout << _("Unable to perform a Snapshot download as the wallet has detected that a new mandatory version is available for download.") << std::endl;
iFoggz marked this conversation as resolved.
Show resolved Hide resolved
std::cout << _("Latest Version github data response:") << std::endl;
std::cout << VersionResponse << std::endl;

throw std::runtime_error("Failed to download snapshot as mandatory client is available for download.");
}

// Create a thread for snapshot to be downloaded
boost::thread SnapshotDownloadThread(std::bind(&Upgrade::DownloadSnapshot, this));

Expand Down Expand Up @@ -345,7 +365,7 @@ bool Upgrade::VerifySHA256SUM()
}
}

bool Upgrade::CleanupBlockchainData()
bool Upgrade::CleanupBlockchainData(bool snapshotreq)
{
fs::path CleanupPath = GetDataDir();

Expand Down Expand Up @@ -400,7 +420,7 @@ bool Upgrade::CleanupBlockchainData()

catch (fs::filesystem_error &ex)
{
LogPrintf("Snapshot (CleanupBlockchainData): Exception occurred: %s", ex.what());
LogPrintf("%s: Exception occurred: %s", snapshotreq ? "Snapshot (CleanupBlockchainData)" : "SyncFromZero (CleanupBlockchainData)", ex.what());

return false;
}
Expand Down Expand Up @@ -571,3 +591,8 @@ void Upgrade::DeleteSnapshot()
LogPrintf("Snapshot Downloader: Exception occurred while attempting to delete snapshot (%s)", e.code().message());
}
}

bool Upgrade::SyncFromZero()
{
return CleanupBlockchainData(false);
}
16 changes: 14 additions & 2 deletions src/gridcoin/upgrade.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@ class Upgrade
//!
Upgrade();

//!
//! \brief Scheduler call to CheckForLatestUpdate
//!
static void ScheduledUpdateCheck();

//!
//! \brief Check for latest updates on github.
//!
static bool CheckForLatestUpdate(bool ui_dialog = true, std::string client_message_out = "");
static bool CheckForLatestUpdate(std::string& client_message_out, bool ui_dialog = true, bool snapshotrequest = false);

//!
//! \brief Function that will be threaded to download snapshot
Expand All @@ -58,7 +63,7 @@ class Upgrade
//!
//! \return Bool on the success of cleanup
//!
static bool CleanupBlockchainData();
static bool CleanupBlockchainData(bool snapshotreq = true);

//!
//! \brief Extracts the snapshot zip file
Expand All @@ -85,6 +90,13 @@ class Upgrade
//! \brief Small function to delete the snapshot.zip file
//!
static void DeleteSnapshot();

//!
//! \brief Small function to allow wallet user to clear blockchain data and sync from 0 while keeping a clean look
//!
//! \returns Bool on the success of blockchain cleanup
//!
static bool SyncFromZero();
};

//!
Expand Down
35 changes: 35 additions & 0 deletions src/gridcoinresearchd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ bool AppInit(int argc, char* argv[])
// Initialize logging as early as possible.
InitLogging();

// Make sure a user does not request snapshotdownload and syncfromzero at same time!
if (mapArgs.count("-snapshotdownload") && mapArgs.count("-syncfromzero"))
{
fprintf(stderr, "-snapshotdownload and -syncfromzero cannot be used in conjunction");

exit(1);
}

// Check to see if the user requested a snapshot and we are not running TestNet!
if (mapArgs.count("-snapshotdownload") && !mapArgs.count("-testnet"))
{
Expand Down Expand Up @@ -134,6 +142,33 @@ bool AppInit(int argc, char* argv[])
snapshot.DeleteSnapshot();
}

// Check to see if the user requested to sync from 0 -- We allow on testnet.
iFoggz marked this conversation as resolved.
Show resolved Hide resolved
if (mapArgs.count("-syncfromzero"))
{
GRC::Upgrade syncfromzero;

// Let's check make sure gridcoin is not already running in the data directory.
if (!LockDirectory(GetDataDir(), ".lock", false))
{
fprintf(stderr, "Cannot obtain a lock on data directory %s. Gridcoin is probably already running.", GetDataDir().string().c_str());

exit(1);
}

else
{
if (syncfromzero.SyncFromZero())
LogPrintf("Syncfromzero: Success");

else
{
LogPrintf("Syncfromzero: Failed to clean up blockchain data");

exit(1);
}
}
}

LogPrintf("AppInit");

fRet = AppInit2(threads);
Expand Down
3 changes: 2 additions & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ std::string HelpMessage()
" -snapshotsha256url=<url> " + _("Optional: URL for the snapshot.sha256 file (ex: https://sub.domain.com/location/snapshot.sha256)") + "\n"
" -disableupdatecheck " + _("Optional: Disable update checks by wallet") + "\n"
" -updatecheckinterval=<n> " + _("Optional: Check for updates every <n> hours (default: 120, minimum: 1)") + "\n"
" -updatecheckurl=<url> " + _("Optional: URL for the update version checks (ex: https://sub.domain.com/location/latest") + "\n";
" -updatecheckurl=<url> " + _("Optional: URL for the update version checks (ex: https://sub.domain.com/location/latest") + "\n"
" -syncfromzero " + _("Sync blockchain from zero. This argument will remove all previous blockchain data") + "\n";

return strUsage;
}
Expand Down
1 change: 1 addition & 0 deletions src/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ std::string VersionMessage();
std::string LogSomething();

extern bool fSnapshotRequest;
extern bool fSyncfromzeroRequest;
#endif
46 changes: 46 additions & 0 deletions src/qt/bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,14 @@ int main(int argc, char *argv[])
// Do this early as we don't want to bother initializing if we are just calling IPC
ipcScanRelay(argc, argv);

// Make sure a user does not request snapshotdownload and syncfromzero at same time!
if (mapArgs.count("-snapshotdownload") && mapArgs.count("-syncfromzero"))
{
LogPrintf("-snapshotdownload and -syncfromzero cannot be used in conjunction");

return EXIT_FAILURE;
}

// Run snapshot main if Gridcoin was started with the snapshot argument and we are not TestNet
if (mapArgs.count("-snapshotdownload") && !mapArgs.count("-testnet"))
{
Expand All @@ -376,6 +384,22 @@ int main(int argc, char *argv[])
snapshot.DeleteSnapshot();
}

// Check to see if the user requested to sync from 0 -- We allow on testnet.
if (mapArgs.count("-syncfromzero"))
{
GRC::Upgrade syncfromzero;

if (syncfromzero.SyncFromZero())
LogPrintf("Syncfromzero: Success");

else
{
LogPrintf("Syncfromzero: Failed to clean up blockchain data");

return EXIT_FAILURE;
}
}

/** Start Qt as normal before it was moved into this function **/
StartGridcoinQt(argc, argv, app, optionsModel);

Expand Down Expand Up @@ -420,6 +444,28 @@ int main(int argc, char *argv[])
Snapshot.DeleteSnapshot();
}

// We received a request to remove blockchain data so client user can start to sync from 0
if (fSyncfromzeroRequest)
{
UpgradeQt Syncfromzero;

// Release LevelDB file handles on Windows so we can remove the old
// blockchain files:
//
// We should really close it in Shutdown() when the main application
// exits. Before we can do that, we need to solve an old outstanding
// conflict with the behavior of "-daemon" on Linux that prematurely
// closes the DB when the process forks.
//
CTxDB().Close();

if (Syncfromzero.SyncFromZero(app))
LogPrintf("Syncfromzero: Success!");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of the logs message start with Syncfromzero: while others say Sync from zero: . Probably should pick one and stay with it. Maybe one of the two or even Sync From Zero: or SyncFromZero:

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all logging is syncfromzero: its the message pop ups in qt that are spaced.


else
LogPrintf("Syncfromzero: Failed!");
}

return EXIT_SUCCESS;
}

Expand Down
Loading