diff --git a/nano/node/active_elections.cpp b/nano/node/active_elections.cpp index 134c649d..2f04243f 100644 --- a/nano/node/active_elections.cpp +++ b/nano/node/active_elections.cpp @@ -30,17 +30,17 @@ nano::active_elections::active_elections (nano::node & node_a, nano::confirming_ { count_by_behavior.fill (0); // Zero initialize array - confirming_set.batch_cemented.add ([this] (nano::confirming_set::cemented_notification const & notification) { + confirming_set.batch_cemented.add ([this] (auto const & cemented) { + auto transaction = node.ledger.tx_begin_read (); + for (auto const & [block, confirmation_root] : cemented) { - auto transaction = node.ledger.tx_begin_read (); - for (auto const & [block, confirmation_root] : notification.cemented) - { - transaction.refresh_if_needed (); - - block_cemented_callback (transaction, block, confirmation_root); - } + transaction.refresh_if_needed (); + block_cemented_callback (transaction, block, confirmation_root); } - for (auto const & hash : notification.already_cemented) + }); + + confirming_set.already_cemented.add ([this] (auto const & already_cemented) { + for (auto const & hash : already_cemented) { block_already_cemented_callback (hash); } diff --git a/nano/node/confirming_set.cpp b/nano/node/confirming_set.cpp index da342a4e..1d2aa2fa 100644 --- a/nano/node/confirming_set.cpp +++ b/nano/node/confirming_set.cpp @@ -12,8 +12,8 @@ nano::confirming_set::confirming_set (confirming_set_config const & config_a, na stats{ stats_a }, notification_workers{ 1, nano::thread_role::name::confirmation_height_notifications } { - batch_cemented.add ([this] (auto const & notification) { - for (auto const & [block, confirmation_root] : notification.cemented) + batch_cemented.add ([this] (auto const & cemented) { + for (auto const & [block, confirmation_root] : cemented) { cemented_observers.notify (block); } @@ -124,17 +124,17 @@ void nano::confirming_set::run_batch (std::unique_lock & lock) std::deque cemented; std::deque already; - auto batch = next_batch (256); + auto batch = next_batch (batch_size); lock.unlock (); - auto notify = [this, &cemented, &already] () { - cemented_notification notification{}; - notification.cemented.swap (cemented); - notification.already_cemented.swap (already); + auto notify = [this, &cemented] () { + std::deque batch; + batch.swap (cemented); std::unique_lock lock{ mutex }; + // It's possible that ledger cementing happens faster than the notifications can be processed by other components, cooldown here while (notification_workers.num_queued_tasks () >= config.max_queued_notifications) { stats.inc (nano::stat::type::confirming_set, nano::stat::detail::cooldown); @@ -145,9 +145,9 @@ void nano::confirming_set::run_batch (std::unique_lock & lock) } } - notification_workers.push_task ([this, notification = std::move (notification)] () { + notification_workers.push_task ([this, batch = std::move (batch)] () { stats.inc (nano::stat::type::confirming_set, nano::stat::detail::notify); - batch_cemented.notify (notification); + batch_cemented.notify (batch); }); }; @@ -211,9 +211,9 @@ void nano::confirming_set::run_batch (std::unique_lock & lock) } notify (); - release_assert (cemented.empty ()); - release_assert (already.empty ()); + + already_cemented.notify (already); } nano::container_info nano::confirming_set::container_info () const diff --git a/nano/node/confirming_set.hpp b/nano/node/confirming_set.hpp index e5ef4857..b9e568db 100644 --- a/nano/node/confirming_set.hpp +++ b/nano/node/confirming_set.hpp @@ -48,16 +48,10 @@ public: nano::container_info container_info () const; public: // Events - // Observers will be called once ledger has blocks marked as confirmed using cemented_t = std::pair, nano::block_hash>; // + nano::observer_set const &> batch_cemented; + nano::observer_set const &> already_cemented; - struct cemented_notification - { - std::deque cemented; - std::deque already_cemented; - }; - - nano::observer_set batch_cemented; nano::observer_set> cemented_observers; private: // Dependencies @@ -79,5 +73,7 @@ private: mutable std::mutex mutex; std::condition_variable condition; std::thread thread; + + static size_t constexpr batch_size = 256; }; }