Merge pull request #3870 from fikumikudev/prs/hinted-elections-2-1

Extract `election::cooldown_time` calculation
This commit is contained in:
clemahieu 2022-07-21 20:37:19 +01:00 committed by GitHub
commit c57c4f8030
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 30 deletions

View file

@ -212,7 +212,7 @@ bool nano::election::transition_time (nano::confirmation_solicitor & solicitor_a
return result;
}
std::chrono::milliseconds nano::election::time_to_live ()
std::chrono::milliseconds nano::election::time_to_live () const
{
switch (behavior)
{
@ -221,7 +221,22 @@ std::chrono::milliseconds nano::election::time_to_live ()
case election_behavior::hinted:
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
@ -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)
{
auto replay (false);
auto online_stake (node.online_reps.trended ());
auto weight (node.ledger.weight (rep));
auto should_process (false);
if (node.network_params.network.is_dev_network () || weight > node.minimum_principal_weight (online_stake))
auto replay = false;
auto weight = node.ledger.weight (rep);
auto should_process = false;
if (node.network_params.network.is_dev_network () || weight > node.minimum_principal_weight ())
{
unsigned int cooldown;
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;
}
const auto cooldown = cooldown_time (weight);
nano::unique_lock<nano::mutex> lock (mutex);

View file

@ -101,9 +101,14 @@ public: // Status
nano::election_status status;
public: // Interface
election (nano::node &, std::shared_ptr<nano::block> const &, std::function<void (std::shared_ptr<nano::block> const &)> const &, std::function<void (nano::account const &)> const &, nano::election_behavior);
election (nano::node &, std::shared_ptr<nano::block> const & block, std::function<void (std::shared_ptr<nano::block> const &)> const & confirmation_action, std::function<void (nano::account const &)> const & vote_action, nano::election_behavior behavior);
std::shared_ptr<nano::block> find (nano::block_hash const &) const;
nano::election_vote_result vote (nano::account const &, uint64_t, nano::block_hash const &);
/*
* Process vote. Internally uses cooldown to throttle non-final votes
* If the election reaches consensus, it will be confirmed
*/
nano::election_vote_result vote (nano::account const & representative, uint64_t timestamp, nano::block_hash const & block_hash);
bool publish (std::shared_ptr<nano::block> const & block_a);
std::size_t insert_inactive_votes_cache (nano::inactive_cache_information const &);
// Confirm this block if quorum is met
@ -126,7 +131,11 @@ private:
void remove_votes (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 &);
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:
std::unordered_map<nano::block_hash, std::shared_ptr<nano::block>> last_blocks;

View file

@ -809,12 +809,7 @@ nano::block_hash nano::node::rep_block (nano::account const & account_a)
nano::uint128_t nano::node::minimum_principal_weight ()
{
return minimum_principal_weight (online_reps.trended ());
}
nano::uint128_t nano::node::minimum_principal_weight (nano::uint128_t const & online_stake)
{
return online_stake / network_params.network.principal_weight_factor;
return online_reps.trended () / network_params.network.principal_weight_factor;
}
void nano::node::long_inactivity_cleanup ()

View file

@ -118,7 +118,6 @@ public:
nano::uint128_t weight (nano::account const &);
nano::block_hash rep_block (nano::account const &);
nano::uint128_t minimum_principal_weight ();
nano::uint128_t minimum_principal_weight (nano::uint128_t const &);
void ongoing_rep_calculation ();
void ongoing_bootstrap ();
void ongoing_peer_store ();