From b50ee16213fbaf0cf967db0bdde7f39fb01e68cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Thu, 14 Mar 2024 18:55:23 +0100 Subject: [PATCH] Separate `vote_cache_entry` class --- nano/node/vote_cache.cpp | 22 +++++----- nano/node/vote_cache.hpp | 88 ++++++++++++++++++++-------------------- 2 files changed, 56 insertions(+), 54 deletions(-) diff --git a/nano/node/vote_cache.cpp b/nano/node/vote_cache.cpp index 78c6fa86ff..63ebd5fc6b 100644 --- a/nano/node/vote_cache.cpp +++ b/nano/node/vote_cache.cpp @@ -3,15 +3,15 @@ #include /* - * entry + * entvote_cache_entryry */ -nano::vote_cache::entry::entry (const nano::block_hash & hash) : +nano::vote_cache_entry::vote_cache_entry (const nano::block_hash & hash) : hash_m{ hash } { } -bool nano::vote_cache::entry::vote (const nano::account & representative, const uint64_t & timestamp, const nano::uint128_t & rep_weight, std::size_t max_voters) +bool nano::vote_cache_entry::vote (const nano::account & representative, const uint64_t & timestamp, const nano::uint128_t & rep_weight, std::size_t max_voters) { bool updated = vote_impl (representative, timestamp, rep_weight, max_voters); if (updated) @@ -21,7 +21,7 @@ bool nano::vote_cache::entry::vote (const nano::account & representative, const return updated; } -bool nano::vote_cache::entry::vote_impl (const nano::account & representative, const uint64_t & timestamp, const nano::uint128_t & rep_weight, std::size_t max_voters) +bool nano::vote_cache_entry::vote_impl (const nano::account & representative, const uint64_t & timestamp, const nano::uint128_t & rep_weight, std::size_t max_voters) { auto existing = std::find_if (voters_m.begin (), voters_m.end (), [&representative] (auto const & item) { return item.representative == representative; }); if (existing != voters_m.end ()) @@ -63,7 +63,7 @@ bool nano::vote_cache::entry::vote_impl (const nano::account & representative, c } } -std::size_t nano::vote_cache::entry::fill (std::shared_ptr const & election) const +std::size_t nano::vote_cache_entry::fill (std::shared_ptr const & election) const { std::size_t inserted = 0; for (const auto & entry : voters_m) @@ -77,32 +77,32 @@ std::size_t nano::vote_cache::entry::fill (std::shared_ptr const return inserted; } -std::size_t nano::vote_cache::entry::size () const +std::size_t nano::vote_cache_entry::size () const { return voters_m.size (); } -nano::block_hash nano::vote_cache::entry::hash () const +nano::block_hash nano::vote_cache_entry::hash () const { return hash_m; } -nano::uint128_t nano::vote_cache::entry::tally () const +nano::uint128_t nano::vote_cache_entry::tally () const { return tally_m; } -nano::uint128_t nano::vote_cache::entry::final_tally () const +nano::uint128_t nano::vote_cache_entry::final_tally () const { return final_tally_m; } -std::vector nano::vote_cache::entry::voters () const +std::vector nano::vote_cache_entry::voters () const { return voters_m; } -std::chrono::steady_clock::time_point nano::vote_cache::entry::last_vote () const +std::chrono::steady_clock::time_point nano::vote_cache_entry::last_vote () const { return last_vote_m; } diff --git a/nano/node/vote_cache.hpp b/nano/node/vote_cache.hpp index 34f84d98b6..fa1bcdcdce 100644 --- a/nano/node/vote_cache.hpp +++ b/nano/node/vote_cache.hpp @@ -41,54 +41,56 @@ class vote_cache_config final std::chrono::seconds age_cutoff{ 5 * 60 }; }; -class vote_cache final +/** + * Stores votes associated with a single block hash + */ +class vote_cache_entry final { public: - /** - * Stores votes associated with a single block hash - */ - class entry final + struct voter_entry { - public: - struct voter_entry - { - nano::account representative; - uint64_t timestamp; - }; - - public: - explicit entry (nano::block_hash const & hash); - - /** - * Adds a vote into a list, checks for duplicates and updates timestamp if new one is greater - * @return true if current tally changed, false otherwise - */ - bool vote (nano::account const & representative, uint64_t const & timestamp, nano::uint128_t const & rep_weight, std::size_t max_voters); - - /** - * Inserts votes stored in this entry into an election - */ - std::size_t fill (std::shared_ptr const & election) const; - - std::size_t size () const; - nano::block_hash hash () const; - nano::uint128_t tally () const; - nano::uint128_t final_tally () const; - std::vector voters () const; - std::chrono::steady_clock::time_point last_vote () const; - - private: - bool vote_impl (nano::account const & representative, uint64_t const & timestamp, nano::uint128_t const & rep_weight, std::size_t max_voters); - - nano::block_hash const hash_m; - std::vector voters_m; - - nano::uint128_t tally_m{ 0 }; - nano::uint128_t final_tally_m{ 0 }; - - std::chrono::steady_clock::time_point last_vote_m{}; + nano::account representative; + uint64_t timestamp; }; +public: + explicit vote_cache_entry (nano::block_hash const & hash); + + /** + * Adds a vote into a list, checks for duplicates and updates timestamp if new one is greater + * @return true if current tally changed, false otherwise + */ + bool vote (nano::account const & representative, uint64_t const & timestamp, nano::uint128_t const & rep_weight, std::size_t max_voters); + + /** + * Inserts votes stored in this entry into an election + */ + std::size_t fill (std::shared_ptr const & election) const; + + std::size_t size () const; + nano::block_hash hash () const; + nano::uint128_t tally () const; + nano::uint128_t final_tally () const; + std::vector voters () const; + std::chrono::steady_clock::time_point last_vote () const; + +private: + bool vote_impl (nano::account const & representative, uint64_t const & timestamp, nano::uint128_t const & rep_weight, std::size_t max_voters); + + nano::block_hash const hash_m; + std::vector voters_m; + + nano::uint128_t tally_m{ 0 }; + nano::uint128_t final_tally_m{ 0 }; + + std::chrono::steady_clock::time_point last_vote_m{}; +}; + +class vote_cache final +{ +public: + using entry = vote_cache_entry; + public: explicit vote_cache (vote_cache_config const &, nano::stats &);