From e1a4fcbec52448c92452d4fc5bb12fc6f8b36be3 Mon Sep 17 00:00:00 2001 From: gr0vity-dev <85646666+gr0vity-dev@users.noreply.github.com> Date: Fri, 25 Oct 2024 14:23:34 +0200 Subject: [PATCH] Optimize vote distribution between PRs and non-PRs (#4766) * Publish votes to all PRs and a subset of non PRs This avoids potential duplicate votes to PRs and better confirmation times for non PRs * small optimisation for list_non_pr --------- Co-authored-by: gr0vity-dev --- nano/node/network.cpp | 19 +++++++++++++++---- nano/node/network.hpp | 1 + nano/node/vote_generator.cpp | 2 +- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/nano/node/network.cpp b/nano/node/network.cpp index 6a74df915d..269edcd87f 100644 --- a/nano/node/network.cpp +++ b/nano/node/network.cpp @@ -281,6 +281,15 @@ void nano::network::flood_vote (std::shared_ptr const & vote, float } } +void nano::network::flood_vote_non_pr (std::shared_ptr const & vote, float scale, bool rebroadcasted) +{ + nano::confirm_ack message{ node.network_params.network, vote, rebroadcasted }; + for (auto & i : list_non_pr (fanout (scale))) + { + i->send (message, nullptr); + } +} + void nano::network::flood_vote_pr (std::shared_ptr const & vote, bool rebroadcasted) { nano::confirm_ack message{ node.network_params.network, vote, rebroadcasted }; @@ -377,11 +386,13 @@ std::deque> nano::network::list_non_pr { std::deque> result; tcp_channels.list (result); + + auto partition_point = std::partition (result.begin (), result.end (), + [this] (std::shared_ptr const & channel) { + return !node.rep_crawler.is_pr (channel); + }); + result.resize (std::distance (result.begin (), partition_point)); nano::random_pool_shuffle (result.begin (), result.end ()); - result.erase (std::remove_if (result.begin (), result.end (), [this] (std::shared_ptr const & channel) { - return node.rep_crawler.is_pr (channel); - }), - result.end ()); if (result.size () > count_a) { result.resize (count_a, nullptr); diff --git a/nano/node/network.hpp b/nano/node/network.hpp index 5aa6ec8772..0ab498ed2b 100644 --- a/nano/node/network.hpp +++ b/nano/node/network.hpp @@ -95,6 +95,7 @@ class network final void flood_keepalive_self (float const scale_a = 0.5f); void flood_vote (std::shared_ptr const &, float scale, bool rebroadcasted = false); void flood_vote_pr (std::shared_ptr const &, bool rebroadcasted = false); + void flood_vote_non_pr (std::shared_ptr const &, float scale, bool rebroadcasted = false); // Flood block to all PRs and a random selection of non-PRs void flood_block_initial (std::shared_ptr const &); // Flood block to a random selection of peers diff --git a/nano/node/vote_generator.cpp b/nano/node/vote_generator.cpp index 68792d690d..e29c88cc97 100644 --- a/nano/node/vote_generator.cpp +++ b/nano/node/vote_generator.cpp @@ -277,7 +277,7 @@ void nano::vote_generator::vote (std::vector const & hashes_a, void nano::vote_generator::broadcast_action (std::shared_ptr const & vote_a) const { network.flood_vote_pr (vote_a); - network.flood_vote (vote_a, 2.0f); + network.flood_vote_non_pr (vote_a, 2.0f); vote_processor.vote (vote_a, inproc_channel); }