From 06e3d71413df36594b9fa9718d925b566fa57e39 Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Thu, 21 Mar 2024 16:26:35 +0000 Subject: [PATCH] Rewrite priority scheduler in terms of ledger sets. --- nano/node/scheduler/priority.cpp | 58 ++++++++++++++------------------ 1 file changed, 25 insertions(+), 33 deletions(-) diff --git a/nano/node/scheduler/priority.cpp b/nano/node/scheduler/priority.cpp index 54c80adee1..9e970c6876 100644 --- a/nano/node/scheduler/priority.cpp +++ b/nano/node/scheduler/priority.cpp @@ -41,42 +41,34 @@ void nano::scheduler::priority::stop () nano::join_or_pass (thread); } -bool nano::scheduler::priority::activate (nano::account const & account_a, store::transaction const & transaction) +bool nano::scheduler::priority::activate (nano::account const & account, store::transaction const & transaction) { - debug_assert (!account_a.is_zero ()); - auto info = node.ledger.any.get (transaction, account_a); - if (info) + debug_assert (!account.is_zero ()); + auto head = node.ledger.confirmed.head (transaction, account); + if (node.ledger.any.head (transaction, account) == head) { - nano::confirmation_height_info conf_info; - node.store.confirmation_height.get (transaction, account_a, conf_info); - if (conf_info.height < info->block_count) - { - debug_assert (conf_info.frontier != info->head); - auto hash = conf_info.height == 0 ? info->open_block : node.ledger.any.successor (transaction, conf_info.frontier).value_or (0); - auto block = node.ledger.any.get (transaction, hash); - debug_assert (block != nullptr); - if (node.ledger.dependents_confirmed (transaction, *block)) - { - auto const balance = node.ledger.any.balance (transaction, hash).value (); - auto const previous_balance = node.ledger.any.balance (transaction, conf_info.frontier).value_or (0); - auto const balance_priority = std::max (balance, previous_balance); - - node.stats.inc (nano::stat::type::election_scheduler, nano::stat::detail::activated); - node.logger.trace (nano::log::type::election_scheduler, nano::log::detail::block_activated, - nano::log::arg{ "account", account_a.to_account () }, // TODO: Convert to lazy eval - nano::log::arg{ "block", block }, - nano::log::arg{ "time", info->modified }, - nano::log::arg{ "priority", balance_priority }); - - nano::lock_guard lock{ mutex }; - buckets->push (info->modified, block, balance_priority); - notify (); - - return true; // Activated - } - } + return false; + } + auto block = node.ledger.any.get (transaction, node.ledger.any.successor (transaction, { head.is_zero () ? static_cast (account) : head, head }).value ()); + if (!node.ledger.dependents_confirmed (transaction, *block)) + { + return false; } - return false; // Not activated + auto const balance_priority = std::max (block->balance ().number (), node.ledger.confirmed.balance (transaction, head).value_or (0)); + auto const time_priority = !head.is_zero () ? node.ledger.confirmed.get (transaction, head)->sideband ().timestamp : nano::seconds_since_epoch (); // New accounts get current timestamp i.e. lowest priority + + node.stats.inc (nano::stat::type::election_scheduler, nano::stat::detail::activated); + node.logger.trace (nano::log::type::election_scheduler, nano::log::detail::block_activated, + nano::log::arg{ "account", account.to_account () }, // TODO: Convert to lazy eval + nano::log::arg{ "block", block }, + nano::log::arg{ "time", time_priority }, + nano::log::arg{ "priority", balance_priority }); + + nano::lock_guard lock{ mutex }; + buckets->push (time_priority, block, balance_priority); + notify (); + + return true; // Activated } void nano::scheduler::priority::notify ()