From d1d2a1d51cd4923068216b7fd37d7365abd33fe8 Mon Sep 17 00:00:00 2001 From: Guilherme Lawless Date: Thu, 26 Mar 2020 18:24:07 +0000 Subject: [PATCH] Optimize vote post-processing operations (#2627) * Add return flag for rep_crawler::response * Optimize vote post-processing operations This can be done now that confirmed elections linger for 1 to 10 seconds in active roots. The rep crawler is always checked, but online weight only for live votes or for rep crawler queries. Gap cache only for indeterminate votes --- nano/node/node.cpp | 14 +++++++++++--- nano/node/repcrawler.cpp | 5 ++++- nano/node/repcrawler.hpp | 4 ++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/nano/node/node.cpp b/nano/node/node.cpp index 74e8db6a..116ffec0 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -301,11 +301,19 @@ startup_time (std::chrono::steady_clock::now ()) } }); observers.vote.add ([this](std::shared_ptr vote_a, std::shared_ptr channel_a, nano::vote_code code_a) { - if (code_a == nano::vote_code::vote || code_a == nano::vote_code::indeterminate) + debug_assert (code_a != nano::vote_code::invalid); + if (code_a != nano::vote_code::replay) + { + auto active_in_rep_crawler (!this->rep_crawler.response (channel_a, vote_a)); + if (active_in_rep_crawler || code_a == nano::vote_code::vote) + { + // Representative is defined as online if replying to live votes or rep_crawler queries + this->online_reps.observe (vote_a->account); + } + } + if (code_a == nano::vote_code::indeterminate) { this->gap_cache.vote (vote_a); - this->online_reps.observe (vote_a->account); - this->rep_crawler.response (channel_a, vote_a); } }); if (websocket_server) diff --git a/nano/node/repcrawler.cpp b/nano/node/repcrawler.cpp index 28d6269f..40d92572 100644 --- a/nano/node/repcrawler.cpp +++ b/nano/node/repcrawler.cpp @@ -194,17 +194,20 @@ bool nano::rep_crawler::is_pr (nano::transport::channel const & channel_a) const return result; } -void nano::rep_crawler::response (std::shared_ptr & channel_a, std::shared_ptr & vote_a) +bool nano::rep_crawler::response (std::shared_ptr & channel_a, std::shared_ptr & vote_a) { + bool error = true; nano::lock_guard lock (active_mutex); for (auto i = vote_a->begin (), n = vote_a->end (); i != n; ++i) { if (active.count (*i) != 0) { responses.emplace_back (channel_a, vote_a); + error = false; break; } } + return error; } nano::uint128_t nano::rep_crawler::total_weight () const diff --git a/nano/node/repcrawler.hpp b/nano/node/repcrawler.hpp index 89ce7118..b94d569b 100644 --- a/nano/node/repcrawler.hpp +++ b/nano/node/repcrawler.hpp @@ -95,9 +95,9 @@ public: /** * Called when a non-replay vote on a block previously sent by query() is received. This indiciates * with high probability that the endpoint is a representative node. - * @return True if the rep entry was updated with new information due to increase in weight. + * @return false if the vote corresponded to any active hash. */ - void response (std::shared_ptr &, std::shared_ptr &); + bool response (std::shared_ptr &, std::shared_ptr &); /** Get total available weight from representatives */ nano::uint128_t total_weight () const;