From 1f69bc061baa028f29da553b376aded28a1b872a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Mon, 9 Oct 2023 19:13:33 +0200 Subject: [PATCH] Fix AEC insert race condition --- nano/node/active_transactions.cpp | 5 +++++ nano/node/election.cpp | 4 ++++ nano/node/node.cpp | 1 - 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/nano/node/active_transactions.cpp b/nano/node/active_transactions.cpp index bffa0b6243..b1bbf6a2de 100644 --- a/nano/node/active_transactions.cpp +++ b/nano/node/active_transactions.cpp @@ -327,6 +327,11 @@ void nano::active_transactions::cleanup_election (nano::unique_lock debug_assert (!mutex.try_lock ()); debug_assert (lock_a.owns_lock ()); + if (election->confirmed ()) + { + debug_assert (recently_confirmed.exists (election->qualified_root)); + } + node.stats.inc (completion_type (*election), nano::to_stat_detail (election->behavior ())); // Keep track of election count by election type debug_assert (count_by_behavior[election->behavior ()] > 0); diff --git a/nano/node/election.cpp b/nano/node/election.cpp index 79243bfece..990370311e 100644 --- a/nano/node/election.cpp +++ b/nano/node/election.cpp @@ -35,6 +35,7 @@ nano::election::election (nano::node & node_a, std::shared_ptr cons void nano::election::confirm_once (nano::unique_lock & lock_a, nano::election_status_type type_a) { debug_assert (lock_a.owns_lock ()); + // This must be kept above the setting of election state, as dependent confirmed elections require up to date changes to election_winner_details nano::unique_lock election_winners_lk{ node.active.election_winner_details_mutex }; if (state_m.exchange (nano::election::state_t::confirmed) != nano::election::state_t::confirmed && (node.active.election_winner_details.count (status.winner->hash ()) == 0)) @@ -48,6 +49,9 @@ void nano::election::confirm_once (nano::unique_lock & lock_a, nano status.voter_count = nano::narrow_cast (last_votes.size ()); status.type = type_a; auto const status_l = status; + + node.active.recently_confirmed.put (qualified_root, status_l.winner->hash ()); + lock_a.unlock (); node.background ([node_l = node.shared (), status_l, confirmation_action_l = confirmation_action] () { diff --git a/nano/node/node.cpp b/nano/node/node.cpp index 0c764d9de4..31531dda0c 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -1386,7 +1386,6 @@ void nano::node::process_confirmed (nano::election_status const & status_a, uint decltype (iteration_a) const num_iters = (config.block_processor_batch_max_time / network_params.node.process_confirmed_interval) * 4; if (auto block_l = ledger.store.block.get (ledger.store.tx_begin_read (), hash)) { - active.recently_confirmed.put (block_l->qualified_root (), hash); confirmation_height_processor.add (block_l); } else if (iteration_a < num_iters)