Skip to content

Commit

Permalink
Remove active_transactions::insert_impl and remove the passing of mutex
Browse files Browse the repository at this point in the history
A mutex was needlessly being passed from function to function and a
function wrapper existed needlessly.
  • Loading branch information
dsiganos committed Jan 12, 2024
1 parent f801846 commit 0406a33
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
26 changes: 8 additions & 18 deletions nano/node/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,16 +405,6 @@ void nano::active_transactions::request_loop ()
}
}

nano::election_insertion_result nano::active_transactions::insert (const std::shared_ptr<nano::block> & block, nano::election_behavior behavior)
{
debug_assert (block != nullptr);

nano::unique_lock<nano::mutex> lock{ mutex };

auto result = insert_impl (lock, block, behavior);
return result;
}

void nano::active_transactions::trim ()
{
/*
Expand All @@ -429,10 +419,10 @@ void nano::active_transactions::trim ()
}
}

nano::election_insertion_result nano::active_transactions::insert_impl (nano::unique_lock<nano::mutex> & lock_a, std::shared_ptr<nano::block> const & block_a, nano::election_behavior election_behavior_a, std::function<void (std::shared_ptr<nano::block> const &)> const & confirmation_action_a)
nano::election_insertion_result nano::active_transactions::insert (std::shared_ptr<nano::block> const & block_a, nano::election_behavior election_behavior_a)
{
debug_assert (!mutex.try_lock ());
debug_assert (lock_a.owns_lock ());
nano::unique_lock<nano::mutex> lock{ mutex };
debug_assert (block_a);
debug_assert (block_a->has_sideband ());
nano::election_insertion_result result;
if (!stopped)
Expand All @@ -446,7 +436,7 @@ nano::election_insertion_result nano::active_transactions::insert_impl (nano::un
result.inserted = true;
auto hash (block_a->hash ());
result.election = nano::make_shared<nano::election> (
node, block_a, confirmation_action_a, [&node = node] (auto const & rep_a) {
node, block_a, nullptr, [&node = node] (auto const & rep_a) {
// Representative is defined as online if replying to live votes or rep_crawler queries
node.online_reps.observe (rep_a);
},
Expand All @@ -457,30 +447,30 @@ nano::election_insertion_result nano::active_transactions::insert_impl (nano::un
debug_assert (count_by_behavior[result.election->behavior ()] >= 0);
count_by_behavior[result.election->behavior ()]++;

lock_a.unlock ();
lock.unlock ();
if (auto const cache = node.vote_cache.find (hash); cache)
{
cache->fill (result.election);
}
node.stats.inc (nano::stat::type::active_started, nano::to_stat_detail (election_behavior_a));
node.observers.active_started.notify (hash);
vacancy_update ();
lock_a.lock ();
lock.lock ();
}
}
else
{
result.election = existing->election;
}

lock_a.unlock ();
lock.unlock ();
// Votes are generated for inserted or ongoing elections
if (result.election)
{
result.election->broadcast_vote ();
}
trim ();
lock_a.lock ();
lock.lock ();
}
return result;
}
Expand Down
4 changes: 1 addition & 3 deletions nano/node/active_transactions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class active_transactions final
/**
* Starts new election with a specified behavior type
*/
nano::election_insertion_result insert (std::shared_ptr<nano::block> const & block, nano::election_behavior behavior = nano::election_behavior::normal);
nano::election_insertion_result insert (std::shared_ptr<nano::block> const &, nano::election_behavior = nano::election_behavior::normal);
// Distinguishes replay votes, cannot be determined if the block is not in any election
nano::vote_code vote (std::shared_ptr<nano::vote> const &);
// Is the root of this block in the roots container
Expand Down Expand Up @@ -181,8 +181,6 @@ class active_transactions final
private:
// Erase elections if we're over capacity
void trim ();
// Call action with confirmed block, may be different than what we started with
nano::election_insertion_result insert_impl (nano::unique_lock<nano::mutex> &, std::shared_ptr<nano::block> const &, nano::election_behavior = nano::election_behavior::normal, std::function<void (std::shared_ptr<nano::block> const &)> const & = nullptr);
void request_loop ();
void request_confirm (nano::unique_lock<nano::mutex> &);
void erase (nano::qualified_root const &);
Expand Down

0 comments on commit 0406a33

Please sign in to comment.