Skip to content

Commit

Permalink
Optimize vote distribution between PRs and non-PRs (#4766)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
gr0vity-dev and gr0vity-dev authored Oct 25, 2024
1 parent 9efd849 commit e1a4fcb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
19 changes: 15 additions & 4 deletions nano/node/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,15 @@ void nano::network::flood_vote (std::shared_ptr<nano::vote> const & vote, float
}
}

void nano::network::flood_vote_non_pr (std::shared_ptr<nano::vote> 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<nano::vote> const & vote, bool rebroadcasted)
{
nano::confirm_ack message{ node.network_params.network, vote, rebroadcasted };
Expand Down Expand Up @@ -377,11 +386,13 @@ std::deque<std::shared_ptr<nano::transport::channel>> nano::network::list_non_pr
{
std::deque<std::shared_ptr<nano::transport::channel>> result;
tcp_channels.list (result);

auto partition_point = std::partition (result.begin (), result.end (),
[this] (std::shared_ptr<nano::transport::channel> 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<nano::transport::channel> const & channel) {
return node.rep_crawler.is_pr (channel);
}),
result.end ());
if (result.size () > count_a)
{
result.resize (count_a, nullptr);
Expand Down
1 change: 1 addition & 0 deletions nano/node/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class network final
void flood_keepalive_self (float const scale_a = 0.5f);
void flood_vote (std::shared_ptr<nano::vote> const &, float scale, bool rebroadcasted = false);
void flood_vote_pr (std::shared_ptr<nano::vote> const &, bool rebroadcasted = false);
void flood_vote_non_pr (std::shared_ptr<nano::vote> 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<nano::block> const &);
// Flood block to a random selection of peers
Expand Down
2 changes: 1 addition & 1 deletion nano/node/vote_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ void nano::vote_generator::vote (std::vector<nano::block_hash> const & hashes_a,
void nano::vote_generator::broadcast_action (std::shared_ptr<nano::vote> 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);
}

Expand Down

0 comments on commit e1a4fcb

Please sign in to comment.