From 7ac49e8ffdec7681c442ec898209543de1920926 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Tue, 23 Jan 2024 00:30:39 +0100 Subject: [PATCH] Election locks fix --- nano/node/election.cpp | 16 ++++++++-------- nano/node/election.hpp | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/nano/node/election.cpp b/nano/node/election.cpp index a8f03dae..228da61a 100644 --- a/nano/node/election.cpp +++ b/nano/node/election.cpp @@ -165,16 +165,16 @@ void nano::election::transition_active () state_change (nano::election::state_t::passive, nano::election::state_t::active); } -bool nano::election::confirmed_locked (nano::unique_lock & lock) const +bool nano::election::confirmed_locked () const { - debug_assert (lock.owns_lock ()); + debug_assert (!mutex.try_lock ()); return state_m == nano::election::state_t::confirmed || state_m == nano::election::state_t::expired_confirmed; } bool nano::election::confirmed () const { nano::unique_lock lock{ mutex }; - return confirmed_locked (lock); + return confirmed_locked (); } bool nano::election::failed () const @@ -245,7 +245,7 @@ bool nano::election::transition_time (nano::confirmation_solicitor & solicitor_a break; } - if (!confirmed_locked (lock) && time_to_live () < std::chrono::steady_clock::now () - election_start) + if (!confirmed_locked () && time_to_live () < std::chrono::steady_clock::now () - election_start) { // It is possible the election confirmed while acquiring the mutex // state_change returning true would indicate it @@ -383,7 +383,7 @@ boost::optional nano::election::try_confirm (nano::b if (winner && winner->hash () == hash) { // Determine if the block was confirmed explicitly via election confirmation or implicitly via confirmation height - if (!confirmed_locked (election_lock)) + if (!confirmed_locked ()) { confirm_once (election_lock, nano::election_status_type::active_confirmation_height); status_type = nano::election_status_type::active_confirmation_height; @@ -466,7 +466,7 @@ nano::election_vote_result nano::election::vote (nano::account const & rep, uint node.stats.inc (nano::stat::type::election, vote_source_a == vote_source::live ? nano::stat::detail::vote_new : nano::stat::detail::vote_cached); - if (!confirmed_locked (lock)) + if (!confirmed_locked ()) { confirm_if_quorum (lock); } @@ -478,7 +478,7 @@ bool nano::election::publish (std::shared_ptr const & block_a) nano::unique_lock lock{ mutex }; // Do not insert new blocks if already confirmed - auto result (confirmed_locked (lock)); + auto result (confirmed_locked ()); if (!result && last_blocks.size () >= max_blocks && last_blocks.find (block_a->hash ()) == last_blocks.end ()) { if (!replace_by_weight (lock, block_a->hash ())) @@ -544,7 +544,7 @@ void nano::election::broadcast_vote_locked (nano::unique_lock & loc { node.stats.inc (nano::stat::type::election, nano::stat::detail::generate_vote); - if (confirmed_locked (lock) || have_quorum (tally_impl ())) + if (confirmed_locked () || have_quorum (tally_impl ())) { node.stats.inc (nano::stat::type::election, nano::stat::detail::generate_vote_final); node.final_generator.add (root, status.winner->hash ()); // Broadcasts vote to the network diff --git a/nano/node/election.hpp b/nano/node/election.hpp index ffc0d998..b9561fc6 100644 --- a/nano/node/election.hpp +++ b/nano/node/election.hpp @@ -168,7 +168,7 @@ public: // Information private: nano::tally_t tally_impl () const; - bool confirmed_locked (nano::unique_lock & lock) const; + bool confirmed_locked () const; // lock_a does not own the mutex on return void confirm_once (nano::unique_lock & lock_a, nano::election_status_type = nano::election_status_type::active_confirmed_quorum); void broadcast_block (nano::confirmation_solicitor &);