core: make update download cancellable

This commit is contained in:
moneromooo-monero 2017-02-26 21:00:38 +00:00
parent 63f0e074eb
commit f6211322e5
No known key found for this signature in database
GPG key ID: 686F07454D6CEFC3
2 changed files with 55 additions and 17 deletions

View file

@ -75,7 +75,8 @@ namespace cryptonote
m_target_blockchain_height(0), m_target_blockchain_height(0),
m_checkpoints_path(""), m_checkpoints_path(""),
m_last_dns_checkpoints_update(0), m_last_dns_checkpoints_update(0),
m_last_json_checkpoints_update(0) m_last_json_checkpoints_update(0),
m_update_download(0)
{ {
set_cryptonote_protocol(pprotocol); set_cryptonote_protocol(pprotocol);
} }
@ -134,6 +135,15 @@ namespace cryptonote
void core::stop() void core::stop()
{ {
m_blockchain_storage.cancel(); 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) void core::init_options(boost::program_options::options_description& desc)
@ -1133,32 +1143,55 @@ namespace cryptonote
boost::filesystem::path path(epee::string_tools::get_current_module_folder()); boost::filesystem::path path(epee::string_tools::get_current_module_folder());
path /= filename; 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; crypto::hash file_hash;
if (!tools::sha256sum(path.string(), file_hash) || (hash != epee::string_tools::pod_to_hex(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"); MCDEBUG("updates", "We don't have that file already, downloading");
if (!tools::download(path.string(), url)) m_last_update_length = 0;
{ m_update_download = tools::download_async(path.string(), url, [this, hash](const std::string &path, const std::string &uri, bool success) {
MCERROR("updates", "Failed to download " << url); if (success)
return false; {
} crypto::hash file_hash;
if (!tools::sha256sum(path.string(), file_hash)) if (!tools::sha256sum(path, file_hash))
{ {
MCERROR("updates", "Failed to hash " << path); MCERROR("updates", "Failed to hash " << path);
return false; }
} if (hash != epee::string_tools::pod_to_hex(file_hash))
if (hash != epee::string_tools::pod_to_hex(file_hash)) {
{ MCERROR("updates", "Download from " << uri << " does not match the expected hash");
MCERROR("updates", "Download from " << url << " does not match the expected hash"); }
return false; MGINFO("New version downloaded to " << path);
} }
MGINFO("New version downloaded to " << path); else
{
MCERROR("updates", "Failed to download " << uri);
}
boost::unique_lock<boost::mutex> lock(m_update_mutex);
m_update_download = 0;
}, [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 else
{ {
MCDEBUG("updates", "We already have " << path << " with expected hash"); MCDEBUG("updates", "We already have " << path << " with expected hash");
} }
lock.unlock();
if (check_updates_level == UPDATES_DOWNLOAD) if (check_updates_level == UPDATES_DOWNLOAD)
return true; return true;

View file

@ -39,6 +39,7 @@
#include "p2p/net_node_common.h" #include "p2p/net_node_common.h"
#include "cryptonote_protocol/cryptonote_protocol_handler_common.h" #include "cryptonote_protocol/cryptonote_protocol_handler_common.h"
#include "storages/portable_storage_template_helper.h" #include "storages/portable_storage_template_helper.h"
#include "common/download.h"
#include "tx_pool.h" #include "tx_pool.h"
#include "blockchain.h" #include "blockchain.h"
#include "cryptonote_basic/miner.h" #include "cryptonote_basic/miner.h"
@ -844,6 +845,10 @@ namespace cryptonote
UPDATES_DOWNLOAD, UPDATES_DOWNLOAD,
UPDATES_UPDATE, UPDATES_UPDATE,
} check_updates_level; } check_updates_level;
tools::download_async_handle m_update_download;
size_t m_last_update_length;
boost::mutex m_update_mutex;
}; };
} }