From f2198c9c0c220a35182b62fc8e62fd7f6a3b5ebe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Thu, 17 Oct 2024 17:34:53 +0200 Subject: [PATCH] Activate successors of cemented blocks --- nano/core_test/active_elections.cpp | 4 ++-- nano/node/active_elections.cpp | 20 -------------------- nano/node/active_elections.hpp | 1 - nano/node/node.cpp | 2 +- nano/node/scheduler/component.cpp | 4 ++-- nano/node/scheduler/component.hpp | 2 +- nano/node/scheduler/priority.cpp | 29 ++++++++++++++++++++++++++++- nano/node/scheduler/priority.hpp | 8 +++++--- 8 files changed, 39 insertions(+), 31 deletions(-) diff --git a/nano/core_test/active_elections.cpp b/nano/core_test/active_elections.cpp index 32bc538d..f592ea13 100644 --- a/nano/core_test/active_elections.cpp +++ b/nano/core_test/active_elections.cpp @@ -1232,8 +1232,8 @@ TEST (active_elections, activate_inactive) ASSERT_TIMELY_EQ (5s, 1, node.stats.count (nano::stat::type::confirmation_observer, nano::stat::detail::active_quorum, nano::stat::dir::out)); ASSERT_ALWAYS_EQ (50ms, 0, node.stats.count (nano::stat::type::confirmation_observer, nano::stat::detail::active_conf_height, nano::stat::dir::out)); - // The first block was not active so no activation takes place - ASSERT_FALSE (node.active.active (open->qualified_root ()) || node.block_confirmed_or_being_confirmed (open->hash ())); + // Cementing of send should activate open + ASSERT_TIMELY (5s, node.active.active (open->qualified_root ())); } TEST (active_elections, list_active) diff --git a/nano/node/active_elections.cpp b/nano/node/active_elections.cpp index 861e7e7e..c161038e 100644 --- a/nano/node/active_elections.cpp +++ b/nano/node/active_elections.cpp @@ -123,15 +123,6 @@ void nano::active_elections::block_cemented (nano::secure::transaction const & t nano::log::arg{ "source_election", source_election }); notify_observers (transaction, status, votes); - - bool cemented_bootstrap_count_reached = node.ledger.cemented_count () >= node.ledger.bootstrap_weight_max_blocks; - bool was_active = status.type == nano::election_status_type::active_confirmed_quorum || status.type == nano::election_status_type::active_confirmation_height; - - // Next-block activations are only done for blocks with previously active elections - if (cemented_bootstrap_count_reached && was_active && !node.flags.disable_activate_successors) - { - activate_successors (transaction, block); - } } void nano::active_elections::notify_observers (nano::secure::transaction const & transaction, nano::election_status const & status, std::vector const & votes) const @@ -169,17 +160,6 @@ void nano::active_elections::notify_observers (nano::secure::transaction const & } } -void nano::active_elections::activate_successors (nano::secure::transaction const & transaction, std::shared_ptr const & block) -{ - node.scheduler.priority.activate (transaction, block->account ()); - - // Start or vote for the next unconfirmed block in the destination account - if (block->is_send () && !block->destination ().is_zero () && block->destination () != block->account ()) - { - node.scheduler.priority.activate (transaction, block->destination ()); - } -} - int64_t nano::active_elections::limit (nano::election_behavior behavior) const { switch (behavior) diff --git a/nano/node/active_elections.hpp b/nano/node/active_elections.hpp index e3a1a8b4..049f4142 100644 --- a/nano/node/active_elections.hpp +++ b/nano/node/active_elections.hpp @@ -136,7 +136,6 @@ private: nano::stat::type completion_type (nano::election const & election) const; // Returns a list of elections sorted by difficulty, mutex must be locked std::vector> list_active_impl (std::size_t) const; - void activate_successors (nano::secure::transaction const &, std::shared_ptr const & block); void notify_observers (nano::secure::transaction const &, nano::election_status const & status, std::vector const & votes) const; void block_cemented (nano::secure::transaction const &, std::shared_ptr const & block, nano::block_hash const & confirmation_root, std::shared_ptr const & source_election); diff --git a/nano/node/node.cpp b/nano/node/node.cpp index 86a4fa16..313a1876 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -137,7 +137,7 @@ nano::node::node (std::shared_ptr io_ctx_a, std::filesy generator{ *generator_impl }, final_generator_impl{ std::make_unique (config, *this, ledger, wallets, vote_processor, history, network, stats, logger, /* final */ true) }, final_generator{ *final_generator_impl }, - scheduler_impl{ std::make_unique (config, *this, ledger, block_processor, active, online_reps, vote_cache, stats, logger) }, + scheduler_impl{ std::make_unique (config, *this, ledger, block_processor, active, online_reps, vote_cache, confirming_set, stats, logger) }, scheduler{ *scheduler_impl }, aggregator_impl{ std::make_unique (config.request_aggregator, *this, stats, generator, final_generator, history, ledger, wallets, vote_router) }, aggregator{ *aggregator_impl }, diff --git a/nano/node/scheduler/component.cpp b/nano/node/scheduler/component.cpp index f0352a39..2d8cf563 100644 --- a/nano/node/scheduler/component.cpp +++ b/nano/node/scheduler/component.cpp @@ -5,11 +5,11 @@ #include #include -nano::scheduler::component::component (nano::node_config & node_config, nano::node & node, nano::ledger & ledger, nano::block_processor & block_processor, nano::active_elections & active, nano::online_reps & online_reps, nano::vote_cache & vote_cache, nano::stats & stats, nano::logger & logger) : +nano::scheduler::component::component (nano::node_config & node_config, nano::node & node, nano::ledger & ledger, nano::block_processor & block_processor, nano::active_elections & active, nano::online_reps & online_reps, nano::vote_cache & vote_cache, nano::confirming_set & confirming_set, nano::stats & stats, nano::logger & logger) : hinted_impl{ std::make_unique (node_config.hinted_scheduler, node, vote_cache, active, online_reps, stats) }, manual_impl{ std::make_unique (node) }, optimistic_impl{ std::make_unique (node_config.optimistic_scheduler, node, ledger, active, node_config.network_params.network, stats) }, - priority_impl{ std::make_unique (node_config, node, ledger, block_processor, active, stats, logger) }, + priority_impl{ std::make_unique (node_config, node, ledger, block_processor, active, confirming_set, stats, logger) }, hinted{ *hinted_impl }, manual{ *manual_impl }, optimistic{ *optimistic_impl }, diff --git a/nano/node/scheduler/component.hpp b/nano/node/scheduler/component.hpp index 40a53d0f..96de3d94 100644 --- a/nano/node/scheduler/component.hpp +++ b/nano/node/scheduler/component.hpp @@ -10,7 +10,7 @@ namespace nano::scheduler class component final { public: - component (nano::node_config &, nano::node &, nano::ledger &, nano::block_processor &, nano::active_elections &, nano::online_reps &, nano::vote_cache &, nano::stats &, nano::logger &); + component (nano::node_config &, nano::node &, nano::ledger &, nano::block_processor &, nano::active_elections &, nano::online_reps &, nano::vote_cache &, nano::confirming_set &, nano::stats &, nano::logger &); ~component (); void start (); diff --git a/nano/node/scheduler/priority.cpp b/nano/node/scheduler/priority.cpp index beb360d6..4063ce60 100644 --- a/nano/node/scheduler/priority.cpp +++ b/nano/node/scheduler/priority.cpp @@ -7,12 +7,13 @@ #include #include -nano::scheduler::priority::priority (nano::node_config & node_config, nano::node & node_a, nano::ledger & ledger_a, nano::block_processor & block_processor_a, nano::active_elections & active_a, nano::stats & stats_a, nano::logger & logger_a) : +nano::scheduler::priority::priority (nano::node_config & node_config, nano::node & node_a, nano::ledger & ledger_a, nano::block_processor & block_processor_a, nano::active_elections & active_a, nano::confirming_set & confirming_set_a, nano::stats & stats_a, nano::logger & logger_a) : config{ node_config.priority_scheduler }, node{ node_a }, ledger{ ledger_a }, block_processor{ block_processor_a }, active{ active_a }, + confirming_set{ confirming_set_a }, stats{ stats_a }, logger{ logger_a } { @@ -58,6 +59,21 @@ nano::scheduler::priority::priority (nano::node_config & node_config, nano::node } } }); + + // Activate successors of cemented blocks + confirming_set.batch_cemented.add ([this] (auto const & batch) { + if (node.flags.disable_activate_successors) + { + return; + } + + auto transaction = ledger.tx_begin_read (); + for (auto const & context : batch) + { + release_assert (context.block != nullptr); + activate_successors (transaction, *context.block); + } + }); } nano::scheduler::priority::~priority () @@ -157,6 +173,17 @@ bool nano::scheduler::priority::activate (secure::transaction const & transactio return false; // Not activated } +bool nano::scheduler::priority::activate_successors (secure::transaction const & transaction, nano::block const & block) +{ + bool result = activate (transaction, block.account ()); + // Start or vote for the next unconfirmed block in the destination account + if (block.is_send () && !block.destination ().is_zero () && block.destination () != block.account ()) + { + result |= activate (transaction, block.destination ()); + } + return result; +} + void nano::scheduler::priority::notify () { condition.notify_all (); diff --git a/nano/node/scheduler/priority.hpp b/nano/node/scheduler/priority.hpp index 8cdeac02..0e901de9 100644 --- a/nano/node/scheduler/priority.hpp +++ b/nano/node/scheduler/priority.hpp @@ -26,7 +26,7 @@ public: class priority final { public: - priority (nano::node_config &, nano::node &, nano::ledger &, nano::block_processor &, nano::active_elections &, nano::stats &, nano::logger &); + priority (nano::node_config &, nano::node &, nano::ledger &, nano::block_processor &, nano::active_elections &, nano::confirming_set &, nano::stats &, nano::logger &); ~priority (); void start (); @@ -36,8 +36,9 @@ public: * Activates the first unconfirmed block of \p account_a * @return true if account was activated */ - bool activate (secure::transaction const &, nano::account const &); - bool activate (secure::transaction const &, nano::account const &, nano::account_info const &, nano::confirmation_height_info const &); + bool activate (nano::secure::transaction const &, nano::account const &); + bool activate (nano::secure::transaction const &, nano::account const &, nano::account_info const &, nano::confirmation_height_info const &); + bool activate_successors (nano::secure::transaction const &, nano::block const &); void notify (); std::size_t size () const; @@ -51,6 +52,7 @@ private: // Dependencies nano::ledger & ledger; nano::block_processor & block_processor; nano::active_elections & active; + nano::confirming_set & confirming_set; nano::stats & stats; nano::logger & logger;