Skip to content

Commit

Permalink
Move mutex acquisition outside write transaction (#4322)
Browse files Browse the repository at this point in the history
  • Loading branch information
clemahieu authored Nov 5, 2023
1 parent 6244918 commit 7bf9790
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
36 changes: 22 additions & 14 deletions nano/node/voting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ nano::vote_generator::~vote_generator ()
stop ();
}

void nano::vote_generator::process (store::write_transaction const & transaction, nano::root const & root_a, nano::block_hash const & hash_a)
bool nano::vote_generator::should_vote (store::write_transaction const & transaction, nano::root const & root_a, nano::block_hash const & hash_a)
{
bool should_vote = false;
if (is_final)
Expand All @@ -198,16 +198,7 @@ void nano::vote_generator::process (store::write_transaction const & transaction
auto block (ledger.store.block.get (transaction, hash_a));
should_vote = block != nullptr && ledger.dependents_confirmed (transaction, *block);
}
if (should_vote)
{
nano::unique_lock<nano::mutex> lock{ mutex };
candidates.emplace_back (root_a, hash_a);
if (candidates.size () >= nano::network::confirm_ack_hashes_max)
{
lock.unlock ();
condition.notify_all ();
}
}
return should_vote;
}

void nano::vote_generator::start ()
Expand Down Expand Up @@ -241,11 +232,28 @@ void nano::vote_generator::add (const root & root, const block_hash & hash)

void nano::vote_generator::process_batch (std::deque<queue_entry_t> & batch)
{
auto transaction = ledger.store.tx_begin_write ({ tables::final_votes });
std::deque<candidate_t> candidates_new;
{
auto transaction = ledger.store.tx_begin_write ({ tables::final_votes });

for (auto & [root, hash] : batch)
for (auto & [root, hash] : batch)
{
if (should_vote (transaction, root, hash))
{
candidates_new.emplace_back (root, hash);
}
}
// Commit write transaction
}
if (!candidates_new.empty ())
{
process (transaction, root, hash);
nano::unique_lock<nano::mutex> lock{ mutex };
candidates.insert (candidates.end (), candidates_new.begin (), candidates_new.end ());
if (candidates.size () >= nano::network::confirm_ack_hashes_max)
{
lock.unlock ();
condition.notify_all ();
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions nano/node/voting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,11 @@ class vote_generator final
void broadcast_action (std::shared_ptr<nano::vote> const &) const;
void process_batch (std::deque<queue_entry_t> & batch);
/**
* Check if block is eligible for vote generation, then generates a vote or broadcasts votes already in cache
* Check if block is eligible for vote generation
* @param transaction : needs `tables::final_votes` lock
* @return: Should vote
*/
void process (store::write_transaction const &, nano::root const &, nano::block_hash const &);
bool should_vote (store::write_transaction const &, nano::root const &, nano::block_hash const &);

private:
std::function<void (std::shared_ptr<nano::vote> const &, std::shared_ptr<nano::transport::channel> &)> reply_action; // must be set only during initialization by using set_reply_action
Expand Down

0 comments on commit 7bf9790

Please sign in to comment.