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, qt: Fix snapshot download #2246

Merged
Show file tree
Hide file tree
Changes from all 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
56 changes: 32 additions & 24 deletions src/gridcoin/scraper/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#define progressinterval 1
#endif

struct_SnapshotStatus DownloadStatus;
SnapshotStatus DownloadStatus;

enum class logattribute {
// Can't use ERROR here because it is defined already in windows.h.
Expand Down Expand Up @@ -75,7 +75,8 @@ namespace
CURL *curl;
};

static int newerprogress_callback(void *ptr, curl_off_t downtotal, curl_off_t downnow, curl_off_t uptotal, curl_off_t uplnow)
static int newerprogress_callback(void *ptr, curl_off_t downtotal, curl_off_t downnow,
curl_off_t uptotal, curl_off_t uplnow)
{
struct progress *pg = (struct progress*)ptr;
CURL *curl = pg->curl;
Expand All @@ -85,8 +86,8 @@ namespace
{
boost::this_thread::interruption_point();
// Set this once.
if (DownloadStatus.SnapshotDownloadSize == 0)
DownloadStatus.SnapshotDownloadSize = downtotal;
if (DownloadStatus.GetSnapshotDownloadSize() == 0)
DownloadStatus.SetSnapshotDownloadSize(downtotal);

timetype currenttime = 0;
curl_easy_getinfo(curl, timeopt, &currenttime);
Expand All @@ -107,20 +108,22 @@ namespace

#if LIBCURL_VERSION_NUM >= 0x073700
if (speed > 0)
DownloadStatus.SnapshotDownloadSpeed = (int64_t)speed;

else {
DownloadStatus.SnapshotDownloadSpeed = 0;

{
DownloadStatus.SetSnapshotDownloadSpeed((int64_t) speed);
}
else
{
DownloadStatus.SetSnapshotDownloadSpeed(0);
}

#else
// Not supported by libcurl
DownloadStatus.SnapshotDownloadSpeed = -1;
#endif
if (DownloadStatus.SnapshotDownloadSize > 0 && (downnow > 0))
if (DownloadStatus.GetSnapshotDownloadSize() > 0 && (downnow > 0))
{
DownloadStatus.SnapshotDownloadProgress = ((downnow / (double)DownloadStatus.SnapshotDownloadSize) * 100);
DownloadStatus.SnapshotDownloadAmount = downnow;
DownloadStatus.SetSnapshotDownloadProgress(downnow * 100 / DownloadStatus.GetSnapshotDownloadSize());
DownloadStatus.SetSnapshotDownloadAmount(downnow);
}
}
}
Expand All @@ -137,7 +140,8 @@ namespace
#if LIBCURL_VERSION_NUM < 0x072000
static int olderprogress_callback(void *ptr, double downtotal, double downnow, double uptotal, double upnow)
{
return newerprogress_callback(ptr, (curl_off_t)downtotal, (curl_off_t)downnow, (curl_off_t)uptotal, (curl_off_t)upnow);
return newerprogress_callback(ptr, (curl_off_t)downtotal, (curl_off_t)downnow,
(curl_off_t)uptotal, (curl_off_t)upnow);
};

#endif
Expand Down Expand Up @@ -270,7 +274,8 @@ std::string Http::GetLatestVersionResponse()
{
std::string buffer;
std::string header;
std::string url = gArgs.GetArg("-updatecheckurl", "https://api.github.com/repos/gridcoin-community/Gridcoin-Research/releases/latest");
std::string url = gArgs.GetArg("-updatecheckurl",
"https://api.github.com/repos/gridcoin-community/Gridcoin-Research/releases/latest");

struct curl_slist* headers = nullptr;
headers = curl_slist_append(headers, "Accept: */*");
Expand All @@ -286,7 +291,8 @@ std::string Http::GetLatestVersionResponse()
CURLcode res = curl_easy_perform(curl.get());

if (res > 0)
throw std::runtime_error(tfm::format("Failed to get version response from URL %s: %s", url, curl_easy_strerror(res)));
throw std::runtime_error(tfm::format("Failed to get version response from URL %s: %s",
url, curl_easy_strerror(res)));

curl_slist_free_all(headers);

Expand All @@ -309,10 +315,11 @@ void Http::DownloadSnapshot()

if (!fp)
{
DownloadStatus.SnapshotDownloadFailed = true;
DownloadStatus.SetSnapshotDownloadFailed(true);

throw std::runtime_error(
tfm::format("Snapshot Downloader: Error opening target %s: %s (%d)", destination.string(), strerror(errno), errno));
tfm::format("Snapshot Downloader: Error opening target %s: %s (%d)",
destination.string(), strerror(errno), errno));
}

std::string buffer;
Expand Down Expand Up @@ -363,9 +370,10 @@ void Http::DownloadSnapshot()

else
{
DownloadStatus.SnapshotDownloadFailed = true;
DownloadStatus.SetSnapshotDownloadFailed(true);

throw std::runtime_error(tfm::format("Snapshot Downloader: Failed to download file %s: %s", url, curl_easy_strerror(res)));
throw std::runtime_error(tfm::format("Snapshot Downloader: Failed to download file %s: %s",
url, curl_easy_strerror(res)));
}
}

Expand All @@ -374,7 +382,7 @@ void Http::DownloadSnapshot()
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
EvaluateResponse(response_code, url);

DownloadStatus.SnapshotDownloadComplete = true;
DownloadStatus.SetSnapshotDownloadComplete(true);

return;
}
Expand Down Expand Up @@ -402,7 +410,7 @@ std::string Http::GetSnapshotSHA256()

if (res > 0)
{
LogPrintf("Snapshot (GetSnapshotSHA256): Failed to SHA256SUM of snapshot.zip for URL %s: %s", url, curl_easy_strerror(res));
error("%s: Failed to SHA256SUM of snapshot.zip for URL %s: %s", __func__, url, curl_easy_strerror(res));

return "";
}
Expand All @@ -414,7 +422,7 @@ std::string Http::GetSnapshotSHA256()

if (buffer.empty())
{
LogPrintf("Snapshot (GetSnapshotSHA256): Failed to receive SHA256SUM from url: %s", url);
error("%s: Failed to receive SHA256SUM from url: %s", __func__, url);

return "";
}
Expand All @@ -423,14 +431,14 @@ std::string Http::GetSnapshotSHA256()

if (loc == std::string::npos)
{
LogPrintf("Snapshot (GetSnapshotSHA256): Malformed SHA256SUM from url: %s", url);
error("%s: Malformed SHA256SUM from url: %s", __func__, url);

return "";
}

else
{
LogPrint(BCLog::LogFlags::VERBOSE, "Snapshot Downloader: Receives SHA256SUM of %s", buffer.substr(0, loc));
LogPrint(BCLog::LogFlags::VERBOSE, "INFO: %s: Receives SHA256SUM of %s", __func__, buffer.substr(0, loc));

return buffer.substr(0, loc);
}
Expand Down
206 changes: 202 additions & 4 deletions src/gridcoin/scraper/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,218 @@

#include <string>
#include <stdexcept>
#include "sync.h"

//!
//! \brief Struct for snapshot download progress updates.
//!
struct struct_SnapshotStatus{
class SnapshotStatus
{
public:
void Reset()
{
LOCK(cs_lock);

SnapshotDownloadComplete = false;
SnapshotDownloadFailed = false;
SnapshotDownloadSpeed = 0;
SnapshotDownloadProgress = 0;
SnapshotDownloadSize = 0;
SnapshotDownloadAmount = 0;
SHA256SUMProgress = 0;
SHA256SUMComplete = false;
SHA256SUMFailed = false;
CleanupBlockchainDataProgress = 0;
CleanupBlockchainDataComplete = false;
CleanupBlockchainDataFailed = false;
}

bool GetSnapshotDownloadComplete()
{
LOCK(cs_lock);

return SnapshotDownloadComplete;
}

bool GetSnapshotDownloadFailed()
{
LOCK(cs_lock);

return SnapshotDownloadFailed;
}

int64_t GetSnapshotDownloadSpeed()
{
LOCK(cs_lock);

return SnapshotDownloadSpeed;
}

int GetSnapshotDownloadProgress()
{
LOCK(cs_lock);

return SnapshotDownloadProgress;
}

long long GetSnapshotDownloadSize()
{
LOCK(cs_lock);

return SnapshotDownloadSize;
}

long long GetSnapshotDownloadAmount()
{
LOCK(cs_lock);

return SnapshotDownloadAmount;
}

int GetSHA256SUMProgress()
{
LOCK(cs_lock);

return SHA256SUMProgress;
}

bool GetSHA256SUMComplete()
{
LOCK(cs_lock);

return SHA256SUMComplete;
}

bool GetSHA256SUMFailed()
{
LOCK(cs_lock);

return SHA256SUMFailed;
}

int GetCleanupBlockchainDataProgress()
{
LOCK(cs_lock);

return CleanupBlockchainDataProgress;
}

bool GetCleanupBlockchainDataComplete()
{
LOCK(cs_lock);

return CleanupBlockchainDataComplete;
}

bool GetCleanupBlockchainDataFailed()
{
LOCK(cs_lock);

return CleanupBlockchainDataFailed;
}

void SetSnapshotDownloadComplete(bool SnapshotDownloadComplete_in)
{
LOCK(cs_lock);

SnapshotDownloadComplete = SnapshotDownloadComplete_in;
}

void SetSnapshotDownloadFailed(bool SnapshotDownloadFailed_in)
{
LOCK(cs_lock);

SnapshotDownloadFailed = SnapshotDownloadFailed_in;
}

void SetSnapshotDownloadSpeed(int64_t SnapshotDownloadSpeed_in)
{
LOCK(cs_lock);

SnapshotDownloadSpeed = SnapshotDownloadSpeed_in;
}

void SetSnapshotDownloadProgress(int SnapshotDownloadProgress_in)
{
LOCK(cs_lock);

SnapshotDownloadProgress = SnapshotDownloadProgress_in;
}

void SetSnapshotDownloadSize(long long SnapshotDownloadSize_in)
{
LOCK(cs_lock);

SnapshotDownloadSize = SnapshotDownloadSize_in;
}

void SetSnapshotDownloadAmount(long long SnapshotDownloadAmount_in)
{
LOCK(cs_lock);

SnapshotDownloadAmount = SnapshotDownloadAmount_in;
}

void SetSHA256SUMProgress(int SHA256SumProgress_in)
{
LOCK(cs_lock);

SHA256SUMProgress = SHA256SumProgress_in;
}

void SetSHA256SUMComplete(bool SHA256SUMComplete_in)
{
LOCK(cs_lock);

SHA256SUMComplete = SHA256SUMComplete_in;
}

void SetSHA256SUMFailed(bool SHA256SUMFailed_in)
{
LOCK(cs_lock);

SHA256SUMFailed = SHA256SUMFailed_in;
}

void SetCleanupBlockchainDataProgress(int CleanupBlockchainDataProgress_in)
{
LOCK(cs_lock);

CleanupBlockchainDataProgress = CleanupBlockchainDataProgress_in;
}

void SetCleanupBlockchainDataComplete(bool CleanupBlockchainDataComplete_in)
{
LOCK(cs_lock);

CleanupBlockchainDataComplete = CleanupBlockchainDataComplete_in;
}

void SetCleanupBlockchainDataFailed(bool CleanupBlockchainDataFailed_in)
{
LOCK(cs_lock);

CleanupBlockchainDataFailed = CleanupBlockchainDataFailed_in;
}

private:
CCriticalSection cs_lock;

bool SnapshotDownloadComplete = false;
bool SnapshotDownloadFailed = false;
int64_t SnapshotDownloadSpeed;
int SnapshotDownloadProgress;
int64_t SnapshotDownloadSpeed = 0;
int SnapshotDownloadProgress = 0;
long long SnapshotDownloadSize = 0;
long long SnapshotDownloadAmount = 0;
int SHA256SUMProgress = 0;
bool SHA256SUMComplete = false;
bool SHA256SUMFailed = false;
int CleanupBlockchainDataProgress = 0;
bool CleanupBlockchainDataComplete = false;
bool CleanupBlockchainDataFailed = false;
};

extern struct_SnapshotStatus DownloadStatus;
extern SnapshotStatus DownloadStatus;

//!
//! \brief HTTP exception.
Expand Down
Loading