Skip to content

Commit

Permalink
Separate vote_cache_entry class
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Mar 14, 2024
1 parent f109cb1 commit b50ee16
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 54 deletions.
22 changes: 11 additions & 11 deletions nano/node/vote_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,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)
Expand All @@ -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 ())
Expand Down Expand Up @@ -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<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)
Expand All @@ -77,32 +77,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;
}
Expand Down
88 changes: 45 additions & 43 deletions nano/node/vote_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<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{};
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:
using entry = vote_cache_entry;

public:
explicit vote_cache (vote_cache_config const &, nano::stats &);

Expand Down

0 comments on commit b50ee16

Please sign in to comment.