Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Election cleanup cleanup #3302

Merged
merged 2 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 12 additions & 24 deletions nano/node/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,39 +347,40 @@ void nano::active_transactions::request_confirm (nano::unique_lock<nano::mutex>
}
}

void nano::active_transactions::cleanup_election (nano::unique_lock<nano::mutex> & lock_a, nano::election_cleanup_info const & info_a)
void nano::active_transactions::cleanup_election (nano::unique_lock<nano::mutex> & lock_a, nano::election const & election)
{
debug_assert (lock_a.owns_lock ());

if (!info_a.confirmed)
if (!election.confirmed ())
{
node.stats.inc (nano::stat::type::election, nano::stat::detail::election_drop);
}

for (auto const & [hash, block] : info_a.blocks)
auto blocks_l = election.blocks ();
for (auto const & [hash, block] : blocks_l)
{
auto erased (blocks.erase (hash));
(void)erased;
debug_assert (erased == 1);
erase_inactive_votes_cache (hash);
}
roots.get<tag_root> ().erase (roots.get<tag_root> ().find (election.qualified_root));

lock_a.unlock ();
for (auto const & [hash, block] : info_a.blocks)
vacancy_update ();
for (auto const & [hash, block] : blocks_l)
{
// Notify observers about dropped elections & blocks lost confirmed elections
if (!info_a.confirmed || hash != info_a.winner)
if (!election.confirmed () || hash != election.winner ()->hash ())
{
node.observers.active_stopped.notify (hash);
}

if (!info_a.confirmed)
if (!election.confirmed ())
{
// Clear from publish filter
node.network.publish_filter.clear (block);
}
}
lock_a.lock ();
node.logger.try_log (boost::str (boost::format ("Election erased for root %1%") % election.qualified_root.to_string ()));
}

std::vector<std::shared_ptr<nano::election>> nano::active_transactions::list_active (size_t max_a)
Expand Down Expand Up @@ -1025,17 +1026,7 @@ void nano::active_transactions::erase_recently_confirmed (nano::block_hash const

void nano::active_transactions::erase (nano::block const & block_a)
{
nano::unique_lock<nano::mutex> lock (mutex);
auto root_it (roots.get<tag_root> ().find (block_a.qualified_root ()));
if (root_it != roots.get<tag_root> ().end ())
{
// This is one of few places where both the active mutex and election mutexes are held
cleanup_election (lock, root_it->election->cleanup_info ());
roots.get<tag_root> ().erase (root_it);
lock.unlock ();
node.logger.try_log (boost::str (boost::format ("Election erased for block block %1% root %2%") % block_a.hash ().to_string () % block_a.root ().to_string ()));
vacancy_update ();
}
erase (block_a.qualified_root ());
}

void nano::active_transactions::erase (nano::qualified_root const & root_a)
Expand All @@ -1044,10 +1035,7 @@ void nano::active_transactions::erase (nano::qualified_root const & root_a)
auto root_it (roots.get<tag_root> ().find (root_a));
if (root_it != roots.get<tag_root> ().end ())
{
// This is one of few places where both the active mutex and election mutexes are held
cleanup_election (lock, root_it->election->cleanup_info ());
roots.get<tag_root> ().erase (root_it);
vacancy_update ();
cleanup_election (lock, *root_it->election);
}
}

Expand Down
2 changes: 1 addition & 1 deletion nano/node/active_transactions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ class active_transactions final
void request_confirm (nano::unique_lock<nano::mutex> &);
void erase (nano::qualified_root const &);
// Erase all blocks from active and, if not confirmed, clear digests from network filters
void cleanup_election (nano::unique_lock<nano::mutex> &, nano::election_cleanup_info const &);
void cleanup_election (nano::unique_lock<nano::mutex> & lock_a, nano::election const &);
// Returns a list of elections sorted by difficulty, mutex must be locked
std::vector<std::shared_ptr<nano::election>> list_active_impl (size_t) const;

Expand Down
16 changes: 0 additions & 16 deletions nano/node/election.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,22 +437,6 @@ bool nano::election::publish (std::shared_ptr<nano::block> const & block_a)
return result;
}

nano::election_cleanup_info nano::election::cleanup_info () const
{
nano::lock_guard<nano::mutex> guard (mutex);
return cleanup_info_impl ();
}

nano::election_cleanup_info nano::election::cleanup_info_impl () const
{
return nano::election_cleanup_info{
confirmed (),
status.winner->qualified_root (),
status.winner->hash (),
last_blocks
};
}

size_t nano::election::insert_inactive_votes_cache (nano::inactive_cache_information const & cache_a)
{
nano::unique_lock<nano::mutex> lock (mutex);
Expand Down
9 changes: 0 additions & 9 deletions nano/node/election.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ enum class election_behavior
normal,
optimistic
};
struct election_cleanup_info final
{
bool confirmed;
nano::qualified_root root;
nano::block_hash winner;
std::unordered_map<nano::block_hash, std::shared_ptr<nano::block>> blocks;
};
struct election_extended_status final
{
nano::election_status status;
Expand Down Expand Up @@ -116,7 +109,6 @@ class election final : public std::enable_shared_from_this<nano::election>
size_t insert_inactive_votes_cache (nano::inactive_cache_information const &);
// Confirm this block if quorum is met
void confirm_if_quorum (nano::unique_lock<nano::mutex> &);
nano::election_cleanup_info cleanup_info () const;

public: // Information
uint64_t const height;
Expand All @@ -135,7 +127,6 @@ class election final : public std::enable_shared_from_this<nano::election>
void remove_votes (nano::block_hash const &);
void remove_block (nano::block_hash const &);
bool replace_by_weight (nano::unique_lock<nano::mutex> & lock_a, nano::block_hash const &);
nano::election_cleanup_info cleanup_info_impl () const;

private:
std::unordered_map<nano::block_hash, std::shared_ptr<nano::block>> last_blocks;
Expand Down