From 7b747607566aa62460a4b4a278f2f8eb86f18bc2 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Wed, 16 Aug 2017 19:27:16 +0100 Subject: [PATCH] cryptonote_protocol: kick idle synchronizing peers In case they dropped off downloading for any reason, they'll get sent to download again. --- src/cryptonote_basic/connection_context.h | 3 +++ src/cryptonote_protocol/block_queue.cpp | 10 +++++++ src/cryptonote_protocol/block_queue.h | 1 + .../cryptonote_protocol_handler.h | 3 +++ .../cryptonote_protocol_handler.inl | 27 +++++++++++++++++++ 5 files changed, 44 insertions(+) diff --git a/src/cryptonote_basic/connection_context.h b/src/cryptonote_basic/connection_context.h index 06f66120..28bd7391 100644 --- a/src/cryptonote_basic/connection_context.h +++ b/src/cryptonote_basic/connection_context.h @@ -46,6 +46,7 @@ namespace cryptonote { state_befor_handshake = 0, //default state state_synchronizing, + state_standby, state_idle, state_normal }; @@ -69,6 +70,8 @@ namespace cryptonote return "state_befor_handshake"; case cryptonote_connection_context::state_synchronizing: return "state_synchronizing"; + case cryptonote_connection_context::state_standby: + return "state_standby"; case cryptonote_connection_context::state_idle: return "state_idle"; case cryptonote_connection_context::state_normal: diff --git a/src/cryptonote_protocol/block_queue.cpp b/src/cryptonote_protocol/block_queue.cpp index 4f760582..94d31404 100644 --- a/src/cryptonote_protocol/block_queue.cpp +++ b/src/cryptonote_protocol/block_queue.cpp @@ -334,6 +334,16 @@ crypto::hash block_queue::get_last_known_hash(const boost::uuids::uuid &connecti return hash; } +bool block_queue::has_spans(const boost::uuids::uuid &connection_id) const +{ + for (const auto &span: blocks) + { + if (span.connection_id == connection_id) + return true; + } + return false; +} + float block_queue::get_speed(const boost::uuids::uuid &connection_id) const { boost::unique_lock lock(mutex); diff --git a/src/cryptonote_protocol/block_queue.h b/src/cryptonote_protocol/block_queue.h index 9a211ac4..fa1a0f21 100644 --- a/src/cryptonote_protocol/block_queue.h +++ b/src/cryptonote_protocol/block_queue.h @@ -86,6 +86,7 @@ namespace cryptonote size_t get_num_filled_spans_prefix() const; size_t get_num_filled_spans() const; crypto::hash get_last_known_hash(const boost::uuids::uuid &connection_id) const; + bool has_spans(const boost::uuids::uuid &connection_id) const; float get_speed(const boost::uuids::uuid &connection_id) const; bool foreach(std::function f, bool include_blockchain_placeholder = false) const; bool requested(const crypto::hash &hash) const; diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.h b/src/cryptonote_protocol/cryptonote_protocol_handler.h index f30aebf3..d9474776 100644 --- a/src/cryptonote_protocol/cryptonote_protocol_handler.h +++ b/src/cryptonote_protocol/cryptonote_protocol_handler.h @@ -38,6 +38,7 @@ #include #include +#include "math_helper.h" #include "storages/levin_abstract_invoke2.h" #include "warnings.h" #include "cryptonote_protocol_defs.h" @@ -131,6 +132,7 @@ namespace cryptonote bool on_connection_synchronized(); bool should_download_next_span(cryptonote_connection_context& context) const; void drop_connection(cryptonote_connection_context &context, bool add_fail, bool flush_all_spans); + bool kick_idle_peers(); t_core& m_core; @@ -141,6 +143,7 @@ namespace cryptonote std::atomic m_stopping; boost::mutex m_sync_lock; block_queue m_block_queue; + epee::math_helper::once_a_time_seconds<30> m_idle_peer_kicker; boost::mutex m_buffer_mutex; double get_avg_block_size(); diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.inl b/src/cryptonote_protocol/cryptonote_protocol_handler.inl index 5b3b059a..c33670a0 100644 --- a/src/cryptonote_protocol/cryptonote_protocol_handler.inl +++ b/src/cryptonote_protocol/cryptonote_protocol_handler.inl @@ -51,6 +51,7 @@ #define BLOCK_QUEUE_NBLOCKS_THRESHOLD 10 // chunks of N blocks #define BLOCK_QUEUE_SIZE_THRESHOLD (100*1024*1024) // MB #define REQUEST_NEXT_SCHEDULED_SPAN_THRESHOLD (5 * 1000000) // microseconds +#define IDLE_PEER_KICK_TIME (45 * 1000000) // microseconds namespace cryptonote { @@ -1129,10 +1130,33 @@ skip: template bool t_cryptonote_protocol_handler::on_idle() { + m_idle_peer_kicker.do_call(boost::bind(&t_cryptonote_protocol_handler::kick_idle_peers, this)); return m_core.on_idle(); } //------------------------------------------------------------------------------------------------------------------------ template + bool t_cryptonote_protocol_handler::kick_idle_peers() + { + MTRACE("Checking for idle peers..."); + m_p2p->for_each_connection([&](cryptonote_connection_context& context, nodetool::peerid_type peer_id, uint32_t support_flags)->bool + { + if (context.m_state == cryptonote_connection_context::state_synchronizing) + { + const boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time(); + const boost::posix_time::time_duration dt = now - context.m_last_request_time; + if (dt.total_microseconds() > IDLE_PEER_KICK_TIME) + { + MINFO(context << " kicking idle peer"); + ++context.m_callback_request_count; + m_p2p->request_callback(context); + } + } + return true; + }); + return true; + } + //------------------------------------------------------------------------------------------------------------------------ + template int t_cryptonote_protocol_handler::handle_request_chain(int command, NOTIFY_REQUEST_CHAIN::request& arg, cryptonote_connection_context& context) { MLOG_P2P_MESSAGE("Received NOTIFY_REQUEST_CHAIN (" << arg.block_ids.size() << " blocks"); @@ -1243,6 +1267,7 @@ skip: { LOG_DEBUG_CC(context, "Block queue is " << nblocks << " and " << size << ", pausing"); first = false; + context.m_state = cryptonote_connection_context::state_standby; } for (size_t n = 0; n < 50; ++n) { @@ -1251,6 +1276,7 @@ skip: boost::this_thread::sleep_for(boost::chrono::milliseconds(100)); } } + context.m_state = cryptonote_connection_context::state_synchronizing; } MDEBUG(context << " request_missing_objects: check " << check_having_blocks << ", force_next_span " << force_next_span << ", m_needed_objects " << context.m_needed_objects.size() << " lrh " << context.m_last_response_height << ", chain " << m_core.get_current_blockchain_height()); @@ -1402,6 +1428,7 @@ skip: //epee::net_utils::network_throttle_manager::get_global_throttle_inreq().logger_handle_net("log/dr-monero/net/req-all.data", sec, get_avg_block_size()); //LOG_PRINT_CCONTEXT_L1("r = " << 200); + context.m_last_request_time = boost::posix_time::microsec_clock::universal_time(); LOG_PRINT_CCONTEXT_L1("-->>NOTIFY_REQUEST_CHAIN: m_block_ids.size()=" << r.block_ids.size() << ", start_from_current_chain " << start_from_current_chain); post_notify(r, context); }else