From 331d21061d88b5e5d798ca782cff53b01b138214 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 15:01:01 +0100 Subject: [PATCH] WIP --- nano/node/active_transactions.cpp | 27 +++++++++------------------ nano/node/active_transactions.hpp | 5 ----- nano/node/vote_cache.cpp | 11 ++++------- nano/node/vote_cache.hpp | 4 ++-- nano/node/vote_processor.cpp | 14 ++++++++++++-- nano/node/vote_processor.hpp | 1 + 6 files changed, 28 insertions(+), 34 deletions(-) diff --git a/nano/node/active_transactions.cpp b/nano/node/active_transactions.cpp index d9d8891848..de210bdd7a 100644 --- a/nano/node/active_transactions.cpp +++ b/nano/node/active_transactions.cpp @@ -405,9 +405,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) @@ -450,15 +452,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); @@ -470,7 +473,9 @@ nano::election_insertion_result nano::active_transactions::insert (std::shared_p { result.election->broadcast_vote (); } + trim (); + return result; } @@ -504,12 +509,6 @@ nano::vote_code nano::active_transactions::vote (std::shared_ptr con } } - // Process inactive votes outside of the critical section - for (auto & hash : inactive) - { - add_vote_cache (hash, vote_a); - } - if (!process.empty ()) { bool replay = false; @@ -680,14 +679,6 @@ boost::optional nano::active_transactions::confirm_b return status_type; } -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 9f56d5cad6..d79d2b1bea 100644 --- a/nano/node/active_transactions.hpp +++ b/nano/node/active_transactions.hpp @@ -190,11 +190,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); boost::optional election_status (std::shared_ptr const & block); void process_inactive_confirmation (nano::store::read_transaction const & transaction, std::shared_ptr const & block); void process_active_confirmation (nano::store::read_transaction const & transaction, std::shared_ptr const & block, nano::election_status_type status); diff --git a/nano/node/vote_cache.cpp b/nano/node/vote_cache.cpp index bc48ccd1b2..5c59da6335 100644 --- a/nano/node/vote_cache.cpp +++ b/nano/node/vote_cache.cpp @@ -127,11 +127,8 @@ nano::vote_cache::vote_cache (vote_cache_config const & config_a, nano::stats & { } -void nano::vote_cache::vote (std::shared_ptr const & vote, std::unordered_set const & filter) +void nano::vote_cache::vote (std::shared_ptr const & vote, std::function const & filter) { - // Assert that supplied hash corresponds to a one of the hashes stored in vote - debug_assert (std::find (vote->hashes.begin (), vote->hashes.end (), hash) != vote->hashes.end ()); - auto const representative = vote->account; auto const timestamp = vote->timestamp (); auto const rep_weight = rep_weight_query (representative); @@ -140,7 +137,7 @@ void nano::vote_cache::vote (std::shared_ptr const & vote, std::unor for (auto const & hash : vote->hashes) { - if (!filter.contains (hash)) + if (!filter (hash)) { continue; } @@ -182,14 +179,14 @@ std::size_t nano::vote_cache::size () const return cache.size (); } -std::optional nano::vote_cache::find (const nano::block_hash & hash) const +std::vector> nano::vote_cache::find (const nano::block_hash & hash) const { nano::lock_guard lock{ mutex }; auto & cache_by_hash = cache.get (); if (auto existing = cache_by_hash.find (hash); existing != cache_by_hash.end ()) { - return *existing; + return existing->votes (); } return {}; } diff --git a/nano/node/vote_cache.hpp b/nano/node/vote_cache.hpp index 68817a20a6..d499d34442 100644 --- a/nano/node/vote_cache.hpp +++ b/nano/node/vote_cache.hpp @@ -109,12 +109,12 @@ class vote_cache final /** * Adds a new vote to cache */ - void vote (std::shared_ptr const & vote, std::unordered_set const & filter); + void vote (std::shared_ptr const & vote, std::function const & filter); /** * Tries to find an entry associated with block hash */ - std::optional find (nano::block_hash const & hash) const; + std::vector> find (nano::block_hash const & hash) const; /** * Removes an entry associated with block hash, does nothing if entry does not exist diff --git a/nano/node/vote_processor.cpp b/nano/node/vote_processor.cpp index 337b0bddfb..60d55ed1e7 100644 --- a/nano/node/vote_processor.cpp +++ b/nano/node/vote_processor.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -10,8 +11,6 @@ #include #include -#include - #include using namespace std::chrono_literals; @@ -167,8 +166,19 @@ nano::vote_code nano::vote_processor::vote_blocking (std::shared_ptr if (validated || !vote_a->validate ()) { result = active.vote (vote_a); + + if (result != nano::vote_code::replay) + { + + } + if (ledger.weight (vote_a->account) > node.minimum_principal_weight ()) + { + node.vote_cache.vote (vote_a); + } + observers.vote.notify (vote_a, channel_a, result); } + std::string status; switch (result) { diff --git a/nano/node/vote_processor.hpp b/nano/node/vote_processor.hpp index b7498180fc..7bed071678 100644 --- a/nano/node/vote_processor.hpp +++ b/nano/node/vote_processor.hpp @@ -56,6 +56,7 @@ class vote_processor final std::atomic total_processed{ 0 }; private: // Dependencies + nano::node & node; nano::active_transactions & active; nano::node_observers & observers; nano::stats & stats;