Skip to content

Commit

Permalink
Bound depth of hinted scheduler block traversal (nanocurrency#4359)
Browse files Browse the repository at this point in the history
* Bound depth of hinted scheduler block traversal

* Return early when stopped
  • Loading branch information
pwojcikdev authored Jan 15, 2024
1 parent fb5bb88 commit b6bf117
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
13 changes: 11 additions & 2 deletions nano/node/scheduler/hinted.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,14 @@ bool nano::scheduler::hinted::predicate () const

void nano::scheduler::hinted::activate (const nano::store::read_transaction & transaction, const nano::block_hash & hash, bool check_dependents)
{
const int max_iterations = 64;

std::set<nano::block_hash> visited;
std::stack<nano::block_hash> stack;
stack.push (hash);

while (!stack.empty ())
int iterations = 0;
while (!stack.empty () && iterations++ < max_iterations)
{
transaction.refresh_if_needed ();

Expand All @@ -91,7 +95,7 @@ void nano::scheduler::hinted::activate (const nano::store::read_transaction & tr
auto dependents = node.ledger.dependent_blocks (transaction, *block);
for (const auto & dependent_hash : dependents)
{
if (!dependent_hash.is_zero ())
if (!dependent_hash.is_zero () && visited.insert (dependent_hash).second) // Avoid visiting the same block twice
{
stack.push (dependent_hash); // Add dependent block to the stack
}
Expand Down Expand Up @@ -125,6 +129,11 @@ void nano::scheduler::hinted::run_iterative ()

for (auto const & entry : tops)
{
if (stopped)
{
return;
}

if (!predicate ())
{
return;
Expand Down
3 changes: 2 additions & 1 deletion nano/node/scheduler/hinted.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index_container.hpp>

#include <atomic>
#include <chrono>
#include <condition_variable>
#include <thread>
Expand Down Expand Up @@ -79,7 +80,7 @@ class hinted final
private:
hinted_config const & config;

bool stopped{ false };
std::atomic<bool> stopped{ false };
nano::condition_variable condition;
mutable nano::mutex mutex;
std::thread thread;
Expand Down

0 comments on commit b6bf117

Please sign in to comment.