From bbd9ea051a3150637a73f82411a2143d661501e5 Mon Sep 17 00:00:00 2001 From: 0xFFFC0000 <0xFFFC0000@proton.me> Date: Thu, 17 Oct 2024 04:54:20 +0000 Subject: [PATCH] src: dynamic span, to calculate span limit dynamically Co-authored-by: nahuhh --- src/cryptonote_core/cryptonote_core.cpp | 6 +++ src/cryptonote_core/cryptonote_core.h | 1 + .../cryptonote_protocol_handler.h | 4 ++ .../cryptonote_protocol_handler.inl | 43 +++++++++++++++---- 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/cryptonote_core/cryptonote_core.cpp b/src/cryptonote_core/cryptonote_core.cpp index 954dc81e4..83323bbd6 100644 --- a/src/cryptonote_core/cryptonote_core.cpp +++ b/src/cryptonote_core/cryptonote_core.cpp @@ -124,6 +124,11 @@ namespace cryptonote , "Set maximum size of block download queue in bytes (0 for default)" , 0 }; + const command_line::arg_descriptor arg_span_limit = { + "span-limit" + , "Set span limit when syncing, can time (m postfix for minutes), default is 2 minutes" + , 2 + }; const command_line::arg_descriptor arg_sync_pruned_blocks = { "sync-pruned-blocks" , "Allow syncing from nodes with only pruned blocks" @@ -341,6 +346,7 @@ namespace cryptonote command_line::add_arg(desc, arg_offline); command_line::add_arg(desc, arg_disable_dns_checkpoints); command_line::add_arg(desc, arg_block_download_max_size); + command_line::add_arg(desc, arg_span_limit); command_line::add_arg(desc, arg_sync_pruned_blocks); command_line::add_arg(desc, arg_max_txpool_weight); command_line::add_arg(desc, arg_block_notify); diff --git a/src/cryptonote_core/cryptonote_core.h b/src/cryptonote_core/cryptonote_core.h index 2ecb88690..19364ea49 100644 --- a/src/cryptonote_core/cryptonote_core.h +++ b/src/cryptonote_core/cryptonote_core.h @@ -69,6 +69,7 @@ namespace cryptonote extern const command_line::arg_descriptor arg_fixed_difficulty; extern const command_line::arg_descriptor arg_offline; extern const command_line::arg_descriptor arg_block_download_max_size; + extern const command_line::arg_descriptor arg_span_limit; extern const command_line::arg_descriptor arg_sync_pruned_blocks; /************************************************************************/ diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.h b/src/cryptonote_protocol/cryptonote_protocol_handler.h index d7fe40d22..c32fb67af 100644 --- a/src/cryptonote_protocol/cryptonote_protocol_handler.h +++ b/src/cryptonote_protocol/cryptonote_protocol_handler.h @@ -167,6 +167,7 @@ namespace cryptonote size_t skip_unneeded_hashes(cryptonote_connection_context& context, bool check_block_queue) const; bool request_txpool_complement(cryptonote_connection_context &context); void hit_score(cryptonote_connection_context &context, int32_t score); + void calculate_dynamic_span(double blocks_per_seconds); t_core& m_core; @@ -191,6 +192,9 @@ namespace cryptonote uint64_t m_sync_download_chain_size, m_sync_download_objects_size; size_t m_block_download_max_size; bool m_sync_pruned_blocks; + size_t m_span_time; + size_t m_span_limit; + size_t m_bss; // Values for sync time estimates boost::posix_time::ptime m_sync_start_time; diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.inl b/src/cryptonote_protocol/cryptonote_protocol_handler.inl index b4a2fda4f..34822a25d 100644 --- a/src/cryptonote_protocol/cryptonote_protocol_handler.inl +++ b/src/cryptonote_protocol/cryptonote_protocol_handler.inl @@ -62,7 +62,8 @@ #define MLOG_PEER_STATE(x) \ MCINFO(MONERO_DEFAULT_LOG_CATEGORY, context << "[" << epee::string_tools::to_string_hex(context.m_pruning_seed) << "] state: " << x << " in state " << cryptonote::get_protocol_state_string(context.m_state)) -#define BLOCK_QUEUE_NSPANS_THRESHOLD 10 // chunks of N blocks +#define BLOCK_QUEUE_NSPANS_THRESHOLD 200 // chunks of N blocks +#define BLOCK_QUEUE_NSPANS_MINIMUM 10 // minimum number of spans #define BLOCK_QUEUE_SIZE_THRESHOLD (100*1024*1024) // MB #define BLOCK_QUEUE_FORCE_DOWNLOAD_NEAR_BLOCKS 1000 #define REQUEST_NEXT_SCHEDULED_SPAN_THRESHOLD_STANDBY (5 * 1000000) // microseconds @@ -87,7 +88,10 @@ namespace cryptonote m_synchronized(offline), m_ask_for_txpool_complement(true), m_stopping(false), - m_no_sync(false) + m_no_sync(false), + m_span_limit(BLOCK_QUEUE_NSPANS_MINIMUM), + m_span_time(0), + m_bss(0) { if(!m_p2p) @@ -110,11 +114,22 @@ namespace cryptonote m_block_download_max_size = command_line::get_arg(vm, cryptonote::arg_block_download_max_size); m_sync_pruned_blocks = command_line::get_arg(vm, cryptonote::arg_sync_pruned_blocks); + m_span_time = command_line::get_arg(vm, cryptonote::arg_span_limit); return true; } //------------------------------------------------------------------------------------------------------------------------ template + void t_cryptonote_protocol_handler::calculate_dynamic_span(double blocks_per_seconds) + { + MINFO("m_bss : " << m_bss << ", blocks_per_seconds : " << blocks_per_seconds << ", m_span_limit : " << m_span_limit); + m_span_limit = (m_bss && blocks_per_seconds) ? (( blocks_per_seconds * 60 * m_span_time ) / m_bss) : BLOCK_QUEUE_NSPANS_MINIMUM; + if (m_span_limit < BLOCK_QUEUE_NSPANS_MINIMUM) + m_span_limit = BLOCK_QUEUE_NSPANS_MINIMUM; + MINFO("calculated dynamic span limit is span_limit : " << m_span_limit); + } + //------------------------------------------------------------------------------------------------------------------------ + template bool t_cryptonote_protocol_handler::deinit() { return true; @@ -1658,10 +1673,12 @@ namespace cryptonote m_block_queue.remove_spans(span_connection_id, start_height); const uint64_t current_blockchain_height = m_core.get_current_blockchain_height(); + const boost::posix_time::time_duration dt = boost::posix_time::microsec_clock::universal_time() - start; + double blocks_per_seconds = (((current_blockchain_height - previous_height) * 1e6) / dt.total_microseconds()); + calculate_dynamic_span(blocks_per_seconds); if (current_blockchain_height > previous_height) { const uint64_t target_blockchain_height = m_core.get_target_blockchain_height(); - const boost::posix_time::time_duration dt = boost::posix_time::microsec_clock::universal_time() - start; std::string progress_message = ""; if (current_blockchain_height < target_blockchain_height) { @@ -1685,7 +1702,7 @@ namespace cryptonote std::string timing_message = ""; if (ELPP->vRegistry()->allowed(el::Level::Info, "sync-info")) timing_message = std::string(" (") + std::to_string(dt.total_microseconds()/1e6) + " sec, " - + std::to_string((current_blockchain_height - previous_height) * 1e6 / dt.total_microseconds()) + + std::to_string(blocks_per_seconds) + " blocks/sec), " + std::to_string(m_block_queue.get_data_size() / 1048576.f) + " MB queued in " + std::to_string(m_block_queue.get_num_filled_spans()) + " spans, stripe " + std::to_string(previous_stripe) + " -> " + std::to_string(current_stripe); @@ -2104,7 +2121,17 @@ skip: const uint32_t peer_stripe = tools::get_pruning_stripe(context.m_pruning_seed); const uint32_t local_stripe = tools::get_pruning_stripe(m_core.get_blockchain_pruning_seed()); const size_t block_queue_size_threshold = m_block_download_max_size ? m_block_download_max_size : BLOCK_QUEUE_SIZE_THRESHOLD; - bool queue_proceed = nspans < BLOCK_QUEUE_NSPANS_THRESHOLD || size < block_queue_size_threshold; + m_span_limit = m_span_limit ? m_span_limit : BLOCK_QUEUE_NSPANS_THRESHOLD; + bool queue_proceed = (nspans < m_span_limit) && (size < block_queue_size_threshold); + MINFO( "block_queue_size_threshold : " << block_queue_size_threshold + << ", queue_proceed : " << queue_proceed + << ", size : " << size + << ", nspans : " << nspans + << ", m_span_limit : " << m_span_limit + << ", bc_height : " << bc_height + << ", add_stripe : " << add_stripe + << ", peer_stripe : " << peer_stripe + << ", local_stripe : " << local_stripe); // get rid of blocks we already requested, or already have if (skip_unneeded_hashes(context, true) && context.m_needed_objects.empty() && context.m_num_requested == 0) { @@ -2222,7 +2249,7 @@ skip: NOTIFY_REQUEST_GET_OBJECTS::request req; bool is_next = false; size_t count = 0; - const size_t count_limit = m_core.get_block_sync_size(m_core.get_current_blockchain_height()); + m_bss = m_core.get_block_sync_size(m_core.get_current_blockchain_height()); std::pair span = std::make_pair(0, 0); if (force_next_span) { @@ -2272,7 +2299,7 @@ skip: const uint64_t first_block_height = context.m_last_response_height - context.m_needed_objects.size() + 1; static const uint64_t bp_fork_height = m_core.get_earliest_ideal_height_for_version(8); bool sync_pruned_blocks = m_sync_pruned_blocks && first_block_height >= bp_fork_height && m_core.get_blockchain_pruning_seed(); - span = m_block_queue.reserve_span(first_block_height, context.m_last_response_height, count_limit, context.m_connection_id, context.m_remote_address, sync_pruned_blocks, m_core.get_blockchain_pruning_seed(), context.m_pruning_seed, context.m_remote_blockchain_height, context.m_needed_objects); + span = m_block_queue.reserve_span(first_block_height, context.m_last_response_height, m_bss, context.m_connection_id, context.m_remote_address, sync_pruned_blocks, m_core.get_blockchain_pruning_seed(), context.m_pruning_seed, context.m_remote_blockchain_height, context.m_needed_objects); MDEBUG(context << " span from " << first_block_height << ": " << span.first << "/" << span.second); if (span.second > 0) { @@ -2358,7 +2385,7 @@ skip: context.m_expect_height = span.first; context.m_expect_response = NOTIFY_RESPONSE_GET_OBJECTS::ID; MLOG_P2P_MESSAGE("-->>NOTIFY_REQUEST_GET_OBJECTS: blocks.size()=" << req.blocks.size() - << "requested blocks count=" << count << " / " << count_limit << " from " << span.first << ", first hash " << req.blocks.front()); + << "requested blocks count=" << count << " / " << m_bss << " from " << span.first << ", first hash " << req.blocks.front()); //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()); MDEBUG("Asking for " << (req.prune ? "pruned" : "full") << " data, start/end "