Skip to content

Commit

Permalink
Relax mutex requirement for retrieving active difficulty (#2901)
Browse files Browse the repository at this point in the history
* Make active multiplier an atomic to avoid an active mutex lock

* Add failing test to ensure the active mutex is not held in the difficulty observer update

* Unlock active mutex updating difficulty observers
  • Loading branch information
guilhermelawless authored Sep 2, 2020
1 parent 17d013a commit 24a5375
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
13 changes: 13 additions & 0 deletions nano/core_test/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1434,3 +1434,16 @@ TEST (active_transactions, activate_inactive)
// The first block was not active so no activation takes place
ASSERT_FALSE (node.active.active (open->qualified_root ()) || node.block_confirmed_or_being_confirmed (node.store.tx_begin_read (), open->hash ()));
}

TEST (active_transactions, difficulty_update_observer)
{
nano::system system (1);
auto & node (*system.nodes[0]);
std::atomic<bool> update_received (false);
node.observers.difficulty.add ([& mutex = node.active.mutex, &update_received](uint64_t difficulty_a) {
nano::unique_lock<std::mutex> lock (mutex, std::defer_lock);
EXPECT_TRUE (lock.try_lock ());
update_received = true;
});
ASSERT_TIMELY (3s, update_received);
}
5 changes: 3 additions & 2 deletions nano/node/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,9 @@ void nano::active_transactions::update_active_multiplier (nano::unique_lock<std:
debug_assert (difficulty >= node.network_params.network.publish_thresholds.entry);

trended_active_multiplier = avg_multiplier;
lock_a.unlock ();
node.observers.difficulty.notify (difficulty);
lock_a.lock ();
}

uint64_t nano::active_transactions::active_difficulty ()
Expand Down Expand Up @@ -912,8 +914,7 @@ uint64_t nano::active_transactions::limited_active_difficulty (nano::work_versio

double nano::active_transactions::active_multiplier ()
{
nano::lock_guard<std::mutex> lock (mutex);
return trended_active_multiplier;
return trended_active_multiplier.load ();
}

// List of active blocks in elections
Expand Down
2 changes: 1 addition & 1 deletion nano/node/active_transactions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class active_transactions final
nano::node & node;
mutable std::mutex mutex;
boost::circular_buffer<double> multipliers_cb;
double trended_active_multiplier;
std::atomic<double> trended_active_multiplier;
size_t priority_cementable_frontiers_size ();
size_t priority_wallet_cementable_frontiers_size ();
boost::circular_buffer<double> difficulty_trend ();
Expand Down

0 comments on commit 24a5375

Please sign in to comment.