Refactor election vote (#4119)
Refactoring nano::election::vote method according to #4108. Main goals: Reduce nesting of if statements Use early returns Simplify longer if statement.
This commit is contained in:
parent
65941a0632
commit
b3f0c53008
1 changed files with 37 additions and 40 deletions
|
|
@ -372,23 +372,26 @@ 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, vote_source vote_source_a)
|
nano::election_vote_result nano::election::vote (nano::account const & rep, uint64_t timestamp_a, nano::block_hash const & block_hash_a, vote_source vote_source_a)
|
||||||
{
|
{
|
||||||
auto replay = false;
|
|
||||||
auto weight = node.ledger.weight (rep);
|
auto weight = node.ledger.weight (rep);
|
||||||
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 ())
|
|
||||||
{
|
{
|
||||||
|
return nano::election_vote_result (false, false);
|
||||||
|
}
|
||||||
nano::unique_lock<nano::mutex> lock{ mutex };
|
nano::unique_lock<nano::mutex> lock{ mutex };
|
||||||
|
|
||||||
auto last_vote_it (last_votes.find (rep));
|
auto last_vote_it (last_votes.find (rep));
|
||||||
if (last_vote_it == last_votes.end ())
|
if (last_vote_it != last_votes.end ())
|
||||||
{
|
|
||||||
should_process = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
auto last_vote_l (last_vote_it->second);
|
auto last_vote_l (last_vote_it->second);
|
||||||
if (last_vote_l.timestamp < timestamp_a || (last_vote_l.timestamp == timestamp_a && last_vote_l.hash < block_hash_a))
|
if (last_vote_l.timestamp > timestamp_a)
|
||||||
{
|
{
|
||||||
|
return nano::election_vote_result (true, false);
|
||||||
|
}
|
||||||
|
if (last_vote_l.timestamp == timestamp_a && !(last_vote_l.hash < block_hash_a))
|
||||||
|
{
|
||||||
|
return nano::election_vote_result (true, false);
|
||||||
|
}
|
||||||
|
|
||||||
auto max_vote = timestamp_a == std::numeric_limits<uint64_t>::max () && last_vote_l.timestamp < timestamp_a;
|
auto max_vote = timestamp_a == std::numeric_limits<uint64_t>::max () && last_vote_l.timestamp < timestamp_a;
|
||||||
|
|
||||||
bool past_cooldown = true;
|
bool past_cooldown = true;
|
||||||
|
|
@ -399,15 +402,11 @@ nano::election_vote_result nano::election::vote (nano::account const & rep, uint
|
||||||
past_cooldown = last_vote_l.time <= std::chrono::steady_clock::now () - cooldown;
|
past_cooldown = last_vote_l.time <= std::chrono::steady_clock::now () - cooldown;
|
||||||
}
|
}
|
||||||
|
|
||||||
should_process = max_vote || past_cooldown;
|
if (!max_vote && !past_cooldown)
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
replay = true;
|
return nano::election_vote_result (false, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (should_process)
|
|
||||||
{
|
|
||||||
last_votes[rep] = { std::chrono::steady_clock::now (), timestamp_a, block_hash_a };
|
last_votes[rep] = { std::chrono::steady_clock::now (), timestamp_a, block_hash_a };
|
||||||
if (vote_source_a == vote_source::live)
|
if (vote_source_a == vote_source::live)
|
||||||
{
|
{
|
||||||
|
|
@ -420,9 +419,7 @@ nano::election_vote_result nano::election::vote (nano::account const & rep, uint
|
||||||
{
|
{
|
||||||
confirm_if_quorum (lock);
|
confirm_if_quorum (lock);
|
||||||
}
|
}
|
||||||
}
|
return nano::election_vote_result (false, true);
|
||||||
}
|
|
||||||
return nano::election_vote_result (replay, should_process);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool nano::election::publish (std::shared_ptr<nano::block> const & block_a)
|
bool nano::election::publish (std::shared_ptr<nano::block> const & block_a)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue