From 4d69ce5af0e1261b819352db45cbf205247b6e26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Wed, 20 Mar 2024 15:11:57 +0100 Subject: [PATCH] Election use `nano::vote_code` --- nano/node/active_transactions.cpp | 4 ++-- nano/node/election.cpp | 12 ++++++------ nano/node/election.hpp | 16 +--------------- nano/node/vote_cache.cpp | 4 ++-- nano/secure/common.hpp | 9 ++++++++- 5 files changed, 19 insertions(+), 26 deletions(-) diff --git a/nano/node/active_transactions.cpp b/nano/node/active_transactions.cpp index 350b4c28bb..b90c5510ae 100644 --- a/nano/node/active_transactions.cpp +++ b/nano/node/active_transactions.cpp @@ -481,8 +481,8 @@ nano::vote_code nano::active_transactions::vote (std::shared_ptr con for (auto const & [election, block_hash] : process) { 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); + processed |= (vote_result == nano::vote_code::vote); + replay |= (vote_result == nano::vote_code::replay); } // Republish vote if it is new and the node does not host a principal representative (or close to) diff --git a/nano/node/election.cpp b/nano/node/election.cpp index a41136e4c0..9dc7faf3e4 100644 --- a/nano/node/election.cpp +++ b/nano/node/election.cpp @@ -431,12 +431,12 @@ std::shared_ptr nano::election::find (nano::block_hash const & hash return result; } -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 +nano::vote_code nano::election::vote (nano::account const & rep, uint64_t timestamp_a, nano::block_hash const & block_hash_a, nano::vote_source vote_source_a) { auto weight = node.ledger.weight (rep); if (!node.network_params.network.is_dev_network () && weight <= node.minimum_principal_weight ()) { - return vote_result::ignored; + return vote_code::indeterminate; } nano::unique_lock lock{ mutex }; @@ -447,11 +447,11 @@ auto nano::election::vote (nano::account const & rep, uint64_t timestamp_a, nano auto last_vote_l (last_vote_it->second); if (last_vote_l.timestamp > timestamp_a) { - return vote_result::replay; + return vote_code::replay; } if (last_vote_l.timestamp == timestamp_a && !(last_vote_l.hash < block_hash_a)) { - return vote_result::replay; + return vote_code::replay; } auto max_vote = timestamp_a == std::numeric_limits::max () && last_vote_l.timestamp < timestamp_a; @@ -465,7 +465,7 @@ auto nano::election::vote (nano::account const & rep, uint64_t timestamp_a, nano if (!max_vote && !past_cooldown) { - return vote_result::ignored; + return vote_code::ignored; } } @@ -491,7 +491,7 @@ auto nano::election::vote (nano::account const & rep, uint64_t timestamp_a, nano confirm_if_quorum (lock); } - return vote_result::processed; + return vote_code::vote; } bool nano::election::publish (std::shared_ptr const & block_a) diff --git a/nano/node/election.hpp b/nano/node/election.hpp index 59cf2ea9e0..b549ecdbb8 100644 --- a/nano/node/election.hpp +++ b/nano/node/election.hpp @@ -71,20 +71,6 @@ class election final : public std::enable_shared_from_this { nano::id_t const id{ nano::next_id () }; // Track individual objects when tracing -public: - enum class vote_source - { - live, - 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; @@ -142,7 +128,7 @@ class election final : public std::enable_shared_from_this * Process vote. Internally uses cooldown to throttle non-final votes * If the election reaches consensus, it will be confirmed */ - vote_result vote (nano::account const & representative, uint64_t timestamp, nano::block_hash const & block_hash, vote_source = vote_source::live); + nano::vote_code vote (nano::account const & representative, uint64_t timestamp, nano::block_hash const & block_hash, nano::vote_source = vote_source::live); bool publish (std::shared_ptr const & block_a); // Confirm this block if quorum is met void confirm_if_quorum (nano::unique_lock &); diff --git a/nano/node/vote_cache.cpp b/nano/node/vote_cache.cpp index 4d40807d25..723f01caaa 100644 --- a/nano/node/vote_cache.cpp +++ b/nano/node/vote_cache.cpp @@ -68,8 +68,8 @@ std::size_t nano::vote_cache::entry::fill (std::shared_ptr const std::size_t inserted = 0; for (const auto & entry : voters_m) { - auto result = election->vote (entry.representative, entry.timestamp, hash_m, nano::election::vote_source::cache); - if (result == nano::election::vote_result::processed) + auto result = election->vote (entry.representative, entry.timestamp, hash_m, nano::vote_source::cache); + if (result == nano::vote_code::vote) { inserted++; } diff --git a/nano/secure/common.hpp b/nano/secure/common.hpp index d585e1b222..8f9161a938 100644 --- a/nano/secure/common.hpp +++ b/nano/secure/common.hpp @@ -199,7 +199,14 @@ enum class vote_code invalid, // Vote is not signed correctly replay, // Vote does not have the highest timestamp, it's a replay vote, // Vote has the highest timestamp - indeterminate // Unknown if replay or vote + indeterminate, // Unknown if replay or vote + ignored, // Vote is valid, but got ingored (e.g. due to cooldown) +}; + +enum class vote_source +{ + live, + cache, }; enum class block_status