diff --git a/src/cryptonote_config.h b/src/cryptonote_config.h index 19b6f9ab8..096a012b5 100644 --- a/src/cryptonote_config.h +++ b/src/cryptonote_config.h @@ -98,6 +98,9 @@ #define BLOCKS_SYNCHRONIZING_DEFAULT_COUNT_PRE_V4 100 //by default, blocks count in blocks downloading #define BLOCKS_SYNCHRONIZING_DEFAULT_COUNT 20 //by default, blocks count in blocks downloading #define BLOCKS_SYNCHRONIZING_MAX_COUNT 2048 //must be a power of 2, greater than 128, equal to SEEDHASH_EPOCH_BLOCKS +#define BATCH_MAX_WEIGHT 10 //by default, maximum size of batch in [mB] +#define BATCH_MAX_ALLOWED_WEIGHT 50 //maximum allowed size of batch in [mB] +#define BLOCKS_MAX_WINDOW CRYPTONOTE_REWARD_BLOCKS_WINDOW //Window to find the historical max block weight (100 blocks) #define CRYPTONOTE_MEMPOOL_TX_LIVETIME (86400*3) //seconds, three days #define CRYPTONOTE_MEMPOOL_TX_FROM_ALT_BLOCK_LIVETIME 604800 //seconds, one week diff --git a/src/cryptonote_core/blockchain.h b/src/cryptonote_core/blockchain.h index 03a0d2196..7d34c7739 100644 --- a/src/cryptonote_core/blockchain.h +++ b/src/cryptonote_core/blockchain.h @@ -1471,7 +1471,11 @@ namespace cryptonote * @param weights return-by-reference the list of weights * @param count the number of blocks to get weights for */ + public: void get_last_n_blocks_weights(std::vector& weights, size_t count) const; +#ifndef IN_UNIT_TESTS + private: +#endif /** * @brief gets block long term weight median diff --git a/src/cryptonote_core/cryptonote_core.cpp b/src/cryptonote_core/cryptonote_core.cpp index b6b8eb974..6507a659c 100644 --- a/src/cryptonote_core/cryptonote_core.cpp +++ b/src/cryptonote_core/cryptonote_core.cpp @@ -167,6 +167,11 @@ namespace cryptonote , "How many blocks to sync at once during chain synchronization (0 = adaptive)." , 0 }; + static const command_line::arg_descriptor arg_batch_max_weight = { + "batch-max-weight" + , "How many megabytes to sync in one batch during chain synchronization, default is 10" + , (BATCH_MAX_WEIGHT) + }; static const command_line::arg_descriptor arg_check_updates = { "check-updates" , "Check for new versions of monero: [disabled|notify|download|update]" @@ -322,6 +327,7 @@ namespace cryptonote command_line::add_arg(desc, arg_fast_block_sync); command_line::add_arg(desc, arg_show_time_stats); command_line::add_arg(desc, arg_block_sync_size); + command_line::add_arg(desc, arg_batch_max_weight); command_line::add_arg(desc, arg_check_updates); command_line::add_arg(desc, arg_test_dbg_lock_sleep); command_line::add_arg(desc, arg_offline); @@ -676,6 +682,24 @@ namespace cryptonote if (block_sync_size > BLOCKS_SYNCHRONIZING_MAX_COUNT) MERROR("Error --block-sync-size cannot be greater than " << BLOCKS_SYNCHRONIZING_MAX_COUNT); + if(block_sync_size) + MWARNING("When --block-sync-size defined, the --batch-max-weight is not going to have any effect."); + + batch_max_weight = command_line::get_arg(vm, arg_batch_max_weight); + if (batch_max_weight > BATCH_MAX_ALLOWED_WEIGHT) + { + MERROR("Error --batch-max-weight cannot be greater than " << BATCH_MAX_ALLOWED_WEIGHT << " [mB]"); + batch_max_weight = BATCH_MAX_ALLOWED_WEIGHT; + } + + if (batch_max_weight == 0) + { + MINFO("Using default --batch-max-weight of " << BATCH_MAX_WEIGHT << " [mB]"); + batch_max_weight = BATCH_MAX_WEIGHT; + } + + batch_max_weight *= 1000000; // transfer it to byte. + MGINFO("Loading checkpoints"); // load json & DNS checkpoints, and verify them @@ -928,16 +952,38 @@ namespace cryptonote return true; } //----------------------------------------------------------------------------------------------- - size_t core::get_block_sync_size(uint64_t height) const + size_t core::get_block_sync_size(uint64_t height, const uint64_t max_average_of_blocksize_in_queue) const { - static const uint64_t quick_height = m_nettype == TESTNET ? 801219 : m_nettype == MAINNET ? 1220516 : 0; size_t res = 0; if (block_sync_size > 0) res = block_sync_size; - else if (height >= quick_height) - res = BLOCKS_SYNCHRONIZING_DEFAULT_COUNT; else - res = BLOCKS_SYNCHRONIZING_DEFAULT_COUNT_PRE_V4; + { + size_t number_of_blocks = BLOCKS_MAX_WINDOW; + std::vector last_n_blocks_weights; + m_blockchain_storage.get_last_n_blocks_weights(last_n_blocks_weights, number_of_blocks); + uint64_t max_weight = *std::max_element(last_n_blocks_weights.begin(), last_n_blocks_weights.end()); + MINFO("Max block size seen within the last " << number_of_blocks + << " blocks is " << max_weight + << " bytes and the max average blocksize in the queue is " << max_average_of_blocksize_in_queue << " bytes"); + uint64_t projected_blocksize = std::max(max_average_of_blocksize_in_queue, max_weight); + uint64_t blocks_huge_threshold = (batch_max_weight / 2); + if ((projected_blocksize * BLOCKS_MAX_WINDOW) < batch_max_weight) + { + res = BLOCKS_MAX_WINDOW; + MINFO("blocks are tiny, " << projected_blocksize << " bytes, sync " << res << " blocks in next batch"); + } + else if (projected_blocksize >= blocks_huge_threshold) + { + res = 1; + MINFO("blocks are projected to surpass 50% of " << batch_max_weight << " bytes, syncing just a single block in next batch"); + } + else + { + res = batch_max_weight / projected_blocksize; + MINFO("projected blocksize is " << projected_blocksize << " bytes, sync " << res << " blocks in next batch"); + } + } static size_t max_block_size = 0; if (max_block_size == 0) diff --git a/src/cryptonote_core/cryptonote_core.h b/src/cryptonote_core/cryptonote_core.h index 763b7e190..187ebbab3 100644 --- a/src/cryptonote_core/cryptonote_core.h +++ b/src/cryptonote_core/cryptonote_core.h @@ -794,9 +794,13 @@ namespace cryptonote /** * @brief get the number of blocks to sync in one go * + * @param height the height that we want to get_block_sync_size for + * @param max_average_of_blocksize_in_queue is max average of block size in batches in the queue + * we are downloading in current active connections. + * * @return the number of blocks to sync in one go */ - size_t get_block_sync_size(uint64_t height) const; + size_t get_block_sync_size(uint64_t height, const uint64_t max_average_of_blocksize_in_queue = 0) const; /** * @brief get the sum of coinbase tx amounts between blocks @@ -1098,6 +1102,7 @@ namespace cryptonote bool m_disable_dns_checkpoints; size_t block_sync_size; + std::uint64_t batch_max_weight; time_t start_time; diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.h b/src/cryptonote_protocol/cryptonote_protocol_handler.h index a8ca04141..dbf1c870d 100644 --- a/src/cryptonote_protocol/cryptonote_protocol_handler.h +++ b/src/cryptonote_protocol/cryptonote_protocol_handler.h @@ -50,6 +50,7 @@ #include "net/levin_base.h" #include "p2p/net_node_common.h" #include +#include PUSH_WARNINGS DISABLE_VS_WARNINGS(4355) @@ -112,6 +113,17 @@ namespace cryptonote void log_connections(); std::list get_connections(); const block_queue &get_block_queue() const { return m_block_queue; } + std::uint64_t max_average_of_blocksize_in_queue() + { + std::vector average_blocksize{0}; + m_block_queue.foreach([&](const cryptonote::block_queue::span &span) + { + average_blocksize.push_back(span.size / span.nblocks); + return true; // we don't care about the return value + }); + MINFO("Maximum average of blocksize for current batches : " << *std::max_element(average_blocksize.begin(), average_blocksize.end())); + return *std::max_element(average_blocksize.begin(), average_blocksize.end()); + } void stop(); void on_connection_close(cryptonote_connection_context &context); void set_max_out_peers(epee::net_utils::zone zone, unsigned int max) { CRITICAL_REGION_LOCAL(m_max_out_peers_lock); m_max_out_peers[zone] = max; } diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.inl b/src/cryptonote_protocol/cryptonote_protocol_handler.inl index 71f4387d0..b06791b50 100644 --- a/src/cryptonote_protocol/cryptonote_protocol_handler.inl +++ b/src/cryptonote_protocol/cryptonote_protocol_handler.inl @@ -2118,7 +2118,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()); + size_t l_m_bss = m_core.get_block_sync_size(m_core.get_current_blockchain_height(), max_average_of_blocksize_in_queue()); std::pair span = std::make_pair(0, 0); if (force_next_span) { @@ -2168,7 +2168,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, l_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) { @@ -2254,7 +2254,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 << " / " << l_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 " diff --git a/tests/unit_tests/node_server.cpp b/tests/unit_tests/node_server.cpp index a5bb09ec4..19ae31be7 100644 --- a/tests/unit_tests/node_server.cpp +++ b/tests/unit_tests/node_server.cpp @@ -78,7 +78,7 @@ public: bool check_incoming_block_size(const cryptonote::blobdata& block_blob) const { return true; } bool update_checkpoints(const bool skip_dns = false) { return true; } uint64_t get_target_blockchain_height() const { return 1; } - size_t get_block_sync_size(uint64_t height) const { return BLOCKS_SYNCHRONIZING_DEFAULT_COUNT; } + size_t get_block_sync_size(uint64_t height, const uint64_t max_average_of_blocksize_in_queue = 0) const { return BLOCKS_SYNCHRONIZING_DEFAULT_COUNT; } virtual void on_transactions_relayed(epee::span tx_blobs, cryptonote::relay_method tx_relay) {} cryptonote::network_type get_nettype() const { return cryptonote::MAINNET; } bool get_pool_transaction(const crypto::hash& id, cryptonote::blobdata& tx_blob, cryptonote::relay_category tx_category) const { return false; }