Skip to content

Commit

Permalink
Update Update naming
Browse files Browse the repository at this point in the history
  • Loading branch information
hooftly authored and hooftly committed May 18, 2019
1 parent 0cfad38 commit 95b956d
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 148 deletions.
29 changes: 9 additions & 20 deletions src/common/updates.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2017-2018, The Monero Project
// Copyright (c) 2017-2019, The Monero Project
// Copyright (c) 2019, The NERVA Project
//
// All rights reserved.
//
Expand Down Expand Up @@ -37,7 +38,7 @@

namespace tools
{
bool check_updates(const std::string &software, const std::string &buildtag, std::string &version, std::string &hash)
bool check_updates(const std::string &software, std::string &version, std::string &codename, std::string &notice)
{
std::vector<std::string> records;
bool found = false;
Expand All @@ -63,33 +64,21 @@ namespace tools
continue;
}

if (software != fields[0] || buildtag != fields[1])
if (software != fields[0])
continue;

bool alnum = true;
for (auto c: fields[3])
if (!isalnum(c))
alnum = false;
if (fields[3].size() != 64 && !alnum)
{
MWARNING("Invalid hash: " << fields[3]);
continue;
}

// use highest version
if (found)
{
int cmp = vercmp(version.c_str(), fields[2].c_str());
int cmp = vercmp(version.c_str(), fields[1].c_str());
if (cmp > 0)
continue;
if (cmp == 0 && hash != fields[3])
MWARNING("Two matches found for " << software << " version " << version << " on " << buildtag);
}

version = fields[2];
hash = fields[3];
version = fields[1];
codename = fields[2];
notice = fields[3];

LOG_PRINT_L1("Found new version " << version << " with hash " << hash);
LOG_PRINT_L1("Found new version " << version << ":" << codename);
found = true;
}
return found;
Expand Down
2 changes: 1 addition & 1 deletion src/common/updates.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@

namespace tools
{
bool check_updates(const std::string &software, const std::string &buildtag, std::string &version, std::string &hash);
bool check_updates(const std::string &software, std::string &version, std::string &codename, std::string &notice);
std::string get_update_url(const std::string &software, const std::string &buildtag, const std::string &version);
}
120 changes: 5 additions & 115 deletions src/cryptonote_core/cryptonote_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ using namespace epee;
#include "common/command_line.h"
#include "common/util.h"
#include "common/updates.h"
#include "common/download.h"
#include "common/threadpool.h"
#include "common/command_line.h"
#include "warnings.h"
Expand Down Expand Up @@ -185,7 +184,6 @@ namespace cryptonote
m_last_json_checkpoints_update(0),
m_disable_dns_checkpoints(false),
m_threadpool(tools::threadpool::getInstance()),
m_update_download(0),
m_nettype(UNDEFINED),
m_pad_transactions(false)
{
Expand Down Expand Up @@ -253,15 +251,6 @@ namespace cryptonote
{
m_miner.stop();
m_blockchain_storage.cancel();

tools::download_async_handle handle;
{
boost::lock_guard<boost::mutex> lock(m_update_mutex);
handle = m_update_download;
m_update_download = 0;
}
if (handle)
tools::download_cancel(handle);
}
//-----------------------------------------------------------------------------------
void core::init_options(boost::program_options::options_description& desc)
Expand Down Expand Up @@ -570,10 +559,6 @@ namespace cryptonote
check_updates_level = UPDATES_DISABLED;
else if (check_updates_string == "notify")
check_updates_level = UPDATES_NOTIFY;
else if (check_updates_string == "download")
check_updates_level = UPDATES_DOWNLOAD;
else if (check_updates_string == "update")
check_updates_level = UPDATES_UPDATE;
else {
MERROR("Invalid argument to --dns-versions-check: " << check_updates_string);
return false;
Expand Down Expand Up @@ -1544,7 +1529,7 @@ namespace cryptonote
//-----------------------------------------------------------------------------------------------
bool core::check_updates()
{
static const char software[] = "amity";
static const char software[] = "amity-cli";
#ifdef BUILD_TAG
static const char buildtag[] = BOOST_PP_STRINGIZE(BUILD_TAG);
#else
Expand All @@ -1557,115 +1542,20 @@ namespace cryptonote
if (check_updates_level == UPDATES_DISABLED)
return true;

std::string version, hash;
std::string version, codename, notice;
MCDEBUG("updates", "Checking for a new " << software << " version for " << buildtag);
if (!tools::check_updates(software, buildtag, version, hash))
if (!tools::check_updates(software, version, codename, notice))
return false;

if (tools::vercmp(version.c_str(), MONERO_VERSION) <= 0)
return true;

std::string url = tools::get_update_url(software, buildtag, version);
MGUSER_CYAN(ENDL
<< "Version " << version << " of " << software << " for " << buildtag << " is available" << ENDL
<< "Version " << version << ":" << codename << " is available" << ENDL
<< url << ENDL
<< "SHA256: " << hash);

if (check_updates_level == UPDATES_NOTIFY)
return true;

url = tools::get_update_url(software, buildtag, version);
std::string filename;
const char *slash = strrchr(url.c_str(), '/');
if (slash)
filename = slash + 1;
else
filename = std::string(software) + "-update-" + version;
boost::filesystem::path path(epee::string_tools::get_current_module_folder());
path /= filename;

boost::unique_lock<boost::mutex> lock(m_update_mutex);

if (m_update_download != 0)
{
MCDEBUG("updates", "Already downloading update");
return true;
}

crypto::hash file_hash;
if (!tools::sha256sum(path.string(), file_hash) || (hash != epee::string_tools::pod_to_hex(file_hash)))
{
MCDEBUG("updates", "We don't have that file already, downloading");
const std::string tmppath = path.string() + ".tmp";
if (epee::file_io_utils::is_file_exist(tmppath))
{
MCDEBUG("updates", "We have part of the file already, resuming download");
}
m_last_update_length = 0;
m_update_download = tools::download_async(tmppath, url, [this, hash, path](const std::string &tmppath, const std::string &uri, bool success) {
bool remove = false, good = true;
if (success)
{
crypto::hash file_hash;
if (!tools::sha256sum(tmppath, file_hash))
{
MCERROR("updates", "Failed to hash " << tmppath);
remove = true;
good = false;
}
else if (hash != epee::string_tools::pod_to_hex(file_hash))
{
MCERROR("updates", "Download from " << uri << " does not match the expected hash");
remove = true;
good = false;
}
}
else
{
MCERROR("updates", "Failed to download " << uri);
good = false;
}
boost::unique_lock<boost::mutex> lock(m_update_mutex);
m_update_download = 0;
if (success && !remove)
{
std::error_code e = tools::replace_file(tmppath, path.string());
if (e)
{
MCERROR("updates", "Failed to rename downloaded file");
good = false;
}
}
else if (remove)
{
if (!boost::filesystem::remove(tmppath))
{
MCERROR("updates", "Failed to remove invalid downloaded file");
good = false;
}
}
if (good)
MCLOG_CYAN(el::Level::Info, "updates", "New version downloaded to " << path.string());
}, [this](const std::string &path, const std::string &uri, size_t length, ssize_t content_length) {
if (length >= m_last_update_length + 1024 * 1024 * 10)
{
m_last_update_length = length;
MCDEBUG("updates", "Downloaded " << length << "/" << (content_length ? std::to_string(content_length) : "unknown"));
}
return true;
});
}
else
{
MCDEBUG("updates", "We already have " << path << " with expected hash");
}

lock.unlock();

if (check_updates_level == UPDATES_DOWNLOAD)
return true;
<< "Release note: " << notice);

MCERROR("updates", "Download/update not implemented yet");
return true;
}
//-----------------------------------------------------------------------------------------------
Expand Down
9 changes: 1 addition & 8 deletions src/cryptonote_core/cryptonote_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

#include "cryptonote_protocol/cryptonote_protocol_handler_common.h"
#include "storages/portable_storage_template_helper.h"
#include "common/download.h"
#include "common/threadpool.h"
#include "common/command_line.h"
#include "tx_pool.h"
Expand Down Expand Up @@ -985,16 +984,10 @@ namespace cryptonote
enum {
UPDATES_DISABLED,
UPDATES_NOTIFY,
UPDATES_DOWNLOAD,
UPDATES_UPDATE,
} check_updates_level;

tools::download_async_handle m_update_download;
size_t m_last_update_length;
boost::mutex m_update_mutex;

bool m_offline;
bool m_pad_transactions;
bool m_pad_transactions;
};
}

Expand Down
9 changes: 5 additions & 4 deletions src/rpc/core_rpc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2225,15 +2225,15 @@ namespace cryptonote
bool core_rpc_server::on_update(const COMMAND_RPC_UPDATE::request& req, COMMAND_RPC_UPDATE::response& res, const connection_context *ctx)
{
PERF_TIMER(on_update);
static const char software[] = "amity";
static const char software[] = "amity-cli";
#ifdef BUILD_TAG
static const char buildtag[] = BOOST_PP_STRINGIZE(BUILD_TAG);
#else
static const char buildtag[] = "x64";
#endif

std::string version, hash;
if (!tools::check_updates(software, buildtag, version, hash))
std::string version, codename, notice;
if (!tools::check_updates(software, version, codename, notice))
{
res.status = "Error checking for updates";
return true;
Expand All @@ -2246,8 +2246,9 @@ namespace cryptonote
}
res.update = true;
res.version = version;
res.codename = codename;
res.uri = tools::get_update_url(software, buildtag, version);
res.hash = hash;
res.release_note = notice;

res.status = CORE_RPC_STATUS_OK;
return true;
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/core_rpc_server_commands_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2029,6 +2029,8 @@ namespace cryptonote
std::string status;
bool update;
std::string version;
std::string codename;
std::string release_note;
std::string uri;
std::string hash;

Expand Down

0 comments on commit 95b956d

Please sign in to comment.