From 8a15dcf3c9bcf4d22646e6ce9a8cd9772250423d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Sun, 12 Nov 2023 15:42:40 +0100 Subject: [PATCH] Remove `block_arrival` --- nano/core_test/node.cpp | 47 --------------------------------- nano/lib/locks.cpp | 2 -- nano/lib/locks.hpp | 1 - nano/node/CMakeLists.txt | 2 -- nano/node/block_arrival.cpp | 35 ------------------------- nano/node/block_arrival.hpp | 49 ----------------------------------- nano/node/block_broadcast.cpp | 10 +++---- nano/node/block_broadcast.hpp | 10 +++---- nano/node/blockprocessor.cpp | 13 ++++++++++ nano/node/blockprocessor.hpp | 4 +++ nano/node/json_handler.cpp | 1 - nano/node/node.cpp | 13 +++------- nano/node/node.hpp | 2 -- nano/rpc_test/rpc.cpp | 1 - 14 files changed, 29 insertions(+), 161 deletions(-) delete mode 100644 nano/node/block_arrival.cpp delete mode 100644 nano/node/block_arrival.hpp diff --git a/nano/core_test/node.cpp b/nano/core_test/node.cpp index 34f6b1b24b..e8296c6e47 100644 --- a/nano/core_test/node.cpp +++ b/nano/core_test/node.cpp @@ -2136,53 +2136,6 @@ TEST (node, block_confirm) ASSERT_TIMELY (10s, node1.active.recently_cemented.list ().size () == 1); } -TEST (node, block_arrival) -{ - nano::test::system system (1); - auto & node (*system.nodes[0]); - ASSERT_EQ (0, node.block_arrival.arrival.size ()); - nano::block_hash hash1 (1); - node.block_arrival.add (hash1); - ASSERT_EQ (1, node.block_arrival.arrival.size ()); - node.block_arrival.add (hash1); - ASSERT_EQ (1, node.block_arrival.arrival.size ()); - nano::block_hash hash2 (2); - node.block_arrival.add (hash2); - ASSERT_EQ (2, node.block_arrival.arrival.size ()); -} - -TEST (node, block_arrival_size) -{ - nano::test::system system (1); - auto & node (*system.nodes[0]); - auto time (std::chrono::steady_clock::now () - nano::block_arrival::arrival_time_min - std::chrono::seconds (5)); - nano::block_hash hash (0); - for (auto i (0); i < nano::block_arrival::arrival_size_min * 2; ++i) - { - node.block_arrival.arrival.push_back (nano::block_arrival_info{ time, hash }); - ++hash.qwords[0]; - } - ASSERT_EQ (nano::block_arrival::arrival_size_min * 2, node.block_arrival.arrival.size ()); - node.block_arrival.recent (0); - ASSERT_EQ (nano::block_arrival::arrival_size_min, node.block_arrival.arrival.size ()); -} - -TEST (node, block_arrival_time) -{ - nano::test::system system (1); - auto & node (*system.nodes[0]); - auto time (std::chrono::steady_clock::now ()); - nano::block_hash hash (0); - for (auto i (0); i < nano::block_arrival::arrival_size_min * 2; ++i) - { - node.block_arrival.arrival.push_back (nano::block_arrival_info{ time, hash }); - ++hash.qwords[0]; - } - ASSERT_EQ (nano::block_arrival::arrival_size_min * 2, node.block_arrival.arrival.size ()); - node.block_arrival.recent (0); - ASSERT_EQ (nano::block_arrival::arrival_size_min * 2, node.block_arrival.arrival.size ()); -} - TEST (node, confirm_quorum) { nano::test::system system (1); diff --git a/nano/lib/locks.cpp b/nano/lib/locks.cpp index 56a018b13d..f40e59daa1 100644 --- a/nano/lib/locks.cpp +++ b/nano/lib/locks.cpp @@ -250,8 +250,6 @@ char const * nano::mutex_identifier (mutexes mutex) { case mutexes::active: return "active"; - case mutexes::block_arrival: - return "block_arrival"; case mutexes::block_processor: return "block_processor"; case mutexes::block_uniquer: diff --git a/nano/lib/locks.hpp b/nano/lib/locks.hpp index cb490e455c..4a52cee4c8 100644 --- a/nano/lib/locks.hpp +++ b/nano/lib/locks.hpp @@ -20,7 +20,6 @@ bool any_filters_registered (); enum class mutexes { active, - block_arrival, block_processor, block_uniquer, blockstore_cache, diff --git a/nano/node/CMakeLists.txt b/nano/node/CMakeLists.txt index aabdf2a273..aec1950014 100644 --- a/nano/node/CMakeLists.txt +++ b/nano/node/CMakeLists.txt @@ -20,8 +20,6 @@ add_library( backlog_population.cpp bandwidth_limiter.hpp bandwidth_limiter.cpp - block_arrival.hpp - block_arrival.cpp block_broadcast.cpp block_broadcast.hpp blocking_observer.cpp diff --git a/nano/node/block_arrival.cpp b/nano/node/block_arrival.cpp deleted file mode 100644 index 914e18dcc0..0000000000 --- a/nano/node/block_arrival.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include - -bool nano::block_arrival::add (nano::block_hash const & hash_a) -{ - nano::lock_guard lock{ mutex }; - auto now (std::chrono::steady_clock::now ()); - auto inserted (arrival.get ().emplace_back (nano::block_arrival_info{ now, hash_a })); - auto result (!inserted.second); - return result; -} - -bool nano::block_arrival::recent (nano::block_hash const & hash_a) -{ - nano::lock_guard lock{ mutex }; - auto now (std::chrono::steady_clock::now ()); - while (arrival.size () > arrival_size_min && arrival.get ().front ().arrival + arrival_time_min < now) - { - arrival.get ().pop_front (); - } - return arrival.get ().find (hash_a) != arrival.get ().end (); -} - -std::unique_ptr nano::collect_container_info (block_arrival & block_arrival, std::string const & name) -{ - std::size_t count = 0; - { - nano::lock_guard guard{ block_arrival.mutex }; - count = block_arrival.arrival.size (); - } - - auto sizeof_element = sizeof (decltype (block_arrival.arrival)::value_type); - auto composite = std::make_unique (name); - composite->add_component (std::make_unique (container_info{ "arrival", count, sizeof_element })); - return composite; -} \ No newline at end of file diff --git a/nano/node/block_arrival.hpp b/nano/node/block_arrival.hpp deleted file mode 100644 index 71aa394632..0000000000 --- a/nano/node/block_arrival.hpp +++ /dev/null @@ -1,49 +0,0 @@ -#pragma once - -#include - -#include -#include -#include -#include - -#include - -namespace nano -{ -class block_arrival_info final -{ -public: - std::chrono::steady_clock::time_point arrival; - nano::block_hash hash; -}; - -// This class tracks blocks that are probably live because they arrived in a UDP packet -// This gives a fairly reliable way to differentiate between blocks being inserted via bootstrap or new, live blocks. -class block_arrival final -{ -public: - // Return `true' to indicated an error if the block has already been inserted - bool add (nano::block_hash const &); - bool recent (nano::block_hash const &); - - // clang-format off - class tag_sequence {}; - class tag_hash {}; - - boost::multi_index_container>, - boost::multi_index::hashed_unique, - boost::multi_index::member>>> - arrival; - // clang-format on - - nano::mutex mutex{ mutex_identifier (mutexes::block_arrival) }; - - static std::size_t constexpr arrival_size_min = 8 * 1024; - static std::chrono::seconds constexpr arrival_time_min = std::chrono::seconds (300); -}; - -std::unique_ptr collect_container_info (block_arrival & block_arrival, std::string const & name); -} \ No newline at end of file diff --git a/nano/node/block_broadcast.cpp b/nano/node/block_broadcast.cpp index 6c41020f06..55d5e2a4cc 100644 --- a/nano/node/block_broadcast.cpp +++ b/nano/node/block_broadcast.cpp @@ -1,14 +1,12 @@ #include #include -#include #include #include #include -nano::block_broadcast::block_broadcast (nano::block_processor & block_processor_a, nano::network & network_a, nano::block_arrival & block_arrival_a, nano::stats & stats_a, bool enabled_a) : +nano::block_broadcast::block_broadcast (nano::block_processor & block_processor_a, nano::network & network_a, nano::stats & stats_a, bool enabled_a) : block_processor{ block_processor_a }, network{ network_a }, - block_arrival{ block_arrival_a }, stats{ stats_a }, enabled{ enabled_a }, queue{ stats_a, nano::stat::type::block_broadcaster, nano::thread_role::name::block_broadcasting, /* single thread */ 1, max_size } @@ -22,7 +20,7 @@ nano::block_broadcast::block_broadcast (nano::block_processor & block_processor_ switch (result.code) { case nano::process_result::progress: - observe (block); + observe (block, context); break; default: break; @@ -54,7 +52,7 @@ void nano::block_broadcast::stop () queue.stop (); } -void nano::block_broadcast::observe (std::shared_ptr const & block) +void nano::block_broadcast::observe (std::shared_ptr const & block, nano::block_processor::context const & context) { bool is_local = local.contains (block->hash ()); if (is_local) @@ -65,7 +63,7 @@ void nano::block_broadcast::observe (std::shared_ptr const & block) } else { - if (block_arrival.recent (block->hash ())) + if (context.recent_arrival ()) { // Block arrived from realtime traffic, do normal gossip. queue.add (entry{ block, broadcast_strategy::normal }); diff --git a/nano/node/block_broadcast.hpp b/nano/node/block_broadcast.hpp index 23db1d184e..5f8182dc47 100644 --- a/nano/node/block_broadcast.hpp +++ b/nano/node/block_broadcast.hpp @@ -3,9 +3,12 @@ #include #include #include +#include +#include #include #include +#include #include #include @@ -16,8 +19,6 @@ namespace mi = boost::multi_index; namespace nano { -class block_arrival; -class block_processor; class network; } @@ -39,7 +40,7 @@ class block_broadcast using queue_t = nano::processing_queue; public: - block_broadcast (nano::block_processor &, nano::network &, nano::block_arrival &, nano::stats &, bool enabled = false); + block_broadcast (nano::block_processor &, nano::network &, nano::stats &, bool enabled = false); ~block_broadcast (); void start (); @@ -51,12 +52,11 @@ class block_broadcast private: // Dependencies nano::block_processor & block_processor; nano::network & network; - nano::block_arrival & block_arrival; nano::stats & stats; private: // Block_processor observer - void observe (std::shared_ptr const & block); + void observe (std::shared_ptr const & block, nano::block_processor::context const & context); void process_batch (queue_t::batch_t & batch); private: diff --git a/nano/node/blockprocessor.cpp b/nano/node/blockprocessor.cpp index be7b420f0f..53c6857dbd 100644 --- a/nano/node/blockprocessor.cpp +++ b/nano/node/blockprocessor.cpp @@ -6,6 +6,10 @@ #include +/* + * block_processor + */ + nano::block_processor::block_processor (nano::node & node_a, nano::write_database_queue & write_database_queue_a) : next_log (std::chrono::steady_clock::now ()), node (node_a), @@ -443,3 +447,12 @@ std::unique_ptr nano::collect_container_info (bl composite->add_component (std::make_unique (container_info{ "forced", forced_count, sizeof (decltype (block_processor.forced)::value_type) })); return composite; } + +/* + * context + */ + +bool nano::block_processor::context::recent_arrival () const +{ + return std::chrono::steady_clock::now () < arrival + recent_arrival_cutoff; +} \ No newline at end of file diff --git a/nano/node/blockprocessor.hpp b/nano/node/blockprocessor.hpp index 5874abebfe..a7aebd0c2c 100644 --- a/nano/node/blockprocessor.hpp +++ b/nano/node/blockprocessor.hpp @@ -39,6 +39,10 @@ class block_processor final { block_source source; std::chrono::steady_clock::time_point arrival; + + public: + bool recent_arrival () const; + static std::chrono::seconds constexpr recent_arrival_cutoff{ 60 * 5 }; }; using entry_t = std::pair, context>; diff --git a/nano/node/json_handler.cpp b/nano/node/json_handler.cpp index 3e6683da74..626b75b0fa 100644 --- a/nano/node/json_handler.cpp +++ b/nano/node/json_handler.cpp @@ -1214,7 +1214,6 @@ void nano::json_handler::block_confirm () nano::election_status status{ block_l, 0, 0, std::chrono::duration_cast (std::chrono::system_clock::now ().time_since_epoch ()), std::chrono::duration_values::zero (), 0, 1, 0, nano::election_status_type::active_confirmation_height }; node.active.recently_cemented.put (status); // Trigger callback for confirmed block - node.block_arrival.add (hash); auto account (node.ledger.account (transaction, hash)); bool error_or_pruned (false); auto amount (node.ledger.amount_safe (transaction, hash, error_or_pruned)); diff --git a/nano/node/node.cpp b/nano/node/node.cpp index 37f0a5b00c..adf9753d52 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -195,7 +195,7 @@ nano::node::node (boost::asio::io_context & io_ctx_a, std::filesystem::path cons epoch_upgrader{ *this, ledger, store, network_params, logger }, startup_time (std::chrono::steady_clock::now ()), node_seq (seq), - block_broadcast{ block_processor, network, block_arrival, stats, !flags.disable_block_processor_republishing }, + block_broadcast{ block_processor, network, stats, !flags.disable_block_processor_republishing }, process_live_dispatcher{ ledger, scheduler.priority, vote_cache, websocket } { process_live_dispatcher.connect (block_processor); @@ -236,7 +236,7 @@ nano::node::node (boost::asio::io_context & io_ctx_a, std::filesystem::path cons { observers.blocks.add ([this] (nano::election_status const & status_a, std::vector const & votes_a, nano::account const & account_a, nano::amount const & amount_a, bool is_state_send_a, bool is_state_epoch_a) { auto block_a (status_a.winner); - if ((status_a.type == nano::election_status_type::active_confirmed_quorum || status_a.type == nano::election_status_type::active_confirmation_height) && this->block_arrival.recent (block_a->hash ())) + if ((status_a.type == nano::election_status_type::active_confirmed_quorum || status_a.type == nano::election_status_type::active_confirmation_height)) { auto node_l (shared_from_this ()); background ([node_l, block_a, account_a, amount_a, is_state_send_a, is_state_epoch_a] () { @@ -555,7 +555,6 @@ std::unique_ptr nano::collect_container_info (no composite->add_component (collect_container_info (node.vote_processor, "vote_processor")); composite->add_component (collect_container_info (node.rep_crawler, "rep_crawler")); composite->add_component (collect_container_info (node.block_processor, "block_processor")); - composite->add_component (collect_container_info (node.block_arrival, "block_arrival")); composite->add_component (collect_container_info (node.online_reps, "online_reps")); composite->add_component (collect_container_info (node.history, "history")); composite->add_component (node.block_uniquer.collect_container_info ("block_uniquer")); @@ -574,7 +573,6 @@ std::unique_ptr nano::collect_container_info (no void nano::node::process_active (std::shared_ptr const & incoming) { - block_arrival.add (incoming->hash ()); block_processor.add (incoming); } @@ -591,18 +589,13 @@ nano::process_return nano::node::process (nano::block & block) std::optional nano::node::process_local (std::shared_ptr const & block_a) { - // Add block hash as recently arrived to trigger automatic rebroadcast and election - block_arrival.add (block_a->hash ()); block_broadcast.track_local (block_a->hash ()); return block_processor.add_blocking (block_a); } void nano::node::process_local_async (std::shared_ptr const & block_a) { - // Add block hash as recently arrived to trigger automatic rebroadcast and election - block_arrival.add (block_a->hash ()); - // Set current time to trigger automatic rebroadcast and election - block_processor.add (block_a); + block_processor.add (block_a, nano::block_processor::block_source::local); } void nano::node::start () diff --git a/nano/node/node.hpp b/nano/node/node.hpp index 6886e93ea8..55e588537a 100644 --- a/nano/node/node.hpp +++ b/nano/node/node.hpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -168,7 +167,6 @@ class node final : public std::enable_shared_from_this nano::vote_processor vote_processor; unsigned warmed_up; nano::block_processor block_processor; - nano::block_arrival block_arrival; nano::local_vote_history history; nano::keypair node_id; nano::block_uniquer block_uniquer; diff --git a/nano/rpc_test/rpc.cpp b/nano/rpc_test/rpc.cpp index f57569ac06..cfb14483ba 100644 --- a/nano/rpc_test/rpc.cpp +++ b/nano/rpc_test/rpc.cpp @@ -4429,7 +4429,6 @@ TEST (rpc, populate_backlog) .work (*node->work_generate_blocking (latest)) .build (); ASSERT_EQ (nano::process_result::progress, node->process (*send).code); - ASSERT_FALSE (node->block_arrival.recent (send->hash ())); auto const rpc_ctx = add_rpc (system, node); boost::property_tree::ptree request;