From f6bdf583fff876750f3a8b7a69324c2b7278e9ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Wed, 20 Mar 2024 17:39:08 +0100 Subject: [PATCH] WIP --- nano/node/active_transactions.cpp | 34 +++++++++++++------------------ nano/node/active_transactions.hpp | 5 ----- nano/node/election.cpp | 13 +++++++++++- nano/node/vote_processor.cpp | 10 ++++----- 4 files changed, 30 insertions(+), 32 deletions(-) diff --git a/nano/node/active_transactions.cpp b/nano/node/active_transactions.cpp index 9603721af3..e79b08a2fd 100644 --- a/nano/node/active_transactions.cpp +++ b/nano/node/active_transactions.cpp @@ -368,9 +368,11 @@ void nano::active_transactions::trim () nano::election_insertion_result nano::active_transactions::insert (std::shared_ptr const & block_a, nano::election_behavior election_behavior_a) { - nano::unique_lock lock{ mutex }; debug_assert (block_a); debug_assert (block_a->has_sideband ()); + + nano::unique_lock lock{ mutex }; + nano::election_insertion_result result; if (stopped) @@ -413,15 +415,16 @@ nano::election_insertion_result nano::active_transactions::insert (std::shared_p result.election = existing->election; } - lock.unlock (); // end of critical section + lock.unlock (); if (result.inserted) { release_assert (result.election); - if (auto const cache = node.vote_cache.find (hash); cache) + auto cached = node.vote_cache.find (hash); + for (auto const & cached_vote : cached) { - cache->fill (result.election); + vote (cached_vote); } node.observers.active_started.notify (hash); @@ -433,7 +436,9 @@ nano::election_insertion_result nano::active_transactions::insert (std::shared_p { result.election->broadcast_vote (); } + trim (); + return result; } @@ -469,12 +474,6 @@ std::unordered_map nano::active_transactions: } } - // Process inactive votes outside of the critical section - for (auto & hash : inactive) - { - add_vote_cache (hash, vote); - } - if (!process.empty ()) { bool processed = false; @@ -609,24 +608,19 @@ bool nano::active_transactions::publish (std::shared_ptr const & bl lock.lock (); blocks.emplace (block_a->hash (), election); lock.unlock (); - if (auto const cache = node.vote_cache.find (block_a->hash ()); cache) + + auto cached = node.vote_cache.find (block_a->hash ()); + for (auto const & cached_vote : cached) { - cache->fill (election); + vote (cached_vote); } + node.stats.inc (nano::stat::type::active, nano::stat::detail::election_block_conflict); } } return result; } -void nano::active_transactions::add_vote_cache (nano::block_hash const & hash, std::shared_ptr const vote) -{ - if (node.ledger.weight (vote->account) > node.minimum_principal_weight ()) - { - node.vote_cache.vote (hash, vote); - } -} - std::size_t nano::active_transactions::election_winner_details_size () { nano::lock_guard guard{ election_winner_details_mutex }; diff --git a/nano/node/active_transactions.hpp b/nano/node/active_transactions.hpp index a0eef10b11..cb29b6bc9e 100644 --- a/nano/node/active_transactions.hpp +++ b/nano/node/active_transactions.hpp @@ -199,11 +199,6 @@ class active_transactions final nano::stat::type completion_type (nano::election const & election) const; // Returns a list of elections sorted by difficulty, mutex must be locked std::vector> list_active_impl (std::size_t) const; - /** - * Checks if vote passes minimum representative weight threshold and adds it to inactive vote cache - * TODO: Should be moved to `vote_cache` class - */ - void add_vote_cache (nano::block_hash const & hash, std::shared_ptr vote); void activate_successors (nano::store::read_transaction const & transaction, std::shared_ptr const & block); void notify_observers (nano::store::read_transaction const & transaction, nano::election_status const & status, std::vector const & votes); diff --git a/nano/node/election.cpp b/nano/node/election.cpp index f67806378f..ca385ae07a 100644 --- a/nano/node/election.cpp +++ b/nano/node/election.cpp @@ -644,11 +644,22 @@ bool nano::election::replace_by_weight (nano::unique_lock & lock_a, sorted.reserve (last_tally.size ()); std::copy (last_tally.begin (), last_tally.end (), std::back_inserter (sorted)); lock_a.unlock (); + // Sort in ascending order std::sort (sorted.begin (), sorted.end (), [] (auto const & left, auto const & right) { return left.second < right.second; }); + + auto votes_tally = [this] (std::vector> const & votes) { + nano::uint128_t result{ 0 }; + for (auto const & vote : votes) + { + result += node.ledger.weight (vote->account); + } + return result; + }; + // Replace if lowest tally is below inactive cache new block weight auto inactive_existing = node.vote_cache.find (hash_a); - auto inactive_tally = inactive_existing ? inactive_existing->tally () : 0; + auto inactive_tally = votes_tally (inactive_existing); if (inactive_tally > 0 && sorted.size () < max_blocks) { // If count of tally items is less than 10, remove any block without tally diff --git a/nano/node/vote_processor.cpp b/nano/node/vote_processor.cpp index 7b70aa0e5e..f78a994f3c 100644 --- a/nano/node/vote_processor.cpp +++ b/nano/node/vote_processor.cpp @@ -10,8 +10,6 @@ #include #include -#include - #include using namespace std::chrono_literals; @@ -161,12 +159,12 @@ void nano::vote_processor::verify_votes (decltype (votes) const & votes_a) } } -nano::vote_code nano::vote_processor::vote_blocking (std::shared_ptr const & vote_a, std::shared_ptr const & channel_a, bool validated) +nano::vote_code nano::vote_processor::vote_blocking (std::shared_ptr const & vote, std::shared_ptr const & channel, bool validated) { auto result = nano::vote_code::invalid; - if (validated || !vote_a->validate ()) + if (validated || !vote->validate ()) { - auto vote_results = active.vote (vote_a); + auto vote_results = active.vote (vote); // Aggregate results for individual hashes bool replay = false; @@ -184,7 +182,7 @@ nano::vote_code nano::vote_processor::vote_blocking (std::shared_ptr stats.inc (nano::stat::type::vote, to_stat_detail (result)); logger.trace (nano::log::type::vote_processor, nano::log::detail::vote_processed, - nano::log::arg{ "vote", vote_a }, + nano::log::arg{ "vote", vote }, nano::log::arg{ "result", result }); return result;