Separate vote_cache_entry class

This commit is contained in:
Piotr Wójcik 2024-03-14 18:55:23 +01:00
commit 02c31f62c2
2 changed files with 57 additions and 55 deletions

View file

@ -4,15 +4,15 @@
#include <nano/node/vote_cache.hpp>
/*
* 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)
@ -22,7 +22,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 ())
@ -64,7 +64,7 @@ bool nano::vote_cache::entry::vote_impl (const nano::account & representative, c
}
}
std::size_t nano::vote_cache::entry::fill (std::shared_ptr<nano::election> const & election) const
std::size_t nano::vote_cache_entry::fill (std::shared_ptr<nano::election> const & election) const
{
std::size_t inserted = 0;
for (const auto & entry : voters_m)
@ -78,32 +78,32 @@ std::size_t nano::vote_cache::entry::fill (std::shared_ptr<nano::election> 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::voter_entry> nano::vote_cache::entry::voters () const
std::vector<nano::vote_cache_entry::voter_entry> 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;
}

View file

@ -41,53 +41,55 @@ public:
std::chrono::seconds age_cutoff{ 5 * 60 };
};
/**
* Stores votes associated with a single block hash
*/
class vote_cache_entry final
{
public:
struct voter_entry
{
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<nano::election> 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<voter_entry> 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<voter_entry> 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:
/**
* Stores votes associated with a single block hash
*/
class entry final
{
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<nano::election> 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<voter_entry> 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<voter_entry> voters_m;
nano::uint128_t tally_m{ 0 };
nano::uint128_t final_tally_m{ 0 };
std::chrono::steady_clock::time_point last_vote_m{};
};
using entry = vote_cache_entry;
public:
explicit vote_cache (vote_cache_config const &, nano::stats &);