Skip to content

Commit

Permalink
Remove coupling of scheduler::priority to node and directly passed in…
Browse files Browse the repository at this point in the history
… used components.
  • Loading branch information
clemahieu committed May 15, 2024
1 parent 86d5eb6 commit 9df3746
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion nano/node/scheduler/component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ nano::scheduler::component::component (nano::node & node) :
hinted_impl{ std::make_unique<nano::scheduler::hinted> (node.config.hinted_scheduler, node, node.vote_cache, node.active, node.online_reps, node.stats) },
manual_impl{ std::make_unique<nano::scheduler::manual> (node) },
optimistic_impl{ std::make_unique<nano::scheduler::optimistic> (node.config.optimistic_scheduler, node, node.ledger, node.active, node.network_params.network, node.stats) },
priority_impl{ std::make_unique<nano::scheduler::priority> (node, node.stats) },
priority_impl{ std::make_unique<nano::scheduler::priority> (node.config.priority_scheduler, node.ledger, node.active, node.stats, node.logger) },
hinted{ *hinted_impl },
manual{ *manual_impl },
optimistic{ *optimistic_impl },
Expand Down
33 changes: 18 additions & 15 deletions nano/node/scheduler/priority.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
#include <nano/lib/blocks.hpp>
#include <nano/lib/thread_roles.hpp>
#include <nano/lib/threading.hpp>
#include <nano/node/active_elections.hpp>
#include <nano/node/election.hpp>
#include <nano/node/node.hpp>
#include <nano/node/scheduler/buckets.hpp>
#include <nano/node/scheduler/priority.hpp>
#include <nano/secure/ledger.hpp>
#include <nano/secure/ledger_set_any.hpp>
#include <nano/secure/ledger_set_confirmed.hpp>

nano::scheduler::priority::priority (nano::node & node_a, nano::stats & stats_a) :
config{ node_a.config.priority_scheduler },
node{ node_a },
stats{ stats_a },
nano::scheduler::priority::priority (priority_config const & config, nano::ledger & ledger, nano::active_elections & active, nano::stats & stats, nano::logger & logger) :
config{ config },
ledger{ ledger },
active{ active },
stats{ stats },
logger{ logger },
buckets{ std::make_unique<scheduler::buckets> (config.bucket_maximum) }
{
}
Expand Down Expand Up @@ -50,21 +53,21 @@ void nano::scheduler::priority::stop ()
bool nano::scheduler::priority::activate (secure::transaction const & transaction, nano::account const & account)
{
debug_assert (!account.is_zero ());
auto head = node.ledger.confirmed.account_head (transaction, account);
if (node.ledger.any.account_head (transaction, account) == head)
auto head = ledger.confirmed.account_head (transaction, account);
if (ledger.any.account_head (transaction, account) == head)
{
return false;
}
auto block = node.ledger.any.block_get (transaction, node.ledger.any.block_successor (transaction, { head.is_zero () ? static_cast<nano::uint256_union> (account) : head, head }).value ());
if (!node.ledger.dependents_confirmed (transaction, *block))
auto block = ledger.any.block_get (transaction, ledger.any.block_successor (transaction, { head.is_zero () ? static_cast<nano::uint256_union> (account) : head, head }).value ());
if (!ledger.dependents_confirmed (transaction, *block))
{
return false;
}
auto const balance_priority = std::max (block->balance ().number (), node.ledger.confirmed.block_balance (transaction, head).value_or (0).number ());
auto const time_priority = !head.is_zero () ? node.ledger.confirmed.block_get (transaction, head)->sideband ().timestamp : nano::seconds_since_epoch (); // New accounts get current timestamp i.e. lowest priority
auto const balance_priority = std::max (block->balance ().number (), ledger.confirmed.block_balance (transaction, head).value_or (0).number ());
auto const time_priority = !head.is_zero () ? ledger.confirmed.block_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,
stats.inc (nano::stat::type::election_scheduler, nano::stat::detail::activated);
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 },
Expand Down Expand Up @@ -101,7 +104,7 @@ bool nano::scheduler::priority::empty () const

bool nano::scheduler::priority::predicate () const
{
return node.active.vacancy (nano::election_behavior::priority) > 0 && !buckets->empty ();
return active.vacancy (nano::election_behavior::priority) > 0 && !buckets->empty ();
}

void nano::scheduler::priority::run ()
Expand All @@ -123,7 +126,7 @@ void nano::scheduler::priority::run ()
buckets->pop ();
lock.unlock ();
stats.inc (nano::stat::type::election_scheduler, nano::stat::detail::insert_priority);
auto result = node.active.insert (block);
auto result = active.insert (block);
if (result.inserted)
{
stats.inc (nano::stat::type::election_scheduler, nano::stat::detail::insert_priority_success);
Expand Down
9 changes: 6 additions & 3 deletions nano/node/scheduler/priority.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@

namespace nano
{
class active_elections;
class block;
class container_info_component;
class node;
class ledger;
class stats;
}
namespace nano::secure
Expand All @@ -38,7 +39,7 @@ class buckets;
class priority final
{
public:
priority (nano::node &, nano::stats &);
priority (priority_config const & config, nano::ledger & ledger, nano::active_elections & active, nano::stats & stats, nano::logger & logger);
~priority ();

void start ();
Expand All @@ -57,8 +58,10 @@ class priority final

private: // Dependencies
priority_config const & config;
nano::node & node;
nano::ledger & ledger;
nano::active_elections & active;
nano::stats & stats;
nano::logger & logger;

private:
void run ();
Expand Down

0 comments on commit 9df3746

Please sign in to comment.