Skip to content

Commit

Permalink
Fix AEC insert race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Oct 9, 2023
1 parent 5bc61a2 commit 1f69bc0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
5 changes: 5 additions & 0 deletions nano/node/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ void nano::active_transactions::cleanup_election (nano::unique_lock<nano::mutex>
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);
Expand Down
4 changes: 4 additions & 0 deletions nano/node/election.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ nano::election::election (nano::node & node_a, std::shared_ptr<nano::block> cons
void nano::election::confirm_once (nano::unique_lock<nano::mutex> & 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<nano::mutex> 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))
Expand All @@ -48,6 +49,9 @@ void nano::election::confirm_once (nano::unique_lock<nano::mutex> & lock_a, nano
status.voter_count = nano::narrow_cast<decltype (status.voter_count)> (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] () {
Expand Down
1 change: 0 additions & 1 deletion nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1f69bc0

Please sign in to comment.