Skip to content

Commit

Permalink
Election vote result enum (#4497)
Browse files Browse the repository at this point in the history
* Remove unused classes

* Election vote result enum
  • Loading branch information
pwojcikdev authored Mar 17, 2024
1 parent ea579c7 commit 5891121
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 154 deletions.
4 changes: 0 additions & 4 deletions nano/node/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ add_library(
election_insertion_result.hpp
epoch_upgrader.hpp
epoch_upgrader.cpp
inactive_cache_information.hpp
inactive_cache_information.cpp
inactive_cache_status.hpp
inactive_cache_status.cpp
ipc/action_handler.hpp
ipc/action_handler.cpp
ipc/flatbuffers_handler.hpp
Expand Down
11 changes: 6 additions & 5 deletions nano/node/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,13 +539,14 @@ nano::vote_code nano::active_transactions::vote (std::shared_ptr<nano::vote> con

if (!process.empty ())
{
bool replay (false);
bool processed (false);
bool replay = false;
bool processed = false;

for (auto const & [election, block_hash] : process)
{
auto const result_l = election->vote (vote_a->account, vote_a->timestamp (), block_hash);
processed = processed || result_l.processed;
replay = replay || result_l.replay;
auto const vote_result = election->vote (vote_a->account, vote_a->timestamp (), block_hash);
processed |= (vote_result == nano::election::vote_result::processed);
replay |= (vote_result == nano::election::vote_result::replay);
}

// Republish vote if it is new and the node does not host a principal representative (or close to)
Expand Down
23 changes: 9 additions & 14 deletions nano/node/election.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ std::chrono::milliseconds nano::election::base_latency () const
return node.network_params.network.is_dev_network () ? 25ms : 1000ms;
}

nano::election_vote_result::election_vote_result (bool replay_a, bool processed_a)
{
replay = replay_a;
processed = processed_a;
}

/*
* election
*/
Expand Down Expand Up @@ -460,13 +454,14 @@ std::shared_ptr<nano::block> nano::election::find (nano::block_hash const & hash
return result;
}

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 nano::election::vote (nano::account const & rep, uint64_t timestamp_a, nano::block_hash const & block_hash_a, vote_source vote_source_a) -> vote_result
{
auto weight = node.ledger.weight (rep);
if (!node.network_params.network.is_dev_network () && weight <= node.minimum_principal_weight ())
{
return nano::election_vote_result (false, false);
return vote_result::ignored;
}

nano::unique_lock<nano::mutex> lock{ mutex };

auto last_vote_it (last_votes.find (rep));
Expand All @@ -475,26 +470,25 @@ nano::election_vote_result nano::election::vote (nano::account const & rep, uint
auto last_vote_l (last_vote_it->second);
if (last_vote_l.timestamp > timestamp_a)
{
return nano::election_vote_result (true, false);
return vote_result::replay;
}
if (last_vote_l.timestamp == timestamp_a && !(last_vote_l.hash < block_hash_a))
{
return nano::election_vote_result (true, false);
return vote_result::replay;
}

auto max_vote = timestamp_a == std::numeric_limits<uint64_t>::max () && last_vote_l.timestamp < timestamp_a;

bool past_cooldown = true;
// Only cooldown live votes
if (vote_source_a == vote_source::live)
if (vote_source_a == vote_source::live) // Only cooldown live votes
{
const auto cooldown = cooldown_time (weight);
past_cooldown = last_vote_l.time <= std::chrono::steady_clock::now () - cooldown;
}

if (!max_vote && !past_cooldown)
{
return nano::election_vote_result (false, false);
return vote_result::ignored;
}
}

Expand All @@ -519,7 +513,8 @@ nano::election_vote_result nano::election::vote (nano::account const & rep, uint
{
confirm_if_quorum (lock);
}
return nano::election_vote_result (false, true);

return vote_result::processed;
}

bool nano::election::publish (std::shared_ptr<nano::block> const & block_a)
Expand Down
18 changes: 8 additions & 10 deletions nano/node/election.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@ class vote_with_weight_info final
nano::uint128_t weight;
};

class election_vote_result final
{
public:
election_vote_result () = default;
election_vote_result (bool, bool);
bool replay{ false };
bool processed{ false };
};

enum class election_behavior
{
normal,
Expand Down Expand Up @@ -87,6 +78,13 @@ class election final : public std::enable_shared_from_this<nano::election>
cache,
};

enum class vote_result
{
ignored,
processed,
replay,
};

private:
// Minimum time between broadcasts of the current winner of an election, as a backup to requesting confirmations
std::chrono::milliseconds base_latency () const;
Expand Down Expand Up @@ -144,7 +142,7 @@ class election final : public std::enable_shared_from_this<nano::election>
* 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, vote_source = vote_source::live);
vote_result vote (nano::account const & representative, uint64_t timestamp, nano::block_hash const & block_hash, vote_source = vote_source::live);
bool publish (std::shared_ptr<nano::block> const & block_a);
// Confirm this block if quorum is met
void confirm_if_quorum (nano::unique_lock<nano::mutex> &);
Expand Down
32 changes: 0 additions & 32 deletions nano/node/inactive_cache_information.cpp

This file was deleted.

42 changes: 0 additions & 42 deletions nano/node/inactive_cache_information.hpp

This file was deleted.

19 changes: 0 additions & 19 deletions nano/node/inactive_cache_status.cpp

This file was deleted.

26 changes: 0 additions & 26 deletions nano/node/inactive_cache_status.hpp

This file was deleted.

4 changes: 2 additions & 2 deletions nano/node/vote_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ std::size_t nano::vote_cache::entry::fill (std::shared_ptr<nano::election> const
std::size_t inserted = 0;
for (const auto & entry : voters_m)
{
auto [is_replay, processed] = election->vote (entry.representative, entry.timestamp, hash_m, nano::election::vote_source::cache);
if (processed)
auto result = election->vote (entry.representative, entry.timestamp, hash_m, nano::election::vote_source::cache);
if (result == nano::election::vote_result::processed)
{
inserted++;
}
Expand Down

0 comments on commit 5891121

Please sign in to comment.