Skip to content

Commit

Permalink
Moving voting constants in to their own class (#3071)
Browse files Browse the repository at this point in the history
* Currently there is no rate limiter on vote generation for a particular root and this can lead to increased, unnecessary vote traffic.

This patch adds a delay between constructing non-cached votes.

* Removing check on hash since we won't produce a new vote if there's an existing one.

* Moving delay in to voting_constants and referencing voting_constants from local_vote_history instead of copying values on construction.

* Erasing duplicate roots.

* Adding test for correct broadcast deduplication.

* Retaining historical blocks until election completion. history::votes filters by root+hash so it won't pick up votes for hashes it's not looking for.

* Moving vote spacing to its own data structure.

* Testing vote_spacing class and adding trimming.

* Revert "Retaining historical blocks until election completion. history::votes filters by root+hash so it won't pick up votes for hashes it's not looking for."

This reverts commit 9890305.

Co-authored-by: Russel <[email protected]>
  • Loading branch information
clemahieu and Russel authored Dec 16, 2020
1 parent d8e2a32 commit d391b18
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 8 deletions.
3 changes: 2 additions & 1 deletion nano/core_test/voting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ namespace nano
{
TEST (local_vote_history, basic)
{
nano::local_vote_history history;
nano::network_params params;
nano::local_vote_history history{ params.voting };
ASSERT_FALSE (history.exists (1));
ASSERT_FALSE (history.exists (2));
ASSERT_TRUE (history.votes (1).empty ());
Expand Down
1 change: 1 addition & 0 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ block_processor_thread ([this]() {
}),
// clang-format on
online_reps (ledger, config),
history{ config.network_params.voting },
vote_uniquer (block_uniquer),
confirmation_height_processor (ledger, write_database_queue, config.conf_height_processor_batch_min_time, config.logging, logger, node_initialized_latch, flags.confirmation_height_processor_mode),
active (*this, confirmation_height_processor),
Expand Down
7 changes: 4 additions & 3 deletions nano/node/voting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ bool nano::local_vote_history::exists (nano::root const & root_a) const

void nano::local_vote_history::clean ()
{
debug_assert (max_size > 0);
debug_assert (constants.max_cache > 0);
auto & history_by_sequence (history.get<tag_sequence> ());
while (history_by_sequence.size () > max_size)
while (history_by_sequence.size () > constants.max_cache)
{
history_by_sequence.erase (history_by_sequence.begin ());
}
Expand Down Expand Up @@ -247,7 +247,8 @@ void nano::vote_generator::reply (nano::unique_lock<std::mutex> & lock_a, reques
roots.reserve (nano::network::confirm_ack_hashes_max);
for (; i != n && hashes.size () < nano::network::confirm_ack_hashes_max; ++i)
{
auto cached_votes = history.votes (i->first, i->second);
auto const & [root, hash] = *i;
auto cached_votes = history.votes (root, hash);
for (auto const & cached_vote : cached_votes)
{
if (cached_sent.insert (cached_vote).second)
Expand Down
6 changes: 5 additions & 1 deletion nano/node/voting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class local_vote_history final
};

public:
local_vote_history (nano::voting_constants const & constants) :
constants{ constants }
{
}
void add (nano::root const & root_a, nano::block_hash const & hash_a, std::shared_ptr<nano::vote> const & vote_a);
void erase (nano::root const & root_a);

Expand All @@ -65,7 +69,7 @@ class local_vote_history final
history;
// clang-format on

size_t const max_size{ nano::network_params{}.voting.max_cache };
nano::voting_constants const & constants;
void clean ();
std::vector<std::shared_ptr<nano::vote>> votes (nano::root const & root_a) const;
// Only used in Debug
Expand Down
5 changes: 3 additions & 2 deletions nano/secure/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,10 @@ nano::node_constants::node_constants (nano::network_constants & network_constant
weight_period = 5 * 60; // 5 minutes
}

nano::voting_constants::voting_constants (nano::network_constants & network_constants)
nano::voting_constants::voting_constants (nano::network_constants & network_constants) :
max_cache{ network_constants.is_dev_network () ? 256U : 128U * 1024 },
delay{ network_constants.is_dev_network () ? 1 : 15 }
{
max_cache = network_constants.is_dev_network () ? 256 : 128 * 1024;
}

nano::portmapping_constants::portmapping_constants (nano::network_constants & network_constants)
Expand Down
3 changes: 2 additions & 1 deletion nano/secure/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,8 @@ class voting_constants
{
public:
voting_constants (nano::network_constants & network_constants);
size_t max_cache;
size_t const max_cache;
std::chrono::seconds const delay;
};

/** Port-mapping related constants whose value depends on the active network */
Expand Down

0 comments on commit d391b18

Please sign in to comment.