Reinstate election request limit on the confirmation solicitor (#2504)

This commit is contained in:
Guilherme Lawless 2020-01-22 10:58:33 +00:00 committed by GitHub
commit 0ce4b647df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 7 deletions

View file

@ -6,6 +6,7 @@ using namespace std::chrono_literals;
nano::confirmation_solicitor::confirmation_solicitor (nano::network & network_a, nano::network_constants const & params_a) : nano::confirmation_solicitor::confirmation_solicitor (nano::network & network_a, nano::network_constants const & params_a) :
max_confirm_req_batches (params_a.is_test_network () ? 1 : 20), max_confirm_req_batches (params_a.is_test_network () ? 1 : 20),
max_block_broadcasts (params_a.is_test_network () ? 4 : 30), max_block_broadcasts (params_a.is_test_network () ? 4 : 30),
max_election_requests (30),
network (network_a) network (network_a)
{ {
} }
@ -35,20 +36,21 @@ bool nano::confirmation_solicitor::add (nano::election const & election_a)
{ {
assert (prepared); assert (prepared);
auto const max_channel_requests (max_confirm_req_batches * nano::network::confirm_req_hashes_max); auto const max_channel_requests (max_confirm_req_batches * nano::network::confirm_req_hashes_max);
bool result = true; unsigned count = 0;
for (auto const & rep : representatives) for (auto i (representatives.begin ()), n (representatives.end ()); i != n && count < max_election_requests; ++i)
{ {
auto rep (*i);
if (election_a.last_votes.find (rep.account) == election_a.last_votes.end ()) if (election_a.last_votes.find (rep.account) == election_a.last_votes.end ())
{ {
auto & request_queue (requests[rep.channel]); auto & request_queue (requests[rep.channel]);
if (request_queue.size () < max_channel_requests) if (request_queue.size () < max_channel_requests)
{ {
request_queue.emplace_back (election_a.status.winner->hash (), election_a.status.winner->root ()); request_queue.emplace_back (election_a.status.winner->hash (), election_a.status.winner->root ());
result = false; ++count;
} }
} }
} }
return result; return count == 0;
} }
void nano::confirmation_solicitor::flush () void nano::confirmation_solicitor::flush ()

View file

@ -22,15 +22,17 @@ public:
bool add (nano::election const &); bool add (nano::election const &);
/** Dispatch bundled requests to each channel*/ /** Dispatch bundled requests to each channel*/
void flush (); void flush ();
/** The maximum amount of confirmation requests (batches) to be sent to each channel */ /** Maximum amount of confirmation requests (batches) to be sent to each channel */
size_t const max_confirm_req_batches; size_t const max_confirm_req_batches;
/** The global maximum amount of block broadcasts */ /** Global maximum amount of block broadcasts */
size_t const max_block_broadcasts; size_t const max_block_broadcasts;
/** Maximum amount of requests to be sent per election */
size_t const max_election_requests;
private: private:
nano::network & network; nano::network & network;
int rebroadcasted{ 0 }; unsigned rebroadcasted{ 0 };
std::vector<nano::representative> representatives; std::vector<nano::representative> representatives;
using vector_root_hashes = std::vector<std::pair<nano::block_hash, nano::root>>; using vector_root_hashes = std::vector<std::pair<nano::block_hash, nano::root>>;
std::unordered_map<std::shared_ptr<nano::transport::channel>, vector_root_hashes> requests; std::unordered_map<std::shared_ptr<nano::transport::channel>, vector_root_hashes> requests;