Skip to content

Commit 076e5b6

Browse files
authored
Merge pull request #2093 from iFoggz/snapshotchanges
Changes for snapshotdownload and add feature sync from zero
2 parents 19fa6b8 + 3078813 commit 076e5b6

File tree

12 files changed

+282
-19
lines changed

12 files changed

+282
-19
lines changed

src/gridcoin/gridcoin.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,12 +406,12 @@ void ScheduleUpdateChecks(CScheduler& scheduler)
406406
LogPrintf("Gridcoin: checking for updates every %" PRId64 " hours", hours);
407407

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

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

@@ -429,6 +429,7 @@ void ScheduleBeaconDBPassivation(CScheduler& scheduler)
429429

430430
std::unique_ptr<Upgrade> g_UpdateChecker;
431431
bool fSnapshotRequest = false;
432+
bool fResetBlockchainRequest = false;
432433

433434
// -----------------------------------------------------------------------------
434435
// Functions

src/gridcoin/upgrade.cpp

Lines changed: 74 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,17 @@ Upgrade::Upgrade()
3838
ExtractStatus.SnapshotExtractProgress = 0;
3939
}
4040

41-
bool Upgrade::CheckForLatestUpdate(bool ui_dialog, std::string client_message_out)
41+
void Upgrade::ScheduledUpdateCheck()
42+
{
43+
std::string VersionResponse = "";
44+
45+
CheckForLatestUpdate(VersionResponse);
46+
}
47+
48+
bool Upgrade::CheckForLatestUpdate(std::string& client_message_out, bool ui_dialog, bool snapshotrequest)
4249
{
4350
// 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)
44-
if (fTestNet || GetBoolArg("-disableupdatecheck", false))
51+
if (fTestNet || (GetBoolArg("-disableupdatecheck", false) && !snapshotrequest))
4552
return false;
4653

4754
Http VersionPull;
@@ -59,14 +66,12 @@ bool Upgrade::CheckForLatestUpdate(bool ui_dialog, std::string client_message_ou
5966

6067
catch (const std::runtime_error& e)
6168
{
62-
LogPrintf("Update Checker: Exception occurred while checking for latest update. (%s)", e.what());
63-
64-
return false;
69+
return error("%s: Exception occurred while checking for latest update. (%s)", __func__, e.what());
6570
}
6671

6772
if (VersionResponse.empty())
6873
{
69-
LogPrintf("Update Checker: No Response from github");
74+
LogPrintf("%s: No Response from github", __func__);
7075

7176
return false;
7277
}
@@ -91,7 +96,7 @@ bool Upgrade::CheckForLatestUpdate(bool ui_dialog, std::string client_message_ou
9196

9297
catch (std::exception& ex)
9398
{
94-
LogPrintf("Update Checker: Exception occurred while parsing json response (%s)", ex.what());
99+
LogPrintf("%s: Exception occurred while parsing json response (%s)", __func__, ex.what());
95100

96101
return false;
97102
}
@@ -118,7 +123,7 @@ bool Upgrade::CheckForLatestUpdate(bool ui_dialog, std::string client_message_ou
118123

119124
if (GithubVersion.size() != 4)
120125
{
121-
LogPrintf("Update Check: Got malformed version (%s)", GithubReleaseData);
126+
LogPrintf("%s: Got malformed version (%s)", __func__, GithubReleaseData);
122127

123128
return false;
124129
}
@@ -144,7 +149,7 @@ bool Upgrade::CheckForLatestUpdate(bool ui_dialog, std::string client_message_ou
144149
}
145150
catch (std::exception& ex)
146151
{
147-
LogPrintf("Update Check: Exception occurred checking client version against github version (%s)", ToString(ex.what()));
152+
LogPrintf("%s: Exception occurred checking client version against github version (%s)", __func__, ToString(ex.what()));
148153

149154
return false;
150155
}
@@ -156,17 +161,17 @@ bool Upgrade::CheckForLatestUpdate(bool ui_dialog, std::string client_message_ou
156161
client_message_out.append(_("Github version: ") + GithubReleaseData + "\r\n");
157162
client_message_out.append(_("This update is ") + GithubReleaseType + "\r\n\r\n");
158163

164+
// For snapshot requests we will handle things differently after this point
165+
if (snapshotrequest && NewMandatory)
166+
return NewVersion;
167+
159168
if (NewMandatory)
160-
{
161169
client_message_out.append(_("WARNING: A mandatory release is available. Please upgrade as soon as possible.") + "\n");
162-
}
163170

164171
std::string ChangeLog = GithubReleaseBody;
165172

166173
if (ui_dialog)
167-
{
168174
uiInterface.UpdateMessageBox(client_message_out, ChangeLog);
169-
}
170175

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

185+
// Verify a mandatory release is not available before we continue to snapshot download.
186+
std::string VersionResponse = "";
187+
188+
if (CheckForLatestUpdate(VersionResponse, false, true))
189+
{
190+
std::cout << this->ResetBlockchainMessages(UpdateAvailable) << std::endl;
191+
std::cout << this->ResetBlockchainMessages(GithubResponse) << std::endl;
192+
std::cout << VersionResponse << std::endl;
193+
194+
throw std::runtime_error(_("Failed to download snapshot as mandatory client is available for download."));
195+
}
196+
180197
// Create a thread for snapshot to be downloaded
181198
boost::thread SnapshotDownloadThread(std::bind(&Upgrade::DownloadSnapshot, this));
182199

@@ -400,7 +417,7 @@ bool Upgrade::CleanupBlockchainData()
400417

401418
catch (fs::filesystem_error &ex)
402419
{
403-
LogPrintf("Snapshot (CleanupBlockchainData): Exception occurred: %s", ex.what());
420+
LogPrintf("%s: Exception occurred: %s", __func__, ex.what());
404421

405422
return false;
406423
}
@@ -571,3 +588,46 @@ void Upgrade::DeleteSnapshot()
571588
LogPrintf("Snapshot Downloader: Exception occurred while attempting to delete snapshot (%s)", e.code().message());
572589
}
573590
}
591+
592+
bool Upgrade::ResetBlockchainData()
593+
{
594+
return CleanupBlockchainData();
595+
}
596+
597+
std::string Upgrade::ResetBlockchainMessages(ResetBlockchainMsg _msg)
598+
{
599+
std::stringstream stream;
600+
601+
switch (_msg) {
602+
case CleanUp:
603+
{
604+
stream << _("Datadir: ");
605+
stream << GetDataDir().string();
606+
stream << "\r\n\r\n";
607+
stream << _("Due to the failure to delete the blockchain data you will be required to manually delete the data before starting your wallet.");
608+
stream << "\r\n";
609+
stream << _("Failure to do so will result in undefined behaviour or failure to start wallet.");
610+
stream << "\r\n\r\n";
611+
stream << _("You will need to delete the following.");
612+
stream << "\r\n\r\n";
613+
stream << _("Files:");
614+
stream << "\r\n";
615+
stream << "blk000*.dat";
616+
stream << "\r\n\r\n";
617+
stream << _("Directories:");
618+
stream << "\r\n";
619+
stream << "txleveldb";
620+
stream << "\r\n";
621+
stream << "accrual";
622+
623+
break;
624+
}
625+
626+
case UpdateAvailable: stream << _("Unable to download a snapshot, as the wallet has detected that a new mandatory version is available for install. The mandatory upgrade must be installed before the snapshot can be downloaded and applied."); break;
627+
case GithubResponse: stream << _("Latest Version github data response:"); break;
628+
}
629+
630+
const std::string& output = stream.str();
631+
632+
return output;
633+
}

src/gridcoin/upgrade.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,24 @@ class Upgrade
4242
//!
4343
Upgrade();
4444

45+
//!
46+
//! \brief Enum for determining the type of message to be returned for ResetBlockchainData functions
47+
//!
48+
enum ResetBlockchainMsg {
49+
CleanUp,
50+
UpdateAvailable,
51+
GithubResponse
52+
};
53+
54+
//!
55+
//! \brief Scheduler call to CheckForLatestUpdate
56+
//!
57+
static void ScheduledUpdateCheck();
58+
4559
//!
4660
//! \brief Check for latest updates on github.
4761
//!
48-
static bool CheckForLatestUpdate(bool ui_dialog = true, std::string client_message_out = "");
62+
static bool CheckForLatestUpdate(std::string& client_message_out, bool ui_dialog = true, bool snapshotrequest = false);
4963

5064
//!
5165
//! \brief Function that will be threaded to download snapshot
@@ -85,6 +99,20 @@ class Upgrade
8599
//! \brief Small function to delete the snapshot.zip file
86100
//!
87101
static void DeleteSnapshot();
102+
103+
//!
104+
//! \brief Small function to allow wallet user to clear blockchain data and sync from 0 while keeping a clean look
105+
//!
106+
//! \returns Bool on the success of blockchain cleanup
107+
//!
108+
static bool ResetBlockchainData();
109+
110+
//!
111+
//! \brief Small function to return translated messages.
112+
//!
113+
//! \returns String containing message.
114+
//!
115+
static std::string ResetBlockchainMessages(ResetBlockchainMsg _msg);
88116
};
89117

90118
//!

src/gridcoinresearchd.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ bool AppInit(int argc, char* argv[])
9999
// Initialize logging as early as possible.
100100
InitLogging();
101101

102+
// Make sure a user does not request snapshotdownload and resetblockchaindata at same time!
103+
if (mapArgs.count("-snapshotdownload") && mapArgs.count("-resetblockchaindata"))
104+
{
105+
fprintf(stderr, "-snapshotdownload and -resetblockchaindata cannot be used in conjunction");
106+
107+
exit(1);
108+
}
109+
102110
// Check to see if the user requested a snapshot and we are not running TestNet!
103111
if (mapArgs.count("-snapshotdownload") && !mapArgs.count("-testnet"))
104112
{
@@ -134,6 +142,37 @@ bool AppInit(int argc, char* argv[])
134142
snapshot.DeleteSnapshot();
135143
}
136144

145+
// Check to see if the user requested to reset blockchain data -- We allow reset blockchain data on testnet, but not a snapshot download.
146+
if (mapArgs.count("-resetblockchaindata"))
147+
{
148+
GRC::Upgrade resetblockchain;
149+
150+
// Let's check make sure gridcoin is not already running in the data directory.
151+
if (!LockDirectory(GetDataDir(), ".lock", false))
152+
{
153+
fprintf(stderr, "Cannot obtain a lock on data directory %s. Gridcoin is probably already running.", GetDataDir().string().c_str());
154+
155+
exit(1);
156+
}
157+
158+
else
159+
{
160+
if (resetblockchain.ResetBlockchainData())
161+
LogPrintf("ResetBlockchainData: success");
162+
163+
else
164+
{
165+
LogPrintf("ResetBlockchainData: failed to clean up blockchain data");
166+
167+
std::string inftext = resetblockchain.ResetBlockchainMessages(resetblockchain.CleanUp);
168+
169+
fprintf(stderr, "%s", inftext.c_str());
170+
171+
exit(1);
172+
}
173+
}
174+
}
175+
137176
LogPrintf("AppInit");
138177

139178
fRet = AppInit2(threads);

src/init.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,8 @@ std::string HelpMessage()
309309
" -snapshotsha256url=<url> " + _("Optional: URL for the snapshot.sha256 file (ex: https://sub.domain.com/location/snapshot.sha256)") + "\n"
310310
" -disableupdatecheck " + _("Optional: Disable update checks by wallet") + "\n"
311311
" -updatecheckinterval=<n> " + _("Optional: Check for updates every <n> hours (default: 120, minimum: 1)") + "\n"
312-
" -updatecheckurl=<url> " + _("Optional: URL for the update version checks (ex: https://sub.domain.com/location/latest") + "\n";
312+
" -updatecheckurl=<url> " + _("Optional: URL for the update version checks (ex: https://sub.domain.com/location/latest") + "\n"
313+
" -resetblockchaindata " + _("Reset blockchain data. This argument will remove all previous blockchain data") + "\n";
313314

314315
return strUsage;
315316
}

src/init.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ std::string VersionMessage();
2525
std::string LogSomething();
2626

2727
extern bool fSnapshotRequest;
28+
extern bool fResetBlockchainRequest;
2829
#endif

src/qt/bitcoin.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,14 @@ int main(int argc, char *argv[])
355355
// Do this early as we don't want to bother initializing if we are just calling IPC
356356
ipcScanRelay(argc, argv);
357357

358+
// Make sure a user does not request snapshotdownload and resetblockchaindata at same time!
359+
if (mapArgs.count("-snapshotdownload") && mapArgs.count("-resetblockchaindata"))
360+
{
361+
LogPrintf("-snapshotdownload and -resetblockchaindata cannot be used in conjunction");
362+
363+
return EXIT_FAILURE;
364+
}
365+
358366
// Run snapshot main if Gridcoin was started with the snapshot argument and we are not TestNet
359367
if (mapArgs.count("-snapshotdownload") && !mapArgs.count("-testnet"))
360368
{
@@ -378,6 +386,27 @@ int main(int argc, char *argv[])
378386
snapshot.DeleteSnapshot();
379387
}
380388

389+
// Check to see if the user requested to reset blockchain data -- We allow on testnet.
390+
if (mapArgs.count("-resetblockchaindata"))
391+
{
392+
GRC::Upgrade resetblockchain;
393+
394+
if (resetblockchain.ResetBlockchainData())
395+
LogPrintf("ResetBlockchainData: success");
396+
397+
else
398+
{
399+
LogPrintf("ResetBlockchainData: failed to clean up blockchain data");
400+
401+
std::string inftext = resetblockchain.ResetBlockchainMessages(resetblockchain.CleanUp);
402+
403+
ThreadSafeMessageBox(inftext, _("Gridcoin"), CClientUIInterface::OK | CClientUIInterface::MODAL);
404+
QMessageBox::critical(nullptr, PACKAGE_NAME, QString::fromStdString(inftext));
405+
406+
return EXIT_FAILURE;
407+
}
408+
}
409+
381410
/** Start Qt as normal before it was moved into this function **/
382411
StartGridcoinQt(argc, argv, app, optionsModel);
383412

@@ -422,6 +451,28 @@ int main(int argc, char *argv[])
422451
Snapshot.DeleteSnapshot();
423452
}
424453

454+
// We received a request to remove blockchain data so client user can start to sync from 0
455+
if (fResetBlockchainRequest)
456+
{
457+
UpgradeQt resetblockchain;
458+
459+
// Release LevelDB file handles on Windows so we can remove the old
460+
// blockchain files:
461+
//
462+
// We should really close it in Shutdown() when the main application
463+
// exits. Before we can do that, we need to solve an old outstanding
464+
// conflict with the behavior of "-daemon" on Linux that prematurely
465+
// closes the DB when the process forks.
466+
//
467+
CTxDB().Close();
468+
469+
if (resetblockchain.ResetBlockchain(app))
470+
LogPrintf("ResetBlockchainData: success");
471+
472+
else
473+
LogPrintf("ResetBlockchainData: failed");
474+
}
475+
425476
return EXIT_SUCCESS;
426477
}
427478

0 commit comments

Comments
 (0)