Extract election::cooldown_time calculation

This commit is contained in:
Piotr Wójcik 2022-07-21 16:26:26 +02:00
commit 64da5cecea
2 changed files with 27 additions and 21 deletions

View file

@ -212,7 +212,7 @@ bool nano::election::transition_time (nano::confirmation_solicitor & solicitor_a
return result; return result;
} }
std::chrono::milliseconds nano::election::time_to_live () std::chrono::milliseconds nano::election::time_to_live () const
{ {
switch (behavior) switch (behavior)
{ {
@ -221,7 +221,22 @@ std::chrono::milliseconds nano::election::time_to_live ()
case election_behavior::hinted: case election_behavior::hinted:
return std::chrono::milliseconds (30 * 1000); return std::chrono::milliseconds (30 * 1000);
} }
release_assert (false); return {};
}
std::chrono::seconds nano::election::cooldown_time (nano::uint128_t weight) const
{
auto online_stake = node.online_reps.trended ();
if (weight > online_stake / 20) // Reps with more than 5% weight
{
return std::chrono::seconds{ 1 };
}
if (weight > online_stake / 100) // Reps with more than 1% weight
{
return std::chrono::seconds{ 5 };
}
// The rest of smaller reps
return std::chrono::seconds{ 15 };
} }
bool nano::election::have_quorum (nano::tally_t const & tally_a) const bool nano::election::have_quorum (nano::tally_t const & tally_a) const
@ -351,25 +366,12 @@ std::shared_ptr<nano::block> nano::election::find (nano::block_hash const & hash
nano::election_vote_result nano::election::vote (nano::account const & rep, uint64_t timestamp_a, nano::block_hash const & block_hash_a) nano::election_vote_result nano::election::vote (nano::account const & rep, uint64_t timestamp_a, nano::block_hash const & block_hash_a)
{ {
auto replay (false); auto replay = false;
auto online_stake (node.online_reps.trended ()); auto weight = node.ledger.weight (rep);
auto weight (node.ledger.weight (rep)); auto should_process = false;
auto should_process (false); if (node.network_params.network.is_dev_network () || weight > node.minimum_principal_weight ())
if (node.network_params.network.is_dev_network () || weight > node.minimum_principal_weight (online_stake))
{ {
unsigned int cooldown; const auto cooldown = cooldown_time (weight);
if (weight < online_stake / 100) // 0.1% to 1%
{
cooldown = 15;
}
else if (weight < online_stake / 20) // 1% to 5%
{
cooldown = 5;
}
else // 5% or above
{
cooldown = 1;
}
nano::unique_lock<nano::mutex> lock (mutex); nano::unique_lock<nano::mutex> lock (mutex);

View file

@ -131,7 +131,11 @@ private:
void remove_votes (nano::block_hash const &); void remove_votes (nano::block_hash const &);
void remove_block (nano::block_hash const &); void remove_block (nano::block_hash const &);
bool replace_by_weight (nano::unique_lock<nano::mutex> & lock_a, nano::block_hash const &); bool replace_by_weight (nano::unique_lock<nano::mutex> & lock_a, nano::block_hash const &);
std::chrono::milliseconds time_to_live (); std::chrono::milliseconds time_to_live () const;
/*
* Calculates minimum time delay between subsequent votes when processing non-final votes
*/
std::chrono::seconds cooldown_time (nano::uint128_t weight) const;
private: private:
std::unordered_map<nano::block_hash, std::shared_ptr<nano::block>> last_blocks; std::unordered_map<nano::block_hash, std::shared_ptr<nano::block>> last_blocks;