From d834d933714201a9eb0b46638de35dbe7e73520b 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:03:48 +0200 Subject: [PATCH] Use `vacancy_updated` event --- nano/core_test/active_elections.cpp | 4 +++- nano/node/active_elections.cpp | 7 +++---- nano/node/active_elections.hpp | 5 ++++- nano/node/node.cpp | 7 ------- nano/node/scheduler/component.cpp | 6 ++++++ 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/nano/core_test/active_elections.cpp b/nano/core_test/active_elections.cpp index 9bd1d6c2..32bc538d 100644 --- a/nano/core_test/active_elections.cpp +++ b/nano/core_test/active_elections.cpp @@ -1308,7 +1308,9 @@ TEST (active_elections, vacancy) .sign (nano::dev::genesis_key.prv, nano::dev::genesis_key.pub) .work (*system.work.generate (nano::dev::genesis->hash ())) .build (); - node.active.vacancy_update = [&updated] () { updated = true; }; + node.active.vacancy_updated.add ([&updated] () { + updated = true; + }); ASSERT_EQ (nano::block_status::progress, node.process (send)); ASSERT_EQ (1, node.active.vacancy (nano::election_behavior::priority)); ASSERT_EQ (0, node.active.size ()); diff --git a/nano/node/active_elections.cpp b/nano/node/active_elections.cpp index 0cc4953f..861e7e7e 100644 --- a/nano/node/active_elections.cpp +++ b/nano/node/active_elections.cpp @@ -312,7 +312,7 @@ void nano::active_elections::cleanup_election (nano::unique_lock & entry.erased_callback (election); } - vacancy_update (); + vacancy_updated.notify (); for (auto const & [hash, block] : blocks_l) { @@ -435,7 +435,7 @@ nano::election_insertion_result nano::active_elections::insert (std::shared_ptr< node.vote_cache_processor.trigger (hash); node.observers.active_started.notify (hash); - vacancy_update (); + vacancy_updated.notify (); } // Votes are generated for inserted or ongoing elections @@ -541,8 +541,7 @@ void nano::active_elections::clear () nano::lock_guard guard{ mutex }; roots.clear (); } - - vacancy_update (); + vacancy_updated.notify (); } nano::container_info nano::active_elections::container_info () const diff --git a/nano/node/active_elections.hpp b/nano/node/active_elections.hpp index 1a76cf04..e3a1a8b4 100644 --- a/nano/node/active_elections.hpp +++ b/nano/node/active_elections.hpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -121,10 +122,12 @@ public: * How many election slots are available for specified election type */ int64_t vacancy (nano::election_behavior behavior) const; - std::function vacancy_update{ [] () {} }; nano::container_info container_info () const; +public: // Events + nano::observer_set<> vacancy_updated; + private: void request_loop (); void request_confirm (nano::unique_lock &); diff --git a/nano/node/node.cpp b/nano/node/node.cpp index 991b5c9c..f2f22608 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -187,13 +187,6 @@ nano::node::node (std::shared_ptr io_ctx_a, std::filesy if (!init_error ()) { - // Notify election schedulers when AEC frees election slot - active.vacancy_update = [this] () { - scheduler.priority.notify (); - scheduler.hinted.notify (); - scheduler.optimistic.notify (); - }; - wallets.observer = [this] (bool active) { observers.wallet.notify (active); }; diff --git a/nano/node/scheduler/component.cpp b/nano/node/scheduler/component.cpp index 97c5926d..560594b0 100644 --- a/nano/node/scheduler/component.cpp +++ b/nano/node/scheduler/component.cpp @@ -15,6 +15,12 @@ nano::scheduler::component::component (nano::node_config & node_config, nano::no optimistic{ *optimistic_impl }, priority{ *priority_impl } { + // Notify election schedulers when AEC frees election slot + active.vacancy_updated.add ([this] () { + priority.notify (); + hinted.notify (); + optimistic.notify (); + }); } nano::scheduler::component::~component ()